7
7
from json import JSONDecodeError
8
8
from textwrap import indent
9
9
10
+ import discord
10
11
from aiohttp import ClientResponseError
11
12
from discord import Embed , Color , Activity
12
13
from discord .enums import ActivityType
@@ -26,15 +27,25 @@ class Utility:
26
27
def __init__ (self , bot : Bot ):
27
28
self .bot = bot
28
29
29
- def format_cog_help (self , ctx , cog ):
30
+ async def check_checks (self , ctx , cmd ):
31
+ predicates = cmd .checks
32
+ if not predicates :
33
+ return True
34
+
35
+ try :
36
+ return await discord .utils .async_all (predicate (ctx ) for predicate in predicates )
37
+ except commands .CheckFailure :
38
+ return False
39
+
40
+ async def format_cog_help (self , ctx , cog ):
30
41
"""Formats the text for a cog help"""
31
42
32
43
prefix = self .bot .prefix
33
44
34
45
fmts = ['' ]
35
46
for cmd in sorted (self .bot .commands ,
36
47
key = lambda cmd : cmd .qualified_name ):
37
- if cmd .instance is cog and not cmd .hidden :
48
+ if cmd .instance is cog and not cmd .hidden and await self . check_checks ( ctx , cmd ) :
38
49
new_fmt = f'`{ prefix + cmd .qualified_name } ` - '
39
50
new_fmt += f'{ cmd .short_doc } \n '
40
51
if len (new_fmt ) + len (fmts [- 1 ]) >= 1024 :
@@ -44,6 +55,8 @@ def format_cog_help(self, ctx, cog):
44
55
45
56
embeds = []
46
57
for fmt in fmts :
58
+ if fmt == '' :
59
+ continue
47
60
embed = Embed (
48
61
description = '*' + inspect .getdoc (cog ) + '*' ,
49
62
color = self .bot .main_color
@@ -58,8 +71,11 @@ def format_cog_help(self, ctx, cog):
58
71
embeds .append (embed )
59
72
return embeds
60
73
61
- def format_command_help (self , cmd ):
74
+ async def format_command_help (self , ctx , cmd ):
62
75
"""Formats command help."""
76
+ if cmd .hidden or not await self .check_checks (ctx , cmd ):
77
+ return None
78
+
63
79
prefix = self .bot .prefix
64
80
embed = Embed (
65
81
color = self .bot .main_color ,
@@ -88,7 +104,7 @@ def format_command_help(self, cmd):
88
104
)
89
105
return embed
90
106
91
- def format_not_found (self , ctx , command ):
107
+ async def format_not_found (self , ctx , command ):
92
108
prefix = ctx .prefix
93
109
embed = Embed (
94
110
title = 'Unable to Find Command or Category' ,
@@ -97,7 +113,19 @@ def format_not_found(self, ctx, command):
97
113
embed .set_footer (text = f'Type "{ prefix } help" to get '
98
114
'a full list of commands.' )
99
115
100
- choices = set (self .bot .cogs .keys ()) | set (self .bot .all_commands .keys ())
116
+ choices = set ()
117
+ # filter out hidden commands & blank cogs
118
+ for i in self .bot .cogs :
119
+ for cmd in self .bot .commands :
120
+ if cmd .cog_name == i and not cmd .hidden and await self .check_checks (ctx , cmd ):
121
+ # as long as there's one valid cmd, add cog
122
+ choices .add (i )
123
+ break
124
+
125
+ for i in self .bot .commands :
126
+ if not i .hidden and await self .check_checks (ctx , i ):
127
+ choices .add (i .name )
128
+
101
129
closest = get_close_matches (command , choices , n = 1 , cutoff = 0.45 )
102
130
if closest :
103
131
# Perhaps you meant:
@@ -106,27 +134,35 @@ def format_not_found(self, ctx, command):
106
134
f'\u2000 - `{ closest [0 ]} `' )
107
135
return embed
108
136
109
- @commands .command ()
137
+ @commands .command (name = 'help' )
110
138
@trigger_typing
111
- async def help (self , ctx , * , command : str = None ):
139
+ async def help_ (self , ctx , * , command : str = None ):
112
140
"""Shows the help message."""
113
141
114
- if command is not None :
142
+ if command :
115
143
cmd = self .bot .get_command (command )
116
144
cog = self .bot .cogs .get (command )
117
- if cmd is not None :
118
- embeds = [self .format_command_help (cmd )]
119
- elif cog is not None :
120
- embeds = self .format_cog_help (ctx , cog )
121
- else :
122
- embeds = [self .format_not_found (ctx , command )]
145
+ embeds = []
146
+
147
+ if cmd :
148
+ help_msg = await self .format_command_help (ctx , cmd )
149
+ if help_msg :
150
+ embeds = [help_msg ]
151
+
152
+ elif cog :
153
+ # checks if cog has commands
154
+ embeds = await self .format_cog_help (ctx , cog )
155
+
156
+ if not embeds :
157
+ embeds = [await self .format_not_found (ctx , command )]
158
+
123
159
p_session = PaginatorSession (ctx , * embeds )
124
160
return await p_session .run ()
125
161
126
162
embeds = []
127
163
for cog in sorted (self .bot .cogs .values (),
128
164
key = lambda cog : cog .__class__ .__name__ ):
129
- embeds .extend (self .format_cog_help (ctx , cog ))
165
+ embeds .extend (await self .format_cog_help (ctx , cog ))
130
166
131
167
p_session = PaginatorSession (ctx , * embeds )
132
168
return await p_session .run ()
0 commit comments