From c2380609b2d0143bc7d535887b57c8039c69a532 Mon Sep 17 00:00:00 2001 From: Dennis Heinrich Date: Sun, 1 Dec 2024 12:34:50 +0100 Subject: [PATCH] Implemented version checking for the discord bot and increased version to 0.1.6 --- package.json | 2 +- source/Main.ts | 19 ++++++++++++++ source/Services/VersionChecker.ts | 41 +++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 source/Services/VersionChecker.ts diff --git a/package.json b/package.json index 42155cf..5ed0409 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ls25-discord-bot", - "version": "0.1.5", + "version": "0.1.6", "description": "A simple discord bot for farming simulator 25", "main": "source/Main.ts", "scripts": { diff --git a/source/Main.ts b/source/Main.ts index dbae6bb..5e8fb01 100644 --- a/source/Main.ts +++ b/source/Main.ts @@ -2,6 +2,7 @@ import {Client, IntentsBitField} from 'discord.js'; import Configuration from "./Services/Configuration"; import Logging from "./Services/Logging"; import DiscordService from "./Services/DiscordEmbed"; +import VersionChecker from './Services/VersionChecker'; // Create a new logger instance and configuration instance const appLogger = Logging.getLogger(); @@ -20,6 +21,24 @@ if(!appConfig.isConfigurationValid()) { process.exit(1); } +/** + * Check the version of the bot and log if it is up to date + */ +const versionChecker = new VersionChecker(); +versionChecker.checkVersionIsUpdated().then((isUpToDate: boolean): void => { + if (!isUpToDate) { + appLogger.warn(`====================================================`); + appLogger.warn(`====================================================`); + appLogger.warn(`The bot is not up to date. Please update it soon.`); + appLogger.warn(`Use the command 'git pull && docker compose up -d --build' to update the bot.`); + appLogger.warn(`====================================================`); + appLogger.warn(`====================================================`); + + } else { + appLogger.info(`The bot is up to date. No update needed.`); + } +}); + /** * Create a new discord client instance */ diff --git a/source/Services/VersionChecker.ts b/source/Services/VersionChecker.ts new file mode 100644 index 0000000..2b0be39 --- /dev/null +++ b/source/Services/VersionChecker.ts @@ -0,0 +1,41 @@ +export default class VersionChecker { + private readonly localPackageVersion: string; + private readonly versionUrl: string = "https://raw.githubusercontent.com/cloudmaker97/FS25-Discord-Bot/refs/heads/main/package.json"; + + constructor() { + this.localPackageVersion = require('../../package.json').version; + } + + /** + * Check if the version of the bot is up to date + */ + public async checkVersionIsUpdated(): Promise { + const latestVersion = await this.getLatestReleasedVersion(); + return this.isNewerVersion(latestVersion, this.localPackageVersion); + } + + /** + * Get the latest released version of the bot from the github repository + */ + public async getLatestReleasedVersion(): Promise { + const response = await fetch(this.versionUrl); + const latestPackage = await response.text(); + const latestVersion = JSON.parse(latestPackage)?.version; + return latestVersion; + } + + /** + * Check if the latest version is newer than the current version + */ + public isNewerVersion(latestVersion: string, currentVersion: string) { + const v1Parts: number[] = latestVersion.split('.').map(Number); + const v2Parts: number[] = currentVersion.split('.').map(Number); + for (let i = 0; i < Math.max(v1Parts.length, v2Parts.length); i++) { + const part1 = v1Parts[i] || 0; + const part2 = v2Parts[i] || 0; + if (part1 > part2) return false; + if (part1 < part2) return true; + } + return true; + } +} \ No newline at end of file