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

@ -1,11 +1,11 @@
const Discord = require('discord.js'); const Discord = require('discord.js');
const fetch = require('node-fetch'); const fetch = require('node-fetch');
const emptyResponses = require('./assets/emptyMessages.js'); const emptyResponses = require('./assets/emptyMessages.js');
const {resetResponses, userResetMessages} = require('./assets/resetMessages.js'); 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,
@ -31,20 +31,20 @@ const conversations = new Map();
function setBusy(userId, isBusy) { function setBusy(userId, isBusy) {
if (conversations.has(userId)) { if (conversations.has(userId)) {
conversations.get(userId).busy = isBusy; conversations.get(userId).busy = isBusy;
} else { } else {
conversations.set(userId, { busy: isBusy }); conversations.set(userId, { busy: isBusy });
} }
} }
function isAnyConversationBusy() { function isAnyConversationBusy() {
for (const conversation of conversations.values()) { for (const conversation of conversations.values()) {
if (conversation.busy) { if (conversation.busy) {
return true; return true;
} }
} }
return false; return false;
} }
client.once('ready', () => { client.once('ready', () => {
@ -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,11 +78,9 @@ 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) || {
@ -79,11 +88,10 @@ client.on('messageCreate', async (message) => {
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,21 +143,18 @@ 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);
} }
}); });
async function generateResponse(conversation) { async function generateResponse(conversation) {