From d84845b79ed721b32a928e3fe3cc44f229c5a31b Mon Sep 17 00:00:00 2001 From: fanyx Date: Tue, 23 Jan 2024 12:09:51 +0100 Subject: [PATCH] Create persistent buttons for role selection prompt --- main.py | 30 +++++++++++------------------- src/ui.py | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 19 deletions(-) create mode 100644 src/ui.py diff --git a/main.py b/main.py index 52a9db2..a9fafb7 100755 --- a/main.py +++ b/main.py @@ -9,15 +9,22 @@ from discord.ext.commands import Greedy, Context from discord import app_commands from src.roles import AddRoleButton, RemoveRoleButton +from src.ui import RoleSelectView class Bot(commands.Bot): + def __init__(self): + super().__init__(command_prefix=",", intents=discord.Intents.default()) + self.intents.message_content = True + + async def setup_hook(self) -> None: + self.add_view(RoleSelectView()) + return await super().setup_hook() + with open('config.yaml', 'r') as file: config = safe_load(file) # Bot -intents=discord.Intents.default() -intents.message_content = True -client = Bot(intents=intents, command_prefix=",") +client = Bot() # Command Groups roles = app_commands.Group(name="roles", description="Manage role dialogues") @@ -26,22 +33,7 @@ roles = app_commands.Group(name="roles", description="Manage role dialogues") ## Group: roles @roles.command(name="create") async def roles_create_button(interaction: discord.Interaction): - view = discord.ui.View() - view.add_item( - AddRoleButton( - style=discord.ButtonStyle.primary, - label="Add Roles", - emoji=discord.PartialEmoji.from_str(emojize(":check_mark_button:")) - ) - ) - view.add_item( - RemoveRoleButton( - style=discord.ButtonStyle.secondary, - label="Remove Roles", - emoji=discord.PartialEmoji.from_str(emojize(":cross_mark:")) - ) - ) - await interaction.response.send_message(view=view) + await interaction.response.send_message(view=RoleSelectView()) # Sync using ,sync ~ @client.command() diff --git a/src/ui.py b/src/ui.py new file mode 100644 index 0000000..e667927 --- /dev/null +++ b/src/ui.py @@ -0,0 +1,25 @@ +from discord import Interaction, ButtonStyle, PartialEmoji +from discord.ui import View +from emoji import emojize + +from src.roles import AddRoleButton, RemoveRoleButton + +class RoleSelectView(View): + def __init__(self): + super().__init__(timeout=None) + self.add_item( + AddRoleButton( + style=ButtonStyle.primary, + label="Add Roles", + emoji=PartialEmoji.from_str(emojize(":check_mark_button:")), + custom_id="role_select:add" + ) + ) + self.add_item( + RemoveRoleButton( + style=ButtonStyle.secondary, + label="Remove Roles", + emoji=PartialEmoji.from_str(emojize(":cross_mark:")), + custom_id="role_select:remove" + ) + )