sugoma-ai/index.js

95 lines
3.7 KiB
JavaScript
Raw Normal View History

2024-08-28 14:44:07 +00:00
const { error } = require("console");
const { Client, Message, MessageAttachment } = require("discord.js-selfbot-v13");
const axios = require("axios");
const config = require("./config.json");
2024-08-28 12:07:32 +00:00
const client = new Client();
2024-08-28 14:44:07 +00:00
client.on("ready", async () => {
2024-08-28 12:07:32 +00:00
console.log(`${client.user.username} is ready!`);
})
let savedMessage;
let count = 0;
2024-08-30 14:39:18 +00:00
const shapeGuild = config.shapeGuild;
const ALLOWED_CHANNEL_TYPES = ["DM", "GROUP_DM"];
2024-08-28 12:07:32 +00:00
function isAllowedUser(id) {
const ALLOWED_IDS = config.ALLOWED_IDS;
if (ALLOWED_IDS.find(string => string == id) || !ALLOWED_IDS.length) return true;
2024-08-28 12:07:32 +00:00
return false;
}
async function getMessageContent(message) {
if (message.attachments.size > 0) {
try {
const attachment = message.attachments.first();
if (!attachment.url) {
2024-08-28 14:44:07 +00:00
console.error("URL вложения не найден.");
2024-08-28 12:20:46 +00:00
return "Я нихуя не понял.";
2024-08-28 12:07:32 +00:00
}
2024-08-28 14:44:07 +00:00
const response = await axios.get(attachment.url, { responseType: "arraybuffer" });
2024-08-28 12:07:32 +00:00
if (!response.data) {
2024-08-28 14:44:07 +00:00
console.error("Ошибка: response.data is undefined.");
2024-08-28 12:20:46 +00:00
return "Я нихуя не понял.";
2024-08-28 12:07:32 +00:00
}
2024-08-28 14:44:07 +00:00
const buffer = Buffer.from(response.data, "binary");
2024-08-28 12:07:32 +00:00
const file = new MessageAttachment(buffer, attachment.name);
2024-08-31 14:50:27 +00:00
if (message.content) return { content: filterPrompt(`${message.author.displayName}: ${message.content}`), files: [file] };
2024-08-28 12:07:32 +00:00
else return {files: [file]};
} catch (error) {
2024-08-28 14:44:07 +00:00
console.error("Ошибка при получении файла:", error.message);
2024-08-28 14:47:59 +00:00
return "Я нихуя не понял.";
2024-08-28 12:07:32 +00:00
}
} else {
2024-08-31 14:50:27 +00:00
if (!message.reference) return filterPrompt(`${message.author.displayName}: ${message.content}`);
2024-08-28 12:07:32 +00:00
else {
let reply = message.channel.messages.fetch(message.reference.messageId);
2024-08-31 14:50:27 +00:00
return filterPrompt(`${message.author.displayName}: ${message.content} \`Ответ на: ${reply.author.displayName}: ${reply.content}\``);
2024-08-28 12:07:32 +00:00
}
}
}
2024-08-31 14:50:27 +00:00
function filterPrompt(string) {
2024-08-31 14:54:44 +00:00
const clientUser = client.user;
const shapeUser = client.guilds.cache.get(config.shapeGuild).members.cache.get(config.shapeUserId).user;
return string.replace(clientUser.id, config.shapeUserId).replace(shapeUser.displayName, clientUser.displayName);
2024-08-31 14:50:27 +00:00
}
2024-08-30 14:39:18 +00:00
async function sendToAI(message) {
if (message.author.id == client.user.id) return;
2024-08-30 14:57:26 +00:00
if (message.author.id == config.admin && message.content.startsWith("e ")) {
eval(message.content.substring(1));
return;
}
2024-08-30 14:39:18 +00:00
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 &&
2024-08-30 14:39:18 +00:00
(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) {
2024-08-31 13:50:19 +00:00
Promise.all([savedMessage.channel.sendTyping(), savedMessage.channel.send("<@" + savedMessage.author.id + "> " + message.content)]).catch(error => {});
2024-08-30 14:39:18 +00:00
}
if (!isAllowedUser(message.author.id)) count++;
}
client.on("messageCreate", function(message){
2024-08-28 12:07:32 +00:00
try {
2024-08-30 14:39:18 +00:00
if (message.channel.type == "GUILD_TEXT") processGuildTextMessage(message);
else if (ALLOWED_CHANNEL_TYPES.find(type => type == message.channel.type)) sendToAI(message);
2024-08-28 12:07:32 +00:00
} catch(error) {
console.error(error);
}
});
2024-08-28 12:20:46 +00:00
client.login(config.token);