-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
116 lines (97 loc) · 3.07 KB
/
utils.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const config = require('./config');
const https = require('https');
const { host } = require('./consts');
const { log } = require('./logger');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { SlashCommandBuilder } = require('@discordjs/builders');
const rest = new REST({ version: '9' }).setToken(config.token);
let endpoints = [];
module.exports.get = ((endpoint, callback) => {
if (typeof callback == "undefined") throw new Error("callback needed");
const options = {
hostname: host,
port: 443,
path: endpoint,
method: "GET",
headers: { "Accept": "application/json" }
};
https.get(options, (res) => {
let buff = "";
res.on("data", (chunk) => {
buff += chunk;
}).on("end", () => {
var body;
try {
body = JSON.parse(buff);
} catch (e) {
callback(e)
};
callback(null, body)
})
})
})
module.exports.build = ((err, res) => {
if (err) return log.error(err.message);
log.info("Building commands...");
let cmds = [];
for (const op of res) {
if (op.name == "Version") continue;
else endpoints.push(op.url);
const name = op.url.split('/')[1];
let cmd = new SlashCommandBuilder()
.setName(name)
.setDescription(op.name.length > 100 ? `${op.name.substring(0, 97)}...` : op.name)
for (const f of op.fields) {
if (f.name == "From" || f.field == "from") continue;
switch (f.field) {
case "company":
case "name":
cmd.addUserOption(opt =>
opt.setName(f.field)
.setDescription(f.name)
.setRequired(true)
);
break;
default:
cmd.addStringOption(opt =>
opt.setName(f.field)
.setDescription(f.name)
.setRequired(true)
)
break;
}
}
cmds.push(cmd.toJSON());
}
this.create(cmds)
})
module.exports.create = (cmds) => {
log.info("Deploy commands...");
rest.put(Routes.applicationCommands(config.clientId), {body: cmds})
.then(() => log.info('Successfully registered application commands.'))
.catch(log.error)
}
module.exports.respond = (interaction) => {
const cmd = interaction.commandName;
const opt = interaction.options;
let endpoint = this.endpoints.find(v => v.startsWith(`/${cmd}/`))
if (!interaction.isCommand()) {
log.warn(`${interaction.user.tag} ${cmd}:${JSON.stringify(opt)}`, "NOT COMMAND")
return
}
if (typeof endpoint != "string") {
log.error(`${interaction.user.tag} ${cmd}:${JSON.stringify(opt)}`, "CAN'T GET ENDPOINT")
return
}
log.debug(`${interaction.user.tag} ${cmd}:${JSON.stringify(opt.data)}`, "INTERACTION")
endpoint = endpoint.replace(":from", new Date().getTime().toString())
for (const arg of opt.data) {
endpoint = endpoint.replace(`:${arg.name}`, arg.type == "USER" ? `<@${arg.value}>` : arg.value)
}
this.get(endpoint, (err, res) => {
if (err) return;
interaction.reply(`${res.message} — <@${interaction.user.id}>`);
})
}
module.exports.endpoints = endpoints;