diff --git a/llamabot.js b/llamabot.js index 10554ed..a994690 100644 --- a/llamabot.js +++ b/llamabot.js @@ -145,6 +145,7 @@ client.on('messageCreate', async (message) => { const response = await generateResponse(conversation, message); + // Append bot message to conversation history conversation.messages.push({ role: 'assistant', @@ -340,13 +341,13 @@ async function generateResponse(conversation, message) { }; // if the message object doesn't exist, create it - if (!botMessage) { + if (botMessage == null) { (async () => { botMessage = await message.channel.send({ embeds: [embedData] }); })(); } else { try { - message.channel.messages.fetch(message.id) + message.channel.messages.fetch(botMessage.id) .then(message => console.log(message.content)) //it fetched the message - good botMessage.edit({ embeds: [embedData] }); // otherwise, update the message } catch (error) { @@ -362,13 +363,13 @@ async function generateResponse(conversation, message) { }; // if the message object doesn't exist, create it - if (!botMessage) { + if (botMessage == null) { (async () => { botMessage = await message.channel.send({ embeds: [embedData] }); })(); } else { try { - message.channel.messages.fetch(message.id) + message.channel.messages.fetch(botMessage.id) .then(message => console.log(message.content)) //it fetched the message - good botMessage.edit({ embeds: [embedData] }); // otherwise, update the message } catch (error) { @@ -408,16 +409,18 @@ async function generateResponse(conversation, message) { const responseText = choice.message.content; + message.channel.messages.fetch(botMessage.id).then(message => message.delete()) + // clear the interval, replace the "please wait" message with the response, and update the message - clearInterval(refreshInterval); + await clearInterval(refreshInterval); console.log(responseText); - await botMessage.delete() botMessage = null; return responseText; } catch (err) { throw err; } finally { + botMessage = null; clearTimeout(timeout); time = 0 }