async def inner(self: Cog, ctx: Context, *args, **kwargs) -> None: if ctx.channel.id == destination_channel: log.trace(f"Command {ctx.command.name} was invoked in destination_channel, not redirecting") await func(self, ctx, *args, **kwargs) return 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") await func(self, ctx, *args, **kwargs) return 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) with suppress(NotFound): await message.delete() log.trace("Redirect output: Deleted user redirection message") with suppress(NotFound): await ctx.message.delete() log.trace("Redirect output: Deleted invocation message")
async def wrapper(ctx: commands.Context) -> True: """Redirecting the output. This returns True so that the command can send the message, None is falsey and will raise CheckFailure, and if checkfailure is raised the the command won't reach the sending part.""" guild = ctx.guild if not guild.get_channel(channel): raise NotThere(f'Channel with id {channel} could not be found.') if any(x.id in bypass_roles for x in ctx.author.roles): logger.info( f'Bypassing on user {str(ctx.author)} on redirecting the output of command {ctx.command}' ) return True logger.info( f'Redirecting the output of {ctx.command} to {guild.get_channel(channel)}' ) redirect_message = await ctx.channel.send( f'{ctx.author.mention} You can find the output of your command in <#{channel}>' ) await asyncio.sleep(10) await redirect_message.delete() ctx.channel = guild.get_channel( channel ) #overwriting ctx.channel to send the output to another channel. return True
async def unpin_all(self, ctx: commands.Context, channel_to_unpin: discord.TextChannel = None): ''' Unpins all the pinned messages in the channel. Called to clean up after archive_pins. Defaults to the channel in which it's called. ''' if (channel_to_unpin == None): channel_to_unpin = ctx.channel() start_message = f"Attempting to remove all pinned messages from {channel_to_unpin.mention}." await ctx.send(content=start_message) if (channel_to_unpin == None): await ctx.send(BOT_ERROR.INACCESSIBLE_CHANNEL) return pins_to_remove = await channel_to_unpin.pins() for pin in pins_to_remove: await pin.unpin() end_message = f"All pinned messages removed from {channel_to_unpin.mention}." await ctx.send(content=end_message)
async def SetPrefix(self, ctx: commands.Context, prefix): ctx.channel().trigger_typing() oldPrefix = self.bot.get_prefix(ctx.guild()) self.bot.commandHandler.insert_or_update_prefix(ctx.guild(), prefix) ctx.send("Changed prefix from `{0}` to `{1}".format(oldPrefix, prefix))