From 999a5546617d500a7379916416c6457d2aa40dad Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Mon, 10 Apr 2023 03:28:41 +0200 Subject: [PATCH] moving busy state global so users cant spawn overrun processes --- llamabot.js | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/llamabot.js b/llamabot.js index 5a7d7d4..5b5d270 100644 --- a/llamabot.js +++ b/llamabot.js @@ -29,6 +29,24 @@ const channelID2 = '1094628334727614605'; // Replace with your channel ID const conversations = new Map(); +function setBusy(userId, isBusy) { + if (conversations.has(userId)) { + conversations.get(userId).busy = isBusy; + } else { + conversations.set(userId, { busy: isBusy }); + } + } + + function isAnyConversationBusy() { + for (const conversation of conversations.values()) { + if (conversation.busy) { + return true; + } + } + return false; + } + + client.once('ready', () => { console.log('Bot is ready.'); client.user.setPresence({ @@ -47,12 +65,13 @@ client.on('messageCreate', async (message) => { } if (message.author.bot) return; // Ignore messages from bots - if (conversations.get(message.author.id)?.busy) { + if (isAnyConversationBusy()) { message.delete() const busyResponse = busyResponses[Math.floor(Math.random() * busyResponses.length)]; await message.author.send(busyResponse); // give a notification of reset using a human like response. return; - } + } + const userID = message.author.id; let conversation = conversations.get(userID) || { @@ -100,7 +119,7 @@ client.on('messageCreate', async (message) => { }); try { - conversation.busy = true; + setBusy(message.author.id, true); const response = await generateResponse(conversation); @@ -113,7 +132,7 @@ client.on('messageCreate', async (message) => { if (response && response.trim()) { // Send response to user if it's not empty await message.channel.send(response); - conversation.busy = false; + setBusy(message.author.id, false); } else { // Handle empty response here const randomResponse = emptyResponses[Math.floor(Math.random() * emptyResponses.length)]; @@ -129,8 +148,8 @@ client.on('messageCreate', async (message) => { console.error(err); const errorMessage = errorMessages[Math.floor(Math.random() * errorMessages.length)]; await message.channel.send(errorMessage); // give a notification of reset using a human like response. } finally { - conversation.busy = false; - } + setBusy(message.author.id, false); + } }); async function generateResponse(conversation) {