@@ -6,6 +6,7 @@ keywords: [disnake, bot, guide, tutorial, creating, commands, python]
6
6
# Creating commands
7
7
8
8
<!-- Import Discord components -->
9
+
9
10
import {
10
11
DiscordButton ,
11
12
DiscordButtons ,
@@ -25,13 +26,16 @@ import isDarkTheme from '../../src/hooks/isDarkTheme';
25
26
26
27
:::info
27
28
28
- This page is a follow-up, and the base code used is from the previous page ([Initial files](./initial-files.mdx)). The code can be found on the GitHub repository [here](https://github.com/DisnakeDev/guide/tree/master/docs/extra-code-samples/code-intial-files).
29
+ This page is a follow-up, and the base code used is from the previous page ([Initial files](./initial-files.mdx)). The
30
+ code can be found on the GitHub repository
31
+ [here](https://github.com/DisnakeDev/guide/tree/master/docs/extra-code-samples/code-intial-files).
29
32
30
33
:::
31
34
32
- Discord also allows developers to register [slash commands](https://discord.com/developers/docs/interactions/application-commands), which
33
- provides users a first-class way of interacting directly with your application. These slash commands shall be covered by
34
- the guide [here](../interactions/application-commands.mdx), in the **Interactions** section.
35
+ Discord also allows developers to register
36
+ [slash commands](https://discord.com/developers/docs/interactions/application-commands), which provides users a
37
+ first-class way of interacting directly with your application. These slash commands shall be covered by the guide
38
+ [here](../interactions/application-commands.mdx), in the **Interactions** section.
35
39
36
40
## A note on prefix commands
37
41
@@ -40,7 +44,9 @@ Bot commands that are initiated when a keyword is used along with a specified pr
40
44
41
45
:::caution Message Intent - Privileged
42
46
43
- It is to be noted that handling prefix commands require the **message intent**, which allows the bot to get content and data of messages sent by users. This intent has recently been privileged, i.e., it needs to be manually enabled for the bot application, and its requirement will eventually be reviewed if your bot is in over 100 servers.
47
+ It is to be noted that handling prefix commands require the **message intent**, which allows the bot to get content and
48
+ data of messages sent by users. This intent has recently been privileged, i.e., it needs to be manually enabled for the
49
+ bot application, and its requirement will eventually be reviewed if your bot is in over 100 servers.
44
50
45
51
You can read more about the message intent [here][message-intent-article].
46
52
@@ -53,8 +59,8 @@ be covered in the [**Interactions**](../interactions/application-commands.mdx) s
53
59
## Registering commands
54
60
55
61
This section covers the bare minimum to get you started with registering slash commands. Once again, you can refer to
56
- [this page](../interactions/application-commands.mdx) for an in-depth coverage of topics, including guild
57
- commands, global commands, options, option types, autocomplete and choices.
62
+ [this page](../interactions/application-commands.mdx) for an in-depth coverage of topics, including guild commands,
63
+ global commands, options, option types, autocomplete and choices.
58
64
59
65
Now, we shall continue with the base code used in the previous section.
60
66
@@ -103,7 +109,11 @@ necessary for replying to the use of the command.
103
109
104
110
:::note Using `ctx` vs. `inter`
105
111
106
- If you have experience with coding bots with [`discord.py`](https://discordpy.readthedocs.io/en/latest), you would be familiar with using `ctx` as an abbreviation for passing context into the function. This guide will primarily be using `inter`, as it is short for `interaction` and refers to [`disnake.ApplicationCommandInteraction`](https://docs.disnake.dev/en/latest/api.html#applicationcommandinteraction). Of course, you're open to using your preferred abbreviation in code.
112
+ If you have experience with coding bots with [`discord.py`](https://discordpy.readthedocs.io/en/latest), you would be
113
+ familiar with using `ctx` as an abbreviation for passing context into the function. This guide will primarily be using
114
+ `inter`, as it is short for `interaction` and refers to
115
+ [`disnake.ApplicationCommandInteraction`](https://docs.disnake.dev/en/latest/api.html#applicationcommandinteraction). Of
116
+ course, you're open to using your preferred abbreviation in code.
107
117
108
118
:::
109
119
@@ -141,9 +151,11 @@ bot.run("YOUR_BOT_TOKEN")
141
151
142
152
:::tip Using `test_guilds` in `commands.Bot()`
143
153
144
- When you have multiple commands registered under the same test guilds, it is convenient to only have your `guild_ids` defined once. Therefore, you can use the `test_guilds` argument in the `commands.Bot()` instance instead of passing `guild_ids` to every single command -
154
+ When you have multiple commands registered under the same test guilds, it is convenient to only have your `guild_ids`
155
+ defined once. Therefore, you can use the `test_guilds` argument in the `commands.Bot()` instance instead of passing
156
+ `guild_ids` to every single command -
145
157
146
- ``` python
158
+ ```python
147
159
bot = commands.Bot(test_guilds=[1234, 5678])
148
160
```
149
161
@@ -178,21 +190,23 @@ bot.run("YOUR_BOT_TOKEN")
178
190
```
179
191
180
192
<br />
181
- <DiscordMessages lightTheme = { ! isDarkTheme ()} >
182
- <DiscordMessage author = " Disnake Bot" avatar = " /public/disnake-logo.png" bot = " true" >
183
- <div slot = " interactions" >
184
- <DiscordInteraction author = " AbhigyanTrips" avatar = " https://i.imgur.com/8xd9SHa.png" command = " true" >ping</DiscordInteraction >
185
- </div >
186
- Pong!
187
- </DiscordMessage >
193
+ <DiscordMessages lightTheme = { ! isDarkTheme ()} >
194
+ <DiscordMessage author = " Disnake Bot" avatar = " /public/disnake-logo.png" bot = " true" >
195
+ <div slot = " interactions" >
196
+ <DiscordInteraction author = " AbhigyanTrips" avatar = " https://i.imgur.com/8xd9SHa.png" command = " true" >
197
+ ping
198
+ </DiscordInteraction >
199
+ </div >
200
+ Pong!
201
+ </DiscordMessage >
188
202
</DiscordMessages >
189
203
<br />
190
204
191
205
### Server info command
192
206
193
207
` inter.guild ` refers to the guild the interaction was sent in (a
194
- [ ` Guild ` ] ( https://docs.disnake.dev/en/latest/api.html#disnake.Guild ) instance), which exposes properties such as ` .name ` or
195
- ` .member_count ` .
208
+ [ ` Guild ` ] ( https://docs.disnake.dev/en/latest/api.html#disnake.Guild ) instance), which exposes properties such as ` .name `
209
+ or ` .member_count ` .
196
210
197
211
``` python title="main.py" {12-16}
198
212
import disnake
@@ -217,20 +231,23 @@ bot.run("YOUR_BOT_TOKEN")
217
231
```
218
232
219
233
<br />
220
- <DiscordMessages lightTheme = { ! isDarkTheme ()} >
221
- <DiscordMessage author = " Disnake Bot" avatar = " /public/disnake-logo.png" bot = " true" >
222
- <div slot = " interactions" >
223
- <DiscordInteraction author = " AbhigyanTrips" avatar = " https://i.imgur.com/8xd9SHa.png" command = " true" >server</DiscordInteraction >
224
- </div >
225
- Server name: Disnake Guide <br />
226
- Total members: 2
227
- </DiscordMessage >
234
+ <DiscordMessages lightTheme = { ! isDarkTheme ()} >
235
+ <DiscordMessage author = " Disnake Bot" avatar = " /public/disnake-logo.png" bot = " true" >
236
+ <div slot = " interactions" >
237
+ <DiscordInteraction author = " AbhigyanTrips" avatar = " https://i.imgur.com/8xd9SHa.png" command = " true" >
238
+ server
239
+ </DiscordInteraction >
240
+ </div >
241
+ Server name: Disnake Guide <br />
242
+ Total members: 2
243
+ </DiscordMessage >
228
244
</DiscordMessages >
229
245
<br />
230
246
231
247
:::tip
232
248
233
- Refer to the [ Guild] ( https://docs.disnake.dev/en/latest/api.html#disnake.Guild ) documentation for a list of all the available properties and methods.
249
+ Refer to the [ Guild] ( https://docs.disnake.dev/en/latest/api.html#disnake.Guild ) documentation for a list of all the
250
+ available properties and methods.
234
251
235
252
:::
236
253
@@ -241,8 +258,8 @@ same manner - use `inter.guild.created_at` or `inter.guild.verification_level`,
241
258
242
259
A "user" refers to a Discord user. ` inter.author ` refers to the user the interaction was sent by (a
243
260
[ User instance] ( https://docs.disnake.dev/en/latest/api.html#disnake.User ) in DM contexts, or a
244
- [ Member instance] ( https://docs.disnake.dev/en/latest/api.html#disnake.Member ) in server contexts), which exposes properties
245
- such as ` .name ` or ` .id ` . (Using just ` inter.author ` will give the user's full tag.)
261
+ [ Member instance] ( https://docs.disnake.dev/en/latest/api.html#disnake.Member ) in server contexts), which exposes
262
+ properties such as ` .name ` or ` .id ` . (Using just ` inter.author ` will give the user's full tag.)
246
263
247
264
``` python title="main.py" {12-14}
248
265
import disnake
@@ -265,20 +282,24 @@ bot.run("YOUR_BOT_TOKEN")
265
282
```
266
283
267
284
<br />
268
- <DiscordMessages lightTheme = { ! isDarkTheme ()} >
269
- <DiscordMessage author = " Disnake Bot" avatar = " /public/disnake-logo.png" bot = " true" >
270
- <div slot = " interactions" >
271
- <DiscordInteraction author = " AbhigyanTrips" avatar = " https://i.imgur.com/8xd9SHa.png" command = " true" >server</DiscordInteraction >
272
- </div >
273
- Your tag: AbhigyanTrips #1234 <br />
274
- Your ID: 123456789012345678
275
- </DiscordMessage >
285
+ <DiscordMessages lightTheme = { ! isDarkTheme ()} >
286
+ <DiscordMessage author = " Disnake Bot" avatar = " /public/disnake-logo.png" bot = " true" >
287
+ <div slot = " interactions" >
288
+ <DiscordInteraction author = " AbhigyanTrips" avatar = " https://i.imgur.com/8xd9SHa.png" command = " true" >
289
+ server
290
+ </DiscordInteraction >
291
+ </div >
292
+ Your tag: AbhigyanTrips #1234 <br />
293
+ Your ID: 123456789012345678
294
+ </DiscordMessage >
276
295
</DiscordMessages >
277
296
<br />
278
297
279
298
:::tip
280
299
281
- Refer to the [ ` User ` ] ( https://docs.disnake.dev/en/latest/api.html#disnake.User ) and [ ` Member ` ] ( https://docs.disnake.dev/en/latest/api.html#disnake.Member ) documentation for a list of all the available properties and methods.
300
+ Refer to the [ ` User ` ] ( https://docs.disnake.dev/en/latest/api.html#disnake.User ) and
301
+ [ ` Member ` ] ( https://docs.disnake.dev/en/latest/api.html#disnake.Member ) documentation for a list of all the available
302
+ properties and methods.
282
303
283
304
:::
284
305
0 commit comments