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