Skip to content

Commit 9fa2db3

Browse files
authored
Merge pull request #14 from cloudmaker97/update-info
2 parents 174f13a + c238060 commit 9fa2db3

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ls25-discord-bot",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"description": "A simple discord bot for farming simulator 25",
55
"main": "source/Main.ts",
66
"scripts": {

source/Main.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {Client, IntentsBitField} from 'discord.js';
22
import Configuration from "./Services/Configuration";
33
import Logging from "./Services/Logging";
44
import DiscordService from "./Services/DiscordEmbed";
5+
import VersionChecker from './Services/VersionChecker';
56

67
// Create a new logger instance and configuration instance
78
const appLogger = Logging.getLogger();
@@ -20,6 +21,24 @@ if(!appConfig.isConfigurationValid()) {
2021
process.exit(1);
2122
}
2223

24+
/**
25+
* Check the version of the bot and log if it is up to date
26+
*/
27+
const versionChecker = new VersionChecker();
28+
versionChecker.checkVersionIsUpdated().then((isUpToDate: boolean): void => {
29+
if (!isUpToDate) {
30+
appLogger.warn(`====================================================`);
31+
appLogger.warn(`====================================================`);
32+
appLogger.warn(`The bot is not up to date. Please update it soon.`);
33+
appLogger.warn(`Use the command 'git pull && docker compose up -d --build' to update the bot.`);
34+
appLogger.warn(`====================================================`);
35+
appLogger.warn(`====================================================`);
36+
37+
} else {
38+
appLogger.info(`The bot is up to date. No update needed.`);
39+
}
40+
});
41+
2342
/**
2443
* Create a new discord client instance
2544
*/

source/Services/VersionChecker.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export default class VersionChecker {
2+
private readonly localPackageVersion: string;
3+
private readonly versionUrl: string = "https://raw.githubusercontent.com/cloudmaker97/FS25-Discord-Bot/refs/heads/main/package.json";
4+
5+
constructor() {
6+
this.localPackageVersion = require('../../package.json').version;
7+
}
8+
9+
/**
10+
* Check if the version of the bot is up to date
11+
*/
12+
public async checkVersionIsUpdated(): Promise<boolean> {
13+
const latestVersion = await this.getLatestReleasedVersion();
14+
return this.isNewerVersion(latestVersion, this.localPackageVersion);
15+
}
16+
17+
/**
18+
* Get the latest released version of the bot from the github repository
19+
*/
20+
public async getLatestReleasedVersion(): Promise<string> {
21+
const response = await fetch(this.versionUrl);
22+
const latestPackage = await response.text();
23+
const latestVersion = JSON.parse(latestPackage)?.version;
24+
return latestVersion;
25+
}
26+
27+
/**
28+
* Check if the latest version is newer than the current version
29+
*/
30+
public isNewerVersion(latestVersion: string, currentVersion: string) {
31+
const v1Parts: number[] = latestVersion.split('.').map(Number);
32+
const v2Parts: number[] = currentVersion.split('.').map(Number);
33+
for (let i = 0; i < Math.max(v1Parts.length, v2Parts.length); i++) {
34+
const part1 = v1Parts[i] || 0;
35+
const part2 = v2Parts[i] || 0;
36+
if (part1 > part2) return false;
37+
if (part1 < part2) return true;
38+
}
39+
return true;
40+
}
41+
}

0 commit comments

Comments
 (0)