From ce0360133be63fa0bf35a20f79355160df24289a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?b=CA=B0edoh=E2=82=82=20sw=C3=A9?= Date: Wed, 28 Aug 2024 19:30:59 +0500 Subject: [PATCH] Initial sugoma-local-ai commit --- .gitignore | 2 ++ config.example.json | 8 +++--- index.js | 69 +++++++++++++++++---------------------------- package-lock.json | 18 +++++++++++- package.json | 4 ++- 5 files changed, 52 insertions(+), 49 deletions(-) diff --git a/.gitignore b/.gitignore index ceaea36..c7ee837 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +config.json + # ---> Node # Logs logs diff --git a/config.example.json b/config.example.json index 7ba9b36..95bc3fb 100644 --- a/config.example.json +++ b/config.example.json @@ -1,7 +1,7 @@ { "token": "Token", "ALLOWED_IDS": ["1266775948548903026", "1151129960141037659", "1275008490959667234", "1234866477086019648", "1277611369578889277", "1242022836529266708", "1275098630059200586", "1277311957405405296", "1277936618749300760", "1275899048171409528"], - "admin": "593524756393754635", - "shapeGuild": "1277635303363973173", - "shapeChannel": "1277652579547086860" -} \ No newline at end of file + "admin": "798192964911235073", + "modelname": "phi3:medium", + "systemd": "" +} diff --git a/index.js b/index.js index 6522cc0..07b77aa 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,25 @@ -const { error } = require('console'); -const { Client, Message, MessageAttachment } = require('discord.js-selfbot-v13'); -const axios = require('axios'); -const config = require('./config.json'); +//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" }; const client = new Client(); +function newAIMessage(text) { + return { role: 'user', content: text } +} + +function requestAIMessage(prevmessages) { + return ollama.chat({ model: config.modelname, messages: prevmessages, stream: false}) +} + client.on('ready', async () => { console.log(`${client.user.username} is ready!`); }) let savedMessage; +let history = [{ role: 'system', content: config.systemd }]; let count = 0; function isAllowedUser(id) { @@ -17,53 +28,25 @@ function isAllowedUser(id) { } async function getMessageContent(message) { - if (message.attachments.size > 0) { - try { - const attachment = message.attachments.first(); - - if (!attachment.url) { - console.error('URL вложения не найден.'); - return "Я нихуя не понял."; - } - - const response = await axios.get(attachment.url, { responseType: 'arraybuffer' }); - - if (!response.data) { - console.error('Ошибка: response.data is undefined.'); - return "Я нихуя не понял."; - } - - const buffer = Buffer.from(response.data, 'binary'); - const file = new MessageAttachment(buffer, attachment.name); - - if (message.content) return { content: `${message.author.displayName}: ${message.content} `, files: [file] }; - else return {files: [file]}; - } catch (error) { - console.error('Ошибка при получении файла:', error.message); - return message.author.displayName + ": " + message.content + " "; - } - } else { - 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 + "`"; - } - } + 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 + "`"; + } } client.on("messageCreate", async function(message){ try { - let shapeGuild = config.shapeGuild + history.push(newAIMessage(await getMessageContent(message))); if (message.author.id == config.admin && message.content.startsWith("e ")) { eval(message.content.substring(1)); } - else if (message.guild.id != shapeGuild && (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)))) { + 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)))) { savedMessage = message; - client.guilds.cache.get(shapeGuild).channels.cache.get(config.shapeChannel).send(await getMessageContent(message)); - } - else if (message.guild.id == shapeGuild && message.author.bot && savedMessage) { savedMessage.channel.sendTyping(); - savedMessage.channel.send("<@" + savedMessage.author.id + "> " + message.content); + let aimessage = (await requestAIMessage(history)).message.content + history.push({role: 'assistant', content: aimessage}); + savedMessage.channel.send("<@" + savedMessage.author.id + "> " + aimessage); } if (!isAllowedUser(message.author.id)) count++; } catch(error) { @@ -71,4 +54,4 @@ client.on("messageCreate", async function(message){ } }); -client.login(config.token); \ No newline at end of file +client.login(config.token); diff --git a/package-lock.json b/package-lock.json index c66b241..df19ab7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,8 @@ "license": "ISC", "dependencies": { "axios": "^1.7.5", - "discord.js-selfbot-v13": "^3.3.0" + "discord.js-selfbot-v13": "^3.3.0", + "ollama": "^0.5.8" } }, "node_modules/@discordjs/builders": { @@ -428,6 +429,15 @@ } } }, + "node_modules/ollama": { + "version": "0.5.8", + "resolved": "https://registry.npmjs.org/ollama/-/ollama-0.5.8.tgz", + "integrity": "sha512-frBGdfSV34i7JybLZUeyCYDx0CMyDiG4On8xOK+cNRWM04HImhoWgIMpF4p7vTkQumadbSxOteR7SZyKqNmOXg==", + "license": "MIT", + "dependencies": { + "whatwg-fetch": "^3.6.20" + } + }, "node_modules/p-limit": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz", @@ -663,6 +673,12 @@ "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" }, + "node_modules/whatwg-fetch": { + "version": "3.6.20", + "resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.6.20.tgz", + "integrity": "sha512-EqhiFU6daOA8kpjOWTL0olhVOF3i7OrFzSYiGsEMB8GcXS+RrzauAERX65xMeNWVqxA6HXH2m69Z9LaKKdisfg==", + "license": "MIT" + }, "node_modules/whatwg-url": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", diff --git a/package.json b/package.json index 88b9b45..7a76d87 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "name": "prankbot", + "type": "module", "version": "1.0.0", "description": "", "main": "index.js", @@ -9,7 +10,8 @@ "author": "", "license": "ISC", "dependencies": { + "axios": "^1.7.5", "discord.js-selfbot-v13": "^3.3.0", - "axios": "^1.7.5" + "ollama": "^0.5.8" } }