fix: Only add user message to history on success
This commit is contained in:
1
bot.py
1
bot.py
@@ -99,6 +99,7 @@ async def on_message(message):
|
|||||||
await message.channel.send(bot_response)
|
await message.channel.send(bot_response)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"An error occurred: {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!")
|
await message.channel.send("Sorry, I had a little hiccup. Baka!")
|
||||||
|
|
||||||
|
|
||||||
|
@@ -107,6 +107,27 @@ class TestAoiBot(unittest.IsolatedAsyncioTestCase):
|
|||||||
# Assertions
|
# Assertions
|
||||||
bot.newchat.callback.assert_awaited_once_with(interaction, prompt=None)
|
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__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Reference in New Issue
Block a user