feat: add a database client
Some checks failed
Node.js CI / Lint and Test (pull_request) Failing after 11s

This commit is contained in:
2025-07-02 10:52:11 -04:00
parent a632f2b924
commit 7e9dc20632
8 changed files with 174 additions and 6 deletions

View File

@ -4,14 +4,12 @@
* @author Naomi Carrigan
*/
import type { ReminderChannel } from "../models/channel";
/**
* The channels to post standup reminders in.
*/
export const channels: Array<{
channelId: string;
roleId: string;
name: string;
}> = [
export const channels: Array<ReminderChannel> = [
{
channelId: "1382093555228606484",
name: "red-script",

View File

@ -7,6 +7,7 @@ import { Client, Events, GatewayIntentBits } from "discord.js";
import { scheduleJob } from "node-schedule";
import { channels } from "./config/channels.js";
import { standup } from "./modules/standup.js";
import { closeDatabase, initializeDatabase } from "./utils/database.js";
import { logger } from "./utils/logger.js";
const activeIds: Array<string> = [];
@ -17,6 +18,7 @@ process.on("unhandledRejection", (error) => {
return;
}
void logger.error("unhandled rejection", new Error(String(error)));
void closeDatabase();
});
process.on("uncaughtException", (error) => {
@ -25,12 +27,15 @@ process.on("uncaughtException", (error) => {
return;
}
void logger.error("uncaught exception", new Error(String(error)));
void closeDatabase();
});
const client = new Client({
intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages ],
});
await initializeDatabase();
client.on(Events.ClientReady, () => {
void logger.log("debug", "Bot is ready.");
scheduleJob("reminders", "0 9 * * 1-5", async() => {

11
src/models/channel.ts Normal file
View File

@ -0,0 +1,11 @@
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Anna
*/
export interface ReminderChannel {
channelId: string;
roleId: string;
name: string;
}

47
src/utils/database.ts Normal file
View File

@ -0,0 +1,47 @@
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Anna
*/
import { type Collection, MongoClient } from "mongodb";
import type { ReminderChannel } from "../models/channel";
const client = new MongoClient(process.env.MONGO_DB_URL ?? "");
const databaseName = "Maribelle";
// eslint-disable-next-line @typescript-eslint/init-declarations -- I need a reference to the collection to insert records
let collection: Collection<ReminderChannel>;
/**
* Initializes a connection to the database
* using the environment variable.
*/
async function initializeDatabase(): Promise<void> {
await client.connect();
const database = client.db(databaseName);
collection = database.collection<ReminderChannel>("channel");
}
/**
* Inserts a new channel into the database.
* @param channelId - The id of the channel to post in.
* @param roleId - The id of the role to ping.
* @param name - The name of the group.
*/
async function insertRecord(channelId: string,
roleId: string,
name: string): Promise<void> {
const document = { channelId, name, roleId };
await collection.insertOne(document);
}
/**
* Closes the connection to the database.
*/
async function closeDatabase(): Promise<void> {
await client.close();
}
export { closeDatabase, initializeDatabase, insertRecord };

View File

@ -10,3 +10,4 @@ export const logger = new Logger(
"Maribelle",
process.env.LOG_TOKEN ?? "",
);