diff options
| author | 2019-01-19 22:05:37 +1000 | |
|---|---|---|
| committer | 2019-01-19 22:05:37 +1000 | |
| commit | e5c864d294412745f7dcfe064f44c6d596c7bc31 (patch) | |
| tree | 18bdf0e83fddf5ecbdde2084f0ad6dcd04e1e16a | |
| parent | Merge branch 'master' into newline-antispam (diff) | |
| parent | Merge pull request #284 from python-discord/redirect-help-botcommands (diff) | |
Merge branch 'master' into newline-antispam
| -rw-r--r-- | bot/cogs/help.py | 5 | ||||
| -rw-r--r-- | bot/constants.py | 7 | ||||
| -rw-r--r-- | bot/decorators.py | 42 | ||||
| -rw-r--r-- | config-default.yml | 3 |
4 files changed, 54 insertions, 3 deletions
diff --git a/bot/cogs/help.py b/bot/cogs/help.py index ded068123..2168e2a60 100644 --- a/bot/cogs/help.py +++ b/bot/cogs/help.py @@ -10,6 +10,8 @@ from discord.ext.commands import CheckFailure from fuzzywuzzy import fuzz, process from bot import constants +from bot.constants import Roles +from bot.decorators import redirect_output from bot.pagination import ( DELETE_EMOJI, FIRST_EMOJI, LAST_EMOJI, LEFT_EMOJI, LinePaginator, RIGHT_EMOJI, @@ -23,7 +25,7 @@ REACTIONS = { LAST_EMOJI: 'end', DELETE_EMOJI: 'stop' } - +STAFF_ROLES = Roles.helpers, Roles.moderator, Roles.admin, Roles.owner Cog = namedtuple('Cog', ['name', 'description', 'commands']) @@ -652,6 +654,7 @@ class Help: Custom Embed Pagination Help feature """ @commands.command('help') + @redirect_output(constants.Channels.bot, STAFF_ROLES) async def new_help(self, ctx, *commands): """ Shows Command Help. diff --git a/bot/constants.py b/bot/constants.py index 0e0c1845b..f23696e4d 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -482,6 +482,13 @@ class Free(metaclass=YAMLGetter): cooldown_per: float +class RedirectOutput(metaclass=YAMLGetter): + section = 'redirect_output' + + delete_invocation: bool + delete_delay: int + + # Debug mode DEBUG_MODE = True if 'local' in os.environ.get("SITE_URL", "local") else False diff --git a/bot/decorators.py b/bot/decorators.py index 710045c10..ce54f28cd 100644 --- a/bot/decorators.py +++ b/bot/decorators.py @@ -1,7 +1,7 @@ import logging import random import typing -from asyncio import Lock +from asyncio import Lock, sleep from functools import wraps from weakref import WeakValueDictionary @@ -9,7 +9,7 @@ from discord import Colour, Embed from discord.ext import commands from discord.ext.commands import CheckFailure, Context -from bot.constants import ERROR_REPLIES +from bot.constants import ERROR_REPLIES, RedirectOutput from bot.utils.checks import with_role_check, without_role_check log = logging.getLogger(__name__) @@ -100,3 +100,41 @@ def locked(): return await func(self, ctx, *args, **kwargs) return inner return wrap + + +def redirect_output(destination_channel: int, bypass_roles: typing.Container[int] = None): + """ + Changes the channel in the context of the command to redirect the output + to a certain channel, unless the author has a role to bypass redirection + """ + + def wrap(func): + @wraps(func) + async def inner(self, ctx, *args, **kwargs): + if ctx.channel.id == destination_channel: + log.trace(f"Command {ctx.command.name} was invoked in destination_channel, not redirecting") + return await func(self, ctx, *args, **kwargs) + + if bypass_roles and any(role.id in bypass_roles for role in ctx.author.roles): + log.trace(f"{ctx.author} has role to bypass output redirection") + return await func(self, ctx, *args, **kwargs) + + redirect_channel = ctx.guild.get_channel(destination_channel) + old_channel = ctx.channel + + log.trace(f"Redirecting output of {ctx.author}'s command '{ctx.command.name}'' to {redirect_channel.name}") + ctx.channel = redirect_channel + await ctx.channel.send(f"Here's the output of your command, {ctx.author.mention}") + await func(self, ctx, *args, **kwargs) + + message = await old_channel.send( + f"Hey, {ctx.author.mention}, you can find the output of your command here: " + f"{redirect_channel.mention}" + ) + + if RedirectOutput.delete_invocation: + await sleep(RedirectOutput.delete_delay) + await message.delete() + await ctx.message.delete() + return inner + return wrap diff --git a/config-default.yml b/config-default.yml index d77f9d186..3ce01916e 100644 --- a/config-default.yml +++ b/config-default.yml @@ -350,6 +350,9 @@ free: cooldown_rate: 1 cooldown_per: 60.0 +redirect_output: + delete_invocation: true + delete_delay: 15 config: required_keys: ['bot.token'] |