From b651e3498fa49de527a0c8d4d5a441a30f85c88b Mon Sep 17 00:00:00 2001 From: Izan Date: Mon, 14 Feb 2022 10:27:34 +0000 Subject: Fix DM handling for code snippets. --- bot/exts/info/code_snippets.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/bot/exts/info/code_snippets.py b/bot/exts/info/code_snippets.py index 07b1b8a2d..95a0fb014 100644 --- a/bot/exts/info/code_snippets.py +++ b/bot/exts/info/code_snippets.py @@ -255,16 +255,23 @@ class CodeSnippets(Cog): except discord.NotFound: # Don't send snippets if the original message was deleted. return - - if len(message_to_send) > 1000 and message.channel.id != Channels.bot_commands: - # Redirects to #bot-commands if the snippet contents are too long - await self.bot.wait_until_guild_available() - destination = self.bot.get_channel(Channels.bot_commands) - - await message.channel.send( - 'The snippet you tried to send was too long. ' - f'Please see {destination.mention} for the full snippet.' - ) + except discord.Forbidden as e: + # We still want to send snippets when in DMs, but if we're in guild then + # reraise error since that means there's a permissions issue with the bot. + if message.guild: + raise e + + # If we're in a guild, then check if we need to redirect to #bot-commands + if destination.guild: + if len(message_to_send) > 1000 and message.channel.id != Channels.bot_commands: + # Redirects to #bot-commands if the snippet contents are too long + await self.bot.wait_until_guild_available() + destination = self.bot.get_channel(Channels.bot_commands) + + await message.channel.send( + 'The snippet you tried to send was too long. ' + f'Please see {destination.mention} for the full snippet.' + ) await wait_for_deletion( await destination.send(message_to_send), -- cgit v1.2.3 From 4daa320a8482bc263b8cac3e912f69aa390056bf Mon Sep 17 00:00:00 2001 From: Izan Date: Thu, 17 Feb 2022 09:15:15 +0000 Subject: Fix two errors - Changed `destination.guild` to `message.guild` since `DMChannel` doesn't have a "guild" attribute - Only call `wait_for_deletion` when inside a guild. --- bot/exts/info/code_snippets.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/bot/exts/info/code_snippets.py b/bot/exts/info/code_snippets.py index 95a0fb014..ebc7ce1c6 100644 --- a/bot/exts/info/code_snippets.py +++ b/bot/exts/info/code_snippets.py @@ -262,7 +262,7 @@ class CodeSnippets(Cog): raise e # If we're in a guild, then check if we need to redirect to #bot-commands - if destination.guild: + if message.guild: if len(message_to_send) > 1000 and message.channel.id != Channels.bot_commands: # Redirects to #bot-commands if the snippet contents are too long await self.bot.wait_until_guild_available() @@ -273,10 +273,12 @@ class CodeSnippets(Cog): f'Please see {destination.mention} for the full snippet.' ) - await wait_for_deletion( - await destination.send(message_to_send), - (message.author.id,) - ) + await wait_for_deletion( + await destination.send(message_to_send), + (message.author.id,) + ) + else: + await destination.send(message_to_send) def setup(bot: Bot) -> None: -- cgit v1.2.3 From 880d42a384e90db34faebbbdeb47d879704c13fd Mon Sep 17 00:00:00 2001 From: ToxicKidz Date: Mon, 28 Feb 2022 16:04:10 -0500 Subject: chore: Disallow code snippets in DMs --- bot/exts/info/code_snippets.py | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/bot/exts/info/code_snippets.py b/bot/exts/info/code_snippets.py index ebc7ce1c6..f2f29020f 100644 --- a/bot/exts/info/code_snippets.py +++ b/bot/exts/info/code_snippets.py @@ -246,6 +246,9 @@ class CodeSnippets(Cog): if message.author.bot: return + if message.guild is None: + return + message_to_send = await self._parse_snippets(message.content) destination = message.channel @@ -255,30 +258,21 @@ class CodeSnippets(Cog): except discord.NotFound: # Don't send snippets if the original message was deleted. return - except discord.Forbidden as e: - # We still want to send snippets when in DMs, but if we're in guild then - # reraise error since that means there's a permissions issue with the bot. - if message.guild: - raise e - - # If we're in a guild, then check if we need to redirect to #bot-commands - if message.guild: - if len(message_to_send) > 1000 and message.channel.id != Channels.bot_commands: - # Redirects to #bot-commands if the snippet contents are too long - await self.bot.wait_until_guild_available() - destination = self.bot.get_channel(Channels.bot_commands) - - await message.channel.send( - 'The snippet you tried to send was too long. ' - f'Please see {destination.mention} for the full snippet.' - ) - await wait_for_deletion( - await destination.send(message_to_send), - (message.author.id,) + if len(message_to_send) > 1000 and message.channel.id != Channels.bot_commands: + # Redirects to #bot-commands if the snippet contents are too long + await self.bot.wait_until_guild_available() + destination = self.bot.get_channel(Channels.bot_commands) + + await message.channel.send( + 'The snippet you tried to send was too long. ' + f'Please see {destination.mention} for the full snippet.' ) - else: - await destination.send(message_to_send) + + await wait_for_deletion( + await destination.send(message_to_send), + (message.author.id,) + ) def setup(bot: Bot) -> None: -- cgit v1.2.3