commit 1b79da12043332f751bdbed7f623e1b1ac3a6ce5 Author: Alfie King Date: Mon Jun 1 00:05:40 2026 +0100 roles diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a35594d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.env +.venv +__pycache__ \ No newline at end of file diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..112a961 --- /dev/null +++ b/src/main.py @@ -0,0 +1,20 @@ +import discord, os, views +from dotenv import load_dotenv + + +load_dotenv() # load all the variables from the env file +bot = discord.Bot() + + +@bot.event +async def on_ready(): + bot.add_view(views.RoleSelectors()) + print(f"{bot.user} is ready and online!") + + +@bot.slash_command(name="role_selector", description="create role selector") +async def species(ctx: discord.ApplicationContext): + await ctx.respond("Role selector menu", view=views.RoleSelectors()) + + +bot.run(os.getenv('TOKEN')) # run the bot with the token diff --git a/src/roles.py b/src/roles.py new file mode 100644 index 0000000..c1ae7b8 --- /dev/null +++ b/src/roles.py @@ -0,0 +1,31 @@ +import discord + + +async def PlatformCallback(interaction: discord.Interaction, values: list): + for platform in ["PC", "Xbox", "Playstation", "Nintendo", "VR"]: + if not discord.utils.get(interaction.guild.roles, name=platform): + await interaction.guild.create_role(name=platform, color=discord.Color.random()) + + if platform not in values: + await interaction.user.remove_roles(discord.utils.get(interaction.guild.roles, name=platform)) + + for platform in values: + await interaction.user.add_roles(discord.utils.get(interaction.guild.roles, name=platform)) + + embed = discord.Embed(title="Platform Roles Updated") + embed.add_field(name="Selected Platforms", value=", ".join(values)) + await interaction.response.send_message(embed=embed, ephemeral=True) + + +async def SpeciesCallback(interaction: discord.Interaction, value: str): + species_list = [species.strip().capitalize() for species in value.split(",")] + + for species in species_list: + if not discord.utils.get(interaction.guild.roles, name=species): + await interaction.guild.create_role(name=species, color=discord.Color.random()) + + await interaction.user.add_roles(discord.utils.get(interaction.guild.roles, name=species)) + + embed = discord.Embed(title="Species Roles Updated") + embed.add_field(name="Selected Species", value=", ".join(species_list)) + await interaction.response.send_message(embed=embed, ephemeral=True) \ No newline at end of file diff --git a/src/views.py b/src/views.py new file mode 100644 index 0000000..1446fe4 --- /dev/null +++ b/src/views.py @@ -0,0 +1,59 @@ +import discord, roles + + +# Platform Role Select View +class Platforms(discord.ui.View): + @discord.ui.select( # the decorator that lets you specify the properties of the select menu + placeholder = "Select Platforms", # the placeholder text that will be displayed if nothing is selected + min_values = 1, # the minimum number of values that must be selected by the users + max_values = 5, # the maximum number of values that can be selected by the users + options = [ # the list of options from which users can choose, a required field + discord.SelectOption( + label="PC", + description="pcmr" + ), + discord.SelectOption( + label="Xbox", + description="microslop" + ), + discord.SelectOption( + label="Playstation", + description="idk what to put here" + ), + discord.SelectOption( + label="Nintendo", + description="who actually uses this" + ), + discord.SelectOption( + label="VR", + description="vrchat" + ) + ] + ) + async def select_callback(self, select, interaction): # the function called when the user is done selecting options + await roles.PlatformCallback(interaction, select.values) + + +# Species Role View +class Species(discord.ui.Modal): + def __init__(self, *args, **kwargs) -> None: + super().__init__(*args, **kwargs) + + self.add_item(discord.ui.InputText(label="Enter Species (comma seperated for multiple)")) + + async def callback(self, interaction: discord.Interaction): + await roles.SpeciesCallback(interaction, self.children[0].value) + + +# Role selectors view +class RoleSelectors(discord.ui.View): + def __init__(self): + super().__init__(timeout=None) + + @discord.ui.button(label="Species", custom_id="species", style=discord.ButtonStyle.primary) + async def species_button_callback(self, button, interaction): + await interaction.response.send_modal(Species(title="Choose your species")) + + @discord.ui.button(label="Platform", custom_id="platform", style=discord.ButtonStyle.primary) + async def platform_button_callback(self, button, interaction): + await interaction.response.send_message("Choose your platforms", view=Platforms(), ephemeral=True) \ No newline at end of file