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'); const { errorMessages, busyResponses } = require('./assets/errorMessages.js');
require('dotenv').config() require('dotenv').config()
// hi
const { const {
Client, Client,
GatewayIntentBits, GatewayIntentBits,
@ -59,6 +59,17 @@ client.once('ready', () => {
}); });
client.on('messageCreate', async (message) => { 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 // Only respond in the specified channels
if (message.channel.id !== channelID && message.channel.id !== channelID2) { if (message.channel.id !== channelID && message.channel.id !== channelID2) {
return; return;
@ -67,23 +78,20 @@ client.on('messageCreate', async (message) => {
if (isAnyConversationBusy()) { if (isAnyConversationBusy()) {
message.delete() message.delete()
const busyResponse = busyResponses[Math.floor(Math.random() * busyResponses.length)]; sendRandDM(busyResponses)
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: [],
busy: false busy: false
}; };
if (conversation.messages.length === 0) { if (conversation.messages.length === 0) {
conversation.messages.push({ conversation.messages.push({
role: 'user', 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({ conversation.messages.push({
role: 'user', role: 'user',
@ -105,10 +113,10 @@ client.on('messageCreate', async (message) => {
content: message.cleanContent content: message.cleanContent
}); });
} }
if (message.content === '!reset' || message.content === '!r') { if (message.content === '!reset' || message.content === '!r') {
conversations.delete(userID); // Delete user's conversation map if they request reset conversations.delete(userID); // Delete user's conversation map if they request reset
const userResetMessage = userResetMessages[Math.floor(Math.random() * userResetMessages.length)]; sendRand(userResetMessages)
await message.channel.send(userResetMessage); // give a notification of reset using a human like response.
return; return;
} }
@ -135,19 +143,16 @@ client.on('messageCreate', async (message) => {
setBusy(message.author.id, 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)]; sendRand(emptyResponses)
// 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
conversations.delete(userID); // Delete user's conversation map if they request reset conversations.delete(userID); // Delete user's conversation map if they request reset
const resetMessage = resetResponses[Math.floor(Math.random() * resetResponses.length)]; sendRand(resetMessage)
await message.channel.send(resetMessage); // give a notification of reset using a human like response.
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);
const errorMessage = errorMessages[Math.floor(Math.random() * errorMessages.length)]; sendRand(errorMessages)
await message.channel.send(errorMessage); // give a notification of reset using a human like response. } finally { } finally {
setBusy(message.author.id, false); setBusy(message.author.id, false);
} }
}); });