Skip to content

Commit

Permalink
Refactor battlemetrics handling, big one
Browse files Browse the repository at this point in the history
  • Loading branch information
alexemanuelol committed Oct 10, 2023
1 parent 00813e8 commit 6ab07a4
Show file tree
Hide file tree
Showing 23 changed files with 1,142 additions and 631 deletions.
8 changes: 4 additions & 4 deletions docs/discord_text_channels.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

> The Server Channel lists all the paired Rust Servers. Given that you have setup your FCM Credentials properly, once you pair a Rust Server In-Game via `ESC -> Rust+ -> Pair With Server`, it should automatically appear in the `servers` channel. From there you can decide which server you want the bot to connect to by clicking the `CONNECT` button for that server.
The server embed displays a bunch of information. The title of the embed is the name of the server. The Battlemetrics Id is also displayed. The Description of the embed is basically the description of the Rust Server. Here you can also find the connect information that could be found in `information` channel. You can also see who is the hoster of the bot for the server.
The server embed displays a bunch of information. The title of the embed is the name of the server. The Battlemetrics Id is also displayed as well as if the server is streamer mode or not. The Description of the embed is basically the description of the Rust Server. Here you can also find the connect information that could be found in `information` channel. You can also see who is the hoster of the bot for the server.
<br>

There are a few buttons for each server. The `CONNECT` button lets you start a connection to the server. Once connected you can disconnect by clicking the `DISCONNECT` button. By clicking the `WEBSITE` button, you will be re-directed to the Rust Servers website. By clicking the `BATTLEMETRICS` button, you will be re-directed to the Battlemetrics page for the Rust Server. By clicking the `EDIT` button, you can change the `Battlemetrics Id` for the server. By clicking the `CUSTOM TIMERS` button, you can change Custom Timers for Cargoship egress time and time before a Locked Crate at Oil Rig is unlocked. By clicking the `CREATE TRACKER` button, you create a battlemetrics tracker that will appear in the `trackers` Text-Channel on Discord. By clicking the `CREATE GROUP` button, you create a Smart Switch Group that can be used to manage several Smart Switches at once. The Smart Switch Group will appear in the `switchgroups` Text-Channel on Discord. To remove the Server, just click the trashcan button.
Expand Down Expand Up @@ -127,12 +127,12 @@ There are a few buttons for each server. The `CONNECT` button lets you start a c

## Trackers Channel

> The Trackers Channel is used to keep track of players or groups on a specific server (Online/Offline/playtime). Given that the server does not use `Streamer mode`, it is possible to create a tracker from the server embed located in the `servers` channel. By clicking `CREATE TRACKER`, a new tracker appears in the trackers channel.
> The Trackers Channel is used to keep track of players or groups on a specific server (Online/Offline/playtime/offlinetime). To create a tracker, just click on the `CREATE TRACKER` button located in the `servers` channel. The new tracker will appear in the trackers channel.
The Tracker embed displays a few things. The Title of the embed is the name of the Tracker. The Tracker embed also displays the Battlemetrics Id of the tracker, potential Clan Tag setting and Server status. Under that, all players in the tracker are displayed. The `Names`, `SteamID` and `Status` of every player. The tracker also detects name changes made.
The Tracker embed displays a few things. The Title of the embed is the name of the Tracker. The Tracker embed also displays the Battlemetrics Id of the tracker, the Server ID, Server status, streamer mode on/off and potential Clan Tag setting. Under that, all players in the tracker are displayed. The `Names`, `Steam ID / Battlemetrics ID` and `Status` of every player.
<br>

By clicking the `ACTIVE` button, you decide if the tracker should be active (in use). By clicking the `@everyone` button, you decide if the @everyone tag should be used whenever a person connect/disconnect/someone join/all offline. By clicking the `EDIT` button, you can change the `Name` of the tracker, the `Battlemetrics Id` of the tracker and the `Clan Tag` for all the players in the tracker. By clicking the `ADD PLAYER` button, you can add a player to the tracker by providing the steamId. By clicking the `REMOVE PLAYER` button, you can remove a player from the tracker by providing the steamId. By clicking the `IN-GAME` button, you let the tracker notify in teamchat. To remove the tracker, click the trashcan button.
By clicking the `ADD PLAYER` button, you can add a player to the tracker by providing the steamId or battlemetrics player id. By clicking the `REMOVE PLAYER` button, you can remove a player from the tracker by providing the steamId or battlemetrics player id. To make the tracker more accurate and make it easier to detect name changes, we recommend using steamID for the tracker. By clicking the `EDIT` button, you can change the `Name` of the tracker, the `Battlemetrics Id` of the tracker and the `Clan Tag` for all the players in the tracker. To remove the tracker, click the trashcan button. By clicking the `IN-GAME` button, you let the tracker notify in teamchat. By clicking the `@everyone` button, you decide if the @everyone tag should be used whenever a person connect/disconnect.

