Skip to content

Commit dfc31a5

Browse files
committed
Add/remove smart devices based on connected server
1 parent 021b9dd commit dfc31a5

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

src/handlers/buttonHandler.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,25 @@ async function serverConnectButtonHandler(dm: DiscordManager, interaction: disco
403403
await discordMessages.sendServerMessage(dm, guildId, previousServerToView, connectionStatus);
404404
}
405405

406+
const creationPromises: Promise<void>[] = [];
407+
for (const content of Object.values(server.smartSwitchMap)) {
408+
creationPromises.push(discordMessages.sendSmartSwitchMessage(dm, guildId, serverId, content.entityId));
409+
}
410+
411+
for (const content of Object.values(server.smartAlarmMap)) {
412+
creationPromises.push(discordMessages.sendSmartAlarmMessage(dm, guildId, serverId, content.entityId));
413+
}
414+
415+
for (const content of Object.values(server.storageMonitorMap)) {
416+
creationPromises.push(
417+
discordMessages.sendStorageMonitorMessage(dm, guildId, serverId, content.entityId));
418+
}
419+
420+
for (const content of Object.values(server.smartSwitchGroupMap)) {
421+
// TODO! Create smartswitchgroup messages
422+
}
423+
await Promise.allSettled(creationPromises);
424+
406425
server.active = true;
407426
gim.updateGuildInstance(guildId);
408427

@@ -448,6 +467,36 @@ async function serverConnectingDisconnectReconnectingButtonHandler(dm: DiscordMa
448467
// TODO! Remove embeds from information channel if serverToView === null
449468
}
450469

470+
const deletionPromises: Promise<boolean>[] = [];
471+
for (const content of Object.values(server.smartSwitchMap)) {
472+
const channelId = gInstance.guildChannelIds.smartSwitches;
473+
if (channelId !== null && content.messageId !== null) {
474+
deletionPromises.push(dm.deleteMessage(guildId, channelId, content.messageId));
475+
}
476+
}
477+
478+
for (const content of Object.values(server.smartAlarmMap)) {
479+
const channelId = gInstance.guildChannelIds.smartAlarms;
480+
if (channelId !== null && content.messageId !== null) {
481+
deletionPromises.push(dm.deleteMessage(guildId, channelId, content.messageId));
482+
}
483+
}
484+
485+
for (const content of Object.values(server.storageMonitorMap)) {
486+
const channelId = gInstance.guildChannelIds.storageMonitors;
487+
if (channelId !== null && content.messageId !== null) {
488+
deletionPromises.push(dm.deleteMessage(guildId, channelId, content.messageId));
489+
}
490+
}
491+
492+
for (const content of Object.values(server.smartSwitchGroupMap)) {
493+
const channelId = gInstance.guildChannelIds.smartSwitchGroups;
494+
if (channelId !== null && content.messageId !== null) {
495+
deletionPromises.push(dm.deleteMessage(guildId, channelId, content.messageId));
496+
}
497+
}
498+
await Promise.allSettled(deletionPromises);
499+
451500
server.active = false;
452501
gim.updateGuildInstance(guildId);
453502

src/managers/fcmListenerManager.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,10 @@ async function pairingEntitySmartSwitch(flm: FcmListenerManager, steamId: types.
519519

520520
// TODO! Add the switch to rustplusManager
521521

522-
// TODO! Only send message if activeServerId is the same as serverId
523-
await discordMessages.sendSmartSwitchMessage(flm.dm, guildId, serverId, body.entityId);
522+
const rustPlusInstance = rpm.getInstance(guildId, serverId);
523+
if (rustPlusInstance && serverInfo.active) {
524+
await discordMessages.sendSmartSwitchMessage(flm.dm, guildId, serverId, body.entityId);
525+
}
524526
}
525527
}
526528

@@ -569,8 +571,10 @@ async function pairingEntitySmartAlarm(flm: FcmListenerManager, steamId: types.S
569571

570572
// TODO! Add the alarm to rustplusManager
571573

572-
// TODO! Only send message if activeServerId is the same as serverId
573-
await discordMessages.sendSmartAlarmMessage(flm.dm, guildId, serverId, body.entityId);
574+
const rustPlusInstance = rpm.getInstance(guildId, serverId);
575+
if (rustPlusInstance && serverInfo.active) {
576+
await discordMessages.sendSmartAlarmMessage(flm.dm, guildId, serverId, body.entityId);
577+
}
574578
}
575579
}
576580

@@ -617,8 +621,10 @@ async function pairingEntityStorageMonitor(flm: FcmListenerManager, steamId: typ
617621

618622
// TODO! Add the storagemonitor to rustplusManager
619623

620-
// TODO! Only send message if activeServerId is the same as serverId
621-
await discordMessages.sendStorageMonitorMessage(flm.dm, guildId, serverId, body.entityId);
624+
const rustPlusInstance = rpm.getInstance(guildId, serverId);
625+
if (rustPlusInstance && serverInfo.active) {
626+
await discordMessages.sendStorageMonitorMessage(flm.dm, guildId, serverId, body.entityId);
627+
}
622628
}
623629
}
624630

0 commit comments

Comments
 (0)