From 8434a32c34bd735232295d6b3a17e4a159a224d1 Mon Sep 17 00:00:00 2001 From: Dennis Heinrich Date: Sun, 17 Nov 2024 00:50:57 +0100 Subject: [PATCH] Restart the bot if an error occurs in the discord service; added Dockerfile --- Dockerfile | 11 +++++++++++ package.json | 1 + source/Main.ts | 19 +++++++++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..745d503 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM node:latest +LABEL name="LS25-Discord-Bot" +LABEL authors="Dennis Heinrich" + +# Copy the source files +WORKDIR /app +COPY . /app +RUN npm install + +CMD ["npm", "start"] +ENTRYPOINT ["npm", "start"] \ No newline at end of file diff --git a/package.json b/package.json index 82e1ccd..d414c17 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "main": "source/Main.ts", "scripts": { "start": "npx tsc && node build/Main.js", + "build": "npx tsc", "schema": "npx json2ts -i source/Schema/ServerStats.json -o ./source/Schema/ServerStats.d.ts --unreachableDefinitions", "test": "echo \"Error: no test specified\" && exit 1" }, diff --git a/source/Main.ts b/source/Main.ts index 220fa0d..3e3c460 100644 --- a/source/Main.ts +++ b/source/Main.ts @@ -17,7 +17,22 @@ discordClient.login(appConfig.discord.botToken).then(() => { appLogger.info(`Login successful to discord with token`); }); +/** + * Start the DiscordService and restart it if an error occurred + */ +function startDiscordService(): void { + try { + new DiscordService(discordClient); + } catch (exception) { + appLogger.error(`Restarting the discord service, an error occurred`, exception); + } finally { + setTimeout(() => { + startDiscordService(); + }, 5000); + } +} + discordClient.on('ready', () => { appLogger.info(`Discord client ready. Logged in as ${discordClient.user?.username}!`); - new DiscordService(discordClient); -}); \ No newline at end of file + startDiscordService(); +});