moving busy state global so users cant spawn overrun processes

This commit is contained in:
Raven Scott 2023-04-10 03:28:41 +02:00
parent c14635769d
commit 999a554661
1 changed files with 25 additions and 6 deletions

View File

@ -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) {