fix convo and hook lookup

This commit is contained in:
2025-08-21 10:18:25 -07:00
parent 452bd41e7e
commit 3d9c5d8e71
2 changed files with 27 additions and 21 deletions

31
bot.py
View File

@@ -47,16 +47,14 @@ async def discord_send(channel, text, name, avatar=DEFAULT_AVATAR):
messages.append(message) messages.append(message)
return messages return messages
# --- Data Storage ---
# Keyed by channel ID
conversations = {}
_webhooks = {}
async def webhook(channel): async def webhook(channel):
if channel.id not in _webhooks: name = f'aoi-{channel.id}'
hook = await channel.create_webhook(name=f'aoi-{channel.id}') channel_hooks = [
_webhooks[channel.id] = hook hook for hook in (await channel.webhooks()) if hook.name == name
return _webhooks[channel.id] ]
if not channel_hooks:
return await channel.create_webhook(name=f'aoi-{channel.id}')
return channel_hooks[0]
# --- Bot Events --- # --- Bot Events ---
@@ -75,9 +73,7 @@ async def on_message(message):
bot_tag = f'<@{bot.user.id}>' bot_tag = f'<@{bot.user.id}>'
channel = message.channel channel = message.channel
if channel.id not in conversations: conversation = await Conversation.get(channel.id)
conversations[channel.id] = await Conversation.create(args.base_url)
conversation = conversations[channel.id]
user_message = message.content user_message = message.content
if user_message.startswith(bot_tag): if user_message.startswith(bot_tag):
user_message = user_message[len(bot_tag):] user_message = user_message[len(bot_tag):]
@@ -92,9 +88,9 @@ async def on_message(message):
try: try:
async with channel.typing(): async with channel.typing():
response = await conversation.generate(user_message, media) response = await conversation.generate(user_message, media)
conversation.last_messages = await discord_send( conversation.last_messages = await discord_send(
channel, response, conversation.bot_name, channel, response, conversation.bot_name,
) )
except Exception as e: except Exception as e:
print(f"An error occurred: {e}") print(f"An error occurred: {e}")
await message.reply("Sorry, I had a little hiccup. Baka!") await message.reply("Sorry, I had a little hiccup. Baka!")
@@ -105,7 +101,7 @@ async def on_reaction_add(reaction, user):
return return
message = reaction.message message = reaction.message
channel = message.channel channel = message.channel
conversation = conversations[channel.id] conversation = await Conversation.get(channel.id)
if message not in conversation.last_messages: if message not in conversation.last_messages:
await reaction.clear() await reaction.clear()
return return
@@ -132,8 +128,7 @@ async def on_reaction_add(reaction, user):
async def newchat(interaction: discord.Interaction, prompt: str = None): async def newchat(interaction: discord.Interaction, prompt: str = None):
await interaction.response.defer() await interaction.response.defer()
channel_id = interaction.channel_id channel_id = interaction.channel_id
conversation = await Conversation.create(args.base_url, prompt) conversation = await Conversation.create(channel_id, args.base_url, prompt)
conversations[channel_id] = conversation
await interaction.followup.send( await interaction.followup.send(
f'Starting a new chat with {conversation.bot_name}: "{prompt}"' f'Starting a new chat with {conversation.bot_name}: "{prompt}"'
) )

View File

@@ -10,6 +10,8 @@ DEFAULT_SYSTEM_PROMPT = (
) )
NAME_PROMPT = "reply with your name, nothing else, no punctuation" NAME_PROMPT = "reply with your name, nothing else, no punctuation"
conversations = {}
class Conversation: class Conversation:
def __init__(self, client, name, prompt): def __init__(self, client, name, prompt):
@@ -19,11 +21,20 @@ class Conversation:
self.client = client self.client = client
@classmethod @classmethod
async def create(cls, base_url, prompt=None): async def get(cls, key):
if key not in conversations:
conversations[key] = await Conversation.create(args.base_url)
return conversations[key]
@classmethod
async def create(cls, channel_id, base_url, prompt=None):
client = AsyncOpenAI(base_url=base_url, api_key=API_KEY) client = AsyncOpenAI(base_url=base_url, api_key=API_KEY)
if not prompt: if not prompt:
return cls(client, DEFAULT_NAME, DEFAULT_SYSTEM_PROMPT) convo = cls(client, DEFAULT_NAME, DEFAULT_SYSTEM_PROMPT)
return cls(client, await cls.get_name(client, prompt), prompt) else:
convo = cls(client, await cls.get_name(client, prompt), prompt)
conversations[channel_id] = convo
return convo
@classmethod @classmethod
async def get_name(self, client, system_prompt): async def get_name(self, client, system_prompt):