fix: use logger everywhere?
Some checks failed
Code Analysis / SonarQube (push) Failing after 15s
Node.js CI / Lint and Test (push) Successful in 42s

This commit is contained in:
2025-07-07 16:38:36 -07:00
parent 9ea5a1f6d1
commit c47c46e69a
11 changed files with 30 additions and 61 deletions

View File

@ -26,7 +26,7 @@ import { onVoiceUpdate } from "./voice/onVoiceUpdate";
export const handleEvents = (bot: ExtendedClient) => {
/* Client Events */
bot.on("ready", async () => await onReady(bot));
bot.on("disconnect", () => onDisconnect(bot));
bot.on("disconnect", () => onDisconnect());
/* Message Events */
bot.on("messageCreate", async (message) => {

View File

@ -1,16 +1,8 @@
import { EmbedBuilder } from "discord.js";
import { ExtendedClient } from "../../interfaces/ExtendedClient";
import { logHandler } from "../../utils/logHandler";
/**
* Sends a message to the debug hook when the bot disconnects.
*
* @param {ExtendedClient} bot The bot's Discord instance.
*/
export const onDisconnect = async (bot: ExtendedClient) => {
const disconnectEmbed = new EmbedBuilder();
disconnectEmbed.setTitle("Disconnected");
disconnectEmbed.setDescription("I have been disconnected from Discord.");
disconnectEmbed.setTimestamp();
await bot.env.debugHook.send({ embeds: [disconnectEmbed] });
export const onDisconnect = async () => {
await logHandler.log("warn", "Bot has disconnected from Discord.");
};

View File

@ -1,18 +1,20 @@
import { Guild } from "discord.js";
import { ExtendedClient } from "../../interfaces/ExtendedClient";
import { logHandler } from "../../utils/logHandler";
/**
*
* @param {ExtendedClient} bot The bot's Discord instance.
* @param {ExtendedClient} _bot The bot's Discord instance.
* @param {Guild} guild The newly joined Discord guild.
*/
export const onGuildCreate = async function (
bot: ExtendedClient,
_bot: ExtendedClient,
guild: Guild
) {
const owner = await guild.fetchOwner();
await bot.env.debugHook.send({
content: `JOINED GUILD: ${guild.name} (${guild.id}) - owned by ${owner?.displayName} (${owner.id})`
});
await logHandler.log(
"info",
`Joined guild: ${guild.name} (${guild.id}) - owned by ${owner?.displayName} (${owner.id})`
);
};

View File

@ -2,6 +2,7 @@ import { Guild } from "discord.js";
import { ExtendedClient } from "../../interfaces/ExtendedClient";
import { errorHandler } from "../../utils/errorHandler";
import { logHandler } from "../../utils/logHandler";
/**
*
* @param {ExtendedClient} bot The bot's Discord instance.
@ -12,9 +13,10 @@ export const onGuildDelete = async function (
guild: Guild
) {
try {
await bot.env.debugHook.send({
content: `LEFT GUILD: ${guild.name} (${guild.id}) `
});
await logHandler.log(
"info",
`Left guild: ${guild.name} (${guild.id}) - owned by ${guild.ownerId}`
);
await bot.db.cases
.deleteMany({ where: { serverId: guild.id } })
.catch(() => null);

View File

@ -18,7 +18,7 @@ import { loadContexts } from "./utils/loadContexts";
await connectDatabase(bot);
handleEvents(bot);
serve(bot);
serve();
await bot.login(bot.env.token);
})();

View File

@ -1,5 +1,5 @@
import { PrismaClient, configs } from "@prisma/client";
import { Client, WebhookClient } from "discord.js";
import { Client } from "discord.js";
import { Command } from "./Command";
import { Context } from "./Context";
@ -7,7 +7,6 @@ import { Context } from "./Context";
export interface ExtendedClient extends Client {
env: {
token: string;
debugHook: WebhookClient;
mongoUri: string;
devMode: boolean;
};

View File

@ -1,5 +1,3 @@
import { WebhookClient } from "discord.js";
import { ExtendedClient } from "../interfaces/ExtendedClient";
import { logHandler } from "../utils/logHandler";
@ -9,18 +7,13 @@ import { logHandler } from "../utils/logHandler";
* @returns {ExtendedClient["env"]} The environment variable object to attach to the bot.
*/
export const validateEnv = (): ExtendedClient["env"] => {
if (
!process.env.BOT_TOKEN ||
!process.env.DEBUG_HOOK ||
!process.env.MONGO_URI
) {
if (!process.env.BOT_TOKEN || !process.env.MONGO_URI) {
logHandler.log("warn", "Missing environment variables!");
process.exit(1);
}
return {
token: process.env.BOT_TOKEN,
debugHook: new WebhookClient({ url: process.env.DEBUG_HOOK }),
mongoUri: process.env.MONGO_URI,
devMode: process.env.NODE_ENV !== "production"
};

View File

@ -3,14 +3,13 @@ import http from "http";
import express from "express";
import { register } from "prom-client";
import { ExtendedClient } from "../interfaces/ExtendedClient";
import { logHandler } from "../utils/logHandler";
/**
* Instantiates the web server for GitHub webhooks.
*
* @param {ExtendedClient} bot The bot's Discord instance.
*/
export const serve = async (bot: ExtendedClient) => {
export const serve = () => {
const app = express();
app.get("/", (_req, res) => {
@ -70,12 +69,6 @@ export const serve = async (bot: ExtendedClient) => {
const httpServer = http.createServer(app);
httpServer.listen(9080, async () => {
await bot.env.debugHook.send({
content: "http server listening on port 9080",
username: bot.user?.username ?? "bot",
avatarURL:
bot.user?.displayAvatarURL() ??
"https://cdn.nhcarrigan.com/avatars/nhcarrigan.png"
});
await logHandler.log("info", "HTTP server listening on port 9080");
});
};

View File

@ -7,7 +7,7 @@ import { logHandler } from "./logHandler";
/**
* Handles logging the error to the terminal and sending it to the debug webhook.
*
* @param {ExtendedClient} bot The bot's Discord instance.
* @param {ExtendedClient} _bot The bot's Discord instance.
* @param {string} context A brief description of where the error occurred.
* @param {Error} err The error object.
* @returns {string} A unique ID to use in logs.
@ -19,9 +19,6 @@ export const errorHandler = async (
) => {
const id = SnowflakeUtil.generate();
const error = err as Error;
void logHandler.error(
context,
error
)
await logHandler.error(context, error);
return id;
};

View File

@ -7,7 +7,4 @@ import { Logger } from "@nhcarrigan/logger";
* @param {string} level - The log level to use.
* @param {string} message - The message to log.
*/
export const logHandler = new Logger(
"Celestine",
process.env.LOG_TOKEN ?? ""
)
export const logHandler = new Logger("Celestine", process.env.LOG_TOKEN ?? "");

View File

@ -1,22 +1,16 @@
import { ExtendedClient } from "../interfaces/ExtendedClient";
import { logHandler } from "./logHandler";
/**
* Sends a log message to the worker log hook.
*
* @param {ExtendedClient} bot The bot's Discord instance.
* @param {ExtendedClient} _bot The bot's Discord instance.
* @param {string} message The message to send.
*/
export const sendDebugMessage = async (
bot: ExtendedClient,
_bot: ExtendedClient,
message: string
) => {
if (bot.env.debugHook) {
await bot.env.debugHook.send({
content: message,
avatarURL:
bot.user?.displayAvatarURL() ??
"https://cdn.nhcarrigan.com/avatars/nhcarrigan.png",
username: bot.user?.username ?? "Mod bot"
});
}
await logHandler.log("debug", message);
};