|
6 | 6 | from datetime import datetime |
7 | 7 | from difflib import get_close_matches |
8 | 8 | from io import StringIO |
| 9 | +from operator import itemgetter |
9 | 10 | from typing import Union |
10 | 11 | from json import JSONDecodeError |
11 | 12 | from pkg_resources import parse_version |
@@ -33,23 +34,27 @@ class Utility: |
33 | 34 | def __init__(self, bot: Bot): |
34 | 35 | self.bot = bot |
35 | 36 |
|
| 37 | + @staticmethod |
| 38 | + def get_perms_required(cmd) -> PermissionLevel: |
| 39 | + for c in cmd.checks: |
| 40 | + perm_level = getattr(c, 'permission_level', |
| 41 | + PermissionLevel.INVALID) |
| 42 | + if perm_level is not PermissionLevel.INVALID: |
| 43 | + return perm_level |
| 44 | + return PermissionLevel.INVALID |
| 45 | + |
36 | 46 | async def format_cog_help(self, ctx, cog): |
37 | 47 | """Formats the text for a cog help""" |
38 | 48 |
|
39 | 49 | prefix = self.bot.prefix |
40 | 50 |
|
41 | | - def perms_required(cmd): |
42 | | - for c in cmd.checks: |
43 | | - return getattr(c, 'permission_level', 0) |
44 | | - return 0 |
45 | | - |
46 | 51 | fmts = [''] |
47 | | - for cmd in sorted(self.bot.commands, |
48 | | - key=lambda cmd: perms_required(cmd)): |
| 52 | + for perm_level, cmd in sorted(((self.get_perms_required(c), c) for c in self.bot.commands), |
| 53 | + key=itemgetter(0)): |
49 | 54 | if cmd.instance is cog and not cmd.hidden: |
50 | | - new_fmt = f'`{prefix + cmd.qualified_name}` ' |
51 | | - perm_level = perms_required(cmd) |
52 | | - if perm_level is not None: |
| 55 | + if perm_level is PermissionLevel.INVALID: |
| 56 | + new_fmt = f'`{prefix + cmd.qualified_name}` ' |
| 57 | + else: |
53 | 58 | new_fmt = f'`[{perm_level}] {prefix + cmd.qualified_name}` ' |
54 | 59 |
|
55 | 60 | new_fmt += f'- {cmd.short_doc}\n' |
@@ -88,15 +93,17 @@ async def format_command_help(self, cmd): |
88 | 93 |
|
89 | 94 | prefix = self.bot.prefix |
90 | 95 |
|
91 | | - perm_level = next(getattr(c, 'permission_level', None) for c in cmd.checks) |
92 | | - perm_level = f'{perm_level.name} [{perm_level}]' if perm_level is not None else '' |
| 96 | + perm_level = self.get_perms_required(cmd) |
| 97 | + if perm_level is not PermissionLevel.INVALID: |
| 98 | + perm_level = f'{perm_level.name} [{perm_level}]' |
| 99 | + else: |
| 100 | + perm_level = '' |
93 | 101 |
|
94 | 102 | embed = Embed( |
95 | 103 | title=f'`{prefix}{cmd.signature}`', |
96 | 104 | color=self.bot.main_color, |
97 | 105 | description=cmd.help |
98 | 106 | ) |
99 | | - |
100 | 107 |
|
101 | 108 | if not isinstance(cmd, commands.Group): |
102 | 109 | embed.set_footer(text=f'Permission level: {perm_level}') |
|
0 commit comments