From b7bbf20171b19fa1dead4ad4ff235bcab27d6127 Mon Sep 17 00:00:00 2001 From: SebastiaanZ <33516116+SebastiaanZ@users.noreply.github.com> Date: Fri, 18 Jan 2019 22:28:55 +0100 Subject: Adding 'redirect_output' decorator to bot/decorators.py --- bot/decorators.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/bot/decorators.py b/bot/decorators.py index 710045c10..6654aa664 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 @@ -100,3 +100,32 @@ def locked(): return await func(self, ctx, *args, **kwargs) return inner return wrap + + +def redirect_output(channel: int, bypass_roles: typing.Container[int] = None): + def wrap(func): + @wraps(func) + async def inner(self, ctx, *args, **kwargs): + if ctx.channel.id == channel: + return await func(self, ctx, *args, **kwargs) + + if bypass_roles and any(role.id in bypass_roles for role in ctx.author.roles): + return await func(self, ctx, *args, **kwargs) + + redirect_channel = ctx.guild.get_channel(channel) + old_channel = ctx.channel + + 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}" + ) + + await sleep(15) + await message.delete() + await ctx.message.delete() + return inner + return wrap -- cgit v1.2.3 From c0b0d2bb1aac8f9f8622a9de38c56610abf05268 Mon Sep 17 00:00:00 2001 From: SebastiaanZ <33516116+SebastiaanZ@users.noreply.github.com> Date: Fri, 18 Jan 2019 22:29:42 +0100 Subject: Redirecting output of help of regular users to bot-commands --- bot/cogs/help.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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. -- cgit v1.2.3 From 36a37523e76e72285c81098771357bb8715d95c0 Mon Sep 17 00:00:00 2001 From: SebastiaanZ <33516116+SebastiaanZ@users.noreply.github.com> Date: Fri, 18 Jan 2019 22:57:50 +0100 Subject: Adding redirection constants to contants.py and config-default.yml --- bot/constants.py | 7 +++++++ config-default.yml | 3 +++ 2 files changed, 10 insertions(+) diff --git a/bot/constants.py b/bot/constants.py index 61f62b09c..53b6092ee 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -481,6 +481,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/config-default.yml b/config-default.yml index 5938ae533..38e6ca86f 100644 --- a/config-default.yml +++ b/config-default.yml @@ -347,6 +347,9 @@ free: cooldown_rate: 1 cooldown_per: 60.0 +redirect_output: + delete_invocation: true + delete_delay: 15 config: required_keys: ['bot.token'] -- cgit v1.2.3 From 42a5892d622cc178b488b3f66573667584e766ce Mon Sep 17 00:00:00 2001 From: SebastiaanZ <33516116+SebastiaanZ@users.noreply.github.com> Date: Fri, 18 Jan 2019 23:01:53 +0100 Subject: Adding log.trace messages to decorator --- bot/decorators.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/bot/decorators.py b/bot/decorators.py index 6654aa664..ce54f28cd 100644 --- a/bot/decorators.py +++ b/bot/decorators.py @@ -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__) @@ -102,19 +102,27 @@ def locked(): return wrap -def redirect_output(channel: int, bypass_roles: typing.Container[int] = None): +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 == channel: + 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(channel) + 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) @@ -124,8 +132,9 @@ def redirect_output(channel: int, bypass_roles: typing.Container[int] = None): f"{redirect_channel.mention}" ) - await sleep(15) - await message.delete() - await ctx.message.delete() + if RedirectOutput.delete_invocation: + await sleep(RedirectOutput.delete_delay) + await message.delete() + await ctx.message.delete() return inner return wrap -- cgit v1.2.3