From 8fd51f30a006029ddf9a10770a61e4366fef49f5 Mon Sep 17 00:00:00 2001 From: Random Guy JCI <22722393+RandomGuyJCI@users.noreply.github.com> Date: Wed, 9 Oct 2024 04:54:37 +0800 Subject: [PATCH] improve qol for multiple attachment reactions - The number reactions now only account for rdzip attachments position, useful for messages with both image/video embeds and rdzips since it might affect the actual order of attachments - The bot no longer reacts for numbers greater than the number of rdzips - Using the reactx command now shows the rdzips that will be ignored (or all rdzips if no number reaction is found) --- .../handlers/interactions/commands/reactx.py | 37 +++++++++++++++---- orchard/scan/sources/discord.py | 4 +- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/orchard/bot/handlers/interactions/commands/reactx.py b/orchard/bot/handlers/interactions/commands/reactx.py index 599febd..995e60d 100644 --- a/orchard/bot/handlers/interactions/commands/reactx.py +++ b/orchard/bot/handlers/interactions/commands/reactx.py @@ -8,10 +8,11 @@ async def _reactx(body, _): - async with Interactor(body["token"]) as i: + async with Interactor(body["token"]) as interactor: channel_id = body["channel_id"] token = body["token"] user_id = body["member"]["user"]["id"] + bot_id = body["application_id"] messages = body["data"]["resolved"]["messages"] number_reactions = [ "1️⃣", @@ -25,20 +26,42 @@ async def _reactx(body, _): "9️⃣", "🔟", ] + ignored_rdzips = [] for key, value in messages.items(): message_id = value["id"] - for reaction in number_reactions: + rdzip_attachments = [ + attachment["filename"] + for attachment in value["attachments"] + if attachment["filename"].endswith(".rdzip") + ] + for i, reaction in enumerate(number_reactions): if "reactions" not in value: continue 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") + if i >= len(rdzip_attachments): + continue + reaction_users = await interactor.get_reactions( + channel_id, message_id, reaction + ) + for reactor in reaction_users.json(): + if bot_id == reactor["id"]: + ignored_rdzips.append(rdzip_attachments[i]) + continue + if user_id == reactor["id"]: + ignored_rdzips.append(rdzip_attachments[i]) + await interactor.react(channel_id, message_id, reaction) + await interactor.react(channel_id, message_id, "🚫") + result = "Done, now ignoring all rdzips." + if len(ignored_rdzips) > 0: + result = ( + "Done, now ignoring the following rdzips:\n```" + + "\n".join(ignored_rdzips) + + "\n```" + ) + await interactor.edit(MessageBuilder().content(result), "@original") reactx = SlashRoute( diff --git a/orchard/scan/sources/discord.py b/orchard/scan/sources/discord.py index 4093345..014185a 100644 --- a/orchard/scan/sources/discord.py +++ b/orchard/scan/sources/discord.py @@ -157,14 +157,16 @@ async def get_iids(self): # check all attachments and corresponding number reactions. if no number reaction is found, then we ignore every attachment ignore_all_attachments = True attachment_numbers = [] + relative_position = 0 for i, attachment in enumerate(post["attachments"]): if attachment["filename"].endswith(".rdzip"): if remove_attachments and await self.check_reaction( - post, number_reactions[i] + post, number_reactions[relative_position], True ): ignore_all_attachments = False continue attachment_numbers.append(i) + relative_position += 1 if (not remove_attachments) or (not ignore_all_attachments): for i in attachment_numbers: