File tree Expand file tree Collapse file tree 3 files changed +61
-1
lines changed Expand file tree Collapse file tree 3 files changed +61
-1
lines changed Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " ls25-discord-bot" ,
3
- "version" : " 0.1.5 " ,
3
+ "version" : " 0.1.6 " ,
4
4
"description" : " A simple discord bot for farming simulator 25" ,
5
5
"main" : " source/Main.ts" ,
6
6
"scripts" : {
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import {Client, IntentsBitField} from 'discord.js';
2
2
import Configuration from "./Services/Configuration" ;
3
3
import Logging from "./Services/Logging" ;
4
4
import DiscordService from "./Services/DiscordEmbed" ;
5
+ import VersionChecker from './Services/VersionChecker' ;
5
6
6
7
// Create a new logger instance and configuration instance
7
8
const appLogger = Logging . getLogger ( ) ;
@@ -20,6 +21,24 @@ if(!appConfig.isConfigurationValid()) {
20
21
process . exit ( 1 ) ;
21
22
}
22
23
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
+
23
42
/**
24
43
* Create a new discord client instance
25
44
*/
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments