Add bot playing status with online player count

This commit is contained in:
Erinç Fırtına 2025-07-08 18:38:42 +03:00
parent 4e6668c2e5
commit 9bfd041585
No known key found for this signature in database
GPG key ID: 3A247D1CAD535F55
2 changed files with 151 additions and 139 deletions

1
.gitignore vendored
View file

@ -6,3 +6,4 @@ config.json
config.prod.json
config.test.json
config.dev.json
.DS_Store

View file

@ -1,4 +1,4 @@
import {Client, EmbedBuilder, Snowflake, TextChannel} from "discord.js";
import { ActivityType, Client, EmbedBuilder, PresenceUpdateStatus, Snowflake, TextChannel } from "discord.js";
import Configuration from "./Configuration";
import ServerStatusFeed from "./ServerStatusFeed";
import { Logger } from "winston";
@ -34,13 +34,18 @@ export default class DiscordEmbed {
try {
await this.serverStatsFeed.updateServerFeed();
if (this.serverStatsFeed.isFetching()) {
this.appLogger.info('Server status feed is still fetching, try again...');
this.appLogger.info("Server status feed is still fetching, try again...");
setTimeout(() => {
this.updateDiscordEmbed();
}, 1000);
return;
}
this.discordAppClient.channels.fetch(this.appConfiguration.discord.channelId as Snowflake).then(async channel => {
this.discordAppClient.user?.setActivity({
name: `with ${this.serverStatsFeed.getPlayerCount() ?? 0} players`,
type: ActivityType.Playing,
});
this.discordAppClient.channels.fetch(this.appConfiguration.discord.channelId as Snowflake).then(async (channel) => {
/**
* Send the initial message to the channel (if the first message id is not set) or
* the message is meanwhile deleted
@ -48,18 +53,21 @@ export default class DiscordEmbed {
*/
let sendInitialMessage = (embedMessage: EmbedBuilder) => {
// noinspection JSAnnotator
(channel as TextChannel).send({embeds: [embedMessage]}).then(message => {
(channel as TextChannel).send({ embeds: [embedMessage] }).then((message) => {
this.firstMessageId = message.id;
});
};
this.generateEmbedFromStatusFeed(this.serverStatsFeed).then(embedMessage => {
this.generateEmbedFromStatusFeed(this.serverStatsFeed).then((embedMessage) => {
if (this.firstMessageId !== null) {
(channel as TextChannel).messages.fetch(this.firstMessageId).then(message => {
(channel as TextChannel).messages
.fetch(this.firstMessageId)
.then((message) => {
this.appLogger.info(`Message found, editing message with new embed`);
message.edit({ embeds: [embedMessage] });
}).catch(() => {
this.appLogger.warn('Message not found, sending new message');
})
.catch(() => {
this.appLogger.warn("Message not found, sending new message");
sendInitialMessage(embedMessage);
});
} else {
@ -84,8 +92,8 @@ export default class DiscordEmbed {
private async deleteAllMessages(): Promise<boolean> {
let textChannel = this.discordAppClient.channels.cache.get(this.appConfiguration.discord.channelId as Snowflake) as TextChannel;
this.appLogger.info(`Deleting all messages in discord text channel ${textChannel.id}`);
textChannel.messages.fetch().then(messages => {
messages.forEach(message => {
textChannel.messages.fetch().then((messages) => {
messages.forEach((message) => {
message.delete();
});
});
@ -99,7 +107,7 @@ export default class DiscordEmbed {
* @returns The truncated string
*/
private async truncateText(text: string, maxLength = 1024): Promise<string> {
return text.length > maxLength ? text.slice(0, maxLength - 3) + '...' : text;
return text.length > maxLength ? text.slice(0, maxLength - 3) + "..." : text;
}
/**
@ -112,12 +120,12 @@ export default class DiscordEmbed {
embed.setTitle(config.translation.discordEmbed.title);
if (!serverStats.isOnline()) {
embed.setColor(0xCA0000);
embed.setColor(0xca0000);
embed.setDescription(config.translation.discordEmbed.descriptionOffline);
} else if (serverStats.isFetching()) {
embed.setDescription(config.translation.discordEmbed.descriptionUnknown);
} else {
embed.setColor(0x00CA00);
embed.setColor(0x00ca00);
embed.setDescription(config.translation.discordEmbed.descriptionOnline);
embed.setTimestamp(new Date());
embed.setThumbnail(config.application.serverMapUrl);
@ -128,7 +136,10 @@ export default class DiscordEmbed {
if (serverStats.getPlayerList().length === 0) {
playerListString = config.translation.discordEmbed.noPlayersOnline;
} else {
playerListString = serverStats.getPlayerList().map(p => p.username).join(', ');
playerListString = serverStats
.getPlayerList()
.map((p) => p.username)
.join(", ");
}
let serverPassword = config.application.serverPassword;
@ -139,7 +150,7 @@ export default class DiscordEmbed {
let serverMods = serverStats.getServerMods();
let serverModsText = "-/-";
if (serverMods.length > 0) {
serverModsText = await this.truncateText(serverMods.map(mod => `${mod.name}`).join(', '));
serverModsText = await this.truncateText(serverMods.map((mod) => `${mod.name}`).join(", "));
}
// @ts-ignore
@ -151,8 +162,8 @@ export default class DiscordEmbed {
{ name: config.translation.discordEmbed.titleServerMods, value: serverModsText },
{
name: playerListTitleString,
value: playerListString
},
value: playerListString,
}
);
}
return embed;