Skip to content

Commit

Permalink
Merge pull request #14 from cloudmaker97/update-info
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudmaker97 authored Dec 1, 2024
2 parents 174f13a + c238060 commit 9fa2db3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
19 changes: 19 additions & 0 deletions source/Main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
*/
Expand Down
41 changes: 41 additions & 0 deletions source/Services/VersionChecker.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 9fa2db3

Please sign in to comment.