Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add more information about application commands #467

Merged
merged 4 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/extensions/tasks/tasks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ doing very useful stuff.
doing very useful stuff.
```

For a more useful example here's a task in a cog context ripped straight from the [docs](https://docs.pycord.dev/en/stable/ext/tasks/index.html#recepies):
For a more useful example here's a task in a cog context ripped straight from the [docs](https://docs.pycord.dev/en/stable/ext/tasks/index.html#recipes):

```py
from discord.ext import tasks, commands
Expand Down
45 changes: 37 additions & 8 deletions docs/interactions/application-commands/slash-commands.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ Here's what the registered subcommands will look like in the Slash Command Menu:

You'll notice that there's the name of the Slash Command Group and then the name of the subcommand separated by a space.

:::

:::info Cogs

If you are looking to add Slash Command Groups to cogs, please look at our [Cogs page](../../popular-topics/cogs)!

:::

## Sub-groups

We've made a subcommand group, but did you know that you could create a group inside another?
Expand All @@ -143,12 +151,14 @@ The command created above can be invoked by typing `/math advanced square_root`.

Whenever you're using Slash Commands, you might notice that you can specify parameters that the user has to set or can optionally set. These are called Options.

Since you want different inputs from Options, you'll have to specify the type for that Option. There are a few ways of doing this.
Options can also include a description to provide more information. [You can learn more about Options in our documentation!](https://docs.pycord.dev/en/stable/api/application_commands.html#discord.Option)

Since you want different inputs from Options, you'll have to specify the type for that Option; there are a few ways of doing this.

<Tabs>
<TabItem value="0" label="Using Type Annotations" default>

You could use Type Annotations and let Pycord figure out the option type, like shown below.
You could use Type Annotations and let Pycord figure out the option type or explicitly specified using the [`SlashCommandOptionType`](https://docs.pycord.dev/en/stable/api/enums.html#discord.SlashCommandOptionType) enum.

```python
import discord
Expand All @@ -162,6 +172,16 @@ async def add(ctx, first: discord.Option(int), second: discord.Option(int)):
sum = first + second
await ctx.respond(f"The sum of {first} and {second} is {sum}.")

@bot.command()
# this explicitly tells pycord what types the options are instead
async def join(
ctx,
first: discord.Option(discord.SlashCommandOptionType.string),
second: discord.Option(discord.SlashCommandOptionType.string)
):
joined = first + second
await ctx.respond(f"When you join \"{first}\" and \"{second}\", you get: \"{joined}\".")

bot.run("TOKEN")
```

Expand All @@ -174,24 +194,33 @@ bot.run("TOKEN")
</div>
The sum of 1 and 1 is 2.
</DiscordMessage>
<DiscordMessage profile="robocord">
<div slot="interactions">
<DiscordInteraction profile="bob" command>
join
</DiscordInteraction>
</div>
When you join "Py" and "cord", you get: "Pycord".
</DiscordMessage>
</DiscordComponent>

</TabItem>
<TabItem value="1" label="Using the SlashCommandOptionType enum">
<TabItem value="1" label="Using option decorator">

You could also explicitly declare the type using the [`SlashCommandOptionType`](https://docs.pycord.dev/en/stable/api/enums.html#discord.SlashCommandOptionType) enum.
Instead of Type Annotations, you can also use the option decorator. This is usually done to have type-hinting.

```python title="Slash Command Type"
import discord

bot = discord.Bot()

@bot.command()
# this explicitly tells pycord what types the options are instead of it figuring it out by itself
@discord.option("first", type=discord.SlashCommandOptionType.string) # type = str also works
@discord.option("second", type=discord.SlashCommandOptionType.string) # type = str also works
async def join(
ctx,
first: discord.Option(discord.SlashCommandOptionType.string),
second: discord.Option(discord.SlashCommandOptionType.string)
first: str,
second: str,
):
joined = first + second
await ctx.respond(f"When you join \"{first}\" and \"{second}\", you get: \"{joined}\".")
Expand All @@ -206,7 +235,7 @@ bot.run("TOKEN")
join
</DiscordInteraction>
</div>
When you join "Hello" and "World!", you get: "Hello World!"
When you join "Py" and "cord", you get: "Pycord".
</DiscordMessage>
</DiscordComponent>

Expand Down
23 changes: 20 additions & 3 deletions docs/popular-topics/cogs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ to avoid making your bot's files messy and cluttered.

## Getting Started

First, you'll need to create a folder to store your cogs, eg. `cogs/`.
First, you'll need to create a folder to store your cogs, e.g. `cogs/`.

Then, create a file inside the folder, eg. `cogs/greetings.py`. By convention, the file name should
Then, create a file inside the folder, e.g. `cogs/greetings.py`. By convention, the file name should
be the same as the name of the cog or module.

We can then create the cog.
Expand All @@ -37,6 +37,23 @@ class Greetings(commands.Cog): # create a class for our cog that inherits from c
async def greet(self, ctx, member: discord.Member):
await ctx.respond(f'{ctx.author.mention} says hello to {member.mention}!')

math = discord.SlashCommandGroup("math", "Spooky math stuff") # create a Slash Command Group called "math"
advanced_math = math.create_subgroup(
"advanced",
"super hard math commands!"
)

@math.command()
async def add(self, a: int, b: int):
c = a + b
await ctx.respond(f"{a} + {b} is {c}.")

@advanced_math.command()
async def midpoint(self, x1: float, y1: float, x2: float, y2: float):
mid_x = (x1 + x2)/2
mid_y = (y1 + y2)/2
await ctx.respond(f"The midpoint between those coordinates is ({mid_x}, {mid_y}).")

@commands.Cog.listener() # we can add event listeners to our cog
async def on_member_join(self, member): # this is called when a member joins the server
# you must enable the proper intents
Expand All @@ -56,7 +73,7 @@ bot.load_extension('cogs.greetings')
```

This loads the file `cogs/greetings.py` and adds it to the bot.
The argument of `load_extension` should be your cog's path (eg. cogs/greetings.py) without the file
The argument of `load_extension` should be your cog's path (e.g. cogs/greetings.py) without the file
extension and with the `/` replaced with `.`

If you have multiple cogs, you can add them all at once by adding the following code:
Expand Down
Loading