2021-05-08 13:32:45 +00:00
|
|
|
// This will keep track of the last event that occurred to provide context to error messages.
|
|
|
|
// Like with logging each command invocation, it's not a good idea to pollute the logs with this kind of stuff when it works most of the time.
|
|
|
|
// However, it's also a pain to debug when no context is provided for an error message.
|
|
|
|
import {client} from "..";
|
|
|
|
|
|
|
|
let lastEvent = "N/A";
|
|
|
|
|
|
|
|
// A generic process handler is set to catch unhandled rejections other than the ones from Lavalink and Discord.
|
|
|
|
process.on("unhandledRejection", (reason: any) => {
|
|
|
|
const isLavalinkError = reason?.code === "ECONNREFUSED";
|
|
|
|
const isDiscordError = reason?.name === "DiscordAPIError";
|
|
|
|
|
2021-05-17 18:48:38 +00:00
|
|
|
// If it's a DiscordAPIError on a message event, I'll make the assumption that it comes from the command handler.
|
|
|
|
if (!isLavalinkError && (!isDiscordError || lastEvent !== "message"))
|
|
|
|
console.error(`@${lastEvent}\n${reason.stack}`);
|
2021-05-08 13:32:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// This will dynamically attach all known events instead of doing it manually.
|
|
|
|
// As such, it needs to be placed after all other events are attached or the tracking won't be done properly.
|
|
|
|
for (const event of client.eventNames()) {
|
2021-05-17 18:48:38 +00:00
|
|
|
client.on(event, () => {
|
|
|
|
lastEvent = event.toString();
|
|
|
|
});
|
2021-05-08 13:32:45 +00:00
|
|
|
}
|