@@ -5,11 +5,16 @@ import {ActivityType} from 'discord-api-types/v10';
55import { existsSync , mkdirSync , readFileSync , writeFileSync } from 'fs' ;
66import { MapsRepository } from '../../domain/maps-repository.js' ;
77
8+ export interface MessageFormats {
9+ playerCount : string ,
10+ queuedPlayers : string ,
11+ }
12+
813export class DiscordPublisher implements GameStatusPublisher {
914 private messageId : string | undefined ;
1015 private channel : TextChannel | undefined ;
1116
12- constructor ( private readonly client : Client , private readonly maps : MapsRepository ) {
17+ constructor ( private readonly client : Client , private readonly maps : MapsRepository , private readonly formats : MessageFormats ) {
1318 if ( ! process . env . DISCORD_MESSAGE_CHANNEL_ID ) {
1419 return
1520 }
@@ -65,32 +70,39 @@ export class DiscordPublisher implements GameStatusPublisher {
6570 text : 'Developed by FlorianSW' ,
6671 } ) ;
6772 if ( status === undefined ) {
68- await this . client . user ?. setPresence ( {
73+ this . client . user ?. setPresence ( {
6974 status : 'idle' ,
7075 activities : [ {
7176 type : ActivityType . Watching ,
7277 name : 'the server boot' ,
7378 } ] ,
7479 } ) ;
75- await this . client . user ?. setStatus ( 'dnd' ) ;
80+ this . client . user ?. setStatus ( 'dnd' ) ;
7681 embed
7782 . setColor ( Colors . DarkGrey )
7883 . setTitle ( 'Server is offline right now, waiting for first status' ) ;
7984 } else {
80- let name = status . playerCount + '/' + status . maxPlayers ;
85+ let message = this . formats . playerCount
86+ . replace ( '${playerCount}' , status . playerCount . toString ( ) )
87+ . replace ( '${maxPlayers}' , status . maxPlayers . toString ( ) ) ;
8188 if ( status . queuedPlayers ) {
82- name = `${ name } (+${ status . queuedPlayers } )` ;
89+ message = message . replace (
90+ '${queuedPlayersMessage}' ,
91+ this . formats . queuedPlayers . replace ( '${queuedPlayers}' , status . queuedPlayers . toString ( 10 ) )
92+ ) ;
93+ } else {
94+ message = message . replace ( '${queuedPlayersMessage}' , '' ) ;
8395 }
84- await this . client . user ?. setPresence ( {
96+ this . client . user ?. setPresence ( {
8597 status : 'online' ,
8698 activities : [ {
8799 type : ActivityType . Playing ,
88- name : name
100+ name : message
89101 } ]
90102 } ) ;
91103 const fields : EmbedField [ ] = [ {
92104 name : 'Players' ,
93- value : name ,
105+ value : message ,
94106 inline : false ,
95107 } ] ;
96108 if ( status . map ) {
0 commit comments