fix: Only add user message to history on success

This commit is contained in:
2025-08-17 11:34:11 -07:00
parent be8689af56
commit 9e804856ca
2 changed files with 22 additions and 0 deletions

1
bot.py
View File

@@ -99,6 +99,7 @@ async def on_message(message):
await message.channel.send(bot_response)
except Exception as e:
print(f"An error occurred: {e}")
conversation_history[channel_id].pop() # Remove user message on error
await message.channel.send("Sorry, I had a little hiccup. Baka!")

View File

@@ -107,6 +107,27 @@ class TestAoiBot(unittest.IsolatedAsyncioTestCase):
# Assertions
bot.newchat.callback.assert_awaited_once_with(interaction, prompt=None)
@patch('bot.openai.OpenAI')
async def test_on_message_api_error(self, MockOpenAI):
# Mock the OpenAI client to raise an error
mock_openai_instance = MockOpenAI.return_value
mock_openai_instance.chat.completions.create.side_effect = Exception("API Error")
# Mock a Discord message
message = AsyncMock()
message.author = MagicMock()
message.author.bot = False
message.channel = AsyncMock()
message.channel.id = 123
message.content = f"<@!{bot.bot.user.id}> This will fail"
message.attachments = []
# Call the on_message event handler
await bot.on_message(message)
# Assertions
bot.on_message.assert_awaited_once_with(message)
if __name__ == '__main__':
unittest.main()