Skip to content

Commit 1470f43

Browse files
committed
Support specifying the message format that is used
Fixes: #12
1 parent 313fe84 commit 1470f43

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

.env.example

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,15 @@ DISCORD_TOKEN=
4040
# and update messages in this channel. The status message might not contain all information depending on the player
4141
# count provider, the game and the used game server.
4242
DISCORD_MESSAGE_CHANNEL_ID=
43+
44+
45+
# Message formats
46+
# You can define your own message formats for the player count display in the user list in Discord.
47+
# Following variables are available:
48+
# - playerCount: The current player count on the server
49+
# - maxPlayers: The maximum number of players that can be on the server
50+
# - queuedPlayersMessage: The message as defined in DISCORD_PUBLISHER_MESSAGE_QUEUED_FORMAT (only makes sense when the server supports a player queue
51+
# - queuedPlayers: The number of players currently in the queue
52+
# Example (uncomment if editing):
53+
# DISCORD_PUBLISHER_MESSAGE_FORMAT=${playerCount}/${maxPlayers} ${queuedPlayersMessage}
54+
# DISCORD_PUBLISHER_MESSAGE_QUEUED_FORMAT=(+${queuedPlayers})

src/adapter/discord/discord-publisher.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('DiscordPublisher', () => {
1818
});
1919
client.login(process.env.DISCORD_TOKEN || '');
2020

21-
publisher = new DiscordPublisher(client, new NoOpMapsRepository());
21+
publisher = new DiscordPublisher(client, new NoOpMapsRepository(), {playerCount: '', queuedPlayers: ''});
2222
});
2323

2424
afterEach(() => {

src/adapter/discord/discord-publisher.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ import {ActivityType} from 'discord-api-types/v10';
55
import {existsSync, mkdirSync, readFileSync, writeFileSync} from 'fs';
66
import {MapsRepository} from '../../domain/maps-repository.js';
77

8+
export interface MessageFormats {
9+
playerCount: string,
10+
queuedPlayers: string,
11+
}
12+
813
export 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) {

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ class App {
2121
public async setup() {
2222
this.client = await this.createDiscordClient();
2323
try {
24-
const publisher = new DiscordPublisher(this.client, new FileBackedMapRepository());
24+
const publisher = new DiscordPublisher(this.client, new FileBackedMapRepository(), {
25+
playerCount: process.env.DISCORD_PUBLISHER_MESSAGE_FORMAT || '${playerCount}/${status.maxPlayers} $queuedPlayers',
26+
queuedPlayers: process.env.DISCORD_PUBLISHER_MESSAGE_FORMAT || '(+${queuedPlayers})',
27+
});
2528
this.useCase = new ProvideGameStatus(providerFactory().build(), publisher);
2629
} catch (e) {
2730
this.client?.destroy();

0 commit comments

Comments
 (0)