Linting
All checks were successful
release-tag / release-image (push) Successful in 1m52s
Lint JavaScript/Node.js / lint-js (push) Successful in 11s
DRB Tests / drb_mocha_tests (push) Successful in 29s

This commit is contained in:
Logan Cusano
2024-08-11 15:57:46 -04:00
parent 5cd47378d6
commit 117cbea67f
37 changed files with 2273 additions and 1738 deletions

View File

@@ -1,83 +1,93 @@
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 dotenv from "dotenv";
dotenv.config();
const approvedLinksChannel = "767303243285790721";
const restrictedChannelIds = process.env.LINKCOP_RESTRICTED_CHANNEL_IDS.split(',');
const restrictedChannelIds =
process.env.LINKCOP_RESTRICTED_CHANNEL_IDS.split(",");
const linkRegExp = /(?:http[s]?:\/\/)?(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)/g
const linkRegExp =
/(?:http[s]?:\/\/)?(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)/g;
export const linkCop = async (nodeIo, message) => {
if (message.channel.id == approvedLinksChannel || !restrictedChannelIds.includes(message.channel.id)) return false;
if (
message.channel.id == approvedLinksChannel ||
!restrictedChannelIds.includes(message.channel.id)
)
return false;
const urls = String(message.content).matchAll(linkRegExp);
if (!urls || urls.length === 0) return false;
log.DEBUG("Found URLs: ", urls);
const urls = String(message.content).matchAll(linkRegExp);
if (!urls || urls.length === 0) return false;
log.DEBUG("Found URLs: ", urls);
let conversation = [];
let conversation = [];
let prevMessages = await message.channel.messages.fetch({ limit: 2 });
prevMessages.reverse();
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;
}
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;
// 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, '');
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,
});
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}>`, ''),
});
});
return;
}
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.`
role: "user",
//name: msg.author.id,
content: msg.content.replace(`<@${nodeIo.serverClient.user.id}>`, ""),
});
});
const response = await gptHandler(conversation);
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.`,
});
if (response) {
const responseMessage = response;
const chunkSize = 2000;
const response = await gptHandler(conversation);
for (let i = 0; i < responseMessage.length; i += chunkSize) {
const chunk = responseMessage.substring(i, i + chunkSize);
if (response) {
const responseMessage = response;
const chunkSize = 2000;
log.DEBUG("Sending message chunk:", chunk);
for (let i = 0; i < responseMessage.length; i += chunkSize) {
const chunk = responseMessage.substring(i, i + chunkSize);
await message.reply(chunk);
}
log.DEBUG("Sending message chunk:", 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);
}
}
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);
}
};