sugoma-local-ai/index.js

58 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2024-08-28 14:30:59 +00:00
//const { ollama } = require('ollama');
import ollama from 'ollama'
import error from 'console';
import { Client, Message, MessageAttachment } from 'discord.js-selfbot-v13';
import axios from 'axios';
import config from './config.json' with { type: "json" };
2024-08-28 12:07:32 +00:00
const client = new Client();
2024-08-28 14:30:59 +00:00
function newAIMessage(text) {
return { role: 'user', content: text }
}
function requestAIMessage(prevmessages) {
return ollama.chat({ model: config.modelname, messages: prevmessages, stream: false})
}
2024-08-28 12:07:32 +00:00
client.on('ready', async () => {
console.log(`${client.user.username} is ready!`);
})
let savedMessage;
2024-08-28 14:30:59 +00:00
let history = [{ role: 'system', content: config.systemd }];
2024-08-28 12:07:32 +00:00
let count = 0;
function isAllowedUser(id) {
2024-08-28 12:20:46 +00:00
if (config.ALLOWED_IDS.find(string => string == id)) return true;
2024-08-28 12:07:32 +00:00
return false;
}
async function getMessageContent(message) {
2024-08-28 14:30:59 +00:00
if (!message.reference) return message.author.displayName + ": " + message.content;
else {
let reply = message.channel.messages.fetch(message.reference.messageId);
return message.author.displayName + ": " + message.content + " `Ответ на: " + reply.author.displayName + ": " + reply.content + "`";
}
2024-08-28 12:07:32 +00:00
}
client.on("messageCreate", async function(message){
try {
2024-08-28 14:30:59 +00:00
history.push(newAIMessage(await getMessageContent(message)));
2024-08-28 12:26:01 +00:00
if (message.author.id == config.admin && message.content.startsWith("e ")) {
2024-08-28 12:07:32 +00:00
eval(message.content.substring(1));
}
2024-08-28 14:30:59 +00:00
else if ((message.author.id != client.user.id && (message.mentions.users.get(client.user.id) || isAllowedUser(message.author.id) || (!isAllowedUser && count % 5 == 0) || (message.reference && message.channel.messages.cache.get(message.reference.messageId).author.id == client.user.id)))) {
2024-08-28 12:07:32 +00:00
savedMessage = message;
savedMessage.channel.sendTyping();
2024-08-28 14:30:59 +00:00
let aimessage = (await requestAIMessage(history)).message.content
history.push({role: 'assistant', content: aimessage});
savedMessage.channel.send("<@" + savedMessage.author.id + "> " + aimessage);
2024-08-28 12:07:32 +00:00
}
if (!isAllowedUser(message.author.id)) count++;
} catch(error) {
console.error(error);
}
});
2024-08-28 14:30:59 +00:00
client.login(config.token);