Skip to content

Commit 91cf6ed

Browse files
committed
Reworked help command
1 parent 6dda89e commit 91cf6ed

File tree

1 file changed

+52
-73
lines changed

1 file changed

+52
-73
lines changed

cogs/utility.py

Lines changed: 52 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -28,41 +28,33 @@ def __init__(self, bot):
2828
def format_cog_help(self, ctx, cog):
2929
"""Formats the text for a cog help"""
3030

31-
maxlen = 0
3231
prefix = self.bot.prefix
3332

34-
for cmd in self.bot.commands:
35-
if cmd.hidden:
36-
continue
37-
if cmd.instance is cog:
38-
len_ = len(cmd.qualified_name) + len(prefix)
39-
if len_ > maxlen:
40-
maxlen = len_
33+
fmts = ['']
34+
for cmd in sorted(self.bot.commands,
35+
key=lambda cmd: cmd.qualified_name):
36+
if cmd.instance is cog and not cmd.hidden:
37+
new_fmt = f'`{prefix+cmd.qualified_name}` - {cmd.short_doc}\n'
38+
if len(new_fmt) + len(fmts[-1]) >= 1024:
39+
fmts.append(new_fmt)
40+
else:
41+
fmts[-1] += new_fmt
4142

42-
if maxlen == 0:
43-
return
44-
45-
fmt = ''
46-
for cmd in self.bot.commands:
47-
if cmd.instance is cog:
48-
if cmd.hidden:
49-
continue
50-
51-
fmt += f'`{prefix+cmd.qualified_name:<{maxlen}}` - '
52-
fmt += f'{cmd.short_doc:<{maxlen}}\n'
53-
54-
em = Embed(
55-
description='*' + inspect.getdoc(cog) + '*',
56-
color=Color.blurple()
57-
)
58-
em.set_author(name=cog.__class__.__name__ + ' - Help',
59-
icon_url=ctx.bot.user.avatar_url)
43+
embeds = []
44+
for fmt in fmts:
45+
em = Embed(
46+
description='*' + inspect.getdoc(cog) + '*',
47+
color=Color.blurple()
48+
)
6049

61-
em.add_field(name='Commands', value=fmt)
50+
em.add_field(name='Commands', value=fmt)
51+
em.set_author(name=cog.__class__.__name__ + ' - Help',
52+
icon_url=ctx.bot.user.avatar_url)
6253

63-
em.set_footer(text=f'Type "{prefix}help command" '
64-
'for more info on a command.')
65-
return em
54+
em.set_footer(text=f'Type "{prefix}help command" '
55+
'for more info on a command.')
56+
embeds.append(em)
57+
return embeds
6658

6759
def format_command_help(self, ctx, cmd):
6860
"""Formats command help."""
@@ -72,27 +64,22 @@ def format_command_help(self, ctx, cmd):
7264
description=cmd.help
7365
)
7466

75-
if hasattr(cmd, 'invoke_without_command') and \
76-
cmd.invoke_without_command:
77-
em.title = f'`Usage: {prefix}{cmd.signature}`'
78-
else:
79-
em.title = f'`{prefix}{cmd.signature}`'
67+
em.title = f'`{prefix}{cmd.signature}`'
8068

81-
if not hasattr(cmd, 'commands'):
69+
if not isinstance(cmd, commands.Group):
8270
return em
8371

84-
maxlen = max(len(prefix + str(c)) for c in cmd.commands)
8572
fmt = ''
86-
87-
for i, c in enumerate(cmd.commands):
88-
if len(cmd.commands) == i + 1: # last
89-
branch = '└─ ' + c.name
73+
length = len(cmd.all_commands)
74+
for i, (name, c) in enumerate(sorted(cmd.all_commands.items(),
75+
key=lambda c: c[0])):
76+
if length == i + 1: # last
77+
branch = '└─'
9078
else:
91-
branch = '├─ ' + c.name
92-
fmt += f"`{branch:<{maxlen+1}}` - "
93-
fmt += f"{c.short_doc:<{maxlen}}\n"
79+
branch = '├─'
80+
fmt += f"`{branch} {name}` - {c.short_doc}\n"
9481

95-
em.add_field(name='Sub-commands', value=fmt)
82+
em.add_field(name='Sub Commands', value=fmt)
9683
em.set_footer(
9784
text=f'Type "{prefix}help {cmd} command" '
9885
'for more info on a command.'
@@ -102,21 +89,16 @@ def format_command_help(self, ctx, cmd):
10289
def format_not_found(self, ctx, command):
10390
prefix = ctx.prefix
10491
em = Embed(
105-
title='Could not find a cog or command by that name.',
92+
title='Unable to Find Command or Category',
10693
color=Color.red()
10794
)
10895
em.set_footer(text=f'Type "{prefix}help" to get '
10996
'a full list of commands.')
110-
cogs = get_close_matches(command, self.bot.cogs.keys())
111-
cmds = get_close_matches(command, self.bot.all_commands.keys())
112-
if cogs or cmds:
113-
em.description = 'Did you mean...'
114-
if cogs:
115-
em.add_field(name='Cogs', value='\n'.join(f'`{x}`'
116-
for x in cogs))
117-
if cmds:
118-
em.add_field(name='Commands', value='\n'.join(f'`{x}`'
119-
for x in cmds))
97+
98+
choices = set(self.bot.cogs.keys()) | set(self.bot.all_commands.keys())
99+
closest = get_close_matches(command, choices, n=1, cutoff=0.45)
100+
if closest:
101+
em.description = f'**Perhaps you meant:**\n\u2000- `{closest[0]}`'
120102
return em
121103

122104
@commands.command()
@@ -125,27 +107,24 @@ async def help(self, ctx, *, command: str = None):
125107
"""Shows the help message."""
126108

127109
if command is not None:
128-
cog = self.bot.cogs.get(command)
129110
cmd = self.bot.get_command(command)
130-
if cog is not None:
131-
em = self.format_cog_help(ctx, cog)
132-
elif cmd is not None:
133-
em = self.format_command_help(ctx, cmd)
111+
cog = self.bot.cogs.get(command)
112+
if cmd is not None:
113+
embeds = [self.format_command_help(ctx, cmd)]
114+
elif cog is not None:
115+
embeds = self.format_cog_help(ctx, cog)
134116
else:
135-
em = self.format_not_found(ctx, command)
136-
if em:
137-
return await ctx.send(embed=em)
138-
139-
pages = []
117+
embeds = [self.format_not_found(ctx, command)]
118+
p_session = PaginatorSession(ctx, *embeds)
119+
return await p_session.run()
140120

141-
for _, cog in sorted(self.bot.cogs.items()):
142-
em = self.format_cog_help(ctx, cog)
143-
if em:
144-
pages.append(em)
145-
146-
p_session = PaginatorSession(ctx, *pages)
121+
embeds = []
122+
for cog in sorted(self.bot.cogs.values(),
123+
key=lambda cog: cog.__class__.__name__):
124+
embeds.extend(self.format_cog_help(ctx, cog))
147125

148-
await p_session.run()
126+
p_session = PaginatorSession(ctx, *embeds)
127+
return await p_session.run()
149128

150129
@commands.command()
151130
@trigger_typing

0 commit comments

Comments
 (0)