-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
101 lines (86 loc) · 2.57 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
require("dotenv").config();
const Discord = require("discord.js");
const client = new Discord.Client();
const yargs = require("yargs");
const fs = require("fs").promises;
const { join } = require("path");
const nono = require("./nono");
//TODO right now, the system will try to use the arduino whethere it is ready or not, so this needs improvement
// if (process.env.HAS_ARDUINO.toLowerCase() === "true") {
// const arduino = require("./arduino");
// }
yargs.scriptName("").help().wrap(60);
//TODO this needs simplification, but it works
//yeah, I know it's the receipe for it to stay like this forever
//<Kernighan's_Law>
const isJSFile = str => str.match(/.+\.js$/gi);
const isDirectory = str => !str.match(/.*\..*/gi);
const relativeJoin = paths => "./" + join(...paths); //path.join() doesn't keep "./" prefix but we need it
const filenameOf = str => str.match(/^.+\./gi).shift().split("").slice(0, -1).join("");
const buildCommands = async (path, node) => {
const files = await fs.readdir(path);
files.forEach(file => {
const fRelPath = relativeJoin([path, file]);
if (isJSFile(file)) {
let newCMD = require(fRelPath);
newCMD.command =
newCMD.command && newCMD.command.trim() !== ""
? filenameOf(file) + " " + newCMD.command
: filenameOf(file);
newCMD.type = "cmd";
node.commands.push(newCMD);
} else if (isDirectory(file)) {
let newDir = {
name: file,
type: "dir",
commands: [],
};
node.commands.push(newDir);
buildCommands(fRelPath, newDir);
}
});
};
const appendCmdNode = (commands, yargs) => {
commands.forEach(cmd => {
switch (cmd.type) {
case "cmd":
yargs.command(cmd);
break;
case "dir":
yargs.command({
command: cmd.name + " <subcommand>",
desc: "commands relative to " + cmd.name,
builder: yargs => {
appendCmdNode(cmd.commands, yargs);
return yargs;
},
});
break;
default:
throw "invalid node type in command tree";
}
});
};
let commandTree = {
name: "",
commands: [],
};
buildCommands("./commands", commandTree).then(() => {
appendCmdNode(commandTree.commands, yargs);
});
//</Kernighan's_Law>
client.on("ready", () => {
client.user.setActivity("https://github.com/AtelierNum/nono", {
type: "WATCHING",
});
client.on("message", msg => {
if (msg.author == client.user || msg.channel.name != process.env.INPUT_CHANNEL) {
return;
}
yargs.parse(msg.content, { msg: msg }, (err, argv, output) => {
nono.execPassiveListeners(argv);
if (output) msg.channel.send("```" + output + "```");
});
});
});
client.login(process.env.DISCORD_BOT_TOKEN);