Skip to content

Commit

Permalink
Add button in servers channel to remove unreachable devices
Browse files Browse the repository at this point in the history
  • Loading branch information
alexemanuelol committed Sep 16, 2023
1 parent 2e02fe9 commit 00813e8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/discordTools/discordButtons.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ module.exports = {
});
}

const deleteUnreachableDevicesButton = module.exports.getButton({
customId: `DeleteUnreachableDevices${identifier}`,
label: Client.client.intlGet(guildId, 'deleteUnreachableDevicesCap'),
style: PRIMARY
});
const customTimersButton = module.exports.getButton({
customId: `CustomTimersEdit${identifier}`,
label: Client.client.intlGet(guildId, 'customTimersCap'),
Expand Down Expand Up @@ -123,6 +128,9 @@ module.exports = {
),
new Discord.ActionRowBuilder().addComponents(
customTimersButton, trackerButton, groupButton
),
new Discord.ActionRowBuilder().addComponents(
deleteUnreachableDevicesButton
)
];
}
Expand All @@ -133,6 +141,9 @@ module.exports = {
),
new Discord.ActionRowBuilder().addComponents(
customTimersButton, groupButton
),
new Discord.ActionRowBuilder().addComponents(
deleteUnreachableDevicesButton
)
];
}
Expand Down
56 changes: 55 additions & 1 deletion src/handlers/buttonHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,51 @@ module.exports = async (client, interaction) => {
const modal = DiscordModals.getServerEditModal(guildId, ids.serverId);
await interaction.showModal(modal);
}
else if (interaction.customId.startsWith('DeleteUnreachableDevices')) {
const ids = JSON.parse(interaction.customId.replace('DeleteUnreachableDevices', ''));
const server = instance.serverList[ids.serverId];

if (!server) {
await interaction.message.delete();
return;
}

interaction.deferUpdate();

const groupsToUpdate = [];
for (const [entityId, content] of Object.entries(server.switches)) {
if (!content.reachable) {
await DiscordTools.deleteMessageById(guildId, instance.channelId.switches, content.messageId);
delete server.switches[entityId];

for (const [groupId, groupContent] of Object.entries(server.switchGroups)) {
if (groupContent.switches.includes(`${entityId}`) && !groupsToUpdate.includes(groupId)) {
groupsToUpdate.push(groupId);
}
}
}
}

for (const groupId of groupsToUpdate) {
await DiscordMessages.sendSmartSwitchGroupMessage(guildId, ids.serverId, groupId);
}

for (const [entityId, content] of Object.entries(server.alarms)) {
if (!content.reachable) {
await DiscordTools.deleteMessageById(guildId, instance.channelId.alarms, content.messageId)
delete server.alarms[entityId];
}
}

for (const [entityId, content] of Object.entries(server.storageMonitors)) {
if (!content.reachable) {
await DiscordTools.deleteMessageById(guildId, instance.channelId.storageMonitors, content.messageId)
delete server.storageMonitors[entityId];
}
}

client.setInstance(guildId, instance);
}
else if (interaction.customId.startsWith('CustomTimersEdit')) {
const ids = JSON.parse(interaction.customId.replace('CustomTimersEdit', ''));
const server = instance.serverList[ids.serverId];
Expand Down Expand Up @@ -773,7 +818,16 @@ module.exports = async (client, interaction) => {
if (!rustplus || (rustplus && rustplus.serverId !== ids.serverId)) return;

const entityInfo = await rustplus.getEntityInfoAsync(ids.entityId);
if (!(await rustplus.isResponseValid(entityInfo))) return;
if (!(await rustplus.isResponseValid(entityInfo))) {
if (server.storageMonitors[ids.entityId].reachable) {
await DiscordMessages.sendStorageMonitorNotFoundMessage(guildId, ids.serverId, ids.entityId);
}
server.storageMonitors[ids.entityId].reachable = false;
client.setInstance(guildId, instance);

await DiscordMessages.sendStorageMonitorMessage(guildId, ids.serverId, ids.entityId);
return;
}

server.storageMonitors[ids.entityId].reachable = true;
client.setInstance(guildId, instance);
Expand Down
1 change: 1 addition & 0 deletions src/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@
"dayOfWipe": "Day {day}",
"deathCap": "DEATH",
"decayingCap": "DECAYING",
"deleteUnreachableDevicesCap": "DELETE UNREACHABLE DEVICES",
"deviceIsCurrentlyOnOff": "{device} is currently {status}.",
"deviceWasTurnedOnOff": "{device} was turned {status}.",
"disabledCap": "DISABLED",
Expand Down

0 comments on commit 00813e8

Please sign in to comment.