Skip to content

Commit b61a5d5

Browse files
committed
Ping check after wake
1 parent f1db8b3 commit b61a5d5

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "wol-discord",
3-
"version": "0.0.2",
4-
"description": "",
3+
"version": "0.0.3",
4+
"description": "Discord bot that can wake a device with Wake-On-Lan",
55
"main": "index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
@@ -18,7 +18,7 @@
1818
"url": "git+https://github.com/testsnake/WOL-Discord.git"
1919
},
2020
"author": "testsnake",
21-
"license": "ISC",
21+
"license": "MIT",
2222
"bugs": {
2323
"url": "https://github.com/testsnake/WOL-Discord/issues"
2424
},

src/commands/wol.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import { ApplicationCommandOptionType, CommandInteraction } from 'discord.js';
22
import { Discord, Guard, Slash, SlashOption } from 'discordx';
33

4-
import { devicePermission, wake } from '../deviceManager';
4+
import { devicePermission, wake, sendPing, ActionResult } from '../deviceManager';
55
import { RateLimit, TIME_UNIT } from '@discordx/utilities';
66
import { deviceAutoComplete, rateLimitMessage } from '../utils';
77
import { commandLocalisation, commandDescription } from '../utils';
88
import { getTfunc } from '../i18n';
99
import config from '../config.json';
10+
import logger from '../logger';
1011

1112
const requiredPermissions: devicePermission = {
1213
wol: true
1314
};
1415

16+
const requiredPingPermissions: devicePermission = {
17+
ping: true
18+
};
19+
1520
@Discord()
1621
@Guard(
1722
RateLimit(TIME_UNIT.seconds, 30, {
@@ -41,8 +46,9 @@ export class WakeOnLan {
4146
searchText: string,
4247
interaction: CommandInteraction
4348
): Promise<void> {
49+
const isEphemeral = interaction.guild ? true : false;
4450
// Command Handler
45-
await interaction.deferReply({ ephemeral: interaction.guild ? true : false });
51+
await interaction.deferReply({ ephemeral: isEphemeral });
4652

4753
const t = getTfunc(interaction.locale);
4854

@@ -54,5 +60,36 @@ export class WakeOnLan {
5460
mac: result.mac
5561
})
5662
);
63+
64+
if (config.checkPingAfterWake) {
65+
// Wait 30 seconds, ping check
66+
setTimeout(async () => {
67+
const pingResult = await sendPing(searchText, interaction, requiredPingPermissions);
68+
if (typeof pingResult !== 'object') {
69+
if (pingResult === ActionResult.PermissionDenied) {
70+
// Permission denied
71+
return;
72+
}
73+
interaction.followUp({
74+
content: t(`common:command.errorTitle`, {
75+
error: t(`commands:ping.error`, { context: pingResult })
76+
}),
77+
ephemeral: isEphemeral
78+
});
79+
} else {
80+
logger.debug(`${pingResult.alive}_false`);
81+
interaction.followUp({
82+
content: t(`commands:ping.deviceResponse`, {
83+
context: `${pingResult.alive}_false`,
84+
avg: pingResult.avg,
85+
max: pingResult.max,
86+
min: pingResult.min,
87+
packetLoss: pingResult.packetLoss
88+
}),
89+
ephemeral: isEphemeral
90+
});
91+
}
92+
}, config.waitTimeAfterWake * 1000);
93+
}
5794
}
5895
}

src/config.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
"allowCommandsInGuilds": true,
33
"showDeviceIP": true,
44
"showDeviceMac": true,
5-
"devicesFilePath": "./devices.json"
5+
"devicesFilePath": "./devices.json",
6+
"checkPingAfterWake": true,
7+
"waitTimeAfterWake": 30
68
}

0 commit comments

Comments
 (0)