From 28d0a2d485fb9e7f400809b070e9777626d53c20 Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 30 Jan 2025 21:02:29 -0500 Subject: [PATCH] add config option for mongo db connection uri --- index.ts | 107 +++++++++++++++++++++++++++---------------------------- 1 file changed, 53 insertions(+), 54 deletions(-) diff --git a/index.ts b/index.ts index 8063e56..5c811c9 100644 --- a/index.ts +++ b/index.ts @@ -1,5 +1,5 @@ import { Client, GatewayIntentBits, Partials, REST, Routes } from "discord.js"; -import { discordBotToken, discordClientID } from "./config.json" +import { discordBotToken, discordClientID, mongoDBConnectionURI } from "./config.json"; import Collection from "./util/Collection"; import DiscordInteractionCommand from "./util/DiscordInteractionCommand"; import DiscordEvent from "./util/DiscordEvent"; @@ -12,66 +12,65 @@ export const DiscordEvents: Collection = new Collection(); // Instantiates a new Discord client const discordClient = new Client({ - intents: [ - GatewayIntentBits.DirectMessages, - GatewayIntentBits.GuildIntegrations, - GatewayIntentBits.GuildPresences, - GatewayIntentBits.GuildMessages, - GatewayIntentBits.Guilds, - GatewayIntentBits.GuildInvites, - GatewayIntentBits.GuildModeration, - ], - partials: [ Partials.GuildMember, Partials.Message, Partials.User, Partials.Channel, ], + intents: [ + GatewayIntentBits.DirectMessages, + GatewayIntentBits.GuildIntegrations, + GatewayIntentBits.GuildPresences, + GatewayIntentBits.GuildMessages, + GatewayIntentBits.Guilds, + GatewayIntentBits.GuildInvites, + GatewayIntentBits.GuildModeration, + ], + partials: [Partials.GuildMember, Partials.Message, Partials.User, Partials.Channel], }); const discordREST = new REST().setToken(discordBotToken); // const stripeClient = new Stripe(stripeToken, { typescript: true }); export async function main() { - // Connect to the databases - try { - mongoose.connection.once("open", () => { - console.info("[Info - Database] Connected to MongoDB"); - }) - // TODO: Fetch the MongoDB URI from the config file - await mongoose.connect("mongodb://localhost:27017/crra-main", {}); - } catch (error) { - console.error(`[Error - Database] Failed to connect to MongoDB: ${error}`); - process.exit(1); - } - // Load Discord interaction commands - for (const Command of Object.values(DiscordInteractionCommandsIndex)) { - const instance = new Command(); - DiscordInteractionCommands.add(instance.name, instance); - console.info(`[Info - Discord] Loaded interaction command: ${instance.name}`); - } - // Load Discord events - for (const Event of Object.values(DiscordEventsIndex)) { - const instance = new Event(discordClient); - DiscordEvents.add(instance.name, instance); - discordClient.on(instance.name, instance.execute); - console.info(`[Info - Discord] Loaded event: ${instance.name}`); - } - await discordClient.login(discordBotToken); + // Connect to the databases + try { + mongoose.connection.once("open", () => { + console.info("[Info - Database] Connected to MongoDB"); + }); + // TODO: Fetch the MongoDB URI from the config file + await mongoose.connect(mongoDBConnectionURI, {}); + } catch (error) { + console.error(`[Error - Database] Failed to connect to MongoDB: ${error}`); + process.exit(1); + } + // Load Discord interaction commands + for (const Command of Object.values(DiscordInteractionCommandsIndex)) { + const instance = new Command(); + DiscordInteractionCommands.add(instance.name, instance); + console.info(`[Info - Discord] Loaded interaction command: ${instance.name}`); + } + // Load Discord events + for (const Event of Object.values(DiscordEventsIndex)) { + const instance = new Event(discordClient); + DiscordEvents.add(instance.name, instance); + discordClient.on(instance.name, instance.execute); + console.info(`[Info - Discord] Loaded event: ${instance.name}`); + } + await discordClient.login(discordBotToken); - try { - console.log(`Started refreshing ${DiscordInteractionCommands.size} application (/) commands.`); - const interactionCommandsData = []; - for (const command of DiscordInteractionCommands.values()) { - interactionCommandsData.push(command.builder.toJSON()); - } - - // The put method is used to fully refresh all commands in the guild with the current set - const data = await discordREST.put( - Routes.applicationCommands(discordClientID), - { body: interactionCommandsData }, - ); - - // @ts-ignore - console.log(`Successfully reloaded ${data?.length} application (/) commands.`); - } catch (error) { - // And of course, make sure you catch and log any errors! - console.error(error); + try { + console.log(`Started refreshing ${DiscordInteractionCommands.size} application (/) commands.`); + const interactionCommandsData = []; + for (const command of DiscordInteractionCommands.values()) { + interactionCommandsData.push(command.builder.toJSON()); } + + // The put method is used to fully refresh all commands in the guild with the current set + const data = await discordREST.put(Routes.applicationCommands(discordClientID), { + body: interactionCommandsData, + }); + + // @ts-ignore + console.log(`Successfully reloaded ${data?.length} application (/) commands.`); + } catch (error) { + // And of course, make sure you catch and log any errors! + console.error(error); + } } main();