Skip to content

Commit

Permalink
updating onmessage stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Mole1424 committed Mar 28, 2024
1 parent e2cce8e commit d4bce48
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 51 deletions.
3 changes: 2 additions & 1 deletion apollo.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
"cogs.commands.date",
"cogs.commands.event_sync",
"cogs.commands.flip",
"cogs.commands.joeltech",
"cogs.commands.karma_admin",
"cogs.commands.karma_blacklist",
"cogs.commands.karma",
"cogs.commands.lcalc",
"cogs.commands.misc",
"cogs.commands.onmessage",
"cogs.commands.openaiadmin",
"cogs.commands.quotes",
"cogs.commands.rolemenu",
Expand All @@ -50,7 +52,6 @@
"cogs.irc",
"cogs.parallelism",
"cogs.welcome",
"cogs.commands.joeltech",
]


Expand Down
75 changes: 75 additions & 0 deletions cogs/commands/onmessage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from re import search, sub

from discord import Message
from discord.abc import GuildChannel
from discord.ext import commands
from discord.ext.commands import Bot, Cog


class OnMessage(commands.Cog):
def __init__(self, bot: Bot):
self.bot = bot

@Cog.listener()
async def on_message(self, message: Message):
if isinstance(message.channel, GuildChannel):
# Get all specified command prefixes for the bot
command_prefixes = self.bot.command_prefix(self.bot, message)
if not any(
message.content.startswith(prefix) for prefix in command_prefixes
):
await self.thanks(message)
await self.scan_replace(
message, r"https?://(twitter\.com|x\.com)", "https://fxtwitter.com"
)
await self.scan_replace(
message,
r"https?://(?:old\.|www\.)?reddit\.com",
"https://rxddit.com",
)

async def thanks(self, message: Message):
# to whoever sees this, you're welcome for the not having a fuck off massive indented if
if message.author.id == self.bot.user.id:
# dont thank itself
return
# Get the previous message (list comprehension my beloved)
previous_message = [
message async for message in message.channel.history(limit=2)
][1]
if message.reference and message.reference.message_id:
# dont thank replies to something that isnt the bot
replied_message = await message.channel.fetch_message(
message.reference.message_id
)
if replied_message.author != self.bot.user.id:
return
elif (
previous_message.author.id != self.bot.user.id
and "apollo" not in message.content.lower()
):
# can only thank replies to bot
return
thanks = ["thx", "thanks", "thank you", "ty"]
# only heart if thanks matches word in message
if not any(
search(r"\b" + thank + r"\b", message.content.lower()) for thank in thanks
):
return
return await message.add_reaction("💜")

async def scan_replace(self, message: Message, regex, replace):
send_message = ""
sentance = message.content.replace("\n", " ").split(" ")
for word in sentance:
# if any word contains a link replace with replace
if search(regex, word):
send_message += "\n" + sub(regex, replace, word)
# if there is a message to send, send it
if send_message != "":
await message.edit(suppress=True)
await message.reply(send_message)


async def setup(bot: Bot):
await bot.add_cog(OnMessage(bot))
51 changes: 1 addition & 50 deletions cogs/database.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from re import search, sub
from re import search

from discord import Message
from discord.abc import GuildChannel
Expand Down Expand Up @@ -69,55 +69,6 @@ async def on_message(self, message: Message):
)
if reply:
await message.channel.send(reply)
if "t" in message.content.lower():
# if maybe some kind of thanks, process it
await self.process_thanks(message)
if search(r"https?://(twitter\.com|x\.com)", message.content):
# if a twitter link, process it
send_message = ""
sentance = message.content.replace("\n", " ").split(" ")
for word in sentance:
# if any word contains a twitter link replace with fxtwitter
if search(r"https?://(twitter\.com|x\.com)", word):
send_message += "\n" + sub(
r"https?://(twitter\.com|x\.com)",
"https://fxtwitter.com",
word,
)
# if there is a message to send, send it
if send_message != "":
await message.edit(suppress=True)
await message.reply(send_message)

async def process_thanks(self, message: Message):
# to whoever sees this, you're welcome for the not having a fuck off massive indented if
if message.author.id == self.bot.user.id:
# dont thank itself
return
# Get the previous message (list comprehension my beloved)
previous_message = [
message async for message in message.channel.history(limit=2)
][1]
if message.reference and message.reference.message_id:
# dont thank replies to something that isnt the bot
replied_message = await message.channel.fetch_message(
message.reference.message_id
)
if replied_message.author != self.bot.user.id:
return
elif (
previous_message.author.id != self.bot.user.id
and "apollo" not in message.content.lower()
):
# can only thank replies to bot
return
thanks = ["thx", "thanks", "thank you", "ty"]
# only heart if thanks matches word in message
if not any(
search(r"\b" + thank + r"\b", message.content.lower()) for thank in thanks
):
return
return await message.add_reaction("💜")


async def setup(bot: Bot):
Expand Down

0 comments on commit d4bce48

Please sign in to comment.