Skip to content

Commit 96ec4b5

Browse files
authored
chore: added section about option types
1 parent 80a96fd commit 96ec4b5

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

docs/Interactions/slash-commands.mdx

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import {
1111
} from "@discord-message-components/react";
1212
import "@discord-message-components/react/styles";
1313

14+
import Tabs from "@theme/Tabs";
15+
import TabItem from "@theme/TabItem";
16+
1417
## About
1518

1619
On March 24, 2021, Discord added Slash Commands to Discord as an easier, more efficient, and better way of using bot commands. Pycord has implemented Slash Commands into the library so it's simple, efficient, and familiar.
@@ -169,6 +172,76 @@ The registered slash commands from the cog should be displayed as normal in the
169172
</DiscordMessage>
170173
</DiscordMessages>
171174

175+
## Options & Option Types
176+
177+
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.
178+
179+
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.
180+
181+
<Tabs>
182+
<TabItem value="0" label="Use type annotations and let Pycord figure out its type" default>
183+
184+
```python
185+
import discord
186+
187+
bot = discord.Bot()
188+
189+
@bot.command()
190+
# pycord will then figure out the types for you
191+
async def add(ctx, first: discord.Option(int), second: discord.Option(int)):
192+
# you can use them as they were actual integers
193+
sum = first + second
194+
await ctx.respond(f"The sum of {first} and {second} is {sum}.")
195+
196+
bot.run("TOKEN")
197+
```
198+
199+
<DiscordMessages>
200+
<DiscordMessage author="Guide Bot" avatar="red" bot>
201+
<div slot="interactions">
202+
<DiscordInteraction author="Guide Man" avatar="green" command>
203+
add
204+
</DiscordInteraction>
205+
</div>
206+
The sum of 1 and 1 is 2.
207+
</DiscordMessage>
208+
</DiscordMessages>
209+
210+
</TabItem>
211+
<TabItem value="1" label="Explicitly set the type using the SlashCommandOptionType enum">
212+
213+
```python
214+
import discord
215+
216+
bot = discord.Bot()
217+
218+
@bot.command()
219+
# this explicitly tells pycord what types the options are instead of it figuring it out by itself
220+
async def join(
221+
ctx,
222+
first: discord.Option(discord.SlashCommandOptionType.string),
223+
second: discord.Option(discord.SlashCommandOptionType.string)
224+
):
225+
joined = first + second
226+
await ctx.respond(f"The joined string of \"{first}\" and \"{second}\" is \"{joined}\".")
227+
228+
bot.run("TOKEN")
229+
```
230+
231+
<DiscordMessages>
232+
<DiscordMessage author="Guide Bot" avatar="red" bot>
233+
<div slot="interactions">
234+
<DiscordInteraction author="Guide Man" avatar="green" command>
235+
join
236+
</DiscordInteraction>
237+
</div>
238+
The joined string of "hi" and " mom" is "hi mom".
239+
</DiscordMessage>
240+
</DiscordMessages>
241+
242+
</TabItem>
243+
</Tabs>
244+
172245
:::info Related Topics
173246

174247
- [Interactions Index](index)

0 commit comments

Comments
 (0)