From 45cc048c88dbe6a50a93f61ad351af12cb05a2b4 Mon Sep 17 00:00:00 2001 From: Seva Maltsev Date: Sat, 7 Sep 2024 20:47:24 -0400 Subject: [PATCH 1/2] Updated discord setup to send errors to a channel --- server/discord.mjs | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/server/discord.mjs b/server/discord.mjs index d64475e..11d5b4e 100644 --- a/server/discord.mjs +++ b/server/discord.mjs @@ -1,7 +1,9 @@ +import util from "util"; import { REST, Routes, Client, GatewayIntentBits } from "discord.js"; const token = process.env.DISCORD_TOKEN; const clientId = process.env.DISCORD_CLIENT_ID; +const errorChannelId = process.env.DISCORD_ERROR_CHANNEL_ID; const commands = [ { @@ -24,9 +26,9 @@ const rest = new REST({ version: "10" }).setToken(token); } })(); -const client = new Client({ intents: [GatewayIntentBits.Guilds] }); +const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages] }); -client.on("ready", () => { +client.on("ready", async () => { console.log(`Logged in as ${client.user.tag}!`); }); @@ -42,4 +44,36 @@ client.on("interactionCreate", async interaction => { } }); +if (errorChannelId) { + async function sendErrorToDiscord(...args) { + const errorChannel = await client.channels.fetch(errorChannelId); + if (errorChannel) { + await errorChannel.send(`An error occurred:\n\`\`\`\n${util.inspect(args, false, null, false)}\n\`\`\``); + } + } + const consoleError = console.error; + console.error = (...args) => { + console.log(args); + if (args.length) { + sendErrorToDiscord(args); + } + return consoleError(...args); + }; + + // Catch all unhandled promise rejections and send them to the Discord channel + process.on("unhandledRejection", async (reason, promise) => { + console.error("Unhandled Rejection at:", promise, "reason:", reason); + await sendErrorToDiscord(reason); + }); + + // Catch all uncaught exceptions and send them to the Discord channel + process.on("uncaughtException", async error => { + console.error("Uncaught Exception:", error); + await sendErrorToDiscord(error); + process.exit(1); + }); +} + client.login(token); + +export { client, rest }; From aaa134f2e6ebf914d79f5c373f8317eb9355b011 Mon Sep 17 00:00:00 2001 From: Seva Maltsev Date: Sat, 7 Sep 2024 20:49:08 -0400 Subject: [PATCH 2/2] Fixed mongoose connect --- server/io/compute.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/io/compute.mjs b/server/io/compute.mjs index 3264780..795f4c6 100644 --- a/server/io/compute.mjs +++ b/server/io/compute.mjs @@ -1,6 +1,6 @@ import { schedule } from "node-cron"; import { User } from "../db.mjs"; -import { mongo } from "mongoose"; +import { mongoose } from "mongoose"; export const counts = { hosts: 0,