From b2929513cc63626f03b1c993b5a9b0109d60240b Mon Sep 17 00:00:00 2001 From: Rapptz Date: Sun, 8 May 2022 04:41:58 -0400 Subject: [PATCH] Update modal example for latest changes --- examples/modal.py | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/examples/modal.py b/examples/modal.py index 158cefc6c..564b56f83 100644 --- a/examples/modal.py +++ b/examples/modal.py @@ -3,29 +3,30 @@ from discord import app_commands import traceback -# Just default intents and a `discord.Client` instance -# We don't need a `commands.Bot` instance because we are not -# creating text-based commands. -intents = discord.Intents.default() -client = discord.Client(intents=intents) +# The guild in which this slash command will be registered. +# It is recommended to have a test guild to separate from your "production" bot +TEST_GUILD = discord.Object(0) -# We need an `discord.app_commands.CommandTree` instance -# to register application commands (slash commands in this case) -tree = app_commands.CommandTree(client) -# The guild in which this slash command will be registered. -# As global commands can take up to an hour to propagate, it is ideal -# to test it in a guild. -TEST_GUILD = discord.Object(ID) +class MyClient(discord.Client): + def __init__(self) -> None: + # Just default intents and a `discord.Client` instance + # We don't need a `commands.Bot` instance because we are not + # creating text-based commands. + intents = discord.Intents.default() + super().__init__(intents=intents) + # We need an `discord.app_commands.CommandTree` instance + # to register application commands (slash commands in this case) + self.tree = app_commands.CommandTree(self) -@client.event -async def on_ready(): - print(f'Logged in as {client.user} (ID: {client.user.id})') - print('------') + async def on_ready(self): + print(f'Logged in as {self.user} (ID: {self.user.id})') + print('------') - # Sync the application command with Discord. - await tree.sync(guild=TEST_GUILD) + async def setup_hook(self) -> None: + # Sync the application command with Discord. + await self.tree.sync(guild=TEST_GUILD) class Feedback(discord.ui.Modal, title='Feedback'): @@ -56,16 +57,21 @@ class Feedback(discord.ui.Modal, title='Feedback'): async def on_submit(self, interaction: discord.Interaction): await interaction.response.send_message(f'Thanks for your feedback, {self.name.value}!', ephemeral=True) - async def on_error(self, error: Exception, interaction: discord.Interaction) -> None: + async def on_error(self, interaction: discord.Interaction, error: Exception) -> None: await interaction.response.send_message('Oops! Something went wrong.', ephemeral=True) # Make sure we know what the error actually is traceback.print_tb(error.__traceback__) -@tree.command(guild=TEST_GUILD, description="Submit feedback") +client = MyClient() + + +@client.tree.command(guild=TEST_GUILD, description="Submit feedback") async def feedback(interaction: discord.Interaction): # Send the modal with an instance of our `Feedback` class + # Since modals require an interaction, they cannot be done as a response to a text command. + # They can only be done as a response to either an application command or a button press. await interaction.response.send_modal(Feedback())