From dd47cc05a6566a03af0c284d3ee047c2960d77b0 Mon Sep 17 00:00:00 2001 From: Dory Date: Thu, 21 Aug 2025 17:23:20 -0700 Subject: [PATCH] add deletion and prepopulated reacts --- bot.py | 22 ++++++++++++++++------ llm_client.py | 5 +++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/bot.py b/bot.py index 4204078..4211829 100644 --- a/bot.py +++ b/bot.py @@ -45,6 +45,8 @@ async def discord_send(channel, text, name, avatar=DEFAULT_AVATAR): else: message = await channel.send(content=chunk) messages.append(message.id) + await message.add_reaction("🔁") + await message.add_reaction("❌") return messages async def webhook(channel): @@ -88,6 +90,10 @@ 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: + old_message = await channel.fetch_message(old_message_id) + await old_message.clear_reaction("🔁") + await old_message.clear_reaction("❌") conversation.last_messages = await discord_send( channel, response, conversation.bot_name, ) @@ -98,7 +104,7 @@ async def on_message(message): @bot.event async def on_reaction_add(reaction, user): - if reaction.emoji != "🔁": + if reaction.emoji not in ("🔁", "❌") or user == bot.user: return message = reaction.message channel = message.channel @@ -120,11 +126,15 @@ async def on_reaction_add(reaction, user): await reaction.clear() for message in messages: await message.delete() - response = await conversation.regenerate() - conversation.last_messages = await discord_send( - channel, response, conversation.bot_name, - ) - conversation.save() + + if reaction.emoji == "❌": + conversation.pop() + elif reaction.emoji == "🔁": + response = await conversation.regenerate() + conversation.last_messages = await discord_send( + channel, response, conversation.bot_name, + ) + conversation.save() except Exception as e: print(f"An error occurred: {e}") await message.reply("Sorry, I had a little hiccup. Baka!") diff --git a/llm_client.py b/llm_client.py index 4e314ab..ee07637 100644 --- a/llm_client.py +++ b/llm_client.py @@ -68,6 +68,11 @@ class Conversation: {"role": "assistant", "content": assistant}, ]) + def pop(self): + if len(self.history) >= 3: + self.history = self.history[:-2] + self.save() + async def generate(self, text, media=tuple()): # prepare text part if text: