mirror of
https://github.com/cloudmaker97/FS25-Discord-Bot.git
synced 2025-12-06 00:18:34 +00:00
Merge 54e21b8029 into ed492e9045
This commit is contained in:
commit
f66634b62e
2 changed files with 151 additions and 139 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -6,3 +6,4 @@ config.json
|
|||
config.prod.json
|
||||
config.test.json
|
||||
config.dev.json
|
||||
.DS_Store
|
||||
|
|
@ -1,160 +1,171 @@
|
|||
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";
|
||||
import { Logger } from "winston";
|
||||
import Logging from "./Logging";
|
||||
|
||||
export default class DiscordEmbed {
|
||||
private appLogger: Logger;
|
||||
private discordAppClient: Client;
|
||||
private appConfiguration: Configuration;
|
||||
private serverStatsFeed: ServerStatusFeed;
|
||||
private firstMessageId: Snowflake | null = null;
|
||||
private appLogger: Logger;
|
||||
private discordAppClient: Client;
|
||||
private appConfiguration: Configuration;
|
||||
private serverStatsFeed: ServerStatusFeed;
|
||||
private firstMessageId: Snowflake | null = null;
|
||||
|
||||
public constructor(discordAppClient: Client) {
|
||||
this.appLogger = Logging.getLogger();
|
||||
this.discordAppClient = discordAppClient;
|
||||
this.appConfiguration = new Configuration();
|
||||
this.serverStatsFeed = new ServerStatusFeed();
|
||||
public constructor(discordAppClient: Client) {
|
||||
this.appLogger = Logging.getLogger();
|
||||
this.discordAppClient = discordAppClient;
|
||||
this.appConfiguration = new Configuration();
|
||||
this.serverStatsFeed = new ServerStatusFeed();
|
||||
|
||||
(async () => {
|
||||
// Delete all messages in the channel
|
||||
await this.deleteAllMessages();
|
||||
// Start the update loop, which updates the discord embed every x seconds itself
|
||||
await this.updateDiscordEmbed();
|
||||
})();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the discord embed with the server status, player list and server time
|
||||
* This method is called every x seconds to update the discord embed.
|
||||
* @private
|
||||
*/
|
||||
private async updateDiscordEmbed(): Promise<void> {
|
||||
try {
|
||||
await this.serverStatsFeed.updateServerFeed();
|
||||
if(this.serverStatsFeed.isFetching()) {
|
||||
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 => {
|
||||
/**
|
||||
* Send the initial message to the channel (if the first message id is not set) or
|
||||
* the message is meanwhile deleted
|
||||
* @param embedMessage
|
||||
*/
|
||||
let sendInitialMessage = (embedMessage: EmbedBuilder) => {
|
||||
// noinspection JSAnnotator
|
||||
(channel as TextChannel).send({embeds: [embedMessage]}).then(message => {
|
||||
this.firstMessageId = message.id;
|
||||
});
|
||||
};
|
||||
|
||||
this.generateEmbedFromStatusFeed(this.serverStatsFeed).then(embedMessage => {
|
||||
if (this.firstMessageId !== null) {
|
||||
(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');
|
||||
sendInitialMessage(embedMessage);
|
||||
});
|
||||
} else {
|
||||
this.appLogger.info(`No message found, sending new message`);
|
||||
sendInitialMessage(embedMessage);
|
||||
}
|
||||
});
|
||||
});
|
||||
} catch (exception) {
|
||||
this.appLogger.error(exception);
|
||||
}
|
||||
(async () => {
|
||||
// Delete all messages in the channel
|
||||
await this.deleteAllMessages();
|
||||
// Start the update loop, which updates the discord embed every x seconds itself
|
||||
await this.updateDiscordEmbed();
|
||||
})();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the discord embed with the server status, player list and server time
|
||||
* This method is called every x seconds to update the discord embed.
|
||||
* @private
|
||||
*/
|
||||
private async updateDiscordEmbed(): Promise<void> {
|
||||
try {
|
||||
await this.serverStatsFeed.updateServerFeed();
|
||||
if (this.serverStatsFeed.isFetching()) {
|
||||
this.appLogger.info("Server status feed is still fetching, try again...");
|
||||
setTimeout(() => {
|
||||
this.updateDiscordEmbed();
|
||||
}, this.appConfiguration.application.updateIntervalSeconds * 1000);
|
||||
}
|
||||
this.updateDiscordEmbed();
|
||||
}, 1000);
|
||||
return;
|
||||
}
|
||||
this.discordAppClient.user?.setActivity({
|
||||
name: `with ${this.serverStatsFeed.getPlayerCount() ?? 0} players`,
|
||||
type: ActivityType.Playing,
|
||||
});
|
||||
|
||||
/**
|
||||
* Delete all messages in a text channel to clear the channel
|
||||
* @private
|
||||
*/
|
||||
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 => {
|
||||
message.delete();
|
||||
});
|
||||
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
|
||||
* @param embedMessage
|
||||
*/
|
||||
let sendInitialMessage = (embedMessage: EmbedBuilder) => {
|
||||
// noinspection JSAnnotator
|
||||
(channel as TextChannel).send({ embeds: [embedMessage] }).then((message) => {
|
||||
this.firstMessageId = message.id;
|
||||
});
|
||||
};
|
||||
|
||||
this.generateEmbedFromStatusFeed(this.serverStatsFeed).then((embedMessage) => {
|
||||
if (this.firstMessageId !== null) {
|
||||
(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");
|
||||
sendInitialMessage(embedMessage);
|
||||
});
|
||||
} else {
|
||||
this.appLogger.info(`No message found, sending new message`);
|
||||
sendInitialMessage(embedMessage);
|
||||
}
|
||||
});
|
||||
return true;
|
||||
});
|
||||
} catch (exception) {
|
||||
this.appLogger.error(exception);
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncates a string at a given length
|
||||
* @param text The input text to truncate
|
||||
* @param maxLength The allowed characters until truncation
|
||||
* @returns The truncated string
|
||||
*/
|
||||
private async truncateText(text: string, maxLength = 1024): Promise<string> {
|
||||
return text.length > maxLength ? text.slice(0, maxLength - 3) + '...' : text;
|
||||
}
|
||||
setTimeout(() => {
|
||||
this.updateDiscordEmbed();
|
||||
}, this.appConfiguration.application.updateIntervalSeconds * 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send server stats embed in a channel
|
||||
* @param serverStats
|
||||
*/
|
||||
private async generateEmbedFromStatusFeed(serverStats: ServerStatusFeed): Promise<EmbedBuilder> {
|
||||
let embed = new EmbedBuilder();
|
||||
let config = this.appConfiguration;
|
||||
/**
|
||||
* Delete all messages in a text channel to clear the channel
|
||||
* @private
|
||||
*/
|
||||
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) => {
|
||||
message.delete();
|
||||
});
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
embed.setTitle(config.translation.discordEmbed.title);
|
||||
if (!serverStats.isOnline()) {
|
||||
embed.setColor(0xCA0000);
|
||||
embed.setDescription(config.translation.discordEmbed.descriptionOffline);
|
||||
} else if (serverStats.isFetching()) {
|
||||
embed.setDescription(config.translation.discordEmbed.descriptionUnknown);
|
||||
} else {
|
||||
embed.setColor(0x00CA00);
|
||||
embed.setDescription(config.translation.discordEmbed.descriptionOnline);
|
||||
embed.setTimestamp(new Date());
|
||||
embed.setThumbnail(config.application.serverMapUrl);
|
||||
/**
|
||||
* Truncates a string at a given length
|
||||
* @param text The input text to truncate
|
||||
* @param maxLength The allowed characters until truncation
|
||||
* @returns The truncated string
|
||||
*/
|
||||
private async truncateText(text: string, maxLength = 1024): Promise<string> {
|
||||
return text.length > maxLength ? text.slice(0, maxLength - 3) + "..." : text;
|
||||
}
|
||||
|
||||
let playerListString: string;
|
||||
let playerListTitleString = `${config.translation.discordEmbed.titlePlayerCount} (${serverStats.getPlayerCount()??0}/${serverStats.getMaxPlayerCount()??0}):`;
|
||||
/**
|
||||
* Send server stats embed in a channel
|
||||
* @param serverStats
|
||||
*/
|
||||
private async generateEmbedFromStatusFeed(serverStats: ServerStatusFeed): Promise<EmbedBuilder> {
|
||||
let embed = new EmbedBuilder();
|
||||
let config = this.appConfiguration;
|
||||
|
||||
if(serverStats.getPlayerList().length === 0) {
|
||||
playerListString = config.translation.discordEmbed.noPlayersOnline;
|
||||
} else {
|
||||
playerListString = serverStats.getPlayerList().map(p => p.username).join(', ');
|
||||
}
|
||||
embed.setTitle(config.translation.discordEmbed.title);
|
||||
if (!serverStats.isOnline()) {
|
||||
embed.setColor(0xca0000);
|
||||
embed.setDescription(config.translation.discordEmbed.descriptionOffline);
|
||||
} else if (serverStats.isFetching()) {
|
||||
embed.setDescription(config.translation.discordEmbed.descriptionUnknown);
|
||||
} else {
|
||||
embed.setColor(0x00ca00);
|
||||
embed.setDescription(config.translation.discordEmbed.descriptionOnline);
|
||||
embed.setTimestamp(new Date());
|
||||
embed.setThumbnail(config.application.serverMapUrl);
|
||||
|
||||
let serverPassword = config.application.serverPassword;
|
||||
if(config.application.serverPassword == "") {
|
||||
serverPassword = "-/-";
|
||||
}
|
||||
let playerListString: string;
|
||||
let playerListTitleString = `${config.translation.discordEmbed.titlePlayerCount} (${serverStats.getPlayerCount() ?? 0}/${serverStats.getMaxPlayerCount() ?? 0}):`;
|
||||
|
||||
let serverMods = serverStats.getServerMods();
|
||||
let serverModsText = "-/-";
|
||||
if(serverMods.length > 0) {
|
||||
serverModsText = await this.truncateText(serverMods.map(mod => `${mod.name}`).join(', '));
|
||||
}
|
||||
if (serverStats.getPlayerList().length === 0) {
|
||||
playerListString = config.translation.discordEmbed.noPlayersOnline;
|
||||
} else {
|
||||
playerListString = serverStats
|
||||
.getPlayerList()
|
||||
.map((p) => p.username)
|
||||
.join(", ");
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
embed.addFields(
|
||||
{name: config.translation.discordEmbed.titleServerName, value: serverStats.getServerName()},
|
||||
{name: config.translation.discordEmbed.titleServerPassword, value: serverPassword},
|
||||
{name: config.translation.discordEmbed.titleServerTime, value: serverStats.getServerTime()},
|
||||
{name: config.translation.discordEmbed.titleServerMap, value: serverStats.getServerMap()},
|
||||
{name: config.translation.discordEmbed.titleServerMods, value: serverModsText},
|
||||
{
|
||||
name: playerListTitleString,
|
||||
value: playerListString
|
||||
},
|
||||
);
|
||||
let serverPassword = config.application.serverPassword;
|
||||
if (config.application.serverPassword == "") {
|
||||
serverPassword = "-/-";
|
||||
}
|
||||
|
||||
let serverMods = serverStats.getServerMods();
|
||||
let serverModsText = "-/-";
|
||||
if (serverMods.length > 0) {
|
||||
serverModsText = await this.truncateText(serverMods.map((mod) => `${mod.name}`).join(", "));
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
embed.addFields(
|
||||
{ name: config.translation.discordEmbed.titleServerName, value: serverStats.getServerName() },
|
||||
{ name: config.translation.discordEmbed.titleServerPassword, value: serverPassword },
|
||||
{ name: config.translation.discordEmbed.titleServerTime, value: serverStats.getServerTime() },
|
||||
{ name: config.translation.discordEmbed.titleServerMap, value: serverStats.getServerMap() },
|
||||
{ name: config.translation.discordEmbed.titleServerMods, value: serverModsText },
|
||||
{
|
||||
name: playerListTitleString,
|
||||
value: playerListString,
|
||||
}
|
||||
return embed;
|
||||
);
|
||||
}
|
||||
return embed;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue