Skip to content
This repository has been archived by the owner on Oct 12, 2024. It is now read-only.

Commit

Permalink
Runtime logging
Browse files Browse the repository at this point in the history
  • Loading branch information
RadiatedExodus committed Feb 7, 2024
1 parent a4247c9 commit 4a016c0
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
93 changes: 92 additions & 1 deletion src/events/ready.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import type { MeteoriumEvent } from ".";
import { ActivityType, REST, Routes } from "discord.js";
import { ActivityType, REST, Routes, codeBlock } from "discord.js";
import moment from "moment";
import { inspect } from "util";
import { MeteoriumEmbedBuilder } from "../util/MeteoriumEmbedBuilder";

export const Event: MeteoriumEvent<"ready"> = {
async Callback(client) {
const readyNS = client.Logging.GetNamespace("Events/ready");
const StartTime = moment().format("DD-MM-YYYY hh:mm:ss:SSS A Z");
const CommandsMapped = client.Commands.map((Command) => Command.InteractionData.toJSON());

readyNS.info("Registering global slash commands at Discord");
Expand All @@ -28,6 +32,93 @@ export const Event: MeteoriumEvent<"ready"> = {
});

readyNS.info("Bot ready");

async function ExitHandler() {
readyNS.info("Bot is shutting down!");
const Promises: Promise<any>[] = [];
const ShutdownEmbed = new MeteoriumEmbedBuilder()
.setTitle("Bot shutting down")
.setDescription("The bot is now shutting down...")
.addFields([
{ name: "Start time", value: StartTime },
{ name: "Shut down time", value: moment().format("DD-MM-YYYY hh:mm:ss:SSS A Z") },
])
.setColor("Red");
for (const ChannelId of client.Config.RuntimeLogChannelIds) {
const Channel = await client.channels.fetch(ChannelId).catch(() => null);

if (Channel && Channel.isTextBased())
Promises.push(
Channel.send({
embeds: [ShutdownEmbed],
}),
);
}
await Promise.all(Promises);
client.destroy();
process.exit(0);
}

process.on("SIGTERM", ExitHandler);
process.on("SIGINT", ExitHandler);

process.on("uncaughtException", async (err) => {
const Promises: Promise<any>[] = [];
const ErrorEmbed = new MeteoriumEmbedBuilder()
.setTitle("Uncaught exception occured")
.setDescription(codeBlock(inspect(err).substring(0, 4500)))
.setColor("Red");

for (const ChannelId of client.Config.RuntimeLogChannelIds) {
const Channel = await client.channels.fetch(ChannelId).catch(() => null);
if (Channel && Channel.isTextBased())
Promises.push(
Channel.send({
embeds: [ErrorEmbed],
}),
);
}
await Promise.all(Promises);
});

process.on("unhandledRejection", async (err) => {
const Promises: Promise<any>[] = [];
const ErrorEmbed = new MeteoriumEmbedBuilder()
.setTitle("Unhandled rejection occured")
.setDescription(codeBlock(inspect(err).substring(0, 4500)))
.setColor("Red");

for (const ChannelId of client.Config.RuntimeLogChannelIds) {
const Channel = await client.channels.fetch(ChannelId).catch(() => null);
if (Channel && Channel.isTextBased())
Promises.push(
Channel.send({
embeds: [ErrorEmbed],
}),
);
}
await Promise.all(Promises);
});

const ReadyEmbedPromises: Promise<any>[] = [];
const ReadyEmbed = new MeteoriumEmbedBuilder()
.setTitle("Bot online")
.setDescription("The bot is now online")
.addFields([{ name: "Start time", value: StartTime }])
.setColor("Green");

for (const ChannelId of client.Config.RuntimeLogChannelIds) {
const Channel = await client.channels.fetch(ChannelId).catch(() => null);
if (Channel && Channel.isTextBased())
ReadyEmbedPromises.push(
Channel.send({
embeds: [ReadyEmbed],
}),
);
}

await Promise.all(ReadyEmbedPromises);

return;
},
};
2 changes: 2 additions & 0 deletions src/util/MeteoriumClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const ParseDotEnvConfig = () => {
config({ path: "./.ENV" });
}
const InteractionFirstDeployGuildIds = String(process.env.DEPLOYGUILDIDS).split(",");
const RuntimeLogChannelIds = String(process.env.RUNTIMELOGCHANNELID).split(",");
return {
MongoDB_URI: String(process.env.METEORIUMMONGODBURI),
DiscordToken: String(process.env.METEORIUMBOTTOKEN),
Expand All @@ -23,6 +24,7 @@ const ParseDotEnvConfig = () => {
RatelimitMaxLimit: Number(process.env.RATELIMITMAXLIMIT),
RatelimitMaxLimitTime: Number(process.env.RATELIMITMAXLIMITTIME),
GeniusAPIKey: String(process.env.GENIUSAPIKEY),
RuntimeLogChannelIds: RuntimeLogChannelIds,
};
};

Expand Down

0 comments on commit 4a016c0

Please sign in to comment.