Skip to content

Commit

Permalink
Detect if edits are made in #yelling (#122)
Browse files Browse the repository at this point in the history
* Detect editing in yelling

* Fix holidays timezone issue

* Black strikes again

* Better function name
  • Loading branch information
JamesDearlove authored May 17, 2023
1 parent 042c7bb commit 052b349
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
3 changes: 2 additions & 1 deletion uqcsbot/holidays.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down
42 changes: 36 additions & 6 deletions uqcsbot/yelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -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,
)

Expand All @@ -36,7 +62,15 @@ async def on_message(self, msg: discord.Message):

text = text.replace("&gt;", ">").replace("&lt;", "<").replace("&amp;", "&")

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!",
Expand All @@ -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
Expand Down

0 comments on commit 052b349

Please sign in to comment.