diff --git a/orchard/bot/handlers/interactions/commands/reactx.py b/orchard/bot/handlers/interactions/commands/reactx.py index 7d3a8cb..1ce29cd 100644 --- a/orchard/bot/handlers/interactions/commands/reactx.py +++ b/orchard/bot/handlers/interactions/commands/reactx.py @@ -11,15 +11,36 @@ async def _reactx(body, _): async with Interactor(body["token"]) as i: channel_id = body["channel_id"] token = body["token"] + user_id = body["member"]["user"]["id"] messages = body["data"]["resolved"]["messages"] + number_reactions = [ + "1️⃣", + "2️⃣", + "3️⃣", + "4️⃣", + "5️⃣", + "6️⃣", + "7️⃣", + "8️⃣", + "9️⃣", + "🔟", + ] for key, value in messages.items(): message_id = value["id"] + for reaction in number_reactions: + if reaction not in [ + react["emoji"]["name"] for react in value["reactions"] + ]: + continue + reaction_users = await i.get_reactions(channel_id, message_id, reaction) + if user_id in [reactor["id"] for reactor in reaction_users.json()]: + await i.react(channel_id, message_id, reaction) await i.react(channel_id, message_id, "🚫") await i.edit(MessageBuilder().content("done."), "@original") reactx = SlashRoute( - name="reactx", + name="Ignore rdzip attachments", description="", handler=_reactx, default_permission=False, diff --git a/orchard/bot/lib/comm/interactor.py b/orchard/bot/lib/comm/interactor.py index 6c7c08a..7e557e5 100644 --- a/orchard/bot/lib/comm/interactor.py +++ b/orchard/bot/lib/comm/interactor.py @@ -10,6 +10,8 @@ from orchard.bot.lib.comm import pager from typing import Callable, List, Awaitable +import urllib.parse + base_url = f"{DISCORD_API_URL}/webhooks/{APPLICATION_ID}" logger = logging.getLogger(__name__) @@ -106,6 +108,13 @@ async def react(self, channel_id, message_id, emoji): headers={"Authorization": f"Bot {BOT_TOKEN}"}, ) + @could_raise + async def get_reactions(self, channel_id, message_id, emoji): + return await self.client.get( + f"{DISCORD_API_URL}/channels/{channel_id}/messages/{message_id}/reactions/{urllib.parse.quote(emoji)}", + headers={"Authorization": f"Bot {BOT_TOKEN}"}, + ) + def uuid(self): """ Return a new UUID. Adds it to a list for cleanup after. diff --git a/orchard/scan/sources/discord.py b/orchard/scan/sources/discord.py index c7fabef..4093345 100644 --- a/orchard/scan/sources/discord.py +++ b/orchard/scan/sources/discord.py @@ -83,15 +83,15 @@ async def get_metadata(self, iid): async def check_reaction(self, post, emoji): if not "reactions" in post: return False - + if emoji not in [react["emoji"]["name"] for react in post["reactions"]]: return False - + async with Client() as client: react_params = {"limit": 100} reactors = await client.get( f"{DISCORD_API_URL}/channels/{self.channel_id}/messages/{post['id']}/reactions/{urllib.parse.quote(emoji)}", - headers = { + headers={ "user-agent": USER_AGENT, "Authorization": f"Bot {self.bot_token}", }, @@ -141,14 +141,27 @@ async def get_iids(self): remove_attachments = await self.check_reaction(post, "🚫") # a message can only have a maximum of 10 attachments, so we use number reactions - number_reactions = ["1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣", "🔟"] + number_reactions = [ + "1️⃣", + "2️⃣", + "3️⃣", + "4️⃣", + "5️⃣", + "6️⃣", + "7️⃣", + "8️⃣", + "9️⃣", + "🔟", + ] # check all attachments and corresponding number reactions. if no number reaction is found, then we ignore every attachment ignore_all_attachments = True attachment_numbers = [] for i, attachment in enumerate(post["attachments"]): if attachment["filename"].endswith(".rdzip"): - if remove_attachments and await self.check_reaction(post, number_reactions[i]): + if remove_attachments and await self.check_reaction( + post, number_reactions[i] + ): ignore_all_attachments = False continue attachment_numbers.append(i)