Create sendRand and sendRandDM to handle message events

This commit is contained in:
Raven Scott 2023-04-10 03:50:45 +02:00
parent 999a554661
commit b1087d554b

View File

@ -5,7 +5,7 @@ const {resetResponses, userResetMessages} = require('./assets/resetMessages.js')
const { errorMessages, busyResponses } = require('./assets/errorMessages.js');
require('dotenv').config()
// hi
const {
Client,
GatewayIntentBits,
@ -59,6 +59,17 @@ client.once('ready', () => {
});
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.
}
async function sendRandDM(array) {
const arrayChoice = array[Math.floor(Math.random() * array.length)];
await message.author.send(arrayChoice); // give a notification of reset using a human like response.
}
// Only respond in the specified channels
if (message.channel.id !== channelID && message.channel.id !== channelID2) {
return;
@ -67,23 +78,20 @@ client.on('messageCreate', async (message) => {
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.
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',
content: `Welcome to our conversation your name is "llama".`
content: `You are rAi, the most helpful writing AI, you code and write without any mistakes.`
});
conversation.messages.push({
role: 'user',
@ -105,10 +113,10 @@ client.on('messageCreate', async (message) => {
content: message.cleanContent
});
}
if (message.content === '!reset' || message.content === '!r') {
conversations.delete(userID); // Delete user's conversation map if they request reset
const userResetMessage = userResetMessages[Math.floor(Math.random() * userResetMessages.length)];
await message.channel.send(userResetMessage); // give a notification of reset using a human like response.
sendRand(userResetMessages)
return;
}
@ -135,19 +143,16 @@ client.on('messageCreate', async (message) => {
setBusy(message.author.id, false);
} else {
// Handle empty response here
const randomResponse = emptyResponses[Math.floor(Math.random() * emptyResponses.length)];
// We had an empty response, create a new memory map for sanity.
await message.channel.send(randomResponse); // give the user a human like reponse about the error
sendRand(emptyResponses)
conversations.delete(userID); // Delete user's conversation map if they request reset
const resetMessage = resetResponses[Math.floor(Math.random() * resetResponses.length)];
await message.channel.send(resetMessage); // give a notification of reset using a human like response.
sendRand(resetMessage)
conversation.busy = false;
}
conversations.set(userID, conversation); // Update user's conversation map in memory
} catch (err) {
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 {
sendRand(errorMessages)
} finally {
setBusy(message.author.id, false);
}
});