![CREATE TRACKER](images/channels/tracker_create.png)
![Discord Text Channel Trackers](images/channels/trackers_channel.png)
Binary file modified docs/images/channels/servers_channel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/channels/tracker_create.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/channels/trackers_channel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion src/commands/credentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,14 @@ async function removeCredentials(client, interaction, verifyId) {
}

if (!steamId) {
steamId = Object.keys(credentials).find(e => credentials[e].discordUserId === interaction.member.user.id);
for (const credential of Object.keys(credentials)) {
if (credential === 'hoster') continue;

if (credentials[credential].discordUserId === interaction.member.user.id) {
steamId = credential;
break;
}
}
}

if (!(steamId in credentials)) {
Expand Down
47 changes: 27 additions & 20 deletions src/commands/players.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,52 +56,59 @@ module.exports = {
}

const battlemetricsId = instance.serverList[rustplus.serverId].battlemetricsId;
if (battlemetricsId === null) {
const str = client.intlGet(interaction.guildId, 'serverUsingStreamerMode');
await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str));
client.log(client.intlGet(null, 'warningCap'), str);
return;
}
const bmInstance = client.battlemetricsInstances[battlemetricsId];

if (!Object.keys(client.battlemetricsOnlinePlayers).includes(battlemetricsId)) {
const str = client.intlGet(interaction.guildId, 'couldNotFindPlayersForThisServer');
if (!bmInstance || !bmInstance.lastUpdateSuccessful) {
const str = client.intlGet(interaction.guildId, 'battlemetricsInstanceCouldNotBeFound', {
id: battlemetricsId
});
await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str));
client.log(client.intlGet(null, 'warningCap'), str);
return;
}

let foundPlayers = [];
if (name === null) {
foundPlayers = client.battlemetricsOnlinePlayers[battlemetricsId].slice();
foundPlayers = bmInstance.getOnlinePlayerIdsOrderedByTime();
if (foundPlayers.length === 0) {
const str = client.intlGet(interaction.guildId, 'couldNotFindAnyPlayers');
await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str));
client.log(client.intlGet(null, 'warningCap'), str);
return;
}
}
else {
for (const player of client.battlemetricsOnlinePlayers[battlemetricsId]) {
if (player.name.includes(name)) {
foundPlayers.push(player);
}
for (const playerId of bmInstance.getOnlinePlayerIdsOrderedByTime()) {
if (bmInstance.players[playerId]['name'].includes(name)) foundPlayers.push(playerId);
}

if (foundPlayers.length === 0) {
const str = client.intlGet(this.guildId, 'couldNotFindPlayer', { name: name });
await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str));
client.log(client.intlGet(null, 'warningCap'), str);
return;
}
}

const allPlayersLength = foundPlayers.length;
const playerColumns = ['', '', ''];
let playerIndex = 0;
let isFull = false;
for (const player of foundPlayers) {
const playerStr = `\`[${player.time}] ${player.name}\`\n`;
for (const playerId of foundPlayers) {
const time = bmInstance.getOnlineTime(playerId);
const playerString = `\`[${time[1]}] ${bmInstance.players[playerId]['name']}\`\n`;

if (playerColumns[playerIndex % 3].length + playerStr.length > 1024) {
if (playerColumns[playerIndex % 3].length + playerString.length > 1024) {
isFull = true;
break;
}

playerColumns[playerIndex % 3] += playerStr;
playerColumns[playerIndex % 3] += playerString;
playerIndex += 1;
}

let title = client.intlGet(interaction.guildId, 'onlinePlayers');
if (name !== null) {
title += ` '${name}'`;
}
if (name !== null) title += ` '${name}'`;

const embed = DiscordEmbeds.getEmbed({
title: title,
Expand Down
3 changes: 2 additions & 1 deletion src/discordEvents/ready.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ module.exports = {
await client.setupGuild(guild);
}

await client.updateBattlemetricsInstances();
BattlemetricsHandler.handler(client, true);
client.battlemetricsIntervalId = setInterval(BattlemetricsHandler.handler, 60000, client);
client.battlemetricsIntervalId = setInterval(BattlemetricsHandler.handler, 60000, client, false);

client.createRustplusInstancesFromConfig();
},
Expand Down
15 changes: 0 additions & 15 deletions src/discordTools/SetupSettingsMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,6 @@ async function setupGeneralSettings(client, guildId, channel) {
Path.join(__dirname, '..', 'resources/images/settings_logo.png'))]
});

