This commit is contained in:
Raven Scott 2023-05-21 00:20:53 +02:00
parent 927b5c834d
commit c75926728c

View File

@ -28,8 +28,11 @@ const client = new Client({
// Grab ChannelIDs from the .env file // Grab ChannelIDs from the .env file
const channelIDs = process.env.CHANNEL_IDS.split(','); const channelIDs = process.env.CHANNEL_IDS.split(',');
// Store Conversations in a MAP
const conversations = new Map(); const conversations = new Map();
// Set busy function this allows us to set our bot into busy mode
// locking out all other tasks until the current one is complete
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;
@ -40,6 +43,8 @@ function setBusy(userId, isBusy) {
} }
} }
// General check, if any conversation is busy
// If yes, flag it and let us know
function isAnyConversationBusy() { function isAnyConversationBusy() {
for (const conversation of conversations.values()) { for (const conversation of conversations.values()) {
if (conversation.busy) { if (conversation.busy) {
@ -50,6 +55,7 @@ function isAnyConversationBusy() {
return false; return false;
} }
// Setting our precence to busy within the bots status
function setPresenceBusy() { function setPresenceBusy() {
client.user.setPresence({ client.user.setPresence({
activities: [{ activities: [{
@ -60,6 +66,8 @@ function setPresenceBusy() {
}); });
} }
// Setting our precence to ready within the bots status
function setPresenceOnline() { function setPresenceOnline() {
client.user.setPresence({ client.user.setPresence({
activities: [{ activities: [{
@ -71,18 +79,23 @@ function setPresenceOnline() {
} }
// When we have logged in to discord api
// Set precence to online.
client.once('ready', () => { client.once('ready', () => {
console.log('Bot is ready.'); console.log('Bot is ready.');
setPresenceOnline() setPresenceOnline()
}); });
// When a message is sent within discord, lets handle it.
client.on('messageCreate', async (message) => { client.on('messageCreate', async (message) => {
// Function to send a random message from any array
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.
} }
// Function to send a random Direct Message from any array
async function sendRandDM(array) { async function sendRandDM(array) {
const arrayChoice = array[Math.floor(Math.random() * array.length)]; const arrayChoice = array[Math.floor(Math.random() * array.length)];
await message.author.send(arrayChoice); // give a notification of reset using a human like response. await message.author.send(arrayChoice); // give a notification of reset using a human like response.
@ -92,8 +105,9 @@ client.on('messageCreate', async (message) => {
if (!channelIDs.includes(message.channel.id)) { if (!channelIDs.includes(message.channel.id)) {
return; return;
} }
if (message.author.bot) return; // Ignore messages from bots // Always ignore bots!
if (message.author.bot) return;
// Check if any conversation is busy // Check if any conversation is busy
if (isAnyConversationBusy()) { if (isAnyConversationBusy()) {
@ -103,12 +117,18 @@ client.on('messageCreate', async (message) => {
sendRandDM(busyResponses); sendRandDM(busyResponses);
return; return;
} }
// Set user ID and get our conversation.
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 we do not have a conversation, lets generate one.
// This requires a chatflow for the API.
// Its better to have a default beginning conversation
// Providing context for the AI Model.
if (conversation.messages.length === 0) { if (conversation.messages.length === 0) {
conversation.messages.push({ conversation.messages.push({
role: 'user', role: 'user',
@ -123,13 +143,16 @@ client.on('messageCreate', async (message) => {
content: ` Hello, ${message.author.username}, how may I help you?` content: ` Hello, ${message.author.username}, how may I help you?`
}); });
} }
// If a user needs a reset, we delete their MAP
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
sendRand(userResetMessages) sendRand(userResetMessages)
return; return;
} }
// Begin processing our conversation, this is our main work flow.
// Append user message to conversation history // Append user message to conversation history
conversation.messages.push({ conversation.messages.push({
role: 'user', role: 'user',
@ -137,12 +160,18 @@ client.on('messageCreate', async (message) => {
}); });
try { try {
// Now we have our conversation set up
// Lets set precence to busy
// We also will set our conversations MAP to busy
// Locking out all other tasks
setPresenceBusy() setPresenceBusy()
setBusy(message.author.id, true); setBusy(message.author.id, true);
// Lets start generating the response
const response = await generateResponse(conversation, message); const response = await generateResponse(conversation, message);
// Append bot message to conversation history // Append bot message to conversation history when it is ready
conversation.messages.push({ conversation.messages.push({
role: 'assistant', role: 'assistant',
content: response content: response
@ -155,22 +184,26 @@ client.on('messageCreate', async (message) => {
// if we are over the discord char limit we need chunks... // if we are over the discord char limit we need chunks...
if (response.length > limit) { if (response.length > limit) {
// We are going to check all of the message chunks if our response is too large for discord.
// We can extend our message size using chunks, the issue?
// Users can abuse this feature, we lock this to 15 to avoid API Abuse.
const chunks = response.match(new RegExp(`.{1,${limit}}`, "g")); const chunks = response.match(new RegExp(`.{1,${limit}}`, "g"));
if (chunks.length >= 15) return await message.channel.send("Response chunks too large. Try again"); if (chunks.length >= 15) return await message.channel.send("Response chunks too large. Try again");
// If we do now have too many chunks, lets send each one using our overflow delay
for (let i = 0; i < chunks.length; i++) { for (let i = 0; i < chunks.length; i++) {
setTimeout(() => { setTimeout(() => {
message.channel.send(chunks[i]); message.channel.send(chunks[i]);
}, i * (process.env.OVERFLOW_DELAY || 3) * 1000); // delay of 3 seconds between each chunk to save on API requests }, i * (process.env.OVERFLOW_DELAY || 3) * 1000); // delay of 3 seconds between each chunk to save on API requests
} }
} else { } else {
// We are good to go, send the response // We are good to go message is not too large for discord, send the response
await message.channel.send(response.replace("@", "")); await message.channel.send(response.replace("@", ""));
} }
// We have completed our task, lets go online
setPresenceOnline() setPresenceOnline()
// set our conversation MAP to not busy
setBusy(message.author.id, false); setBusy(message.author.id, false);
} else { } else {
// Handle empty response here // Handle empty response here
@ -180,24 +213,30 @@ client.on('messageCreate', async (message) => {
setPresenceOnline() setPresenceOnline()
conversation.busy = false; conversation.busy = false;
} }
conversations.set(userID, conversation); // Update user's conversation map in memory
console.log(conversation)
conversations.set(userID, conversation); // Update user's conversation map in memory
// Print the current conversation as it stands
console.log(conversation)
} catch (err) { } catch (err) {
// If we have any errors lets send a response
console.error(err); console.error(err);
return sendRand(errorMessages) return sendRand(errorMessages)
} finally { } finally {
// We are done! Lets finish up going online
setPresenceOnline() setPresenceOnline()
setBusy(message.author.id, false); setBusy(message.author.id, false);
} }
}); });
// Import cheerio for scraping
import cheerio from 'cheerio'; import cheerio from 'cheerio';
async function generateResponse(conversation, message) { async function generateResponse(conversation, message) {
// Begin web scraper if a https:// OR http:// URL is detected
// Check if message contains a URL // Check if message contains a URL
const urlRegex = /(https?:\/\/[^\s]+)/g; const urlRegex = /(https?:\/\/[^\s]+)/g;
// Match our REGEX
const urls = message.content.match(urlRegex); const urls = message.content.match(urlRegex);
if (urls) { if (urls) {
@ -219,15 +258,17 @@ async function generateResponse(conversation, message) {
response += `Description: ${pageDescription}\n`; response += `Description: ${pageDescription}\n`;
} }
if (pageContent) { if (pageContent) {
// Lets check for content and grab only the amount as configured.
const MAX_CONTENT_LENGTH = process.env.MAX_CONTENT_LENGTH; const MAX_CONTENT_LENGTH = process.env.MAX_CONTENT_LENGTH;
let plainTextContent = $('<div>').html(pageContent).text().trim().replace(/[\r\n\t]+/g, ' '); let plainTextContent = $('<div>').html(pageContent).text().trim().replace(/[\r\n\t]+/g, ' ');
// Clean up code remove it from processing
const codePattern = /\/\/|\/\*|\*\/|\{|\}|\[|\]|\bfunction\b|\bclass\b|\b0x[0-9A-Fa-f]+\b|\b0b[01]+\b/; const codePattern = /\/\/|\/\*|\*\/|\{|\}|\[|\]|\bfunction\b|\bclass\b|\b0x[0-9A-Fa-f]+\b|\b0b[01]+\b/;
const isCode = codePattern.test(plainTextContent); const isCode = codePattern.test(plainTextContent);
if (isCode) { if (isCode) {
plainTextContent = plainTextContent.replace(codePattern, ''); plainTextContent = plainTextContent.replace(codePattern, '');
} }
// Remove anything enclosed in brackets // Remove anything enclosed in brackets JUNK DATA
plainTextContent = plainTextContent.replace(/ *\([^)]*\) */g, ''); plainTextContent = plainTextContent.replace(/ *\([^)]*\) */g, '');
if (plainTextContent.length > MAX_CONTENT_LENGTH) { if (plainTextContent.length > MAX_CONTENT_LENGTH) {
plainTextContent = plainTextContent.substring(0, MAX_CONTENT_LENGTH) + '...'; plainTextContent = plainTextContent.substring(0, MAX_CONTENT_LENGTH) + '...';
@ -250,28 +291,38 @@ async function generateResponse(conversation, message) {
} }
} }
} }
// We need an abort controller to stop our progress message editor
const controller = new AbortController(); const controller = new AbortController();
// Set our timeout for the controller
const timeout = setTimeout(() => { const timeout = setTimeout(() => {
controller.abort(); controller.abort();
}, 900000); }, 900000);
// Copy our messages from MAP
const messagesCopy = [...conversation.messages]; // create a copy of the messages array const messagesCopy = [...conversation.messages]; // create a copy of the messages array
let botMessage; // define a variable to hold the message object let botMessage; // define a variable to hold the message object
let time = 0 let time = 0
// define a function that shows the system load percentage and updates the message // define a function that shows the system load percentage and updates the message
const showSystemLoad = async () => { const showSystemLoad = async () => {
// Configure our inital time
time = Number(time) + Number(process.env.REFRESH_INTERVAL); time = Number(time) + Number(process.env.REFRESH_INTERVAL);
// Get system stats
cpuStat.usagePercent(function (err, percent, seconds) { cpuStat.usagePercent(function (err, percent, seconds) {
if (err) { if (err) {
return console.log(err); return console.log(err);
} }
// Setting out system stat vars
const systemLoad = percent; const systemLoad = percent;
const freeMemory = os.freemem() / 1024 / 1024 / 1024; const freeMemory = os.freemem() / 1024 / 1024 / 1024;
const totalMemory = os.totalmem() / 1024 / 1024 / 1024; const totalMemory = os.totalmem() / 1024 / 1024 / 1024;
const usedMemory = totalMemory - freeMemory; const usedMemory = totalMemory - freeMemory;
// lets build some embed data
let embedData; let embedData;
// If we have NO GPU config lets send system stats only
if (process.env.GPU == 0) { if (process.env.GPU == 0) {
embedData = { embedData = {
color: 0x0099ff, color: 0x0099ff,
@ -300,6 +351,7 @@ async function generateResponse(conversation, message) {
botMessage.edit({ embeds: [embedData] }); // otherwise, update the message botMessage.edit({ embeds: [embedData] }); // otherwise, update the message
} }
} else { } else {
// If we do have GPU=1 lets send some card info too!
smi(function (err, data) { smi(function (err, data) {
if (err) { if (err) {
// Handle error if smi function fails // Handle error if smi function fails
@ -365,6 +417,7 @@ async function generateResponse(conversation, message) {
const refreshInterval = setInterval(showSystemLoad, (process.env.REFRESH_INTERVAL || 7) * 1000); const refreshInterval = setInterval(showSystemLoad, (process.env.REFRESH_INTERVAL || 7) * 1000);
try { try {
// Sending request to our API
const response = await fetch(`http://${process.env.ROOT_IP}:${process.env.ROOT_PORT}/v1/chat/completions`, { const response = await fetch(`http://${process.env.ROOT_IP}:${process.env.ROOT_PORT}/v1/chat/completions`, {
method: 'POST', method: 'POST',
headers: { headers: {