From 61f0c9423e21b08f550b019a57482947be33fc67 Mon Sep 17 00:00:00 2001 From: Abigail Gold Date: Mon, 20 Jan 2020 20:52:22 -0500 Subject: [PATCH 1/4] WIP: help command checks --- exts/base.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/exts/base.py b/exts/base.py index d99cbc7..5498c32 100644 --- a/exts/base.py +++ b/exts/base.py @@ -61,7 +61,8 @@ class QrmHelpCommand(commands.HelpCommand): '. Many commands have shorter aliases.') for cat, cmds in mapping.items(): - cmds = list(filter(lambda x: not x.hidden, cmds)) + 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]) @@ -72,18 +73,23 @@ class QrmHelpCommand(commands.HelpCommand): await self.context.send(embed=embed) async def send_command_help(self, command): - embed = cmn.embed_factory(self.context) - embed.title = self.get_command_signature(command) - embed.description = command.help - await self.context.send(embed=embed) + 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) async def send_group_help(self, group): - embed = cmn.embed_factory(self.context) - embed.title = self.get_command_signature(group) - embed.description = group.help - for cmd in group.commands: - embed.add_field(name=self.get_command_signature(cmd), value=cmd.help, inline=False) - await self.context.send(embed=embed) + 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) class BaseCog(commands.Cog): From 831667ec109e826e1925e774034448a9d5379f04 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 21 Jan 2020 22:21:30 -0500 Subject: [PATCH 2/4] helpcommand now verifies checks when displaying help Additionally: - added admin category - removed hidden flag from commands - added checks to all extctl commands Fixes #109 --- common.py | 3 ++- exts/base.py | 46 ++++++++++++++++++++++++---------------------- exts/fun.py | 2 +- main.py | 18 +++++++++++------- 4 files changed, 38 insertions(+), 31 deletions(-) diff --git a/common.py b/common.py index bb46f3f..839ce6b 100644 --- a/common.py +++ b/common.py @@ -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='❌', diff --git a/exts/base.py b/exts/base.py index 5498c32..163b52e 100644 --- a/exts/base.py +++ b/exts/base.py @@ -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.""" diff --git a/exts/fun.py b/exts/fun.py index 136643d..ca6ebb6 100644 --- a/exts/fun.py +++ b/exts/fun.py @@ -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:') diff --git a/main.py b/main.py index c3b4ae4..48c3b5b 100644 --- a/main.py +++ b/main.py @@ -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) From ea508d2a5974cba7f491ecad54c7b791f215bddf Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 28 Jan 2020 01:19:21 -0500 Subject: [PATCH 3/4] remove unnecessary categories --- main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 48c3b5b..b68303e 100644 --- a/main.py +++ b/main.py @@ -84,7 +84,7 @@ async def _extctl(ctx: commands.Context): await ctx.invoke(cmd) -@_extctl.command(name="list", category=cmn.cat.admin) +@_extctl.command(name="list") @commands.check(cmn.check_if_owner) async def _extctl_list(ctx: commands.Context): """Lists Extensions.""" @@ -94,7 +94,7 @@ async def _extctl_list(ctx: commands.Context): await ctx.send(embed=embed) -@_extctl.command(name="load", category=cmn.cat.admin) +@_extctl.command(name="load") @commands.check(cmn.check_if_owner) async def _extctl_load(ctx: commands.Context, extension: str): try: @@ -105,7 +105,7 @@ async def _extctl_load(ctx: commands.Context, extension: str): await ctx.send(embed=embed) -@_extctl.command(name="reload", aliases=["relaod"], category=cmn.cat.admin) +@_extctl.command(name="reload", aliases=["relaod"]) @commands.check(cmn.check_if_owner) async def _extctl_reload(ctx: commands.Context, extension: str): if ctx.invoked_with == "relaod": @@ -120,7 +120,7 @@ async def _extctl_reload(ctx: commands.Context, extension: str): await ctx.send(embed=embed) -@_extctl.command(name="unload", category=cmn.cat.admin) +@_extctl.command(name="unload") @commands.check(cmn.check_if_owner) async def _extctl_unload(ctx: commands.Context, extension: str): try: From 6c32dcbead8be2cfc851a7250435473040cafacd Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 28 Jan 2020 22:45:10 -0500 Subject: [PATCH 4/4] fix can_run logic, re-hide xd, remove unnecessary checks --- exts/base.py | 10 ++++++---- exts/fun.py | 2 +- main.py | 4 ---- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/exts/base.py b/exts/base.py index 6f36582..15b20b2 100644 --- a/exts/base.py +++ b/exts/base.py @@ -76,9 +76,12 @@ class QrmHelpCommand(commands.HelpCommand): await self.context.send(embed=embed) async def send_command_help(self, command): - if self.verify_checks and not await command.can_run(self.context): - raise commands.CheckFailure - return + if self.verify_checks: + if not await command.can_run(self.context): + raise commands.CheckFailure + for p in command.parents: + if not await p.can_run(self.context): + raise commands.CheckFailure embed = cmn.embed_factory(self.context) embed.title = await self.get_command_signature(command) embed.description = command.help @@ -87,7 +90,6 @@ class QrmHelpCommand(commands.HelpCommand): async def send_group_help(self, group): 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 diff --git a/exts/fun.py b/exts/fun.py index 6fcbae5..d84f513 100644 --- a/exts/fun.py +++ b/exts/fun.py @@ -30,7 +30,7 @@ class FunCog(commands.Cog): '''Returns an xkcd about tar.''' await ctx.send('http://xkcd.com/1168') - @commands.command(name="xd", category=cmn.cat.fun) + @commands.command(name="xd", hidden=True, category=cmn.cat.fun) async def _xd(self, ctx: commands.Context): '''ecks dee''' await ctx.send('ECKS DEE :smirk:') diff --git a/main.py b/main.py index 3cb103d..4fb577b 100644 --- a/main.py +++ b/main.py @@ -93,7 +93,6 @@ async def _extctl(ctx: commands.Context): @_extctl.command(name="list", aliases=["ls"]) -@commands.check(cmn.check_if_owner) async def _extctl_list(ctx: commands.Context): """Lists Extensions.""" embed = cmn.embed_factory(ctx) @@ -103,7 +102,6 @@ async def _extctl_list(ctx: commands.Context): @_extctl.command(name="load", aliases=["ld"]) -@commands.check(cmn.check_if_owner) async def _extctl_load(ctx: commands.Context, extension: str): try: bot.load_extension(ext_dir + "." + extension) @@ -114,7 +112,6 @@ async def _extctl_load(ctx: commands.Context, extension: str): @_extctl.command(name="reload", aliases=["rl", "r", "relaod"]) -@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) @@ -129,7 +126,6 @@ async def _extctl_reload(ctx: commands.Context, extension: str): @_extctl.command(name="unload", aliases=["ul"]) -@commands.check(cmn.check_if_owner) async def _extctl_unload(ctx: commands.Context, extension: str): try: bot.unload_extension(ext_dir + "." + extension)