From 9ae9951e345bd662bd9ea335e6a6c91a58fc455c Mon Sep 17 00:00:00 2001 From: clover caruso Date: Thu, 6 Apr 2023 22:20:04 +0000 Subject: [PATCH] Add error handling stuff from @Toshizuno --- index.ts | 51 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/index.ts b/index.ts index 9b80b9135a4083261aba7c3727213bf32c6bd545..4eb228d4de0e5c21f74909a95c5a89e8334dd157 100644 --- a/index.ts +++ b/index.ts @@ -1,7 +1,8 @@ import Color from "color"; import * as Discord from "discord.js"; import NodeCache from "node-cache"; - +import fs from 'fs'; +import path from 'path'; import 'dotenv/config'; const helpMessage = `**I'm the Name Painter. I let users customize their name color.** @@ -188,9 +189,45 @@ client.on('guildCreate', async(guild) => { client.login(process.env.TOKEN); -// if (process.env.HEALTHCHECKS_URL) { -// var https = require('https'); -// setInterval(() => { -// https.get(process.env.HEALTHCHECKS_URL).on('error', () => {}); -// }, 5 * 60 * 1000); -// } +if (process.env.HEALTHCHECKS_URL) { + var https = require('https'); + setInterval(() => { + https.get(process.env.HEALTHCHECKS_URL).on('error', () => {}); + }, 5 * 60 * 1000); +} + +// Client error handler. +client.on('error', function (error) { + // Writes a log of what happened to a log file. + fs.writeFileSync( + path.join('logs', `${Date.now()}.err`), + `${error.name}: ${error.message}` + ); + + // Exits the process (and hopefully restarts!) + process.exit(); +}); + +// Invalidation handler. +client.on('invalidated', function () { + // Writes a log of what happened to a log file. + fs.writeFileSync( + path.join('logs', `${Date.now()}.inv`), + 'Error: The client was invalidated by Discord.' + ); + + // Exits the process (and hopefully restarts!) + process.exit(); +}); + +// Client warning handler (which might not require restarting but just to be safe.) +client.on('error', function (info) { + // Writes a log of what happened to a log file. + fs.writeFileSync( + path.join('logs', `${Date.now()}.warn`), + `Warning: ${info}` + ); + + // Exits the process (and hopefully restarts!) + process.exit(); +}); -- 2.54.0