Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability for the bot to react using the reactx command #72

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading