From c7a3316d45324e99c6efb9ac14054d577fa176e1 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Fri, 19 May 2023 23:49:55 +0200 Subject: [PATCH] bug fix --- llamabot.js | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/llamabot.js b/llamabot.js index f664933..10554ed 100644 --- a/llamabot.js +++ b/llamabot.js @@ -15,6 +15,8 @@ import { Partials } from 'discord.js'; +let botMessage; // define a variable to hold the message object + const client = new Client({ intents: [ GatewayIntentBits.DirectMessages, @@ -258,7 +260,6 @@ async function generateResponse(conversation, message) { const messagesCopy = [...conversation.messages]; // create a copy of the messages array - let botMessage; // define a variable to hold the message object let time = 0 // define a function that shows the system load percentage and updates the message const showSystemLoad = async () => { @@ -344,23 +345,35 @@ async function generateResponse(conversation, message) { botMessage = await message.channel.send({ embeds: [embedData] }); })(); } else { - botMessage.edit({ embeds: [embedData] }); // otherwise, update the message + try { + message.channel.messages.fetch(message.id) + .then(message => console.log(message.content)) //it fetched the message - good + botMessage.edit({ embeds: [embedData] }); // otherwise, update the message + } catch (error) { + return; //the message no longer exists and will be ignored + } } }); } else { const embedData = { color: 0x0099ff, title: 'Please wait.. I am thinking...', - fields: filedsData, // It seems like a typo, it should be `filedsData` instead of `filedsData` + fields: filedsData, }; // if the message object doesn't exist, create it if (!botMessage) { - (async () => { + (async () => { botMessage = await message.channel.send({ embeds: [embedData] }); })(); } else { - botMessage.edit({ embeds: [embedData] }); // otherwise, update the message + try { + message.channel.messages.fetch(message.id) + .then(message => console.log(message.content)) //it fetched the message - good + botMessage.edit({ embeds: [embedData] }); // otherwise, update the message + } catch (error) { + return; //the message no longer exists and will be ignored + } } } @@ -398,8 +411,8 @@ async function generateResponse(conversation, message) { // clear the interval, replace the "please wait" message with the response, and update the message clearInterval(refreshInterval); console.log(responseText); - botMessage.delete() - + await botMessage.delete() + botMessage = null; return responseText; } catch (err) { @@ -410,4 +423,4 @@ async function generateResponse(conversation, message) { } } -client.login(process.env.THE_TOKEN); // Replace with your bot token +client.login(process.env.THE_TOKEN); // Replace with your bot token \ No newline at end of file