109 lines
3.2 KiB
JavaScript
109 lines
3.2 KiB
JavaScript
import { DebugBuilder } from "../../modules/debugger.mjs";
|
|
const log = new DebugBuilder("server", "discordBot.addons.linkCop");
|
|
import { gptHandler } from "../modules/gptHandler.mjs";
|
|
import dotenv from "dotenv";
|
|
import {
|
|
getGuildConfig,
|
|
setGuildConfig,
|
|
} from "../../modules/mongo-wrappers/mongoConfigWrappers.mjs";
|
|
dotenv.config();
|
|
|
|
const linkRegExp = /http[s]?:\/\/\S+/g;
|
|
|
|
export const linkCop = async (nodeIo, message) => {
|
|
// Set the channel IDs based on the guild the message was sent in
|
|
const approvedLinksChannel =
|
|
(await getGuildConfig(message.guild.id, "approvedLinksChannel")) ||
|
|
"767303243285790721";
|
|
const restrictedChannelIds = await getGuildConfig(
|
|
message.guild.id,
|
|
"restrictedChannelIds",
|
|
);
|
|
|
|
// Check if the message was sent in an restricted channel
|
|
if (
|
|
message.channel.id == approvedLinksChannel ||
|
|
!Array.isArray(restrictedChannelIds) ||
|
|
Array.isArray(restrictedChannelIds) ||
|
|
!restrictedChannelIds.includes(message.channel.id)
|
|
) {
|
|
return false;
|
|
}
|
|
|
|
// Check if there are URLs in the sent message
|
|
const urls = [...message.content.matchAll(linkRegExp)];
|
|
log.DEBUG("Parsed URLs from message:", urls);
|
|
|
|
if (!urls || urls.length === 0) return false;
|
|
log.INFO("Found URLs: ", urls);
|
|
|
|
let conversation = [];
|
|
|
|
let prevMessages = await message.channel.messages.fetch({ limit: 2 });
|
|
prevMessages.reverse();
|
|
|
|
prevMessages.forEach((msg) => {
|
|
// Check if the message was sent within the last 5 minutes
|
|
if (new Date().getTime() - msg.createdTimestamp > 5 * 60 * 1000) {
|
|
return;
|
|
}
|
|
|
|
// Check if it's from a bot other than the server
|
|
if (msg.author.bot && msg.author.id !== nodeIo.serverClient.user.id) return;
|
|
|
|
const username = msg.author.username
|
|
.replace(/\s+/g, "_")
|
|
.replace(/[^\w\s]/gi, "");
|
|
|
|
if (msg.author.id === nodeIo.serverClient.user.id) {
|
|
conversation.push({
|
|
role: "assistant",
|
|
//name: msg.author.id,
|
|
content: msg.content,
|
|
});
|
|
|
|
return;
|
|
}
|
|
|
|
conversation.push({
|
|
role: "user",
|
|
//name: msg.author.id,
|
|
content: msg.content.replace(`<@${nodeIo.serverClient.user.id}>`, ""),
|
|
});
|
|
});
|
|
|
|
conversation.push({
|
|
role: "assistant",
|
|
content: `There has been a link posted to a channel that links are not allowed in. The above messages are from the channel that links are not allowed including the message with the link. The message with the link is going to be deleted and moved to the '#links' channels. You are replying to the message with the link to let the user know.`,
|
|
});
|
|
|
|
const response = await gptHandler(conversation);
|
|
|
|
if (response) {
|
|
const responseMessage = response;
|
|
const chunkSize = 2000;
|
|
|
|
for (let i = 0; i < responseMessage.length; i += chunkSize) {
|
|
const chunk = responseMessage.substring(i, i + chunkSize);
|
|
|
|
log.DEBUG("Sending message chunk:", chunk);
|
|
|
|
await message.reply(chunk);
|
|
}
|
|
|
|
const messageContent = {
|
|
author: message.author,
|
|
content: `<@${message.author.id}> - ${String(message.content)}`,
|
|
channelId: message.channelId,
|
|
links: urls,
|
|
};
|
|
|
|
await message.delete();
|
|
log.DEBUG("Message content: ", messageContent);
|
|
|
|
message.client.channels.cache
|
|
.get(approvedLinksChannel)
|
|
.send(messageContent);
|
|
}
|
|
};
|