Skip to content

Commit

Permalink
Merge pull request #383 from TwoAbove/develop
Browse files Browse the repository at this point in the history
Backend fixes
  • Loading branch information
TwoAbove authored Sep 8, 2024
2 parents 87887ae + aaa134f commit edca5a6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
38 changes: 36 additions & 2 deletions server/discord.mjs
Original file line number Diff line number Diff line change
@@ -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 = [
{
Expand All @@ -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}!`);
});

Expand All @@ -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 };
2 changes: 1 addition & 1 deletion server/io/compute.mjs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit edca5a6

Please sign in to comment.