-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.js
67 lines (60 loc) · 1.44 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const { exec } = require("child_process");
const pingHost = async (host) => {
console.log("pingHost", host);
return true;
// return new Promise((resolve, reject) => {
// exec(`ping -c 1 ${host.trim()}`, (error, stdout, stderr) => {
// if (error) {
// reject(new Error(`Ping host failed\n${stderr}`));
// } else {
// resolve(stdout);
// }
// });
// });
};
const isBotCommand = (message) => {
if (!message || !message.entities) {
return false;
}
const botCommands = message.entities.filter(
(entity) => entity.type === "bot_command"
);
return botCommands.length > 0;
};
const validateServerData = (server) => {
if (typeof server === "string" || typeof server === "undefined") return false;
return true;
};
const trimQuotes = (str) => {
return str.replace(/^['"]+|['"]+$/g, "");
};
const parseInput = (
input,
key = {
password: "-pass",
port: "-p",
note: "-n",
pathPrivateKey: "-pri",
keyPassword: "-keypass",
}
) => {
const arguments = input
.trim()
.split(/(?= -\w)/)
.map((arg) => arg.trim());
let data = {};
arguments.forEach((arg) => {
Object.entries(key).forEach(([option, flag]) => {
if (arg.startsWith(flag + " ")) {
data[option] = trimQuotes(arg?.split(flag + " ")[1].trim());
}
});
});
return { ...data, _args: arguments };
};
module.exports = {
isBotCommand,
validateServerData,
parseInput,
pingHost,
};