-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
160 lines (144 loc) · 6.04 KB
/
utils.js
File metadata and controls
160 lines (144 loc) · 6.04 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const SlashArgType = require('discord-api-types/v10').ApplicationCommandOptionType;
const { argString } = require('./commands/commands.js');
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error%3E#custom_error_types
class LegacyParserError extends Error {
constructor(...params) {
// Pass remaining arguments (including vendor specific ones) to parent constructor
super(...params);
// Maintains proper stack trace for where our error was thrown (only available on V8)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, LegacyParserError);
}
this.name = 'LegacyParserError';
}
}
class LegacyCommandOptions {
args = {};
subcommand;
#message;
#opts;
#users = [];
#attachments;
constructor(opts, message, varargs) {
this.#opts = opts;
this.#message = message;
this.#attachments = message.attachments.map((v) => v);
const s = message.content;
const [, ...args] = s.slice(1).split(/ +/gi);
const optsToParse = opts.slice();
while (optsToParse.length != 0) {
const currentOpt = optsToParse.shift();
if (currentOpt.required && (currentOpt.type == SlashArgType.Attachment ? this.#attachments.length == 0 : args.length == 0)) throw new LegacyParserError(`Not enough arguments. Expected: ${argString(opts)}`);
if (args.length == 0) break;
const currentArg = args.shift();
if (currentOpt.type == SlashArgType.Subcommand) {
// Grab all the subcommand options
const possibleSubcommands = [currentOpt];
while (optsToParse[0]?.type == SlashArgType.Subcommand) possibleSubcommands.push(optsToParse.shift());
// Get matching subcommand
const subcommands = possibleSubcommands.filter((sc) => sc.name.startsWith(currentArg));
if (subcommands.length != 1) throw new LegacyParserError(`Could not uniquely identify a subcommand. Options are: ${possibleSubcommands.map((c) => c.name).join(', ')}`);
const subcommand = subcommands.shift();
this.subcommand = subcommand.name;
subcommand.options.forEach((opt) => {
if (args.length == 0 && opt.required) throw new LegacyParserError(`Not enough arguments for subcommand \`${subcommand.name}\`. Expected: ${argString(opts)}`);
if (args.length != 0) {
const v = this.#processType(opt.type, args.shift());
if (!v) throw new LegacyParserError(`Error parsing argument \`${opt.name}\``);
this.args[opt.name] = v;
}
});
} else {
const v = this.#processType(currentOpt.type, currentArg);
if (!v) throw new LegacyParserError(`Error parsing argument \`${currentOpt.name}\``);
this.args[currentOpt.name] = v;
}
}
if (varargs) {
this.args.varargs = args;
} else if (args.length != 0) {
throw new LegacyParserError(`Too many arguments. Expected: ${argString(opts)}`);
}
}
get data() {
return Object.entries(this.args).map(([k, v]) => ({ name: k, value: v, type: this.#opts.find((opt) => opt.name == k).type }));
}
get resolved() {
return {
users: this.#users
};
}
getVarargs() {
return this.args.varargs || [];
}
getString(k) {
if (this.#optTypeMatch(SlashArgType.String, k)) return this.args[k];
}
getMember(k) {
if (this.#optTypeMatch(SlashArgType.User, k)) return this.args[k];
}
getInteger(k) {
if (this.#optTypeMatch(SlashArgType.Integer, k)) return this.args[k];
}
getAttachment(k) {
if (this.#optTypeMatch(SlashArgType.Attachment, k)) return this.args[k];
}
getSubcommand() {
return this.subcommand;
}
#optTypeMatch(expectedType, k) {
return this.#opts.find((opt) => opt.name == k)?.type == expectedType
|| (this.getSubcommand() && this.#opts.find((opt) => opt.name == this.getSubcommand()).options.find((opt) => opt.name == k)?.type == expectedType);
}
#processType(type, value) {
switch (type) {
case SlashArgType.User: {
const member = this.#message.guild.findMember(value);
if (!member) throw new LegacyParserError(`User \`${value}\` not found`);
this.#users.push(member);
return member;
}
case SlashArgType.String: return value;
case SlashArgType.Integer: return parseInt(value);
case SlashArgType.Attachment: return this.#attachments.shift();
default: throw new Error('Unhandled type');
}
}
}
module.exports = {
slashArg(type, name, opts) {
const obj = {
name,
type,
...opts
};
if (type != SlashArgType.Subcommand) {
obj.required = opts.required === undefined ? true : opts.required;
}
return obj;
},
slashChoices(opts) {
if (Array.isArray(opts)) return opts.map((v) => ({ name: v, value: v.toLowerCase() }));
if (typeof(opts) == 'object') return Object.entries(opts).map(([k, v]) => ({ name: k, value: v }));
},
slashCommandJSON(obj, guild) {
const name = obj.slashCommandName || [obj.name, ...(obj.alias || [])].reduce((i, acc) => i.length < acc.length ? i : acc);
const jsons = [{
name,
type: 1,
description: obj.description,
// eslint-disable-next-line camelcase
guild_id: guild.id, /* Not currently necessary bc we use the per-guild command creation endpoint, but just in case */
options: obj.args
}];
if (obj.userCommand) {
jsons.push({
name,
type: 2
});
}
return jsons;
},
LegacyCommandOptions,
LegacyParserError
};