diff --git a/discordBot/addons/linkCop.mjs b/discordBot/addons/linkCop.mjs index 107263b..acf771c 100644 --- a/discordBot/addons/linkCop.mjs +++ b/discordBot/addons/linkCop.mjs @@ -2,20 +2,30 @@ 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"; +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"); + 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)) + message.channel.id == approvedLinksChannel || + !Array.isArray(restrictedChannelIds) || + Array.isArray(restrictedChannelIds) || + !restrictedChannelIds.includes(message.channel.id) ) { return false; } diff --git a/discordBot/events/guildMemberAdd.mjs b/discordBot/events/guildMemberAdd.mjs index 70f39fb..b94a2f7 100644 --- a/discordBot/events/guildMemberAdd.mjs +++ b/discordBot/events/guildMemberAdd.mjs @@ -4,12 +4,20 @@ import dotenv from "dotenv"; dotenv.config(); import { Events } from "discord.js"; 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 async function execute(nodeIo, member) { - const welcomeChannel = await getGuildConfig(message.guild.id, "welcomeChannelId"); +export async function execute(nodeIo, member) { + const welcomeChannel = await getGuildConfig( + message.guild.id, + "welcomeChannelId", + ); log.INFO("New user joined the server", member); let conversation = []; conversation.push({ diff --git a/discordBot/events/messageCreate.mjs b/discordBot/events/messageCreate.mjs index 2c8b1b8..83adca1 100644 --- a/discordBot/events/messageCreate.mjs +++ b/discordBot/events/messageCreate.mjs @@ -6,19 +6,33 @@ import { Events } from "discord.js"; import { gptInteraction } from "../addons/gptInteraction.mjs"; import { linkCop } from "../addons/linkCop.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 async function execute(nodeIo, message) { // 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 - 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 - if (message.author.bot) { return; } + if (message.author.bot) { + return; + } log.INFO("Message create", message); diff --git a/discordBot/modules/presenceManager.mjs b/discordBot/modules/presenceManager.mjs index 183f550..c25487f 100644 --- a/discordBot/modules/presenceManager.mjs +++ b/discordBot/modules/presenceManager.mjs @@ -1,5 +1,8 @@ -import { getConfig, setConfig } from "../../modules/mongo-wrappers/mongoConfigWrappers.mjs"; -import { ActivityType, PresenceUpdateStatus } from 'discord.js'; +import { + getConfig, + setConfig, +} from "../../modules/mongo-wrappers/mongoConfigWrappers.mjs"; +import { ActivityType, PresenceUpdateStatus } from "discord.js"; class PresenceManager { /** @@ -41,20 +44,26 @@ class PresenceManager { if (!defaultPresence) { defaultPresence = { - status: 'idle', - activities: [{ - name: 'your commands', - type: 'LISTENING' - }] + status: "idle", + activities: [ + { + name: "your commands", + type: "LISTENING", + }, + ], }; - await setConfig('presence', defaultPresence); + await setConfig("presence", defaultPresence); } console.log("Default Presence:", defaultPresence); // 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) { switch (activityType.toUpperCase()) { - case 'PLAYING': + case "PLAYING": return ActivityType.Playing; - case 'STREAMING': + case "STREAMING": return ActivityType.Streaming; - case 'LISTENING': + case "LISTENING": return ActivityType.Listening; - case 'WATCHING': + case "WATCHING": return ActivityType.Watching; - case 'COMPETING': + case "COMPETING": return ActivityType.Competing; default: - throw new Error('Invalid activity type'); + throw new Error("Invalid activity type"); } } @@ -86,19 +95,18 @@ class PresenceManager { */ convertStatus(status) { switch (status.toLowerCase()) { - case 'online': + case "online": return PresenceUpdateStatus.Online; - case 'idle': + case "idle": return PresenceUpdateStatus.Idle; - case 'dnd': + case "dnd": return PresenceUpdateStatus.DoNotDisturb; - case 'invisible': + case "invisible": return PresenceUpdateStatus.Invisible; default: - throw new Error('Invalid status'); + throw new Error("Invalid status"); } } } - export default PresenceManager; diff --git a/modules/mongo-wrappers/mongoConfigWrappers.mjs b/modules/mongo-wrappers/mongoConfigWrappers.mjs index bf20344..16e348e 100644 --- a/modules/mongo-wrappers/mongoConfigWrappers.mjs +++ b/modules/mongo-wrappers/mongoConfigWrappers.mjs @@ -1,82 +1,113 @@ -import { getDocumentByField, deleteDocumentByField, getDocumentByFields, upsertDocumentByField, deleteDocumentByFields, upsertDocumentByFields } from './mongoHandler.mjs'; // Import your MongoDB handlers -import { DebugBuilder } from '../debugger.mjs'; +import { + 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 export const getConfig = async (key) => { - try { - const config = await getDocumentByField(collectionName, 'key', key); - log.DEBUG(`Configuration for key "${key}" retrieved:`, config); - return config ? config[key] : null; // Return null if no configuration is found - } catch (error) { - log.ERROR('Error retrieving configuration:', error); - throw error; - } + try { + const config = await getDocumentByField(collectionName, "key", key); + log.DEBUG(`Configuration for key "${key}" retrieved:`, config); + return config ? config[key] : null; // Return null if no configuration is found + } catch (error) { + log.ERROR("Error retrieving configuration:", error); + throw error; + } }; // Function to set a configuration by key export const setConfig = async (key, value) => { - // Set the config object - value = {key : value}; - try { - const result = await upsertDocumentByField(collectionName, 'key', key, value); - 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; - } + // Set the config object + value = { key: value }; + try { + const result = await upsertDocumentByField( + collectionName, + "key", + key, + value, + ); + 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) export const deleteConfig = async (key) => { - try { - const result = await deleteDocumentByField(collectionName, 'key', key); - log.DEBUG(`Configuration for key "${key}" deleted:`, result); - return result; // Return the count of deleted documents - } catch (error) { - log.ERROR('Error deleting configuration:', error); - throw error; - } + try { + const result = await deleteDocumentByField(collectionName, "key", key); + log.DEBUG(`Configuration for key "${key}" deleted:`, result); + return result; // Return the count of deleted documents + } catch (error) { + log.ERROR("Error deleting configuration:", error); + throw error; + } }; // Function to get a configuration by key for a specific guild export const getGuildConfig = async (guildId, key) => { - try { - const config = await getDocumentByFields(collectionName, ['guildId', guildId], ['key', key]); - 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; - } + try { + const config = await getDocumentByFields( + collectionName, + ["guildId", guildId], + ["key", key], + ); + 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 export const setGuildConfig = async (guildId, key, value) => { - // Set the config object - value = {key : value}; - try { - const result = await upsertDocumentByFields(collectionName, value, ['guildId', guildId], ['key', key]); - 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; - } + // Set the config object + value = { key: value }; + try { + const result = await upsertDocumentByFields( + collectionName, + value, + ["guildId", guildId], + ["key", key], + ); + 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) export const deleteGuildConfig = async (guildId, key) => { - try { - const result = await deleteDocumentByFields(collectionName, ['guildId', guildId], ['key', key]); - 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; - } + try { + const result = await deleteDocumentByFields( + collectionName, + ["guildId", guildId], + ["key", key], + ); + 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; + } }; diff --git a/modules/mongo-wrappers/mongoHandler.mjs b/modules/mongo-wrappers/mongoHandler.mjs index c38ca41..b960ebf 100644 --- a/modules/mongo-wrappers/mongoHandler.mjs +++ b/modules/mongo-wrappers/mongoHandler.mjs @@ -63,7 +63,10 @@ export const getDocumentByField = async (collectionName, field, value) => { }; // 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); const db = await connectToDatabase(); try { @@ -99,7 +102,12 @@ export const upsertDocumentByField = async ( value, 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 @@ -112,9 +120,14 @@ export const upsertDocumentByFields = async ( "Upsert document by fields:", collectionName, 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 @@ -133,7 +146,10 @@ export const updateDocumentByField = async ( updatedFields, 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 @@ -163,7 +179,7 @@ export const updateDocumentByFields = async ( const result = await collection.updateOne( query, { $set: updatedFields }, - options + options, ); log.DEBUG("Document updated:", result.modifiedCount); return result.modifiedCount; @@ -182,7 +198,10 @@ export const deleteDocumentByField = async (collectionName, field, value) => { }; // 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); const db = await connectToDatabase(); try { @@ -203,4 +222,4 @@ export const deleteDocumentByFields = async (collectionName, ...fieldValuePairs) } finally { await db.close(); } -}; \ No newline at end of file +};