This commit is contained in:
Raven Scott 2023-05-22 18:23:17 +02:00
parent da0650d3b6
commit bc6157e4a1

View File

@ -31,6 +31,9 @@ const channelIDs = process.env.CHANNEL_IDS.split(',');
// Store Conversations in a MAP // Store Conversations in a MAP
const conversations = new Map(); const conversations = new Map();
let botMessage; // define a variable to hold the message object
// Set busy function this allows us to set our bot into busy mode // Set busy function this allows us to set our bot into busy mode
// locking out all other tasks until the current one is complete // locking out all other tasks until the current one is complete
function setBusy(userId, isBusy) { function setBusy(userId, isBusy) {
@ -105,7 +108,7 @@ client.on('messageCreate', async (message) => {
if (!channelIDs.includes(message.channel.id)) { if (!channelIDs.includes(message.channel.id)) {
return; return;
} }
// Always ignore bots! // Always ignore bots!
if (message.author.bot) return; if (message.author.bot) return;
@ -124,7 +127,7 @@ client.on('messageCreate', async (message) => {
messages: [], messages: [],
busy: false busy: false
}; };
// If we do not have a conversation, lets generate one. // If we do not have a conversation, lets generate one.
// This requires a chatflow for the API. // This requires a chatflow for the API.
// Its better to have a default beginning conversation // Its better to have a default beginning conversation
@ -143,7 +146,7 @@ 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 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
@ -161,10 +164,10 @@ client.on('messageCreate', async (message) => {
try { try {
// Now we have our conversation set up // Now we have our conversation set up
// Lets set precence to busy // Lets set precence to busy
// We also will set our conversations MAP to busy // We also will set our conversations MAP to busy
// Locking out all other tasks // Locking out all other tasks
setPresenceBusy() setPresenceBusy()
setBusy(message.author.id, true); setBusy(message.author.id, true);
@ -215,7 +218,7 @@ client.on('messageCreate', async (message) => {
} }
conversations.set(userID, conversation); // Update user's conversation map in memory conversations.set(userID, conversation); // Update user's conversation map in memory
// Print the current conversation as it stands // Print the current conversation as it stands
console.log(conversation) console.log(conversation)
} catch (err) { } catch (err) {
@ -302,7 +305,6 @@ async function generateResponse(conversation, message) {
// Copy our messages from MAP // 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 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 () => {
@ -318,10 +320,10 @@ async function generateResponse(conversation, message) {
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 // lets build some embed data
let embedData; let embedData;
// If we have NO GPU config lets send system stats only // If we have NO GPU config lets send system stats only
if (process.env.GPU == 0) { if (process.env.GPU == 0) {
embedData = { embedData = {
@ -348,7 +350,13 @@ async function generateResponse(conversation, message) {
botMessage = await message.channel.send({ embeds: [embedData] }); botMessage = await message.channel.send({ embeds: [embedData] });
})(); })();
} else { } else {
botMessage.edit({ embeds: [embedData] }); // otherwise, update the message (async () => {
if (!isAnyConversationBusy()) {
botMessage.delete()
} else {
await botMessage.edit({ embeds: [embedData] }); // otherwise, update the message
}
})();
} }
} else { } else {
// If we do have GPU=1 lets send some card info too! // If we do have GPU=1 lets send some card info too!
@ -410,13 +418,14 @@ async function generateResponse(conversation, message) {
}); });
}; };
// call the function initially
await showSystemLoad();
// Grab the REFRESH_INTERVAL from ENV if not exist, lets use 7 (seconds)
const refreshInterval = setInterval(showSystemLoad, (process.env.REFRESH_INTERVAL || 7) * 1000);
try { try {
// call the function initially
await showSystemLoad();
// Grab the REFRESH_INTERVAL from ENV if not exist, lets use 7 (seconds)
const refreshInterval = setInterval(showSystemLoad, (process.env.REFRESH_INTERVAL || 7) * 1000);
// Sending request to our API // 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',
@ -442,7 +451,7 @@ async function generateResponse(conversation, message) {
// clear the interval, replace the "please wait" message with the response, and update the message // clear the interval, replace the "please wait" message with the response, and update the message
clearInterval(refreshInterval); clearInterval(refreshInterval);
console.log(responseText); console.log(responseText);
botMessage.delete() await botMessage.delete()
botMessage = null; botMessage = null;
return responseText; return responseText;