bot: fix removing question mark reactions on query

This commit is contained in:
Ske 2019-05-03 09:00:39 +02:00
parent 07eeb3085b
commit 2c0a588fcc
2 changed files with 20 additions and 11 deletions

View File

@ -112,7 +112,7 @@ def run(config: Config):
await proxy.try_delete_by_reaction(conn, client, payload.message_id, payload.user_id, logger) await proxy.try_delete_by_reaction(conn, client, payload.message_id, payload.user_id, logger)
if payload.emoji.name in "\u2753\u2754": # Question mark if payload.emoji.name in "\u2753\u2754": # Question mark
async with pool.acquire() as conn: async with pool.acquire() as conn:
await proxy.do_query_message(conn, client, payload) await proxy.do_query_message(conn, client, payload.user_id, payload.message_id, payload.emoji)
@client.event @client.event
async def on_error(event_name, *args, **kwargs): async def on_error(event_name, *args, **kwargs):

View File

@ -233,26 +233,35 @@ async def try_delete_by_reaction(conn, client: discord.Client, message_id: int,
await handle_deleted_message(conn, client, message_id, original_message.content, logger) await handle_deleted_message(conn, client, message_id, original_message.content, logger)
async def do_query_message(conn, client: discord.Client, payload: discord.RawReactionActionEvent) -> bool: async def do_query_message(conn, client: discord.Client, queryer_id: int, message_id: int, reacted_emoji: str) -> bool:
# Find the message that was queried # Find the message that was queried
msg = await db.get_message(conn, payload.message_id) msg = await db.get_message(conn, message_id)
if not msg: if not msg:
return False return False
# Then DM the queryer the message embed # Then DM the queryer the message embed
card = await embeds.message_card(client, msg, include_pronouns=True) card = await embeds.message_card(client, msg, include_pronouns=True)
user = client.get_user(payload.user_id) user = client.get_user(queryer_id)
if not user: if not user:
# We couldn't find this user in the cache - bail # We couldn't find this user in the cache - bail
return False return False
# Remove reaction and send the card to the user # Remove the original reaction by the user if we have "Manage Messages"
channel = client.get_channel(msg.channel)
if isinstance(channel, discord.TextChannel) and channel.permissions_for(channel.guild.get_member(client.user.id)).manage_messages:
# We need the message instance itself to remove the reaction, since discord.py doesn't let you
# call HTTP endpoints with arbitrary IDs (at least, not without internals-hacking)
try:
message_instance = await channel.get_message(message_id)
member = channel.guild.get_member(queryer_id)
await message_instance.remove_reaction(reacted_emoji, member)
except (discord.Forbidden, discord.NotFound):
# Oh yeah, and this can also fail. yeet.
pass
# Send the card to the user
try: try:
channel = await client.get_channel(payload.channel_id)
message = await channel.get_message(payload.message_id)
if message.guild and message.channel.permissions_for(message.guild.get_member(client.user.id)).manage_messages:
await message.remove_reaction(payload.emoji, user)
await user.send(embed=card) await user.send(embed=card)
except discord.Forbidden: except discord.Forbidden:
# User doesn't have DMs enabled, not much we can do about that # User doesn't have DMs enabled, not much we can do about that
pass pass