You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/Extensions/Commands/help-command.mdx
+40-21Lines changed: 40 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -132,7 +132,8 @@ bot.run("token")
132
132
133
133
## Updating Build-in Help Commands
134
134
135
-
Let's try to make the `MinimalHelpCommand` look better. We can do this by putting its content in an embed.
135
+
Let's try to make the `MinimalHelpCommand` look better. We can do this by putting its content in an
136
+
embed.
136
137
137
138
```python
138
139
bot = commands.Bot(command_prefix='!')
@@ -152,21 +153,24 @@ Let's go through the code.
152
153
153
154
First, we create a new class called `MyNewHelp`. This class is a subclass of `MinimalHelpCommand`.
154
155
155
-
Next, we override the `send_pages` method. This method is responsible for sending the help command to the user.
156
-
We override this method because we don't want to change the content of the pages, just how they are sent.
156
+
Next, we override the `send_pages` method. This method is responsible for sending the help command to
157
+
the user. We override this method because we don't want to change the content of the pages, just how
158
+
they are sent.
157
159
158
-
We use the `get_destination` method to get the destination of the message. This is the channel or user that the help command is to be sent to.
160
+
We use the `get_destination` method to get the destination of the message. This is the channel or use
161
+
that the help command is to be sent to.
159
162
160
-
We use the `paginator.pages` property to get the different pages of the help command. We put the page in an embed, and then send the embed to the destination channel.
163
+
We use the `paginator.pages` property to get the different pages of the help command. We put the page
164
+
in an embed, and then send the embed to the destination channel.
161
165
162
166
Finally, we set this as our new help command using `bot.help_command = MyNewHelp()`.
163
167
164
168
<DiscordMessages>
165
169
<DiscordMessageauthor="Guide Bot"avatar="red"bot>
166
170
<DiscordEmbed>
167
-
Use <code>uwu help [command]</code> for more info on a command.
171
+
Use <code>!help [command]</code> for more info on a command.
168
172
<br />
169
-
You can also use <code>uwu help [category]</code> for more info on a
173
+
You can also use <code>!help [category]</code> for more info on a
170
174
category.
171
175
<br />
172
176
<br />
@@ -215,17 +219,21 @@ Let's go through the code.
215
219
216
220
- First, we create a new class called `MyHelp`. This class is a subclass of `HelpCommand`.
217
221
218
-
- Next, we override the `send_bot_help` method. This method is responsible for sending the main help command to the user.
222
+
- Next, we override the `send_bot_help` method. This method is responsible for sending the main help
223
+
command to the user.
219
224
220
225
- We create an embed with title "Help".
221
226
222
-
- We iterate through `mapping.items()`, which returns a list of tuples, the first element being the cog and the second element being a list of commands.
227
+
- We iterate through `mapping.items()`, which returns a list of tuples, the first element being the
228
+
cog and the second element being a list of commands.
223
229
224
-
- By using `self.get_command_signature(c)` we get the signature of the command, also known as the `parameters` or `arguments`.
230
+
- By using `self.get_command_signature(c)` we get the signature of the command, also known as the
231
+
`parameters` or `arguments`.
225
232
226
233
- There is a chance that the cog is empty, so we use `if command_signatures:`.
227
234
228
-
- We get the name of the cog using `getattr(cog, "qualified_name", "No Category")`. This calls the cog's attribute `qualified_name` that returns "No Category" if the cog has no name.
235
+
- We get the name of the cog using `getattr(cog, "qualified_name", "No Category")`. This calls the
236
+
cog's attribute `qualified_name` that returns "No Category" if the cog has no name.
229
237
230
238
- We add a field to the embed with the name of the cog and the value of the command signatures.
231
239
@@ -240,9 +248,11 @@ class MyHelp(commands.HelpCommand):
@@ -253,7 +263,8 @@ class MyHelp(commands.HelpCommand):
253
263
bot.help_command = MyHelp()
254
264
```
255
265
256
-
Now it won't show any commands that can't be used by the user. It won't show all the aliases of the command either. Let's instead make it show the aliases in the command help menu.
266
+
Now the help command won't show commands that the user can't use, as well as aliases for commands.
267
+
Let's make the help command show command aliases.
257
268
258
269
### Command Help
259
270
@@ -276,8 +287,12 @@ bot.help_command = MyHelp()
276
287
277
288
Let's quickly go through the code we haven't discussed yet.
278
289
279
-
- In line 3, we create an embed with title the signature of the command (so that the title of the embed looks like `<command> <parameter> [parameter]`), and a random color.
280
-
- In lines 4 and 5, we get the help of the command and add it to the embed. The help of a command can be specified in docstrings of a command function, for example,
290
+
- In line 3, we create an embed with title the signature of the command (so that the title of the
291
+
embed looks like `<command> <parameter> [parameter]`), and a random color.
292
+
293
+
- In lines 4 and 5, we get the command's `help` description and add it to the embed. The help description
294
+
of a command can be specified in docstrings of a command function. For example:
295
+
281
296
```python
282
297
@bot.command()
283
298
asyncdefping(ctx):
@@ -297,7 +312,7 @@ Let's quickly go through the code we haven't discussed yet.
297
312
...
298
313
```
299
314
300
-
A very cool but not well-known Python shorthand!
315
+
A very helpful (but not well-known) Python shorthand!
301
316
302
317
- In line 7, we get the aliases of the command and add them to the embed.
303
318
@@ -349,7 +364,7 @@ Add all of these methods together and you have a fully functioning help command!
349
364
350
365
:::note
351
366
352
-
The code was updated a little bit to make it better.
367
+
The following code has been slightly edited from the tutorial version.
When a user types something like `!help pycordupdate`, we need to inform the user that the command does not exist. We do this with a simple error handler.
441
+
When a user attempts to use a command that does not exist,, we need to inform the user.
442
+
We can do this by overriding the `send_error_message` function.
@@ -445,5 +463,6 @@ class MyHelp(commands.HelpCommand):
445
463
446
464
## Credits
447
465
448
-
Most of the content from this guide is from [InterStella0's walkthrough guide on subclassing HelpCommand](https://gist.github.com/InterStella0/b78488fb28cadf279dfd3164b9f0cf96#why).
466
+
Most of the content from this guide is from
467
+
[InterStella0's walkthrough guide on subclassing HelpCommand](https://gist.github.com/InterStella0/b78488fb28cadf279dfd3164b9f0cf96).
449
468
Thanks to InterStella0 for making this guide amazing.
Before Discord added slash commands, all bots had prefixed commands. A user would type `!ping` or `?ping` or whatever the bot's prefix was to get a response.
19
-
However, this prefixed commands system isn't native to Discord! Developers made use of the `on_message` event to check if the message began with a `!ping`. So every time a message was sent, the bot would check if it began with the prefix, or with the command name.
18
+
Before Discord added slash commands, all bots had prefixed commands. A user would type the bot's prefix
19
+
followed by a word or phrase to invoke a command, such as `?help` or `!help`.
20
+
However, this prefixed commands system isn't native to Discord! Developers made use of an `on_message`
21
+
event to check if the message began with a certain character, then invoke the command. Every time a
22
+
message was sent, the bot would see the message and check for its "prefix"
20
23
21
-
The syntax becomes a little more complicated when you want to have multiple commands. There are several more disadvances to this system. This is where the commands extension comes in.
22
-
`ext.commands` has various advantages, such as:
24
+
The syntax becomes a little more complicated when you want to have multiple commands. There are several
25
+
disadvantages to this system. This is where the commands extension comes in. `ext.commands` has
You can do much more with the commands extension! This example only showcases the same features as the previous example. You can use a different built-in help command, or even create your own! You will learn about creating Help Commands and Categories in the next tutorials.
155
+
The commands extension has many more uses. This example only showcases the basic features mentioned
156
+
in the previous example. Other things you can do with the commands extension include using a different
157
+
built-in help command and creating your own. The following tutorials showcase these.
152
158
153
159
:::
154
160
@@ -165,7 +171,8 @@ Before we check out the syntax, let's take a look at the bot classes.
165
171
>
166
172
> `discord.ext.commands.Bot` - Subclasses `discord.Bot`, adds prefixed commands, cogs, and more.
167
173
168
-
This means that `discord.ext.commands.Bot` has both slash commands and prefixed commands, as well as events, cogs and more.
174
+
This means that `discord.ext.commands.Bot` has both slash commands and prefixed commands, as well as
175
+
events, cogs and more.
169
176
170
177
Now let's look at the syntax.
171
178
@@ -185,13 +192,13 @@ bot.run("token") # Run the bot with your token.
@@ -215,22 +222,27 @@ bot.run("token") # Run the bot with your token.
215
222
216
223
:::tip
217
224
218
-
The help command is a built-in command and is enabled by default. You will learn more about it in the next tutorials.
225
+
The help command is a built-in command and is enabled by default. You will learn more about it in the
226
+
following guides.
219
227
220
228
:::
221
229
222
230
## Parameters
223
231
224
-
Prefixed commands can take parameters, just like slash commands. You can specify the parameters in the function itself.
232
+
Prefixed commands can take parameters, just like slash commands. You can specify the parameters in
233
+
the function itself.
225
234
226
235
```python {2}
227
236
@bot.command()
228
237
asyncdefecho(ctx, *, message):
229
238
await ctx.send(message)
230
239
```
231
240
232
-
`ctx` is the context of the message. `*` means that the parameter can be any number of words. `message` is the parameter. If you had not passed `*`, `message` would only have been one word.
233
-
For example, if a user had passed `!echo hello world`, `message` would have been `hello`. Since we passed `*`, `message` is `hello world`, or the rest of the message.
241
+
`ctx` is the context of the message. `*` means that the parameter can be any number of words. `message`
242
+
is the parameter. If you had not passed `*`, `message` would only have been one word.
243
+
244
+
For example, if a user had used `!echo hello world`, `message` would have been `hello`. Since we
245
+
passed `*`, `message` is `hello world`, or the rest of the message.
In the example above, `channel` is a parameter that is of type `discord.TextChannel`. When you specify the type of the parameter, Pycord will automatically try to convert the parameter to that type. That is why you are able to use `channel.send` directly without needing to convert it first.
255
+
In the example above, `channel` is a parameter that is of type `discord.TextChannel`. When you
256
+
specify the type of the parameter, Pycord will automatically try to convert the parameter to that type.
257
+
That is why you are able to use `channel.send` directly without needing to convert it first.
244
258
245
-
We also have a new parameter, the `title`. This does not have a type, so it will be a string. `*` means that the rest of the message belongs to the next parameter, in this case, `message`.
259
+
We also have a new parameter, `title`. This does not have a type, so it will be a string. `*` means
260
+
that the rest of the message belongs to the next parameter, in this case, `message`.
246
261
247
-
When a user types `!echo #general Greetings! Hello World!`, `channel` will be the text channel `#general`, `title` will be `Greetings!` and `message` will be `Hello World!`.
262
+
When a user types `!echo #general Greetings! Hello World!`, `channel` will be the text channel
263
+
`#general`, `title` will be `Greetings!` and `message` will be `Hello World!`.
248
264
249
-
Let's take an example where the user passes `!echo #general Holiday Greetings! Greetings to you all!`. Here, the user wants the title to be "Holiday Greetings!" and the message to be "Greetings to you all!". However, since Pycord parses the message at whitespaces, the title will end up being "Holiday" and the message "Greetings! Greetings to you all!". The user can prevent this by typing `!echo "Holiday Greetings!" Greetings to you all!`.
265
+
Let's take an example where the user passes `!echo #general Holiday Greetings! Greetings to you all!`.
266
+
Here, the user wants the title to be "Holiday Greetings!" and the message to be "Greetings to you all!".
267
+
However, since Pycord parses the message at whitespaces, the title will end up being "Holiday" and the
268
+
message "Greetings! Greetings to you all!". The user can prevent this by typing `!echo "Holiday
269
+
Greetings!" Greetings to you all!`.
250
270
251
271
<DiscordMessages>
252
-
<DiscordMessageauthor="Guide User"avatar="blue">
272
+
<DiscordMessageauthor="Santa Claus"avatar="blue">
253
273
!echo #general Holiday Greetings! Greetings to you all!
254
274
</DiscordMessage>
255
-
<DiscordMessageauthor="Guide Bot"avatar="red"bot>
275
+
<DiscordMessageauthor="Elf"avatar="red"bot>
256
276
<strong>Holiday</strong>
257
277
<br />
258
278
Greetings! Greetings to you all!
259
279
</DiscordMessage>
260
-
<DiscordMessageauthor="User 2"avatar="green">
280
+
<DiscordMessageauthor="Mrs. Claus"avatar="green">
261
281
!echo #general "Holiday Greetings!" Greetings to you all!
If you had not specified the type of the parameter, it would have been a string. And since "5"isnot the same as5in python, the bot would have responded with"Nope! Better luck next time :)". Even if you do not specify the type of the parameter, you can still convert it later on, in this case, with`int(guess)`.
304
+
If you had not specified the type of the parameter, it would have been a string. And since `"5"`isnot
305
+
the same as`5`in python, the bot would have responded with"Nope! Better luck next time :)".
306
+
Even if you do not specify the type of the parameter, you can still convert it later on, in this case,
0 commit comments