Restart the bot if an error occurs in the discord service; added Dockerfile

This commit is contained in:
Dennis Heinrich 2024-11-17 00:50:57 +01:00
parent 6b6b422f3d
commit 8434a32c34
3 changed files with 29 additions and 2 deletions

11
Dockerfile Normal file
View file

@ -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"]

View file

@ -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"
},

View file

@ -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);
startDiscordService();
});