Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend fixes #383

Merged
merged 2 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading