Skip to content

Commit 13619c5

Browse files
committed
Improvine help-command, prefixed-commands, and hosting-your-bot
1 parent ddd28a4 commit 13619c5

File tree

3 files changed

+104
-50
lines changed

3 files changed

+104
-50
lines changed

docs/Extensions/Commands/help-command.mdx

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ bot.run("token")
132132

133133
## Updating Build-in Help Commands
134134

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.
136137

137138
```python
138139
bot = commands.Bot(command_prefix='!')
@@ -152,21 +153,24 @@ Let's go through the code.
152153

153154
First, we create a new class called `MyNewHelp`. This class is a subclass of `MinimalHelpCommand`.
154155

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.
157159

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.
159162

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.
161165

162166
Finally, we set this as our new help command using `bot.help_command = MyNewHelp()`.
163167

164168
<DiscordMessages>
165169
<DiscordMessage author="Guide Bot" avatar="red" bot>
166170
<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.
168172
<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
170174
category.
171175
<br />
172176
<br />
@@ -215,17 +219,21 @@ Let's go through the code.
215219

216220
- First, we create a new class called `MyHelp`. This class is a subclass of `HelpCommand`.
217221

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.
219224

220225
- We create an embed with title "Help".
221226

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.
223229

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`.
225232

226233
- There is a chance that the cog is empty, so we use `if command_signatures:`.
227234

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.
229237

230238
- We add a field to the embed with the name of the cog and the value of the command signatures.
231239

@@ -240,9 +248,11 @@ class MyHelp(commands.HelpCommand):
240248

241249
async def send_bot_help(self, mapping):
242250
embed = discord.Embed(title="Help", color=discord.Color.blurple())
251+
243252
for cog, commands in mapping.items():
244253
filtered = await self.filter_commands(commands, sort=True)
245254
command_signatures = [self.get_command_signature(c) for c in filtered]
255+
246256
if command_signatures:
247257
cog_name = getattr(cog, "qualified_name", "No Category")
248258
embed.add_field(name=cog_name, value="\n".join(command_signatures), inline=False)
@@ -253,7 +263,8 @@ class MyHelp(commands.HelpCommand):
253263
bot.help_command = MyHelp()
254264
```
255265

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.
257268

258269
### Command Help
259270

@@ -276,8 +287,12 @@ bot.help_command = MyHelp()
276287

277288
Let's quickly go through the code we haven't discussed yet.
278289

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+
281296
```python
282297
@bot.command()
283298
async def ping(ctx):
@@ -297,7 +312,7 @@ Let's quickly go through the code we haven't discussed yet.
297312
...
298313
```
299314

300-
A very cool but not well-known Python shorthand!
315+
A very helpful (but not well-known) Python shorthand!
301316

302317
- In line 7, we get the aliases of the command and add them to the embed.
303318

@@ -349,7 +364,7 @@ Add all of these methods together and you have a fully functioning help command!
349364

350365
:::note
351366

352-
The code was updated a little bit to make it better.
367+
The following code has been slightly edited from the tutorial version.
353368

354369
:::
355370

@@ -406,7 +421,8 @@ bot.help_command = SupremeHelpCommand()
406421

407422
## Command Attributes
408423

409-
How can you add cooldowns to your help command? Or set aliases, or change it's name? The answer to all of these questions lies in command attributes.
424+
How can you add cooldowns, set aliases, and change the name of help commands? Command attributes can
425+
help you do all of that, and more!
410426

411427
```python
412428
attributes = {
@@ -422,21 +438,23 @@ bot.help_command = MyHelp(command_attrs=attributes)
422438

423439
## Error Handling
424440

425-
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.
426443

427444
```python
428445
class MyHelp(commands.HelpCommand):
429446
async def send_error_message(self, error):
430447
embed = discord.Embed(title="Error", description=error, color=discord.Color.red())
431448
channel = self.get_destination()
449+
432450
await channel.send(embed=embed)
433451
```
434452

435453
<DiscordMessages>
436-
<DiscordMessage author="NoobGuy" avatar="green">
454+
<DiscordMessage author="Guide Bot" avatar="green">
437455
!help update_pycord
438456
</DiscordMessage>
439-
<DiscordMessage author="Guide Bot" avatar="blue" bot>
457+
<DiscordMessage author="Guide Bot" avatar="red" bot>
440458
<DiscordEmbed embedTitle="Error" borderColor="#ff0000">
441459
Command 'update_pycord' not found.
442460
</DiscordEmbed>
@@ -445,5 +463,6 @@ class MyHelp(commands.HelpCommand):
445463

446464
## Credits
447465

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).
449468
Thanks to InterStella0 for making this guide amazing.

docs/Extensions/Commands/prefixed-commands.mdx

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ import "@discord-message-components/react/styles";
1515
import Tabs from "@theme/Tabs";
1616
import TabItem from "@theme/TabItem";
1717

18-
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"
2023

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
26+
various advantages, such as:
2327

