From 052b349b536410837b1347fbe57d9a323d10c2d0 Mon Sep 17 00:00:00 2001 From: James Dearlove <39483549+JamesDearlove@users.noreply.github.com> Date: Thu, 18 May 2023 09:41:07 +1000 Subject: [PATCH] Detect if edits are made in #yelling (#122) * Detect editing in yelling * Fix holidays timezone issue * Black strikes again * Better function name --- uqcsbot/holidays.py | 3 ++- uqcsbot/yelling.py | 42 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/uqcsbot/holidays.py b/uqcsbot/holidays.py index de329a0..fe7cbe7 100644 --- a/uqcsbot/holidays.py +++ b/uqcsbot/holidays.py @@ -8,6 +8,7 @@ import requests from requests.exceptions import RequestException from typing import List +from zoneinfo import ZoneInfo from uqcsbot.bot import UQCSBot from uqcsbot.utils.command_utils import HYPE_REACTS @@ -28,7 +29,7 @@ def is_today(self) -> bool: """ Returns true if the holiday is celebrated today """ - now = datetime.now() + now = datetime.now(tz=ZoneInfo("Australia/Brisbane")) return self.date.month == now.month and self.date.day == now.day diff --git a/uqcsbot/yelling.py b/uqcsbot/yelling.py index 6c4f8a6..035df44 100644 --- a/uqcsbot/yelling.py +++ b/uqcsbot/yelling.py @@ -10,6 +10,23 @@ class Yelling(commands.Cog): def __init__(self, bot: commands.Bot): self.bot = bot + @commands.Cog.listener() + async def on_message_edit(self, old: discord.Message, new: discord.Message): + """Detects if a message was edited, and call them out for it.""" + if ( + not self.bot.user + or not isinstance(new.channel, discord.TextChannel) + or new.author.id == self.bot.user.id + or new.channel.name != self.CHANNEL_NAME + or old.content == new.content + ): + return + + text = self.clean_text(new.content) + + if self.contains_lowercase(text): + await new.reply(self.generate_response(text)) + @commands.Cog.listener() async def on_message(self, msg: discord.Message): """Detects if a user is not yelling in #yelling and responds accordingly""" @@ -21,11 +38,20 @@ async def on_message(self, msg: discord.Message): ): return + text = self.clean_text(msg.content) + + # check if minuscule in message, and if so, post response + if self.contains_lowercase(text): + await msg.reply(self.generate_response(text)) + + def clean_text(self, message: str) -> str: + """Cleans text of links, emoji, and any character escaping.""" + # ignore emoji and links text = re.sub( r":[\w\-\+\~]+:", lambda m: m.group(0).upper(), - msg.content, + message, flags=re.UNICODE, ) @@ -36,7 +62,15 @@ async def on_message(self, msg: discord.Message): text = text.replace(">", ">").replace("<", "<").replace("&", "&") - response = choice( + return text + + def contains_lowercase(self, message: str) -> bool: + """Checks if message contains any lowercase characters""" + return any(char.islower() for char in message) + + def generate_response(self, text: str) -> str: + """Gives a random response for the bot to send back.""" + return choice( [ "WHAT’S THAT‽", "SPEAK UP!", @@ -63,10 +97,6 @@ async def on_message(self, msg: discord.Message): ) ) - # check if minuscule in message, and if so, post response - if any(char.islower() for char in text): - await msg.reply(response) - def mutate_minuscule(self, message: str) -> str: """ Randomly mutates 40% of minuscule letters to other minuscule letters and then inserts the