From ede777ee42512bb38ae411a760cf25876e731695 Mon Sep 17 00:00:00 2001 From: Dory Date: Fri, 22 Aug 2025 18:29:34 -0700 Subject: [PATCH] clear reactions on /newchat --- bot.py | 22 +++++++++++++++------- llm_client.py | 6 ++++-- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/bot.py b/bot.py index 83a5dc5..2de3095 100644 --- a/bot.py +++ b/bot.py @@ -64,6 +64,15 @@ async def webhook(channel): return await channel.create_webhook(name=f'aoi-{channel.id}') return channel_hooks[0] +async def clear_reactions(channel, message_ids): + for message_id in message_ids: + try: + message = await channel.fetch_message(message_id) + await message.clear_reaction("🔁") + await message.clear_reaction("❌") + except (discord.NotFound, discord.Forbidden): + pass # Ignore if message is not found or we don't have perms + # --- Bot Events --- @bot.event @@ -96,13 +105,7 @@ async def on_message(message): try: async with channel.typing(): response = await conversation.generate(user_message, media) - for old_message_id in conversation.last_messages: - try: - old_message = await channel.fetch_message(old_message_id) - await old_message.clear_reaction("🔁") - await old_message.clear_reaction("❌") - except (discord.NotFound, discord.Forbidden): - pass # Ignore if message is not found or we don't have perms + await clear_reactions(channel, conversation.last_messages) conversation.last_messages = await discord_send( channel, response, conversation.bot_name, ) @@ -158,6 +161,11 @@ async def on_reaction_add(reaction, user): async def newchat(interaction: discord.Interaction, prompt: str = None): await interaction.response.defer() channel_id = interaction.channel_id + old_convo = await Conversation.get( + channel_id, args.base_url, bot.db, create_if_not_exist=False, + ) + if old_convo: + await clear_reactions(interaction.channel, old_convo.last_messages) conversation = await Conversation.create( channel_id, args.base_url, bot.db, prompt ) diff --git a/llm_client.py b/llm_client.py index 6e9ff1e..60c99c9 100644 --- a/llm_client.py +++ b/llm_client.py @@ -33,7 +33,7 @@ class Conversation: ) @classmethod - async def get(cls, key, base_url, db): + async def get(cls, key, base_url, db, create_if_not_exist=True): convo_data = db.get_conversation(key) if convo_data: history, bot_name, last_messages = convo_data @@ -42,7 +42,9 @@ class Conversation: convo.history = history convo.last_messages = last_messages return convo - return await Conversation.create(key, base_url, db) + if create_if_not_exists: + return await Conversation.create(key, base_url, db) + return None @classmethod async def create(cls, key, base_url, db, prompt=None):