sugoma-ai/index.js
2024-09-02 14:41:41 +03:00

102 lines
3.9 KiB
JavaScript

const { error } = require("console");
const { Client, Message, MessageAttachment } = require("discord.js-selfbot-v13");
const axios = require("axios");
const config = require("./config.json");
const client = new Client();
client.on("ready", async () => {
console.log(`${client.user.username} is ready!`);
})
let savedMessage;
let count = 0;
const shapeGuild = config.shapeGuild;
const ALLOWED_CHANNEL_TYPES = ["DM", "GROUP_DM"];
function isAllowedUser(id) {
const ALLOWED_IDS = config.ALLOWED_IDS;
if (ALLOWED_IDS.find(string => string == id) || !ALLOWED_IDS.length) return true;
return false;
}
function isDisallowedUser(id) {
const DISALLOWED_IDS = config.DISALLOWED_IDS;
if (DISALLOWED_IDS.find(string => string == id) || !DISALLOWED_IDS.length) return true;
return false;
}
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: filterPrompt(`${message.author.displayName}: ${message.content}`), files: [file] };
else return {files: [file]};
} catch (error) {
console.error("Ошибка при получении файла:", error.message);
return "Я нихуя не понял.";
}
} else {
if (!message.reference) return filterPrompt(`${message.author.displayName}: ${message.content}`);
else {
let reply = message.channel.messages.fetch(message.reference.messageId);
return filterPrompt(`${message.author.displayName}: ${message.content} \`Ответ на: ${reply.author.displayName}: ${reply.content}\``);
}
}
}
function filterPrompt(string) {
const clientUser = client.user;
return string.replace(clientUser.id, config.shapeUserId).replace(clientUser.displayName, config.shapeName);
}
async function sendToAI(message) {
if (message.author.id == client.user.id && isDisallowedUser(message.author.id)) return;
if (message.author.id == config.admin && message.content.startsWith("e ")) {
eval(message.content.substring(1));
return;
}
if (message.content.length >= 1800) return;
savedMessage = message;
client.guilds.cache.get(shapeGuild).channels.cache.get(config.shapeChannel).send(await getMessageContent(message));
}
async function processGuildTextMessage(message) {
if (message.guild.id != shapeGuild &&
(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)))
sendToAI(message);
else if (message.guild.id == shapeGuild && message.author.bot && savedMessage) {
Promise.all([savedMessage.channel.sendTyping(), savedMessage.channel.send("<@" + savedMessage.author.id + "> " + message.content)]).catch(error => {});
}
if (!isAllowedUser(message.author.id)) count++;
}
client.on("messageCreate", function(message){
try {
if (message.channel.type == "GUILD_TEXT") processGuildTextMessage(message);
else if (ALLOWED_CHANNEL_TYPES.find(type => type == message.channel.type)) sendToAI(message);
} catch(error) {
console.error(error);
}
});
client.login(config.token);