Create sendRand and sendRandDM to handle message events
This commit is contained in:
parent
999a554661
commit
b1087d554b
61
llamabot.js
61
llamabot.js
@ -1,11 +1,11 @@
|
||||
const Discord = require('discord.js');
|
||||
const fetch = require('node-fetch');
|
||||
const emptyResponses = require('./assets/emptyMessages.js');
|
||||
const {resetResponses, userResetMessages} = require('./assets/resetMessages.js');
|
||||
const {errorMessages, busyResponses} = require('./assets/errorMessages.js');
|
||||
const { resetResponses, userResetMessages } = require('./assets/resetMessages.js');
|
||||
const { errorMessages, busyResponses } = require('./assets/errorMessages.js');
|
||||
|
||||
require('dotenv').config()
|
||||
// hi
|
||||
|
||||
const {
|
||||
Client,
|
||||
GatewayIntentBits,
|
||||
@ -31,20 +31,20 @@ const conversations = new Map();
|
||||
|
||||
function setBusy(userId, isBusy) {
|
||||
if (conversations.has(userId)) {
|
||||
conversations.get(userId).busy = isBusy;
|
||||
conversations.get(userId).busy = isBusy;
|
||||
} else {
|
||||
conversations.set(userId, { busy: isBusy });
|
||||
conversations.set(userId, { busy: isBusy });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isAnyConversationBusy() {
|
||||
function isAnyConversationBusy() {
|
||||
for (const conversation of conversations.values()) {
|
||||
if (conversation.busy) {
|
||||
return true;
|
||||
}
|
||||
if (conversation.busy) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
client.once('ready', () => {
|
||||
@ -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,11 +78,9 @@ 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) || {
|
||||
@ -79,11 +88,10 @@ client.on('messageCreate', async (message) => {
|
||||
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,21 +143,18 @@ 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 {
|
||||
setBusy(message.author.id, false);
|
||||
}
|
||||
sendRand(errorMessages)
|
||||
} finally {
|
||||
setBusy(message.author.id, false);
|
||||
}
|
||||
});
|
||||
|
||||
async function generateResponse(conversation) {
|
||||
|
Loading…
Reference in New Issue
Block a user