moving busy state global so users cant spawn overrun processes
This commit is contained in:
parent
c14635769d
commit
999a554661
27
llamabot.js
27
llamabot.js
@ -29,6 +29,24 @@ const channelID2 = '1094628334727614605'; // Replace with your channel ID
|
|||||||
|
|
||||||
const conversations = new Map();
|
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', () => {
|
client.once('ready', () => {
|
||||||
console.log('Bot is ready.');
|
console.log('Bot is ready.');
|
||||||
client.user.setPresence({
|
client.user.setPresence({
|
||||||
@ -47,13 +65,14 @@ client.on('messageCreate', async (message) => {
|
|||||||
}
|
}
|
||||||
if (message.author.bot) return; // Ignore messages from bots
|
if (message.author.bot) return; // Ignore messages from bots
|
||||||
|
|
||||||
if (conversations.get(message.author.id)?.busy) {
|
if (isAnyConversationBusy()) {
|
||||||
message.delete()
|
message.delete()
|
||||||
const busyResponse = busyResponses[Math.floor(Math.random() * busyResponses.length)];
|
const busyResponse = busyResponses[Math.floor(Math.random() * busyResponses.length)];
|
||||||
await message.author.send(busyResponse); // give a notification of reset using a human like response.
|
await message.author.send(busyResponse); // give a notification of reset using a human like response.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const userID = message.author.id;
|
const userID = message.author.id;
|
||||||
let conversation = conversations.get(userID) || {
|
let conversation = conversations.get(userID) || {
|
||||||
messages: [],
|
messages: [],
|
||||||
@ -100,7 +119,7 @@ client.on('messageCreate', async (message) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
conversation.busy = true;
|
setBusy(message.author.id, true);
|
||||||
|
|
||||||
const response = await generateResponse(conversation);
|
const response = await generateResponse(conversation);
|
||||||
|
|
||||||
@ -113,7 +132,7 @@ client.on('messageCreate', async (message) => {
|
|||||||
if (response && response.trim()) {
|
if (response && response.trim()) {
|
||||||
// Send response to user if it's not empty
|
// Send response to user if it's not empty
|
||||||
await message.channel.send(response);
|
await message.channel.send(response);
|
||||||
conversation.busy = false;
|
setBusy(message.author.id, false);
|
||||||
} else {
|
} else {
|
||||||
// Handle empty response here
|
// Handle empty response here
|
||||||
const randomResponse = emptyResponses[Math.floor(Math.random() * emptyResponses.length)];
|
const randomResponse = emptyResponses[Math.floor(Math.random() * emptyResponses.length)];
|
||||||
@ -129,7 +148,7 @@ client.on('messageCreate', async (message) => {
|
|||||||
console.error(err);
|
console.error(err);
|
||||||
const errorMessage = errorMessages[Math.floor(Math.random() * errorMessages.length)];
|
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 {
|
await message.channel.send(errorMessage); // give a notification of reset using a human like response. } finally {
|
||||||
conversation.busy = false;
|
setBusy(message.author.id, false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user