Adding busy state tracker to the setPrecence activty for the bot allowing users to know when the bot is busy

This commit is contained in:
Raven Scott 2023-04-10 04:12:42 +02:00
parent b1087d554b
commit 40e865521d
1 changed files with 35 additions and 8 deletions

View File

@ -40,26 +40,44 @@ function setBusy(userId, isBusy) {
function isAnyConversationBusy() {
for (const conversation of conversations.values()) {
if (conversation.busy) {
setPresenceBusy()
return true;
}
}
return false;
}
client.once('ready', () => {
console.log('Bot is ready.');
function setPresenceBusy() {
client.user.setPresence({
activities: [{
name: `AI`,
name: `Processing a Request`,
type: ActivityType.Playing
}],
status: 'dnd',
});
}
function setPresenceOnline() {
client.user.setPresence({
activities: [{
name: `Ready for Request`,
type: ActivityType.Playing
}],
status: 'online',
});
}
client.once('ready', () => {
console.log('Bot is ready.');
setPresenceOnline()
});
client.on('messageCreate', async (message) => {
async function sendRand(array) {
const arrayChoice = array[Math.floor(Math.random() * array.length)];
await message.channel.send(arrayChoice); // give a notification of reset using a human like response.
@ -76,18 +94,23 @@ client.on('messageCreate', async (message) => {
}
if (message.author.bot) return; // Ignore messages from bots
// Check if any conversation is busy
if (isAnyConversationBusy()) {
message.delete()
sendRandDM(busyResponses)
// Update bot presence to "Busy"
setPresenceBusy()
message.delete();
sendRandDM(busyResponses);
return;
}
const userID = message.author.id;
let conversation = conversations.get(userID) || {
messages: [],
busy: false
};
if (conversation.messages.length === 0) {
conversation.messages.push({
role: 'user',
@ -127,6 +150,7 @@ client.on('messageCreate', async (message) => {
});
try {
setPresenceBusy()
setBusy(message.author.id, true);
const response = await generateResponse(conversation);
@ -140,19 +164,22 @@ client.on('messageCreate', async (message) => {
if (response && response.trim()) {
// Send response to user if it's not empty
await message.channel.send(response);
setPresenceOnline()
setBusy(message.author.id, false);
} else {
// Handle empty response here
sendRand(emptyResponses)
conversations.delete(userID); // Delete user's conversation map if they request reset
sendRand(resetMessage)
setPresenceOnline()
conversation.busy = false;
}
conversations.set(userID, conversation); // Update user's conversation map in memory
} catch (err) {
console.error(err);
sendRand(errorMessages)
sendRand(errorMessages)
} finally {
setPresenceOnline()
setBusy(message.author.id, false);
}
});