Skip to content

Commit f309cd3

Browse files
authored
docs: add more information about application commands (#467)
* docs: add option decorator * docs: slash command groups in cogs * docs: minor grammar fix * fix: spelling mistake
1 parent db42b64 commit f309cd3

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

docs/extensions/tasks/tasks.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ doing very useful stuff.
3535
doing very useful stuff.
3636
```
3737

38-
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):
38+
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):
3939

4040
```py
4141
from discord.ext import tasks, commands

docs/interactions/application-commands/slash-commands.mdx

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ Here's what the registered subcommands will look like in the Slash Command Menu:
119119

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

122+
:::
123+
124+
:::info Cogs
125+
126+
If you are looking to add Slash Command Groups to cogs, please look at our [Cogs page](../../popular-topics/cogs)!
127+
128+
:::
129+
122130
## Sub-groups
123131

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

144152
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.
145153

146-
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.
154+
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)
155+
156+
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.
147157

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

151-
You could use Type Annotations and let Pycord figure out the option type, like shown below.
161+
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.
152162

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

175+
@bot.command()
176+
# this explicitly tells pycord what types the options are instead
177+
async def join(
178+
ctx,
179+
first: discord.Option(discord.SlashCommandOptionType.string),
180+
second: discord.Option(discord.SlashCommandOptionType.string)
181+
):
182+
joined = first + second
183+
await ctx.respond(f"When you join \"{first}\" and \"{second}\", you get: \"{joined}\".")
184+
165185
bot.run("TOKEN")
166186
```
167187

@@ -174,24 +194,33 @@ bot.run("TOKEN")
174194
</div>
175195
The sum of 1 and 1 is 2.
176196
</DiscordMessage>
197+
<DiscordMessage profile="robocord">
198+
<div slot="interactions">
199+
<DiscordInteraction profile="bob" command>
200+
join
201+
</DiscordInteraction>
202+
</div>
203+
When you join "Py" and "cord", you get: "Pycord".
204+
</DiscordMessage>
177205
</DiscordComponent>
178206

179207
</TabItem>
180-
<TabItem value="1" label="Using the SlashCommandOptionType enum">
208+
<TabItem value="1" label="Using option decorator">
181209

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

184212
```python title="Slash Command Type"
185213
import discord
186214

187215
bot = discord.Bot()
188216

189217
@bot.command()
190-
# this explicitly tells pycord what types the options are instead of it figuring it out by itself
218+
@discord.option("first", type=discord.SlashCommandOptionType.string) # type = str also works
219+
@discord.option("second", type=discord.SlashCommandOptionType.string) # type = str also works
191220
async def join(
192221
ctx,
193-
first: discord.Option(discord.SlashCommandOptionType.string),
194-
second: discord.Option(discord.SlashCommandOptionType.string)
222+
first: str,
223+
second: str,
195224
):
196225
joined = first + second
197226
await ctx.respond(f"When you join \"{first}\" and \"{second}\", you get: \"{joined}\".")
@@ -206,7 +235,7 @@ bot.run("TOKEN")
206235
join
207236
</DiscordInteraction>
208237
</div>
209-
When you join "Hello" and "World!", you get: "Hello World!"
238+
When you join "Py" and "cord", you get: "Pycord".
210239
</DiscordMessage>
211240
</DiscordComponent>
212241

docs/popular-topics/cogs.mdx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ to avoid making your bot's files messy and cluttered.
88

99
## Getting Started
1010

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

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

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

40+
math = discord.SlashCommandGroup("math", "Spooky math stuff") # create a Slash Command Group called "math"
41+
advanced_math = math.create_subgroup(
42+
"advanced",
43+
"super hard math commands!"
44+
)
45+
46+
@math.command()
47+
async def add(self, a: int, b: int):
48+
c = a + b
49+
await ctx.respond(f"{a} + {b} is {c}.")
50+
51+
@advanced_math.command()
52+
async def midpoint(self, x1: float, y1: float, x2: float, y2: float):
53+
mid_x = (x1 + x2)/2
54+
mid_y = (y1 + y2)/2
55+
await ctx.respond(f"The midpoint between those coordinates is ({mid_x}, {mid_y}).")
56+
4057
@commands.Cog.listener() # we can add event listeners to our cog
4158
async def on_member_join(self, member): # this is called when a member joins the server
4259
# you must enable the proper intents
@@ -56,7 +73,7 @@ bot.load_extension('cogs.greetings')
5673
```
5774

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

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

0 commit comments

Comments
 (0)