Skip to content

Implemented version checking for the discord bot and increased versio… #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}