2428
- Simpler syntax
2529
- Easier to use
@@ -56,21 +60,21 @@ async def on_message(message):
5660
```
5761

5862
<DiscordMessages>
59-
<DiscordMessage author="Guide User" avatar="blue">
63+
<DiscordMessage author="Guide Man" avatar="blue">
6064
!ping
6165
</DiscordMessage>
6266
<DiscordMessage author="Guide Bot" avatar="red" bot>
6367
Pong!
6468
</DiscordMessage>
65-
<DiscordMessage author="Guide User" avatar="blue">
69+
<DiscordMessage author="Guide Man" avatar="blue">
6670
!announce Hello Hello World!
6771
</DiscordMessage>
6872
<DiscordMessage author="Guide Bot" avatar="red" bot>
6973
<strong>Hello</strong>
7074
<br />
7175
Hello World!
7276
</DiscordMessage>
73-
<DiscordMessage author="Guide User" avatar="blue">
77+
<DiscordMessage author="Guide Man" avatar="blue">
7478
!help
7579
</DiscordMessage>
7680
<DiscordMessage author="Guide Bot" avatar="red" bot>
@@ -102,27 +106,27 @@ async def on_command_error(ctx, error):
102106
```
103107

104108
<DiscordMessages>
105-
<DiscordMessage author="Guide User" avatar="blue">
109+
<DiscordMessage author="Guide Man" avatar="blue">
106110
!ping
107111
</DiscordMessage>
108112
<DiscordMessage author="Guide Bot" avatar="red" bot>
109113
Pong!
110114
</DiscordMessage>
111-
<DiscordMessage author="Guide User" avatar="blue">
115+
<DiscordMessage author="Guide Man" avatar="blue">
112116
!announce Hello Hello World!
113117
</DiscordMessage>
114118
<DiscordMessage author="Guide Bot" avatar="red" bot>
115119
<strong>Hello</strong>
116120
<br />
117121
Hello World!
118122
</DiscordMessage>
119-
<DiscordMessage author="Guide User" avatar="blue">
123+
<DiscordMessage author="Guide Man" avatar="blue">
120124
!pycord
121125
</DiscordMessage>
122126
<DiscordMessage author="Guide Bot" avatar="red" bot>
123127
Unknown command.
124128
</DiscordMessage>
125-
<DiscordMessage author="Guide User" avatar="blue">
129+
<DiscordMessage author="Guide Man" avatar="blue">
126130
!help
127131
</DiscordMessage>
128132
<DiscordMessage author="Guide Bot" avatar="red" bot>
@@ -148,7 +152,9 @@ async def on_command_error(ctx, error):
148152

149153
:::tip
150154

151-
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.
152158

153159
:::
154160

@@ -165,7 +171,8 @@ Before we check out the syntax, let's take a look at the bot classes.
165171
>
166172
> `discord.ext.commands.Bot` - Subclasses `discord.Bot`, adds prefixed commands, cogs, and more.
167173
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.
169176

170177
Now let's look at the syntax.
171178

@@ -185,13 +192,13 @@ bot.run("token") # Run the bot with your token.
185192
```
186193

187194
<DiscordMessages>
188-
<DiscordMessage author="Guide User" avatar="blue">
195+
<DiscordMessage author="Guide Man" avatar="blue">
189196
!ping
190197
</DiscordMessage>
191198
<DiscordMessage author="Guide Bot" avatar="red" bot>
192199
Pong!
193200
</DiscordMessage>
194-
<DiscordMessage author="User 2" avatar="green">
201+
<DiscordMessage author="Guide Woman" avatar="green">
195202
!help
196203
</DiscordMessage>
197204
<DiscordMessage author="Guide Bot" avatar="red" bot>
@@ -215,22 +222,27 @@ bot.run("token") # Run the bot with your token.
215222

216223
:::tip
217224

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.
219227

220228
:::
221229

222230
## Parameters
223231

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.
225234

226235
```python {2}
227236
@bot.command()
228237
async def echo(ctx, *, message):
229238
await ctx.send(message)
230239
```
231240

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.
234246

235247
We can pass multiple parameters too!
236248

@@ -240,27 +252,35 @@ async def echo(ctx, channel:discord.TextChannel, title, *, message):
240252
await channel.send("**{}**\n{}".format(title, message))
241253
```
242254

243-
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.
244258

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`.
246261

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!`.
248264

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!`.
250270

251271
<DiscordMessages>
252-
<DiscordMessage author="Guide User" avatar="blue">
272+
<DiscordMessage author="Santa Claus" avatar="blue">
253273
!echo #general Holiday Greetings! Greetings to you all!
254274
</DiscordMessage>
255-
<DiscordMessage author="Guide Bot" avatar="red" bot>
275+
<DiscordMessage author="Elf" avatar="red" bot>
256276
<strong>Holiday</strong>
257277
<br />
258278
Greetings! Greetings to you all!
259279
</DiscordMessage>
260-
<DiscordMessage author="User 2" avatar="green">
280+
<DiscordMessage author="Mrs. Claus" avatar="green">
261281
!echo #general "Holiday Greetings!" Greetings to you all!
262282
</DiscordMessage>
263-
<DiscordMessage author="Guide Bot" avatar="red" bot>
283+
<DiscordMessage author="Elf" avatar="red" bot>
264284
<strong>Holiday Greetings!</strong>
265285
<br />
266286
Greetings to you all!
@@ -281,4 +301,7 @@ async def gtn(ctx, guess:int):
281301
await ctx.send("Nope! Better luck next time :)")
282302
```
283303

284-
If you had not specified the type of the parameter, it would have been a string. And since "5" is not the same as 5 in 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"` is not
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,
307+
with `int(guess)`.

0 commit comments

Comments
 (0)