From e7b1cc221906beac5c407a67b2da4d732174c900 Mon Sep 17 00:00:00 2001 From: jad Date: Fri, 5 Apr 2024 08:02:24 -0600 Subject: [PATCH 1/3] Reaction events now fire on uncached messages (#86) * fix: votedelete and starboard works on uncached messages --- cogs/starboard.py | 42 ++++++++++++++++++++++++++---------------- cogs/votedelete.py | 8 +++++--- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/cogs/starboard.py b/cogs/starboard.py index 6b302ed..cf1489f 100644 --- a/cogs/starboard.py +++ b/cogs/starboard.py @@ -23,6 +23,7 @@ class Starboard(commands.Cog): def __init__(self, bot: discord.Client): + self.bot = bot self.starboard_emoji_str = STARBOARD_EMOJI_STR self.threshold = STARBOARD_THESHOLD # Starboarded Message ID -> ID of the message the bot sent. @@ -106,30 +107,39 @@ async def create_starboard_post(self, react: discord.Reaction): self.starboard_msgs[react.message.id] = msg.id @commands.Cog.listener() - async def on_reaction_add(self, react: discord.Reaction, _: discord.User): - if str(react.emoji) != self.starboard_emoji_str: - return + async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent): + channel = self.bot.get_channel(payload.channel_id) - if react.message.channel.is_nsfw(): + if channel.is_nsfw(): return - if react.count < self.threshold: - return + message = await channel.fetch_message(payload.message_id) - if react.message.id in self.starboard_msgs: - await self.update_reaction_count(react) - else: - await self.create_starboard_post(react) + for react in message.reactions: + if str(react.emoji) != self.starboard_emoji_str: + continue + + if react.count < self.threshold: + continue + + if react.message.id in self.starboard_msgs: + await self.update_reaction_count(react) + else: + await self.create_starboard_post(react) @commands.Cog.listener() - async def on_reaction_remove(self, react: discord.Reaction, _: discord.User): - if str(react.emoji) != self.starboard_emoji_str: - return + async def on_raw_reaction_remove(self, payload: discord.RawReactionActionEvent): + channel = self.bot.get_channel(payload.channel_id) + message = await channel.fetch_message(payload.message_id) - if not react.message.id in self.starboard_msgs: - return + for react in message.reactions: + if str(react.emoji) != self.starboard_emoji_str: + continue - await self.update_reaction_count(react) + if not react.message.id in self.starboard_msgs: + continue + + await self.update_reaction_count(react) @commands.Cog.listener() async def on_message_delete(self, msg: discord.Message): diff --git a/cogs/votedelete.py b/cogs/votedelete.py index d2cc0f3..ffc6d6f 100644 --- a/cogs/votedelete.py +++ b/cogs/votedelete.py @@ -51,9 +51,11 @@ def checkDelete(self, reaction): return True @commands.Cog.listener() - async def on_reaction_add(self, reaction, user): - message = reaction.message - if self.checkDelete(reaction): + async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent): + channel = self.bot.get_channel(payload.channel_id) + message = await channel.fetch_message(payload.message_id) + + if any(self.checkDelete(reaction) for reaction in message.reactions): try: await message.delete() except discord.Forbidden: From 3168ff582c91e645b08f997027c34b1323021ff4 Mon Sep 17 00:00:00 2001 From: Aaryan <44059614+AaryanHazCompter@users.noreply.github.com> Date: Fri, 5 Apr 2024 05:48:57 -0600 Subject: [PATCH 2/3] add sherpmail I saw an issue Ian raised about wanting a way to (a) communicate to the server through the bot (b) have the server communicate to him through the bot and the idea seemed funny so I coded it - there's little error handling, no accounting for excess characters, no graphics and attachment handling, and definitely no spam prevention - however I am sleep deprived and wanted to make what is basically a letter mail system work to a reasonable extent. --- cogs/sherpmail.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 cogs/sherpmail.py diff --git a/cogs/sherpmail.py b/cogs/sherpmail.py new file mode 100644 index 0000000..832c4d4 --- /dev/null +++ b/cogs/sherpmail.py @@ -0,0 +1,68 @@ +import discord +from discord.ext import commands +from discord import app_commands + +# I couldn't work out replying directly to the initial messages so this is not the best +# Nonetheless it is nearly 6am and I felt the need to complete this +# I hope it works out and nobody abuses it lol good luck + +class sherpMailbox(commands.Cog): + def __init__(self, bot): + self.bot = bot + self.sherp = "" + + async def cog_load(self): + await super().cog_load() + print("Sherpmail loaded.") + + @app_commands.command( + name = "sherpmail", + description = "Leave a message directly to Sherp's DMs" + ) + @app_commands.describe( + msg = "The message you would like to leave for Sherp (be respectful)" + ) + async def sendMessage(self, interaction: discord.Interaction, msg: str): + # Sherp's Discord ID is currently 212613981465083906 + self.sherp = await interaction.client.fetch_user(212613981465083906) + try: + embed = discord.Embed( + title = f"📬 You've got mail! **@{interaction.user} | ID: {interaction.user.id}** sent you the following:", + description = f"{msg}", + colour = discord.Colour.gold() + ) + await self.sherp.send(embed = embed) + + embed = discord.Embed( + title = f"📫 Outgoing! The following message has been mailed to {self.sherp.display_name}:", + description = f"{msg}", + colour = discord.Colour.green() + ) + await interaction.response.send_message(embed = embed) + except: + await interaction.response.send_message(f"❌ An unexpected error has occurred!") + + @commands.Cog.listener() + async def on_message(self, message: discord.Message): + if (message.guild == None) and (message.author.id == 214657501193306112): + try: + embed = discord.Embed( + title = "📬 You've got mail! This is what Sherp said:", + description = f"{message.content}", + colour = discord.Colour.gold() + ) + # I used the ID 1183281507842924645 from the channel "non-cs-nonsense" + ch = self.bot.get_channel(1183281507842924645) + await ch.send(embed = embed) + + embed = discord.Embed( + title = f"📫 Outgoing! The following reply was sent:", + description = f"{message.content}", + colour = discord.Colour.green() + ) + await message.reply(embed = embed, mention_author = False) + except: + await message.reply(f"❌ An unexpected error has occurred!") + +async def setup_sherpMailbox_cog(bot, guilds): + await bot.add_cog(sherpMailbox(bot), guilds=guilds) \ No newline at end of file From a5b9b0e8ddfca832e20f7c6b14ea7f925962ad81 Mon Sep 17 00:00:00 2001 From: Aaryan <44059614+AaryanHazCompter@users.noreply.github.com> Date: Sun, 14 Apr 2024 23:54:55 -0600 Subject: [PATCH 3/3] sherpmail modifications + init addition Modified sherpmail cog from a cosmetic perspective based on inputs from Matt; added sherpmail cog to init file for setup and consistency --- cogs/__init__.py | 2 ++ cogs/sherpmail.py | 16 ++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/cogs/__init__.py b/cogs/__init__.py index a9948d9..3d048b1 100644 --- a/cogs/__init__.py +++ b/cogs/__init__.py @@ -6,6 +6,7 @@ from .starboard import setup_starboard from .faq import setup_faq from .votedelete import setup_votedelete +from .sherpmail import setup_SherpMailbox_cog import asyncio from aiohttp import ClientSession @@ -23,4 +24,5 @@ async def setup_all_cogs(bot, guilds, client=None): setup_starboard(bot, guilds), setup_faq(bot, guilds), setup_votedelete(bot, guilds), + setup_SherpMailbox_cog(bot, guilds) ) diff --git a/cogs/sherpmail.py b/cogs/sherpmail.py index 832c4d4..b8d5dfa 100644 --- a/cogs/sherpmail.py +++ b/cogs/sherpmail.py @@ -6,7 +6,7 @@ # Nonetheless it is nearly 6am and I felt the need to complete this # I hope it works out and nobody abuses it lol good luck -class sherpMailbox(commands.Cog): +class SherpMailbox(commands.Cog): def __init__(self, bot): self.bot = bot self.sherp = "" @@ -27,30 +27,33 @@ async def sendMessage(self, interaction: discord.Interaction, msg: str): self.sherp = await interaction.client.fetch_user(212613981465083906) try: embed = discord.Embed( - title = f"📬 You've got mail! **@{interaction.user} | ID: {interaction.user.id}** sent you the following:", + title = f"📬 You've got mail! **@{interaction.user.name}** sent you the following:", description = f"{msg}", colour = discord.Colour.gold() ) + embed.set_footer(text = f"From: {interaction.user} | UID: {interaction.user.id}") await self.sherp.send(embed = embed) embed = discord.Embed( - title = f"📫 Outgoing! The following message has been mailed to {self.sherp.display_name}:", + title = f"📫 Outgoing! The following message has been mailed to Sherp:", description = f"{msg}", colour = discord.Colour.green() ) + embed.set_footer(text = f"From: {interaction.user} | UID: {interaction.user.id}") await interaction.response.send_message(embed = embed) except: await interaction.response.send_message(f"❌ An unexpected error has occurred!") @commands.Cog.listener() async def on_message(self, message: discord.Message): - if (message.guild == None) and (message.author.id == 214657501193306112): + if (message.channel.type.name == "private") and (message.author.id == 212613981465083906): try: embed = discord.Embed( title = "📬 You've got mail! This is what Sherp said:", description = f"{message.content}", colour = discord.Colour.gold() ) + embed.set_footer(text = f"Another task completed by the Sherpmail distribution system.") # I used the ID 1183281507842924645 from the channel "non-cs-nonsense" ch = self.bot.get_channel(1183281507842924645) await ch.send(embed = embed) @@ -60,9 +63,10 @@ async def on_message(self, message: discord.Message): description = f"{message.content}", colour = discord.Colour.green() ) + embed.set_footer(text = f"Another task completed by the Sherpmail distribution system.") await message.reply(embed = embed, mention_author = False) except: await message.reply(f"❌ An unexpected error has occurred!") -async def setup_sherpMailbox_cog(bot, guilds): - await bot.add_cog(sherpMailbox(bot), guilds=guilds) \ No newline at end of file +async def setup_SherpMailbox_cog(bot, guilds): + await bot.add_cog(SherpMailbox(bot), guilds=guilds) \ No newline at end of file