Last active
January 12, 2022 06:09
-
-
Save DriftAsimov/cf993824343a9d369b38f6a08d2946df to your computer and use it in GitHub Desktop.
Discord.py hardcoded help command but it's better
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@bot.command() | |
async def help(ctx: commands.Context, *, name: str = None): | |
name = name.strip() | |
if not name: | |
commands = bot.commands | |
embed = discord.Embed(title="Commands Menu", color=0x8DDFE3) | |
embed.set_thumbnail(url=bot.user.avatar_url) | |
for name, cog in bot.cogs.items(): | |
embed.add_field( | |
name=cog.qualified_name, | |
value=", ".join([i.name for i in cog.get_commands()]), | |
inline=False, | |
) | |
misc = ", ".join([i.name for i in bot.commands if not i.cog]) | |
if len(misc) > 1: | |
embed.add_field(name="Miscellanous", value=misc) | |
await ctx.send(embed=embed) | |
else: | |
if name.lower() in [i.lower() for i in bot.cogs]: | |
raw_cog = [i for i in bot.cogs if name.lower() == i.lower()][0] | |
clog = bot.get_cog(raw_cog) | |
embed = discord.Embed(color=0x8DDFE3) | |
embed.title = clog.qualified_name | |
embed.description = clog.description | |
for command in clog.get_commands(): | |
embed.add_field(name=command.name, value=command.short_doc) | |
await ctx.send(embed=embed) | |
elif name.lower() in [i.name.lower() for i in bot.commands]: | |
await ctx.send("ay") | |
command = [i for i in bot.commands if name.lower() == i.name.lower()][0] | |
if not hasattr(command, "commands"): | |
embed = discord.Embed( | |
title=command.name, description=command.short_doc, color=0x8DDFE3 | |
) | |
embed.add_field( | |
name="Usage", | |
value=f"{ctx.prefix}{command.qualified_name} {command.signature}", | |
) | |
await ctx.send(embed=embed) | |
else: | |
embed = discord.Embed( | |
title=command.name, description=command.short_doc, color=0x8DDFE3 | |
) | |
for child in list(command.commands): | |
embed.add_field( | |
name=child.name, value=f"{command.name} {child.name}" | |
) | |
await ctx.send(embed=embed) |
Here's a version of this command subclassed under commands.HelpCommand
which is ironically a better version than the original one since this version shows the subcommands of the asked group
(for the ones who aren't the type of ppl to "doing things the way they are not intended to")
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!", help_command=None)
class HelpCmd(commands.HelpCommand):
async def send_bot_help(self, mapping):
embed = discord.Embed(title="Commands Menu", color=0x8DDFE3)
embed.set_thumbnail(url=bot.user.avatar_url)
for cog, cmds in mapping.items():
if not cog:
if len(cmds):
embed.add_field(
name="Miscellaneous",
value=", ".join([i.name for i in cmds]),
inline=False,
)
else:
embed.add_field(
name=cog.qualified_name,
value=", ".join([i.name for i in cmds]),
inline=False,
)
await self.context.send(embed=embed)
async def send_cog_help(self, cog):
embed = discord.Embed(color=0x8DDFE3)
embed.title = cog.qualified_name
embed.description = cog.description
for command in cog.get_commands():
embed.add_field(name=command.name, value=command.short_doc)
await self.context.send(embed=embed)
async def send_group_help(self, group):
embed = discord.Embed(
title=group.name, description=group.short_doc, color=0x8DDFE3
)
embed.add_field(
name="Usage",
value=f"{self.context.prefix}{group.qualified_name} {group.signature}",
)
if len(group.commands):
embed.add_field(
name="Subcommands",
value=", ".join([i.name for i in group.commands]),
inline=False,
)
await self.context.send(embed=embed)
async def send_command_help(self, command):
embed = discord.Embed(
title=command.name, description=command.short_doc, color=0x8DDFE3
)
embed.add_field(
name="Usage",
value=f"{self.context.prefix}{command.qualified_name} {command.signature}",
)
await self.context.send(embed=embed)
bot.help_command = HelpCmd()
I didn't test this lmao
References:
- Documentation for the
Group
class - Stella's walkthrough guide on subclassing commands.HelpCommand
- and ofc, the original gist
Arigato! I am totally not late in replying
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please Note
This example is just another one of those "doing things the way they are not intended to". It is not recommended to follow this way, prefer subclassing the default help command instead. You can refer this gist for that.