This commit is contained in:
Logan Cusano
2024-08-11 20:14:36 -04:00
parent 94374b4d45
commit cf49ac414a
6 changed files with 187 additions and 97 deletions

View File

@@ -2,20 +2,30 @@ import { DebugBuilder } from "../../modules/debugger.mjs";
const log = new DebugBuilder("server", "discordBot.addons.linkCop"); const log = new DebugBuilder("server", "discordBot.addons.linkCop");
import { gptHandler } from "../modules/gptHandler.mjs"; import { gptHandler } from "../modules/gptHandler.mjs";
import dotenv from "dotenv"; import dotenv from "dotenv";
import { getGuildConfig, setGuildConfig } from "../../modules/mongo-wrappers/mongoConfigWrappers.mjs"; import {
getGuildConfig,
setGuildConfig,
} from "../../modules/mongo-wrappers/mongoConfigWrappers.mjs";
dotenv.config(); dotenv.config();
const linkRegExp = /http[s]?:\/\/\S+/g; const linkRegExp = /http[s]?:\/\/\S+/g;
export const linkCop = async (nodeIo, message) => { export const linkCop = async (nodeIo, message) => {
// Set the channel IDs based on the guild the message was sent in // Set the channel IDs based on the guild the message was sent in
const approvedLinksChannel = await getGuildConfig(message.guild.id, "approvedLinksChannel") || "767303243285790721"; const approvedLinksChannel =
const restrictedChannelIds = await getGuildConfig(message.guild.id, "restrictedChannelIds"); (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 // Check if the message was sent in an restricted channel
if ( if (
message.channel.id == approvedLinksChannel || !Array.isArray(restrictedChannelIds) || message.channel.id == approvedLinksChannel ||
(Array.isArray(restrictedChannelIds) || !restrictedChannelIds.includes(message.channel.id)) !Array.isArray(restrictedChannelIds) ||
Array.isArray(restrictedChannelIds) ||
!restrictedChannelIds.includes(message.channel.id)
) { ) {
return false; return false;
} }

View File

@@ -4,12 +4,20 @@ import dotenv from "dotenv";
dotenv.config(); dotenv.config();
import { Events } from "discord.js"; import { Events } from "discord.js";
import { gptHandler } from "../modules/gptHandler.mjs"; import { gptHandler } from "../modules/gptHandler.mjs";
import { getGuildConfig, setGuildConfig, getConfig, setConfig } from "../../modules/mongo-wrappers/mongoConfigWrappers.mjs"; import {
getGuildConfig,
setGuildConfig,
getConfig,
setConfig,
} from "../../modules/mongo-wrappers/mongoConfigWrappers.mjs";
export const name = Events.GuildMemberAdd; export const name = Events.GuildMemberAdd;
export async function execute(nodeIo, member) { export async function execute(nodeIo, member) {
const welcomeChannel = await getGuildConfig(message.guild.id, "welcomeChannelId"); const welcomeChannel = await getGuildConfig(
message.guild.id,
"welcomeChannelId",
);
log.INFO("New user joined the server", member); log.INFO("New user joined the server", member);
let conversation = []; let conversation = [];
conversation.push({ conversation.push({

View File

@@ -6,19 +6,33 @@ import { Events } from "discord.js";
import { gptInteraction } from "../addons/gptInteraction.mjs"; import { gptInteraction } from "../addons/gptInteraction.mjs";
import { linkCop } from "../addons/linkCop.mjs"; import { linkCop } from "../addons/linkCop.mjs";
import PresenceManager from "../modules/presenceManager.mjs"; import PresenceManager from "../modules/presenceManager.mjs";
import { getGuildConfig, setGuildConfig } from "../../modules/mongo-wrappers/mongoConfigWrappers.mjs"; import {
getGuildConfig,
setGuildConfig,
} from "../../modules/mongo-wrappers/mongoConfigWrappers.mjs";
export const name = Events.MessageCreate; export const name = Events.MessageCreate;
export async function execute(nodeIo, message) { export async function execute(nodeIo, message) {
// Get the ignored channels from the server config // Get the ignored channels from the server config
const IGNORED_CHANNELS = await getGuildConfig(message.guild.id, "ignoredChannels"); const IGNORED_CHANNELS = await getGuildConfig(
message.guild.id,
"ignoredChannels",
);
// Ignore ignored channels // Ignore ignored channels
if (!Array.isArray(IGNORED_CHANNELS) || Array.isArray(IGNORED_CHANNELS) && IGNORED_CHANNELS.includes(message.channel.id)) { return; } if (
!Array.isArray(IGNORED_CHANNELS) ||
(Array.isArray(IGNORED_CHANNELS) &&
IGNORED_CHANNELS.includes(message.channel.id))
) {
return;
}
// Ignore messages from a bot // Ignore messages from a bot
if (message.author.bot) { return; } if (message.author.bot) {
return;
}
log.INFO("Message create", message); log.INFO("Message create", message);

View File

@@ -1,5 +1,8 @@
import { getConfig, setConfig } from "../../modules/mongo-wrappers/mongoConfigWrappers.mjs"; import {
import { ActivityType, PresenceUpdateStatus } from 'discord.js'; getConfig,
setConfig,
} from "../../modules/mongo-wrappers/mongoConfigWrappers.mjs";
import { ActivityType, PresenceUpdateStatus } from "discord.js";
class PresenceManager { class PresenceManager {
/** /**
@@ -41,20 +44,26 @@ class PresenceManager {
if (!defaultPresence) { if (!defaultPresence) {
defaultPresence = { defaultPresence = {
status: 'idle', status: "idle",
activities: [{ activities: [
name: 'your commands', {
type: 'LISTENING' name: "your commands",
}] type: "LISTENING",
},
],
}; };
await setConfig('presence', defaultPresence); await setConfig("presence", defaultPresence);
} }
console.log("Default Presence:", defaultPresence); console.log("Default Presence:", defaultPresence);
// Update your bot's presence using this configuration // Update your bot's presence using this configuration
await this.setPresence(defaultPresence.status, defaultPresence.activities[0].type, defaultPresence.activities[0].name); await this.setPresence(
defaultPresence.status,
defaultPresence.activities[0].type,
defaultPresence.activities[0].name,
);
} }
/** /**
@@ -64,18 +73,18 @@ class PresenceManager {
*/ */
convertActivityType(activityType) { convertActivityType(activityType) {
switch (activityType.toUpperCase()) { switch (activityType.toUpperCase()) {
case 'PLAYING': case "PLAYING":
return ActivityType.Playing; return ActivityType.Playing;
case 'STREAMING': case "STREAMING":
return ActivityType.Streaming; return ActivityType.Streaming;
case 'LISTENING': case "LISTENING":
return ActivityType.Listening; return ActivityType.Listening;
case 'WATCHING': case "WATCHING":
return ActivityType.Watching; return ActivityType.Watching;
case 'COMPETING': case "COMPETING":
return ActivityType.Competing; return ActivityType.Competing;
default: default:
throw new Error('Invalid activity type'); throw new Error("Invalid activity type");
} }
} }
@@ -86,19 +95,18 @@ class PresenceManager {
*/ */
convertStatus(status) { convertStatus(status) {
switch (status.toLowerCase()) { switch (status.toLowerCase()) {
case 'online': case "online":
return PresenceUpdateStatus.Online; return PresenceUpdateStatus.Online;
case 'idle': case "idle":
return PresenceUpdateStatus.Idle; return PresenceUpdateStatus.Idle;
case 'dnd': case "dnd":
return PresenceUpdateStatus.DoNotDisturb; return PresenceUpdateStatus.DoNotDisturb;
case 'invisible': case "invisible":
return PresenceUpdateStatus.Invisible; return PresenceUpdateStatus.Invisible;
default: default:
throw new Error('Invalid status'); throw new Error("Invalid status");
} }
} }
} }
export default PresenceManager; export default PresenceManager;

View File

@@ -1,82 +1,113 @@
import { getDocumentByField, deleteDocumentByField, getDocumentByFields, upsertDocumentByField, deleteDocumentByFields, upsertDocumentByFields } from './mongoHandler.mjs'; // Import your MongoDB handlers import {
import { DebugBuilder } from '../debugger.mjs'; getDocumentByField,
deleteDocumentByField,
getDocumentByFields,
upsertDocumentByField,
deleteDocumentByFields,
upsertDocumentByFields,
} from "./mongoHandler.mjs"; // Import your MongoDB handlers
import { DebugBuilder } from "../debugger.mjs";
const log = new DebugBuilder("server", 'mongoConfigWrappers'); const log = new DebugBuilder("server", "mongoConfigWrappers");
const collectionName = 'configurations'; const collectionName = "configurations";
// Function to get a configuration by key // Function to get a configuration by key
export const getConfig = async (key) => { export const getConfig = async (key) => {
try { try {
const config = await getDocumentByField(collectionName, 'key', key); const config = await getDocumentByField(collectionName, "key", key);
log.DEBUG(`Configuration for key "${key}" retrieved:`, config); log.DEBUG(`Configuration for key "${key}" retrieved:`, config);
return config ? config[key] : null; // Return null if no configuration is found return config ? config[key] : null; // Return null if no configuration is found
} catch (error) { } catch (error) {
log.ERROR('Error retrieving configuration:', error); log.ERROR("Error retrieving configuration:", error);
throw error; throw error;
} }
}; };
// Function to set a configuration by key // Function to set a configuration by key
export const setConfig = async (key, value) => { export const setConfig = async (key, value) => {
// Set the config object // Set the config object
value = {key : value}; value = { key: value };
try { try {
const result = await upsertDocumentByField(collectionName, 'key', key, value); const result = await upsertDocumentByField(
log.DEBUG(`Configuration for key "${key}" set:`, value, result); collectionName,
return result > 0 ? key : null; // Return key if updated successfully, otherwise null "key",
} catch (error) { key,
log.ERROR('Error setting configuration:', error); value,
throw error; );
} log.DEBUG(`Configuration for key "${key}" set:`, value, result);
return result > 0 ? key : null; // Return key if updated successfully, otherwise null
} catch (error) {
log.ERROR("Error setting configuration:", error);
throw error;
}
}; };
// Function to delete a configuration by key (optional) // Function to delete a configuration by key (optional)
export const deleteConfig = async (key) => { export const deleteConfig = async (key) => {
try { try {
const result = await deleteDocumentByField(collectionName, 'key', key); const result = await deleteDocumentByField(collectionName, "key", key);
log.DEBUG(`Configuration for key "${key}" deleted:`, result); log.DEBUG(`Configuration for key "${key}" deleted:`, result);
return result; // Return the count of deleted documents return result; // Return the count of deleted documents
} catch (error) { } catch (error) {
log.ERROR('Error deleting configuration:', error); log.ERROR("Error deleting configuration:", error);
throw error; throw error;
} }
}; };
// Function to get a configuration by key for a specific guild // Function to get a configuration by key for a specific guild
export const getGuildConfig = async (guildId, key) => { export const getGuildConfig = async (guildId, key) => {
try { try {
const config = await getDocumentByFields(collectionName, ['guildId', guildId], ['key', key]); const config = await getDocumentByFields(
log.DEBUG(`Guild ${guildId} configuration for key "${key}" retrieved:`, config); collectionName,
return config ? config[key] : null; // Return null if no configuration is found ["guildId", guildId],
} catch (error) { ["key", key],
log.ERROR('Error retrieving guild configuration:', error); );
throw error; log.DEBUG(
} `Guild ${guildId} configuration for key "${key}" retrieved:`,
config,
);
return config ? config[key] : null; // Return null if no configuration is found
} catch (error) {
log.ERROR("Error retrieving guild configuration:", error);
throw error;
}
}; };
// Function to set a configuration by key for a specific guild // Function to set a configuration by key for a specific guild
export const setGuildConfig = async (guildId, key, value) => { export const setGuildConfig = async (guildId, key, value) => {
// Set the config object // Set the config object
value = {key : value}; value = { key: value };
try { try {
const result = await upsertDocumentByFields(collectionName, value, ['guildId', guildId], ['key', key]); const result = await upsertDocumentByFields(
log.DEBUG(`Guild ${guildId} configuration for key "${key}" set:`, value); collectionName,
return result > 0 ? key : null; // Return key if updated successfully, otherwise null value,
} catch (error) { ["guildId", guildId],
log.ERROR('Error setting guild configuration:', error); ["key", key],
throw error; );
} log.DEBUG(`Guild ${guildId} configuration for key "${key}" set:`, value);
return result > 0 ? key : null; // Return key if updated successfully, otherwise null
} catch (error) {
log.ERROR("Error setting guild configuration:", error);
throw error;
}
}; };
// Function to delete a configuration by key for a specific guild (optional) // Function to delete a configuration by key for a specific guild (optional)
export const deleteGuildConfig = async (guildId, key) => { export const deleteGuildConfig = async (guildId, key) => {
try { try {
const result = await deleteDocumentByFields(collectionName, ['guildId', guildId], ['key', key]); const result = await deleteDocumentByFields(
log.DEBUG(`Guild ${guildId} configuration for key "${key}" deleted:`, result); collectionName,
return result; // Return the count of deleted documents ["guildId", guildId],
} catch (error) { ["key", key],
log.ERROR('Error deleting guild configuration:', error); );
throw error; log.DEBUG(
} `Guild ${guildId} configuration for key "${key}" deleted:`,
result,
);
return result; // Return the count of deleted documents
} catch (error) {
log.ERROR("Error deleting guild configuration:", error);
throw error;
}
}; };

View File

@@ -63,7 +63,10 @@ export const getDocumentByField = async (collectionName, field, value) => {
}; };
// Function to retrieve a document by multiple fields // Function to retrieve a document by multiple fields
export const getDocumentByFields = async (collectionName, ...fieldValuePairs) => { export const getDocumentByFields = async (
collectionName,
...fieldValuePairs
) => {
log.DEBUG("Getting document by fields:", collectionName, fieldValuePairs); log.DEBUG("Getting document by fields:", collectionName, fieldValuePairs);
const db = await connectToDatabase(); const db = await connectToDatabase();
try { try {
@@ -99,7 +102,12 @@ export const upsertDocumentByField = async (
value, value,
updatedFields, updatedFields,
); );
return await updateDocumentByFields(collectionName, updatedFields, { upsert: true }, [field, value]); return await updateDocumentByFields(
collectionName,
updatedFields,
{ upsert: true },
[field, value],
);
}; };
// Function to update a document by a specific field // Function to update a document by a specific field
@@ -112,9 +120,14 @@ export const upsertDocumentByFields = async (
"Upsert document by fields:", "Upsert document by fields:",
collectionName, collectionName,
updatedFields, updatedFields,
fieldValuePairs fieldValuePairs,
);
return await updateDocumentByFields(
collectionName,
updatedFields,
{ upsert: true },
fieldValuePairs,
); );
return await updateDocumentByFields(collectionName, updatedFields, { upsert: true }, fieldValuePairs);
}; };
// Function to update a document by a specific field // Function to update a document by a specific field
@@ -133,7 +146,10 @@ export const updateDocumentByField = async (
updatedFields, updatedFields,
options, options,
); );
return await updateDocumentByFields(collectionName, updatedFields, options, [field, value]) return await updateDocumentByFields(collectionName, updatedFields, options, [
field,
value,
]);
}; };
// Function to update a document by multiple fields // Function to update a document by multiple fields
@@ -163,7 +179,7 @@ export const updateDocumentByFields = async (
const result = await collection.updateOne( const result = await collection.updateOne(
query, query,
{ $set: updatedFields }, { $set: updatedFields },
options options,
); );
log.DEBUG("Document updated:", result.modifiedCount); log.DEBUG("Document updated:", result.modifiedCount);
return result.modifiedCount; return result.modifiedCount;
@@ -182,7 +198,10 @@ export const deleteDocumentByField = async (collectionName, field, value) => {
}; };
// Function to delete a document by multiple fields // Function to delete a document by multiple fields
export const deleteDocumentByFields = async (collectionName, ...fieldValuePairs) => { export const deleteDocumentByFields = async (
collectionName,
...fieldValuePairs
) => {
log.DEBUG("Delete document by fields:", collectionName, fieldValuePairs); log.DEBUG("Delete document by fields:", collectionName, fieldValuePairs);
const db = await connectToDatabase(); const db = await connectToDatabase();
try { try {