Skip to content

Commit

Permalink
Merge pull request #72 from RandomGuyJCI/multiple-attachment-reaction
Browse files Browse the repository at this point in the history
Add the ability for the bot to react using the reactx command
  • Loading branch information
auburnsummer authored Oct 6, 2024
2 parents 9184759 + 213e2ae commit ffc6560
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
23 changes: 22 additions & 1 deletion orchard/bot/handlers/interactions/commands/reactx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions orchard/bot/lib/comm/interactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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.
Expand Down
23 changes: 18 additions & 5 deletions orchard/scan/sources/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
},
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ffc6560

Please sign in to comment.