Merge pull request #14 from cloudmaker97/update-info

This commit is contained in:
Dennis Heinrich 2024-12-01 12:35:30 +01:00 committed by GitHub
commit 9fa2db35be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 61 additions and 1 deletions

View file

@ -1,6 +1,6 @@
{ {
"name": "ls25-discord-bot", "name": "ls25-discord-bot",
"version": "0.1.5", "version": "0.1.6",
"description": "A simple discord bot for farming simulator 25", "description": "A simple discord bot for farming simulator 25",
"main": "source/Main.ts", "main": "source/Main.ts",
"scripts": { "scripts": {

View file

@ -2,6 +2,7 @@ import {Client, IntentsBitField} from 'discord.js';
import Configuration from "./Services/Configuration"; import Configuration from "./Services/Configuration";
import Logging from "./Services/Logging"; import Logging from "./Services/Logging";
import DiscordService from "./Services/DiscordEmbed"; import DiscordService from "./Services/DiscordEmbed";
import VersionChecker from './Services/VersionChecker';
// Create a new logger instance and configuration instance // Create a new logger instance and configuration instance
const appLogger = Logging.getLogger(); const appLogger = Logging.getLogger();
@ -20,6 +21,24 @@ if(!appConfig.isConfigurationValid()) {
process.exit(1); 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 * Create a new discord client instance
*/ */

View file

@ -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<boolean> {
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<string> {
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;
}
}