From f1fac10410f29e668bea9f7b02074f331eda6576 Mon Sep 17 00:00:00 2001 From: Daniel Brown Date: Thu, 6 Sep 2018 15:22:41 -0500 Subject: Completed editing deletion. Still working on reaction deletion. --- bot/cogs/bot.py | 55 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/bot/cogs/bot.py b/bot/cogs/bot.py index 2f8600c06..fd49733eb 100644 --- a/bot/cogs/bot.py +++ b/bot/cogs/bot.py @@ -40,6 +40,9 @@ class Bot: Channels.devtest, ) + # Stores improperly formatted Python codeblock message ids and the corresponding bot message + self.py_msg_ids = {} + @group(invoke_without_command=True, name="bot", hidden=True) @with_role(Roles.verified) async def bot_group(self, ctx: Context): @@ -168,6 +171,7 @@ class Bot: """ Attempts to fix badly indented code. """ + def unindent(code, skip_spaces=0): """ Unindents all code down to the number of spaces given ins skip_spaces @@ -178,7 +182,7 @@ class Bot: # Get numbers of spaces before code in the first line. while current == " ": - current = code[leading_spaces+1] + current = code[leading_spaces + 1] leading_spaces += 1 leading_spaces -= skip_spaces @@ -225,6 +229,16 @@ class Bot: log.trace(f"Found REPL code in \n\n{msg}\n\n") return final.rstrip(), True + def bad_ticks(self, msg: Message): + not_backticks = [ + "'''", '"""', "\u00b4\u00b4\u00b4", "\u2018\u2018\u2018", "\u2019\u2019\u2019", + "\u2032\u2032\u2032", "\u201c\u201c\u201c", "\u201d\u201d\u201d", "\u2033\u2033\u2033", + "\u3003\u3003\u3003" + ] + + bad_ticks = msg.content[:3] in not_backticks + return bad_ticks + async def on_message(self, msg: Message): """ Detect poorly formatted Python code and send the user @@ -233,26 +247,19 @@ class Bot: """ parse_codeblock = ( - ( - msg.channel.id in self.channel_cooldowns - or msg.channel.id in self.channel_whitelist - ) - and not msg.author.bot - and len(msg.content.splitlines()) > 3 + ( + msg.channel.id in self.channel_cooldowns + or msg.channel.id in self.channel_whitelist + ) + and not msg.author.bot + and len(msg.content.splitlines()) > 3 ) if parse_codeblock: on_cooldown = (time.time() - self.channel_cooldowns.get(msg.channel.id, 0)) < 300 if not on_cooldown: try: - not_backticks = [ - "'''", '"""', "\u00b4\u00b4\u00b4", "\u2018\u2018\u2018", "\u2019\u2019\u2019", - "\u2032\u2032\u2032", "\u201c\u201c\u201c", "\u201d\u201d\u201d", "\u2033\u2033\u2033", - "\u3003\u3003\u3003" - ] - - bad_ticks = msg.content[:3] in not_backticks - if bad_ticks: + if self.bad_ticks(msg): ticks = msg.content[:3] content = self.codeblock_stripping(f"```{msg.content[3:-3]}```", True) if content is None: @@ -270,7 +277,7 @@ class Bot: current_length = 0 lines_walked = 0 for line in content.splitlines(keepends=True): - if current_length+len(line) > space_left or lines_walked == 10: + if current_length + len(line) > space_left or lines_walked == 10: break current_length += len(line) lines_walked += 1 @@ -311,11 +318,11 @@ class Bot: current_length = 0 lines_walked = 0 for line in content.splitlines(keepends=True): - if current_length+len(line) > space_left or lines_walked == 10: + if current_length + len(line) > space_left or lines_walked == 10: break current_length += len(line) lines_walked += 1 - content = content[:current_length]+"#..." + content = content[:current_length] + "#..." howto += ( "It looks like you're trying to paste code into this channel.\n\n" @@ -334,7 +341,8 @@ class Bot: if howto != "": howto_embed = Embed(description=howto) - await msg.channel.send(f"Hey {msg.author.mention}!", embed=howto_embed) + bot_msg = await msg.channel.send(f"Hey {msg.author.mention}!", embed=howto_embed) + self.py_msg_ids[msg.id] = bot_msg.id else: return @@ -348,6 +356,15 @@ class Bot: f"The message that was posted was:\n\n{msg.content}\n\n" ) + async def on_message_edit(self, before: Message, after: Message): + if before.id in self.py_msg_ids and self.codeblock_stripping(after.content, self.bad_ticks(after)) is None: + bot_msg = await after.channel.get_message(self.py_msg_ids[after.id]) + await bot_msg.delete() + + async def on_reaction_add(self, reaction, user): + if user.id == self.id: + return + def setup(bot): bot.add_cog(Bot(bot)) -- cgit v1.2.3 From dbb58437192baff1641a7ada5b5863c60f5d3f5d Mon Sep 17 00:00:00 2001 From: Daniel Brown Date: Thu, 6 Sep 2018 17:30:14 -0500 Subject: Added Python format embed removal, either by the user correcting their formatting or by clicking the reaction, which can be done by the user who caused it or by staff. --- bot/cogs/bot.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/bot/cogs/bot.py b/bot/cogs/bot.py index fd49733eb..001e2e2fe 100644 --- a/bot/cogs/bot.py +++ b/bot/cogs/bot.py @@ -343,6 +343,7 @@ class Bot: howto_embed = Embed(description=howto) bot_msg = await msg.channel.send(f"Hey {msg.author.mention}!", embed=howto_embed) self.py_msg_ids[msg.id] = bot_msg.id + await bot_msg.add_reaction("❌") else: return @@ -362,9 +363,24 @@ class Bot: await bot_msg.delete() async def on_reaction_add(self, reaction, user): - if user.id == self.id: + if user.id == self.id or reaction.message.id not in self.py_msg_ids.values(): return + for k, v in self.py_msg_ids.items(): + if v == reaction.message.id: + msg = await reaction.message.channel.get_message(k) + bot_msg = await reaction.message.channel.get_message(v) + break + + if user == msg.author: + await bot_msg.delete() + return + + for role in user.roles: + if role.id in (Roles.owner, Roles.admin, Roles.moderator): + await bot_msg.delete() + return + def setup(bot): bot.add_cog(Bot(bot)) -- cgit v1.2.3 From 139e74a49c43993530ee5f0391ef3e6228bc6c40 Mon Sep 17 00:00:00 2001 From: Daniel Brown Date: Thu, 6 Sep 2018 17:36:38 -0500 Subject: Added Python format embed removal, either by the user correcting their formatting or by clicking the reaction, which can be done by the user who caused it or by staff. Also corrected linting errors. --- bot/cogs/bot.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bot/cogs/bot.py b/bot/cogs/bot.py index 001e2e2fe..1b5662ab1 100644 --- a/bot/cogs/bot.py +++ b/bot/cogs/bot.py @@ -247,12 +247,12 @@ class Bot: """ parse_codeblock = ( - ( - msg.channel.id in self.channel_cooldowns - or msg.channel.id in self.channel_whitelist - ) - and not msg.author.bot - and len(msg.content.splitlines()) > 3 + ( + msg.channel.id in self.channel_cooldowns + or msg.channel.id in self.channel_whitelist + ) + and not msg.author.bot + and len(msg.content.splitlines()) > 3 ) if parse_codeblock: -- cgit v1.2.3 From eb6acfc11a24f8301503f5b0c5717a3f43c5b7ce Mon Sep 17 00:00:00 2001 From: Daniel Brown Date: Fri, 7 Sep 2018 12:24:11 -0500 Subject: Corrected a check for the bot id --- bot/cogs/bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/bot.py b/bot/cogs/bot.py index 1b5662ab1..bad494071 100644 --- a/bot/cogs/bot.py +++ b/bot/cogs/bot.py @@ -363,7 +363,7 @@ class Bot: await bot_msg.delete() async def on_reaction_add(self, reaction, user): - if user.id == self.id or reaction.message.id not in self.py_msg_ids.values(): + if user.id == self.user.id or reaction.message.id not in self.py_msg_ids.values(): return for k, v in self.py_msg_ids.items(): -- cgit v1.2.3 From f30508ce9dbb4e0984293369fd7ae1262118e0c7 Mon Sep 17 00:00:00 2001 From: Daniel Brown Date: Thu, 13 Sep 2018 10:32:00 -0500 Subject: - Changed function name from bad_ticks to has_bad_ticks - Added x emoji to the constants file and changed the add_reaction to use the Emoji object instead of the emoji icon - Adjusted variable names to be more informative - Added comments to better explain how on_reaction_add functions Signed-off-by: Daniel Brown --- bot/cogs/bot.py | 39 ++++++++++++++++++++++----------------- bot/constants.py | 1 + 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/bot/cogs/bot.py b/bot/cogs/bot.py index bad494071..f9a3ae8c3 100644 --- a/bot/cogs/bot.py +++ b/bot/cogs/bot.py @@ -8,7 +8,7 @@ from discord.ext.commands import Bot, Context, command, group from dulwich.repo import Repo from bot.constants import ( - Channels, Guild, Roles, URLs + Channels, Emojis, Guild, Roles, URLs ) from bot.decorators import with_role @@ -41,7 +41,7 @@ class Bot: ) # Stores improperly formatted Python codeblock message ids and the corresponding bot message - self.py_msg_ids = {} + self.codeblock_message_ids = {} @group(invoke_without_command=True, name="bot", hidden=True) @with_role(Roles.verified) @@ -229,7 +229,7 @@ class Bot: log.trace(f"Found REPL code in \n\n{msg}\n\n") return final.rstrip(), True - def bad_ticks(self, msg: Message): + def has_bad_ticks(self, msg: Message): not_backticks = [ "'''", '"""', "\u00b4\u00b4\u00b4", "\u2018\u2018\u2018", "\u2019\u2019\u2019", "\u2032\u2032\u2032", "\u201c\u201c\u201c", "\u201d\u201d\u201d", "\u2033\u2033\u2033", @@ -259,7 +259,7 @@ class Bot: on_cooldown = (time.time() - self.channel_cooldowns.get(msg.channel.id, 0)) < 300 if not on_cooldown: try: - if self.bad_ticks(msg): + if self.has_bad_ticks(msg): ticks = msg.content[:3] content = self.codeblock_stripping(f"```{msg.content[3:-3]}```", True) if content is None: @@ -341,9 +341,9 @@ class Bot: if howto != "": howto_embed = Embed(description=howto) - bot_msg = await msg.channel.send(f"Hey {msg.author.mention}!", embed=howto_embed) - self.py_msg_ids[msg.id] = bot_msg.id - await bot_msg.add_reaction("❌") + bot_message = await msg.channel.send(f"Hey {msg.author.mention}!", embed=howto_embed) + self.codeblock_message_ids[msg.id] = bot_message.id + await bot_message.add_reaction(Emojis.x) else: return @@ -358,27 +358,32 @@ class Bot: ) async def on_message_edit(self, before: Message, after: Message): - if before.id in self.py_msg_ids and self.codeblock_stripping(after.content, self.bad_ticks(after)) is None: - bot_msg = await after.channel.get_message(self.py_msg_ids[after.id]) - await bot_msg.delete() + if before.id in self.codeblock_message_ids\ + and self.codeblock_stripping(after.content, self.has_bad_ticks(after)) is None: + bot_message = await after.channel.get_message(self.codeblock_message_ids[after.id]) + await bot_message.delete() async def on_reaction_add(self, reaction, user): - if user.id == self.user.id or reaction.message.id not in self.py_msg_ids.values(): + # Ignores reactions added by the bot or added to non-codeblock correction embed messages + if user.id == self.user.id or reaction.message.id not in self.codeblock_message_ids.values(): return - for k, v in self.py_msg_ids.items(): + # Finds the appropriate bot message/ user message pair and assigns them to variables + for k, v in self.codeblock_message_ids.items(): if v == reaction.message.id: - msg = await reaction.message.channel.get_message(k) - bot_msg = await reaction.message.channel.get_message(v) + user_message = await reaction.message.channel.get_message(k) + bot_message = await reaction.message.channel.get_message(v) break - if user == msg.author: - await bot_msg.delete() + # If the reaction was clicked on by the author of the user message, deletes the bot message + if user == user_message.author: + await bot_message.delete() return + # If the reaction was clicked by staff (mod or higher), deletes the bot message for role in user.roles: if role.id in (Roles.owner, Roles.admin, Roles.moderator): - await bot_msg.delete() + await bot_message.delete() return diff --git a/bot/constants.py b/bot/constants.py index ccbe307f5..568871498 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -248,6 +248,7 @@ class Emojis(metaclass=YAMLGetter): bullet: str new: str pencil: str + x: str class Icons(metaclass=YAMLGetter): -- cgit v1.2.3 From 245e7b19911f656b8cf52de6f1c1761417524145 Mon Sep 17 00:00:00 2001 From: Daniel Brown Date: Thu, 13 Sep 2018 10:32:00 -0500 Subject: - Changed variable name from bad_ticks to has_bad_ticks - Added x emoji to the constants file and changed the add_reaction to use the Emoji object instead of the emoji icon - Adjusted variable names to be more informative - Added comments to better explain how on_reaction_add functions Signed-off-by: Daniel Brown --- bot/cogs/bot.py | 39 ++++++++++++++++++++++----------------- bot/constants.py | 1 + 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/bot/cogs/bot.py b/bot/cogs/bot.py index bad494071..3b1c83840 100644 --- a/bot/cogs/bot.py +++ b/bot/cogs/bot.py @@ -8,7 +8,7 @@ from discord.ext.commands import Bot, Context, command, group from dulwich.repo import Repo from bot.constants import ( - Channels, Guild, Roles, URLs + Channels, Emojis, Guild, Roles, URLs ) from bot.decorators import with_role @@ -41,7 +41,7 @@ class Bot: ) # Stores improperly formatted Python codeblock message ids and the corresponding bot message - self.py_msg_ids = {} + self.codeblock_message_ids = {} @group(invoke_without_command=True, name="bot", hidden=True) @with_role(Roles.verified) @@ -236,8 +236,8 @@ class Bot: "\u3003\u3003\u3003" ] - bad_ticks = msg.content[:3] in not_backticks - return bad_ticks + has_bad_ticks = msg.content[:3] in not_backticks + return has_bad_ticks async def on_message(self, msg: Message): """ @@ -341,9 +341,9 @@ class Bot: if howto != "": howto_embed = Embed(description=howto) - bot_msg = await msg.channel.send(f"Hey {msg.author.mention}!", embed=howto_embed) - self.py_msg_ids[msg.id] = bot_msg.id - await bot_msg.add_reaction("❌") + bot_message = await msg.channel.send(f"Hey {msg.author.mention}!", embed=howto_embed) + self.codeblock_message_ids[msg.id] = bot_message.id + await bot_message.add_reaction(Emojis.x) else: return @@ -358,27 +358,32 @@ class Bot: ) async def on_message_edit(self, before: Message, after: Message): - if before.id in self.py_msg_ids and self.codeblock_stripping(after.content, self.bad_ticks(after)) is None: - bot_msg = await after.channel.get_message(self.py_msg_ids[after.id]) - await bot_msg.delete() + if before.id in self.codeblock_message_ids\ + and self.codeblock_stripping(after.content, self.bad_ticks(after)) is None: + bot_message = await after.channel.get_message(self.codeblock_message_ids[after.id]) + await bot_message.delete() async def on_reaction_add(self, reaction, user): - if user.id == self.user.id or reaction.message.id not in self.py_msg_ids.values(): + # Ignores reactions added by the bot or added to non-codeblock correction embed messages + if user.id == self.user.id or reaction.message.id not in self.codeblock_message_ids.values(): return - for k, v in self.py_msg_ids.items(): + # Finds the appropriate bot message/ user message pair and assigns them to variables + for k, v in self.codeblock_message_ids.items(): if v == reaction.message.id: - msg = await reaction.message.channel.get_message(k) - bot_msg = await reaction.message.channel.get_message(v) + user_message = await reaction.message.channel.get_message(k) + bot_message = await reaction.message.channel.get_message(v) break - if user == msg.author: - await bot_msg.delete() + # If the reaction was clicked on by the author of the user message, deletes the bot message + if user == user_message.author: + await bot_message.delete() return + # If the reaction was clicked by staff (mod or higher), deletes the bot message for role in user.roles: if role.id in (Roles.owner, Roles.admin, Roles.moderator): - await bot_msg.delete() + await bot_message.delete() return diff --git a/bot/constants.py b/bot/constants.py index ccbe307f5..568871498 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -248,6 +248,7 @@ class Emojis(metaclass=YAMLGetter): bullet: str new: str pencil: str + x: str class Icons(metaclass=YAMLGetter): -- cgit v1.2.3 From 5106168704020679e3261e5687f118726dbc09ad Mon Sep 17 00:00:00 2001 From: Daniel Brown Date: Thu, 13 Sep 2018 10:39:17 -0500 Subject: - Changed variable name from bad_ticks to has_bad_ticks - Added x emoji to the constants file and changed the add_reaction to use the Emoji object instead of the emoji icon - Adjusted variable names to be more informative - Added comments to better explain how on_reaction_add functions Signed-off-by: Daniel Brown --- bot/cogs/bot.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/cogs/bot.py b/bot/cogs/bot.py index 3b1c83840..12647bb05 100644 --- a/bot/cogs/bot.py +++ b/bot/cogs/bot.py @@ -229,7 +229,7 @@ class Bot: log.trace(f"Found REPL code in \n\n{msg}\n\n") return final.rstrip(), True - def bad_ticks(self, msg: Message): + def has_bad_ticks(self, msg: Message): not_backticks = [ "'''", '"""', "\u00b4\u00b4\u00b4", "\u2018\u2018\u2018", "\u2019\u2019\u2019", "\u2032\u2032\u2032", "\u201c\u201c\u201c", "\u201d\u201d\u201d", "\u2033\u2033\u2033", @@ -259,7 +259,7 @@ class Bot: on_cooldown = (time.time() - self.channel_cooldowns.get(msg.channel.id, 0)) < 300 if not on_cooldown: try: - if self.bad_ticks(msg): + if self.has_bad_ticks(msg): ticks = msg.content[:3] content = self.codeblock_stripping(f"```{msg.content[3:-3]}```", True) if content is None: @@ -359,7 +359,7 @@ class Bot: async def on_message_edit(self, before: Message, after: Message): if before.id in self.codeblock_message_ids\ - and self.codeblock_stripping(after.content, self.bad_ticks(after)) is None: + and self.codeblock_stripping(after.content, self.has_bad_ticks(after)) is None: bot_message = await after.channel.get_message(self.codeblock_message_ids[after.id]) await bot_message.delete() -- cgit v1.2.3 From 023ca2d08532e6450e071a5724bb2a056d3b3681 Mon Sep 17 00:00:00 2001 From: Daniel Brown Date: Thu, 13 Sep 2018 13:05:45 -0500 Subject: - Broke up "has_fixed_codeblock" up for clarity. - Changed loop variables for clarity - Corrected implementation of emoji - Changed emoji name from "x" to "cross_mark" for clarity Signed-off-by: Daniel Brown --- bot/cogs/bot.py | 17 +++++++++++------ bot/constants.py | 2 +- config-default.yml | 7 ++++--- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/bot/cogs/bot.py b/bot/cogs/bot.py index 12647bb05..9a5c365fb 100644 --- a/bot/cogs/bot.py +++ b/bot/cogs/bot.py @@ -358,8 +358,13 @@ class Bot: ) async def on_message_edit(self, before: Message, after: Message): - if before.id in self.codeblock_message_ids\ - and self.codeblock_stripping(after.content, self.has_bad_ticks(after)) is None: + has_fixed_codeblock = ( + # Checks if the original message was previously called out by the bot + before.id in self.codeblock_message_ids + # Checks to see if the user has corrected their codeblock + and self.codeblock_stripping(after.content, self.has_bad_ticks(after)) is None + ) + if has_fixed_codeblock: bot_message = await after.channel.get_message(self.codeblock_message_ids[after.id]) await bot_message.delete() @@ -369,10 +374,10 @@ class Bot: return # Finds the appropriate bot message/ user message pair and assigns them to variables - for k, v in self.codeblock_message_ids.items(): - if v == reaction.message.id: - user_message = await reaction.message.channel.get_message(k) - bot_message = await reaction.message.channel.get_message(v) + for user_message_id, bot_message_id in self.codeblock_message_ids.items(): + if bot_message_id == reaction.message.id: + user_message = await reaction.message.channel.get_message(user_message_id) + bot_message = await reaction.message.channel.get_message(bot_message_id) break # If the reaction was clicked on by the author of the user message, deletes the bot message diff --git a/bot/constants.py b/bot/constants.py index 568871498..3ade4ac7b 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -248,7 +248,7 @@ class Emojis(metaclass=YAMLGetter): bullet: str new: str pencil: str - x: str + cross_mark: str class Icons(metaclass=YAMLGetter): diff --git a/config-default.yml b/config-default.yml index ed5394b6b..c72fc2231 100644 --- a/config-default.yml +++ b/config-default.yml @@ -31,9 +31,10 @@ style: status_dnd: "<:status_dnd:470326272082313216>" status_offline: "<:status_offline:470326266537705472>" - bullet: "\u2022" - pencil: "\u270F" - new: "\U0001F195" + bullet: "\u2022" + pencil: "\u270F" + new: "\U0001F195" + cross_mark: "\u274C" icons: crown_blurple: "https://cdn.discordapp.com/emojis/469964153289965568.png" -- cgit v1.2.3