Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ping check after wake #4

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "wol-discord",
"version": "0.0.2",
"description": "",
"version": "0.0.3",
"description": "Discord bot that can wake a device with Wake-On-Lan",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
Expand All @@ -18,7 +18,7 @@
"url": "git+https://github.com/testsnake/WOL-Discord.git"
},
"author": "testsnake",
"license": "ISC",
"license": "MIT",
"bugs": {
"url": "https://github.com/testsnake/WOL-Discord/issues"
},
Expand Down
41 changes: 39 additions & 2 deletions src/commands/wol.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { ApplicationCommandOptionType, CommandInteraction } from 'discord.js';
import { Discord, Guard, Slash, SlashOption } from 'discordx';

import { devicePermission, wake } from '../deviceManager';
import { devicePermission, wake, sendPing, ActionResult } from '../deviceManager';
import { RateLimit, TIME_UNIT } from '@discordx/utilities';
import { deviceAutoComplete, rateLimitMessage } from '../utils';
import { commandLocalisation, commandDescription } from '../utils';
import { getTfunc } from '../i18n';
import config from '../config.json';
import logger from '../logger';

const requiredPermissions: devicePermission = {
wol: true
};

const requiredPingPermissions: devicePermission = {
ping: true
};

@Discord()
@Guard(
RateLimit(TIME_UNIT.seconds, 30, {
Expand Down Expand Up @@ -41,8 +46,9 @@ export class WakeOnLan {
searchText: string,
interaction: CommandInteraction
): Promise<void> {
const isEphemeral = interaction.guild ? true : false;
// Command Handler
await interaction.deferReply({ ephemeral: true });
await interaction.deferReply({ ephemeral: isEphemeral });

const t = getTfunc(interaction.locale);

Expand All @@ -54,5 +60,36 @@ export class WakeOnLan {
mac: result.mac
})
);

if (config.checkPingAfterWake) {
// Wait 30 seconds, ping check
setTimeout(async () => {
const pingResult = await sendPing(searchText, interaction, requiredPingPermissions);
if (typeof pingResult !== 'object') {
if (pingResult === ActionResult.PermissionDenied) {
// Permission denied
return;
}
interaction.followUp({
content: t(`common:command.errorTitle`, {
error: t(`commands:ping.error`, { context: pingResult })
}),
ephemeral: isEphemeral
});
} else {
logger.debug(`${pingResult.alive}_false`);
interaction.followUp({
content: t(`commands:ping.deviceResponse`, {
context: `${pingResult.alive}_false`,
avg: pingResult.avg,
max: pingResult.max,
min: pingResult.min,
packetLoss: pingResult.packetLoss
}),
ephemeral: isEphemeral
});
}
}, config.waitTimeAfterWake * 1000);
}
}
}
4 changes: 3 additions & 1 deletion src/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
"allowCommandsInGuilds": true,
"showDeviceIP": true,
"showDeviceMac": true,
"devicesFilePath": "./devices.json"
"devicesFilePath": "./devices.json",
"checkPingAfterWake": true,
"waitTimeAfterWake": 30
}
Loading