await client.messageSend(channel, {
embeds: [DiscordEmbeds.getEmbed({
color: Constants.COLOR_SETTINGS,
title: client.intlGet(guildId, 'whenTrackersNotifySetting'),
thumbnail: `attachment://settings_logo.png`
})],
components: [DiscordButtons.getTrackerNotifyButtons(
guildId,
instance.generalSettings.trackerNotifyAllOffline,
instance.generalSettings.trackerNotifyAnyOnline,
instance.generalSettings.trackerNotifyInGameConnections)],
files: [new Discord.AttachmentBuilder(
Path.join(__dirname, '..', 'resources/images/settings_logo.png'))]
});

await client.messageSend(channel, {
embeds: [DiscordEmbeds.getEmbed({
color: Constants.COLOR_SETTINGS,
Expand Down
52 changes: 15 additions & 37 deletions src/discordTools/discordButtons.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,16 +404,14 @@ module.exports = {
return [
new Discord.ActionRowBuilder().addComponents(
module.exports.getButton({
customId: `TrackerActive${identifier}`,
label: tracker.active ?
Client.client.intlGet(guildId, 'activeCap') :
Client.client.intlGet(guildId, 'inactiveCap'),
style: tracker.active ? SUCCESS : DANGER
customId: `TrackerAddPlayer${identifier}`,
label: Client.client.intlGet(guildId, 'addPlayerCap'),
style: SUCCESS
}),
module.exports.getButton({
customId: `TrackerEveryone${identifier}`,
label: '@everyone',
style: tracker.everyone ? SUCCESS : DANGER
customId: `TrackerRemovePlayer${identifier}`,
label: Client.client.intlGet(guildId, 'removePlayerCap'),
style: DANGER
}),
module.exports.getButton({
customId: `TrackerEdit${identifier}`,
Expand All @@ -427,43 +425,23 @@ module.exports = {
})),
new Discord.ActionRowBuilder().addComponents(
module.exports.getButton({
customId: `TrackerAddPlayer${identifier}`,
label: Client.client.intlGet(guildId, 'addPlayerCap'),
style: SUCCESS
customId: `TrackerInGame${identifier}`,
label: Client.client.intlGet(guildId, 'inGameCap'),
style: tracker.inGame ? SUCCESS : DANGER
}),
module.exports.getButton({
customId: `TrackerRemovePlayer${identifier}`,
label: Client.client.intlGet(guildId, 'removePlayerCap'),
style: DANGER
customId: `TrackerEveryone${identifier}`,
label: '@everyone',
style: tracker.everyone ? SUCCESS : DANGER
}),
module.exports.getButton({
customId: `TrackerInGame${identifier}`,
label: Client.client.intlGet(guildId, 'inGameCap'),
style: tracker.inGame ? SUCCESS : DANGER
customId: `TrackerUpdate${identifier}`,
label: Client.client.intlGet(guildId, 'updateCap'),
style: PRIMARY
}))
];
},

getTrackerNotifyButtons: function (guildId, allOffline, anyOnline, inGameConnections) {
return new Discord.ActionRowBuilder().addComponents(
module.exports.getButton({
customId: 'TrackerNotifyAllOffline',
label: Client.client.intlGet(guildId, 'allOfflineCap'),
style: allOffline ? SUCCESS : DANGER
}),
module.exports.getButton({
customId: 'TrackerNotifyAnyOnline',
label: Client.client.intlGet(guildId, 'anyOnlineCap'),
style: anyOnline ? SUCCESS : DANGER
}),
module.exports.getButton({
customId: 'TrackerNotifyInGameConnections',
label: `${Client.client.intlGet(guildId, 'inGameCap')} ` +
`${Client.client.intlGet(guildId, 'connectionsCap')}`,
style: inGameConnections ? SUCCESS : DANGER
}));
},

getNewsButton: function (guildId, body, validURL) {
return new Discord.ActionRowBuilder().addComponents(
module.exports.getButton({
Expand Down
Loading

0 comments on commit 6ab07a4

Please sign in to comment.