clear reactions on /newchat
This commit is contained in:
22
bot.py
22
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
|
||||
)
|
||||
|
@@ -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):
|
||||
|
Reference in New Issue
Block a user