Skip to content

Commit f36c181

Browse files
committed
Fix help cmd (check checks, blank cogs)
1 parent b70f9b0 commit f36c181

File tree

3 files changed

+60
-20
lines changed

3 files changed

+60
-20
lines changed

cogs/plugins.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Plugins:
2020
2121
These addons could have a range of features from moderation to simply
2222
making your life as a moderator easier!
23-
Learn how to create a plugin yourself here: https://link.com
23+
Learn how to create a plugin yourself here: https://github.com/kyb3r/modmail/wiki/Plugins
2424
"""
2525
def __init__(self, bot: Bot):
2626
self.bot = bot
@@ -110,6 +110,9 @@ async def add(self, ctx, *, plugin_name):
110110
"""Adds a plugin"""
111111
if plugin_name in self.bot.config.plugins:
112112
return await ctx.send('Plugin already installed')
113+
if plugin_name in self.bot.cogs.keys():
114+
# another class with the same name
115+
return await ctx.send('Another cog exists with the same name')
113116

114117
message = await ctx.send('Downloading plugin...')
115118
async with ctx.typing():

cogs/utility.py

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from json import JSONDecodeError
88
from textwrap import indent
99

10+
import discord
1011
from aiohttp import ClientResponseError
1112
from discord import Embed, Color, Activity
1213
from discord.enums import ActivityType
@@ -26,15 +27,25 @@ class Utility:
2627
def __init__(self, bot: Bot):
2728
self.bot = bot
2829

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):
3041
"""Formats the text for a cog help"""
3142

3243
prefix = self.bot.prefix
3344

3445
fmts = ['']
3546
for cmd in sorted(self.bot.commands,
3647
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):
3849
new_fmt = f'`{prefix + cmd.qualified_name}` - '
3950
new_fmt += f'{cmd.short_doc}\n'
4051
if len(new_fmt) + len(fmts[-1]) >= 1024:
@@ -44,6 +55,8 @@ def format_cog_help(self, ctx, cog):
4455

4556
embeds = []
4657
for fmt in fmts:
58+
if fmt == '':
59+
continue
4760
embed = Embed(
4861
description='*' + inspect.getdoc(cog) + '*',
4962
color=self.bot.main_color
@@ -58,8 +71,11 @@ def format_cog_help(self, ctx, cog):
5871
embeds.append(embed)
5972
return embeds
6073

61-
def format_command_help(self, cmd):
74+
async def format_command_help(self, ctx, cmd):
6275
"""Formats command help."""
76+
if cmd.hidden or not await self.check_checks(ctx, cmd):
77+
return None
78+
6379
prefix = self.bot.prefix
6480
embed = Embed(
6581
color=self.bot.main_color,
@@ -88,7 +104,7 @@ def format_command_help(self, cmd):
88104
)
89105
return embed
90106

91-
def format_not_found(self, ctx, command):
107+
async def format_not_found(self, ctx, command):
92108
prefix = ctx.prefix
93109
embed = Embed(
94110
title='Unable to Find Command or Category',
@@ -97,7 +113,19 @@ def format_not_found(self, ctx, command):
97113
embed.set_footer(text=f'Type "{prefix}help" to get '
98114
'a full list of commands.')
99115

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+
101129
closest = get_close_matches(command, choices, n=1, cutoff=0.45)
102130
if closest:
103131
# Perhaps you meant:
@@ -106,27 +134,35 @@ def format_not_found(self, ctx, command):
106134
f'\u2000- `{closest[0]}`')
107135
return embed
108136

109-
@commands.command()
137+
@commands.command(name='help')
110138
@trigger_typing
111-
async def help(self, ctx, *, command: str = None):
139+
async def help_(self, ctx, *, command: str = None):
112140
"""Shows the help message."""
113141

114-
if command is not None:
142+
if command:
115143
cmd = self.bot.get_command(command)
116144
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+
123159
p_session = PaginatorSession(ctx, *embeds)
124160
return await p_session.run()
125161

126162
embeds = []
127163
for cog in sorted(self.bot.cogs.values(),
128164
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))
130166

131167
p_session = PaginatorSession(ctx, *embeds)
132168
return await p_session.run()

config.json.example

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
2-
"TOKEN": "MyBotToken",
3-
"MODMAIL_API_TOKEN": "MyApiToken",
4-
"GUILD_ID": 1234567890,
5-
"OWNERS": "Owner1ID,Owner2ID,Owner3ID",
2+
"TOKEN": "MyBotToken",
3+
"MODMAIL_API_TOKEN": "MyApiToken",
4+
"GUILD_ID": 1234567890,
5+
"OWNERS": "Owner1ID,Owner2ID,Owner3ID",
6+
"MONGO_URI": "mongodb+srv://mongodburi"
67
}

0 commit comments

Comments
 (0)