Skip to content

Commit 270a6f2

Browse files
Migrate discord.py custom help command pin to site (#698)
1 parent 8ee14fb commit 270a6f2

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
title: Custom Help Command
3+
description: "Overwrite discord.py's help command to implement custom functionality"
4+
---
5+
6+
First, a basic walkthrough can be found [here](https://gist.github.com/InterStella0/b78488fb28cadf279dfd3164b9f0cf96) by Stella#2000 on subclassing the HelpCommand. It will provide some foundational knowledge that is required before attempting a more customizable help command.
7+
8+
## Custom Subclass of Help Command
9+
If the types of classes of the HelpCommand do not fit your needs, you can subclass HelpCommand and use the class mehods to customize the output. Below is a simple demonstration using the following methods that can also be found on the documenation:
10+
11+
- [filter_commands](https://discordpy.readthedocs.io/en/stable/ext/commands/api.html#discord.ext.commands.HelpCommand.filter_commands)
12+
13+
- [send_group_help](https://discordpy.readthedocs.io/en/stable/ext/commands/api.html#discord.ext.commands.HelpCommand.send_bot_help)
14+
15+
- [send_command_help](https://discordpy.readthedocs.io/en/stable/ext/commands/api.html#discord.ext.commands.HelpCommand.send_command_help)
16+
17+
- [send_group_help](https://discordpy.readthedocs.io/en/stable/ext/commands/api.html#discord.ext.commands.HelpCommand.send_group_help)
18+
19+
- [send_error_message](https://discordpy.readthedocs.io/en/stable/ext/commands/api.html#discord.ext.commands.HelpCommand.send_error_message)
20+
21+
```python
22+
class MyHelp(commands.HelpCommand):
23+
24+
async def send_bot_help(self, mapping):
25+
"""
26+
This is triggered when !help is invoked.
27+
28+
This example demonstrates how to list the commands that the member invoking the help command can run.
29+
"""
30+
filtered = await self.filter_commands(self.context.bot.commands, sort=True) # returns a list of command objects
31+
names = [command.name for command in filtered] # iterating through the commands objects getting names
32+
available_commands = "\n".join(names) # joining the list of names by a new line
33+
embed = disnake.Embed(description=available_commands)
34+
await self.context.send(embed=embed)
35+
36+
async def send_command_help(self, command):
37+
"""This is triggered when !help <command> is invoked."""
38+
await self.context.send("This is the help page for a command")
39+
40+
async def send_group_help(self, group):
41+
"""This is triggered when !help <group> is invoked."""
42+
await self.context.send("This is the help page for a group command")
43+
44+
async def send_cog_help(self, cog):
45+
"""This is triggered when !help <cog> is invoked."""
46+
await self.context.send("This is the help page for a cog")
47+
48+
async def send_error_message(self, error):
49+
"""If there is an error, send a embed containing the error."""
50+
channel = self.get_destination() # this defaults to the command context channel
51+
await channel.send(error)
52+
53+
bot.help_command = MyHelp()
54+
```
55+
56+
You can handle when a user does not pass a command name when invoking the help command and make a fancy and customized embed; here a page that describes the bot and shows a list of commands is generally used. However if a command is passed in, you can display detailed information of the command. Below are references from the documentation below that can be utilised:
57+
58+
- [Get the command object](https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.Bot.get_command)
59+
60+
- [Get the command name](https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.Command.name)
61+
62+
- [Get the command aliases](https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.Command.aliases)
63+
64+
- [Get the command brief](https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.Command.brief)
65+
66+
- [Get the command usage](https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.Command.usage)

0 commit comments

Comments
 (0)