helpcommand now verifies checks when displaying help

Additionally:
- added admin category
- removed hidden flag from commands
- added checks to all extctl commands

Fixes #109
This commit is contained in:
Abigail 2020-01-21 22:21:30 -05:00
parent 61f0c9423e
commit 831667ec10
4 changed files with 38 additions and 31 deletions

View File

@ -40,7 +40,8 @@ cat = SimpleNamespace(lookup='Information Lookup',
maps='Mapping',
ref='Reference',
study='Exam Study',
weather='Land and Space Weather')
weather='Land and Space Weather',
admin='Bot Control')
emojis = SimpleNamespace(check_mark='',
x='',

View File

@ -23,11 +23,13 @@ import common as cmn
class QrmHelpCommand(commands.HelpCommand):
def __init__(self):
super().__init__(command_attrs={'help': 'Shows help about qrm or a command', 'aliases': ['h']})
self.verify_checks = True
def get_bot_mapping(self):
async def get_bot_mapping(self):
bot = self.context.bot
mapping = {}
for cmd in bot.commands:
for cmd in await self.filter_commands(bot.commands, sort=True):
cat = cmd.__original_kwargs__.get('category', None)
if cat in mapping:
mapping[cat].append(cmd)
@ -35,7 +37,7 @@ class QrmHelpCommand(commands.HelpCommand):
mapping[cat] = [cmd]
return mapping
def get_command_signature(self, command):
async def get_command_signature(self, command):
parent = command.full_parent_name
if command.aliases != []:
aliases = ', '.join(command.aliases)
@ -59,10 +61,9 @@ class QrmHelpCommand(commands.HelpCommand):
embed.title = 'qrm Help'
embed.description = (f'For command-specific help and usage, use `{opt.prefix}help [command name]`'
'. Many commands have shorter aliases.')
mapping = await mapping
for cat, cmds in mapping.items():
if self.context.author.id not in opt.owners_uids:
cmds = list(filter(lambda x: not x.hidden, cmds))
if cmds == []:
continue
names = sorted([cmd.name for cmd in cmds])
@ -73,23 +74,24 @@ class QrmHelpCommand(commands.HelpCommand):
await self.context.send(embed=embed)
async def send_command_help(self, command):
if not command.hidden or self.context.author.id in opt.owners_uids:
embed = cmn.embed_factory(self.context)
embed.title = self.get_command_signature(command)
embed.description = command.help
await self.context.send(embed=embed)
else:
await cmn.add_react(self.context, cmn.no_entry)
if self.verify_checks and not await command.can_run(self.context):
raise commands.CheckFailure
return
embed = cmn.embed_factory(self.context)
embed.title = await self.get_command_signature(command)
embed.description = command.help
await self.context.send(embed=embed)
async def send_group_help(self, group):
if not group.hidden or self.context.author.id in opt.owners_uids:
embed = cmn.embed_factory(self.context)
embed.title = self.get_command_signature(group)
embed.description = group.help
for cmd in group.commands:
if not cmd.hidden or self.context.author.id in opt.owners_uids:
embed.add_field(name=self.get_command_signature(cmd), value=cmd.help, inline=False)
await self.context.send(embed=embed)
if self.verify_checks and not await group.can_run(self.context):
raise commands.CheckFailure
return
embed = cmn.embed_factory(self.context)
embed.title = await self.get_command_signature(group)
embed.description = group.help
for cmd in await self.filter_commands(group.commands, sort=True):
embed.add_field(name=await self.get_command_signature(cmd), value=cmd.help, inline=False)
await self.context.send(embed=embed)
class BaseCog(commands.Cog):
@ -170,12 +172,12 @@ class BaseCog(commands.Cog):
"(https://github.com/classabbyamp/discord-qrm2/issues)!")
await ctx.send(embed=embed)
@commands.command(name="bruce", hidden=True)
@commands.command(name="bruce")
async def _b_issue(self, ctx: commands.Context):
"""Shows how to create an issue for the bot."""
await ctx.invoke(self._issue)
@commands.command(name="echo", aliases=["e"], hidden=True)
@commands.command(name="echo", aliases=["e"], category=cmn.cat.admin)
@commands.check(cmn.check_if_owner)
async def _echo(self, ctx: commands.Context, channel: commands.TextChannelConverter, *, msg: str):
"""Send a message in a channel as qrm. Only works within a server or DM to server, not between servers."""

View File

@ -30,7 +30,7 @@ class FunCog(commands.Cog):
'''Returns an xkcd about tar.'''
await ctx.send('http://xkcd.com/1168')
@commands.command(name="xd", hidden=True, category=cmn.cat.fun)
@commands.command(name="xd", category=cmn.cat.fun)
async def _xd(self, ctx: commands.Context):
'''ecks dee'''
await ctx.send('ECKS DEE :smirk:')

18
main.py
View File

@ -50,7 +50,7 @@ bot.qrm.debug_mode = debug_mode
# --- Commands ---
@bot.command(name="restart", hidden=True)
@bot.command(name="restart", category=cmn.cat.admin)
@commands.check(cmn.check_if_owner)
async def _restart_bot(ctx: commands.Context):
"""Restarts the bot."""
@ -62,7 +62,7 @@ async def _restart_bot(ctx: commands.Context):
await bot.logout()
@bot.command(name="shutdown", hidden=True)
@bot.command(name="shutdown", category=cmn.cat.admin)
@commands.check(cmn.check_if_owner)
async def _shutdown_bot(ctx: commands.Context):
"""Shuts down the bot."""
@ -74,7 +74,7 @@ async def _shutdown_bot(ctx: commands.Context):
await bot.logout()
@bot.group(name="extctl", hidden=True)
@bot.group(name="extctl", category=cmn.cat.admin)
@commands.check(cmn.check_if_owner)
async def _extctl(ctx: commands.Context):
"""Extension control commands.
@ -84,7 +84,8 @@ async def _extctl(ctx: commands.Context):
await ctx.invoke(cmd)
@_extctl.command(name="list")
@_extctl.command(name="list", category=cmn.cat.admin)
@commands.check(cmn.check_if_owner)
async def _extctl_list(ctx: commands.Context):
"""Lists Extensions."""
embed = cmn.embed_factory(ctx)
@ -93,7 +94,8 @@ async def _extctl_list(ctx: commands.Context):
await ctx.send(embed=embed)
@_extctl.command(name="load")
@_extctl.command(name="load", category=cmn.cat.admin)
@commands.check(cmn.check_if_owner)
async def _extctl_load(ctx: commands.Context, extension: str):
try:
bot.load_extension(ext_dir + "." + extension)
@ -103,7 +105,8 @@ async def _extctl_load(ctx: commands.Context, extension: str):
await ctx.send(embed=embed)
@_extctl.command(name="reload", aliases=["relaod"])
@_extctl.command(name="reload", aliases=["relaod"], category=cmn.cat.admin)
@commands.check(cmn.check_if_owner)
async def _extctl_reload(ctx: commands.Context, extension: str):
if ctx.invoked_with == "relaod":
pika = bot.get_emoji(opt.pika)
@ -117,7 +120,8 @@ async def _extctl_reload(ctx: commands.Context, extension: str):
await ctx.send(embed=embed)
@_extctl.command(name="unload")
@_extctl.command(name="unload", category=cmn.cat.admin)
@commands.check(cmn.check_if_owner)
async def _extctl_unload(ctx: commands.Context, extension: str):
try:
bot.unload_extension(ext_dir + "." + extension)