made it pretty
This commit is contained in:
parent
875ab7fdd2
commit
451205cb7d
245
llamabot.js
245
llamabot.js
@ -6,17 +6,22 @@ const errorMessages = require('./assets/errorMessages.js');
|
|||||||
|
|
||||||
require('dotenv').config()
|
require('dotenv').config()
|
||||||
// hi
|
// hi
|
||||||
const { Client, GatewayIntentBits, ActivityType, Partials } = require('discord.js');
|
const {
|
||||||
|
Client,
|
||||||
|
GatewayIntentBits,
|
||||||
|
ActivityType,
|
||||||
|
Partials
|
||||||
|
} = require('discord.js');
|
||||||
|
|
||||||
const client = new Client({
|
const client = new Client({
|
||||||
intents: [
|
intents: [
|
||||||
GatewayIntentBits.DirectMessages,
|
GatewayIntentBits.DirectMessages,
|
||||||
GatewayIntentBits.Guilds,
|
GatewayIntentBits.Guilds,
|
||||||
GatewayIntentBits.GuildBans,
|
GatewayIntentBits.GuildBans,
|
||||||
GatewayIntentBits.GuildMessages,
|
GatewayIntentBits.GuildMessages,
|
||||||
GatewayIntentBits.MessageContent,
|
GatewayIntentBits.MessageContent,
|
||||||
],
|
],
|
||||||
partials: [Partials.Channel],
|
partials: [Partials.Channel],
|
||||||
});
|
});
|
||||||
|
|
||||||
const channelID = '1094494101631680653'; // Replace with your channel ID
|
const channelID = '1094494101631680653'; // Replace with your channel ID
|
||||||
@ -25,114 +30,142 @@ const channelID2 = '1094628334727614605'; // Replace with your channel ID
|
|||||||
const conversations = new Map();
|
const conversations = new Map();
|
||||||
|
|
||||||
client.once('ready', () => {
|
client.once('ready', () => {
|
||||||
console.log('Bot is ready.');
|
console.log('Bot is ready.');
|
||||||
client.user.setPresence({
|
client.user.setPresence({
|
||||||
activities: [{ name: `AI`, type: ActivityType.Playing }],
|
activities: [{
|
||||||
status: 'dnd',
|
name: `AI`,
|
||||||
});
|
type: ActivityType.Playing
|
||||||
|
}],
|
||||||
|
status: 'dnd',
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('messageCreate', async (message) => {
|
client.on('messageCreate', async (message) => {
|
||||||
// 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;
|
||||||
}
|
}
|
||||||
if (message.author.bot) return; // Ignore messages from bots
|
if (message.author.bot) return; // Ignore messages from bots
|
||||||
|
|
||||||
if (conversations.get(message.author.id)?.busy) {
|
if (conversations.get(message.author.id)?.busy) {
|
||||||
message.delete()
|
message.delete()
|
||||||
await message.author.send("I'm currently busy with another process. Please try again later.");
|
await message.author.send("I'm currently busy with another process. Please try again later.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const userID = message.author.id;
|
const userID = message.author.id;
|
||||||
let conversation = conversations.get(userID) || { messages: [], busy: false };
|
let conversation = conversations.get(userID) || {
|
||||||
|
messages: [],
|
||||||
|
busy: false
|
||||||
if (conversation.messages.length === 0) {
|
};
|
||||||
conversation.messages.push({ role: 'user', content: `You are the smartest AI and help with anything I ask. You are great at coding! When giving a list always use one line per item. My name is ${message.author.username}.` });
|
|
||||||
conversation.messages.push({ role: 'user', content: `My name is ${message.author.username}.` });
|
|
||||||
conversation.messages.push({ role: 'assistant', content: `Hello, ${message.author.username}, how may I help you?` });
|
if (conversation.messages.length === 0) {
|
||||||
conversation.messages.push({ role: 'user', content: message.cleanContent });
|
conversation.messages.push({
|
||||||
|
role: 'user',
|
||||||
} else {
|
content: `You are the smartest AI and help with anything I ask. You are great at coding! When giving a list always use one line per item. My name is ${message.author.username}.`
|
||||||
// Append user message to conversation history
|
});
|
||||||
conversation.messages.push({ role: 'user', content: message.cleanContent });
|
conversation.messages.push({
|
||||||
}
|
role: 'user',
|
||||||
if (message.content === '!reset' || message.content === '!r') {
|
content: `My name is ${message.author.username}.`
|
||||||
conversations.delete(userID); // Delete user's conversation map if they request reset
|
});
|
||||||
await message.channel.send('Conversation reset.');
|
conversation.messages.push({
|
||||||
return;
|
role: 'assistant',
|
||||||
}
|
content: `Hello, ${message.author.username}, how may I help you?`
|
||||||
|
});
|
||||||
// Append user message to conversation history
|
conversation.messages.push({
|
||||||
conversation.messages.push({ role: 'user', content: message.cleanContent });
|
role: 'user',
|
||||||
|
content: message.cleanContent
|
||||||
try {
|
});
|
||||||
conversation.busy = true;
|
|
||||||
|
} else {
|
||||||
const response = await generateResponse(conversation);
|
// Append user message to conversation history
|
||||||
|
conversation.messages.push({
|
||||||
// Append bot message to conversation history
|
role: 'user',
|
||||||
conversation.messages.push({ role: 'assistant', content: response });
|
content: message.cleanContent
|
||||||
|
});
|
||||||
if (response && response.trim()) {
|
}
|
||||||
// Send response to user if it's not empty
|
if (message.content === '!reset' || message.content === '!r') {
|
||||||
await message.channel.send(response);
|
conversations.delete(userID); // Delete user's conversation map if they request reset
|
||||||
conversation.busy = false;
|
await message.channel.send('Conversation reset.');
|
||||||
} else {
|
return;
|
||||||
// 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.
|
// Append user message to conversation history
|
||||||
await message.channel.send(randomResponse); // give the user a human like reponse about the error
|
conversation.messages.push({
|
||||||
conversations.delete(userID); // Delete user's conversation map if they request reset
|
role: 'user',
|
||||||
const resetMessage = resetResponses[Math.floor(Math.random() * resetResponses.length)];
|
content: message.cleanContent
|
||||||
await message.channel.send(resetMessage); // give a notification of reset using a human like response.
|
});
|
||||||
conversation.busy = false;
|
|
||||||
|
try {
|
||||||
|
conversation.busy = true;
|
||||||
|
|
||||||
|
const response = await generateResponse(conversation);
|
||||||
|
|
||||||
|
// Append bot message to conversation history
|
||||||
|
conversation.messages.push({
|
||||||
|
role: 'assistant',
|
||||||
|
content: response
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response && response.trim()) {
|
||||||
|
// Send response to user if it's not empty
|
||||||
|
await message.channel.send(response);
|
||||||
|
conversation.busy = 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
|
||||||
|
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.
|
||||||
|
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 {
|
||||||
|
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 {
|
|
||||||
conversation.busy = false;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
async function generateResponse(conversation) {
|
async function generateResponse(conversation) {
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
const timeout = setTimeout(() => {
|
const timeout = setTimeout(() => {
|
||||||
controller.abort();
|
controller.abort();
|
||||||
}, 900000);
|
}, 900000);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch('http://192.168.0.15/v1/chat/completions', {
|
const response = await fetch('http://192.168.0.15/v1/chat/completions', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'accept': 'application/json',
|
'accept': 'application/json',
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ messages: conversation.messages }),
|
body: JSON.stringify({
|
||||||
signal: controller.signal
|
messages: conversation.messages
|
||||||
});
|
}),
|
||||||
|
signal: controller.signal
|
||||||
|
});
|
||||||
|
|
||||||
const responseData = await response.json();
|
const responseData = await response.json();
|
||||||
const choice = responseData.choices[0];
|
const choice = responseData.choices[0];
|
||||||
|
|
||||||
// Remove "user None:" and any text after it from the response
|
// Remove "user None:" and any text after it from the response
|
||||||
const responseText = choice.message.content.trim();
|
const responseText = choice.message.content.trim();
|
||||||
const startIndex = responseText.indexOf('user None:');
|
const startIndex = responseText.indexOf('user None:');
|
||||||
const sanitizedResponse = startIndex === -1 ? responseText : responseText.substring(0, startIndex);
|
const sanitizedResponse = startIndex === -1 ? responseText : responseText.substring(0, startIndex);
|
||||||
|
|
||||||
return sanitizedResponse;
|
return sanitizedResponse;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
return 'Oops, something went wrong!';
|
return 'Oops, something went wrong!';
|
||||||
} finally {
|
} finally {
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
client.login(process.env.THE_TOKEN); // Replace with your bot token
|
client.login(process.env.THE_TOKEN); // Replace with your bot token
|
||||||
|
|
Loading…
Reference in New Issue
Block a user