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

View File

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