diff options
Diffstat (limited to 'pydis_site')
| -rw-r--r-- | pydis_site/apps/content/resources/guides/python-guides/discord-app-commands.md | 56 | 
1 files changed, 28 insertions, 28 deletions
| diff --git a/pydis_site/apps/content/resources/guides/python-guides/discord-app-commands.md b/pydis_site/apps/content/resources/guides/python-guides/discord-app-commands.md index 57423fa4..e6095252 100644 --- a/pydis_site/apps/content/resources/guides/python-guides/discord-app-commands.md +++ b/pydis_site/apps/content/resources/guides/python-guides/discord-app-commands.md @@ -56,12 +56,12 @@ import discord  class SlashClient(discord.Client):      def __init__(self) -> None:          super().__init__(intents=discord.Intents.default()) -     +      async def setup_hook(self) -> None:          #perform tasks  '''Another way of creating a "setup_hook" is as follows''' -  +  client = discord.Client(intents=discord.Intents.default())  async def my_setup_hook() -> None:      #perform tasks @@ -83,7 +83,7 @@ class SlashClient(discord.Client):      def __init__(self) -> None:          super().__init__(intents=discord.Intents.default())          self.tree = discord.app_commands.CommandTree(self) -     +      async def setup_hook(self) -> None:          self.tree.copy_global_to(guild=discord.Object(id=12345678900987654))          await self.tree.sync() @@ -100,11 +100,11 @@ client.run("token")  __**EXPLANATION**__ -- `import discord` imports the **discord.py** package.  -- `class SlashClient(discord.Client)` is a class subclassing **Client**. Though there is no particular reason except readability to subclass the **Client** class, using the `Client.setup_hook = my_func` is equally valid.  -- Next up `super().__init__(...)` runs the `__init__` function of the **Client** class, this is equivalent to `discord.Client(...)`. Then, `self.tree = discord.app_commands.CommandTree(self)` creates a CommandTree which acts as the container for slash commaands.  -- Then in the `setup_hook`, `self.tree.copy_global_to(...)` adds the slash command to the guild of which the ID is provided as a `discord.Object` object. Further up, `self.tree.sync()` updates the API with any changes to the slash commands.  -- Finishing up with the **Client** subclass, we create an instance of the subclassed Client class which here has been named as `SlashClient` with `client = SlashClient()`.  +- `import discord` imports the **discord.py** package. +- `class SlashClient(discord.Client)` is a class subclassing **Client**. Though there is no particular reason except readability to subclass the **Client** class, using the `Client.setup_hook = my_func` is equally valid. +- Next up `super().__init__(...)` runs the `__init__` function of the **Client** class, this is equivalent to `discord.Client(...)`. Then, `self.tree = discord.app_commands.CommandTree(self)` creates a CommandTree which acts as the container for slash commaands. +- Then in the `setup_hook`, `self.tree.copy_global_to(...)` adds the slash command to the guild of which the ID is provided as a `discord.Object` object. Further up, `self.tree.sync()` updates the API with any changes to the slash commands. +- Finishing up with the **Client** subclass, we create an instance of the subclassed Client class which here has been named as `SlashClient` with `client = SlashClient()`.  - Then using the `command` method of the `CommandTree` we decorate a function with it as `client.tree` is an instance of `CommandTree` for the current application. The command function takes a default argument as said, which acts as the interaction that took place. Catching up is `await interaction.response.send_message("pong")` which sends back a message to the slash command invoker.  - And the classic old `client.run("token")` is used to connect the client to the discord gateway.  - Note that the `send_message` is a method of the `InteractionResponse` class and `interaction.response` in this case is an instance of the `InteractionResponse` object. The `send_message` method will not function if the response is not sent within 3 seconds of command invocation. I will discuss about how to handle this issue later following the gist. @@ -117,7 +117,7 @@ import discord  class SlashBot(commands.Bot):      def __init__(self) -> None:          super().__init__(command_prefix=".", intents=discord.Intents.default()) -         +      async def setup_hook(self) -> None:          self.tree.copy_global_to(guild=discord.Object(id=12345678900987654))          await self.tree.sync() @@ -156,20 +156,20 @@ from discord import app_commands  class MySlashCog(commands.Cog):      def __init__(self, bot: commands.Bot) -> None:          self.bot = bot -     +      @app_commands.command(name="ping", description="...")      async def _ping(self, interaction: discord.Interaction):          await interaction.response.send_message("pong!") -     +  class MySlashBot(commands.Bot):      def __init__(self) -> None:          super().__init__(command_prefix="!", intents=discord.Intents.default()) -     +      async def setup_hook(self) -> None:          await self.add_cog(MySlashCog(self))          await self.tree.copy_global_to(discord.Object(id=123456789098765432))          await self.tree.sync() -         +  bot = MySlashBot()  bot.run("token") @@ -207,20 +207,20 @@ class MySlashGroupCog(commands.Cog):      @app_commands.command(name="ping", description="...")      async def _ping(self, interaction: discord.) -> None:          await interaction.response.send_message("pong!") -     +      @group.command(name="command", description="...")      async def _cmd(self, interaction: discord.Interaction) -> None:          await interaction.response.send_message("uwu") -     +  class MySlashBot(commands.Bot):      def __init__(self) -> None:          super().__init__(command_prefix="!", intents=discord.Intents.default()) -     +      async def setup_hook(self) -> None:          await self.add_cog(MySlashGroupCog(self))          await self.tree.copy_global_to(discord.Object(id=123456789098765432))          await self.tree.sync() -         +  bot = MySlashBot()  bot.run("token") @@ -246,20 +246,20 @@ class MySlashGroup(commands.GroupCog, name="uwu"):      @app_commands.command(name="ping", description="...")      async def _ping(self, interaction: discord.) -> None:          await interaction.response.send_message("pong!") -     +      @app_commands.command(name="command", description="...")      async def _cmd(self, interaction: discord.Interaction) -> None:          await interaction.response.send_message("uwu") -     +  class MySlashBot(commands.Bot):      def __init__(self) -> None:          super().__init__(command_prefix="!", intents=discord.Intents.default()) -     +      async def setup_hook(self) -> None:          await self.add_cog(MySlashGroup(self))          await self.tree.copy_global_to(discord.Object(id=123456789098765432))          await self.tree.sync() -         +  bot = MySlashBot()  bot.run("token") @@ -271,7 +271,7 @@ __**EXPLANATION**__  # Some common methods and features used for slash commands. ----  +---  ### A common function used for slash commands is the `describe` function. This is used to add descriptions to the arguments of a slash command. The command function can decorated with this function. It goes by the following syntax as shown below. @@ -353,13 +353,13 @@ import discord  class Bot(commands.Bot):      def __init__(self):          super().__init__(command_prefix="uwu", intents=discord.Intents.all()) -         -     + +      async def setup_hook(self): -        self.tree.copy_global_to(guild=discord.Object(id=12345678909876543))  +        self.tree.copy_global_to(guild=discord.Object(id=12345678909876543))          await self.tree.sync() -         -     + +  bot = Bot()  @bot.tree.command(name="ping") @@ -374,7 +374,7 @@ bot.run("token")  __**EXPLANATION**__  - The first argument the `cooldown` method takes is the amount of times the command can be run in a specific period of time. -- The second argument it takes is the period of time in which the command can be run the specified number of times.  +- The second argument it takes is the period of time in which the command can be run the specified number of times.  - The `CommandOnCooldown` exception can be handled using an error handler. I will discuss about making an error handler for slash commands later in the gist. | 
