Repository navigation
Expand file tree
/
Copy pathSlashCommandBase.js
More file actions
127 lines (107 loc) · 3.1 KB
/
Copy pathSlashCommandBase.js
File metadata and controls
127 lines (107 loc) · 3.1 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
const { getRegisteredSlashCommands } = require('./SlashCommandRegistry.js');
class SlashCommandBase {
static get name() {
throw new Error('Name not implemented');
}
static get aliases() {
return [];
}
static get description() {
throw new Error('Description not implemented');
}
static get args() {
throw new Error('Args not implemented');
// Follow this format:
/*
return [
{ name: 'amount', type: 'integer', required: true },
{ name: 'character', type: 'string', required: false }
]
*/
}
static get usage() {
// Assemble a usage string based on the args
const argsUsage = this.args.map(arg => {
if (arg.required) {
return `<${arg.name}>`;
} else {
return `[${arg.name}]`;
}
}).join(' ');
return `/${this.name} ${argsUsage}`;
}
static validateArgs(providedArgs) {
const argsDef = this.args || [];
const errors = [];
for (const argDef of argsDef) {
const { name, type, required } = argDef;
const value = providedArgs[name];
if (required && (value === undefined || value === null)) {
errors.push(`Missing required argument: ${name}`);
continue;
}
if (value !== undefined && value !== null) {
switch (type) {
case 'string':
if (typeof value !== 'string') {
errors.push(`Argument "${name}" must be a string.`);
}
break;
case 'integer':
if (!Number.isInteger(value)) {
errors.push(`Argument "${name}" must be an integer.`);
}
break;
case 'boolean':
if (typeof value !== 'boolean') {
errors.push(`Argument "${name}" must be a boolean.`);
}
break;
default:
errors.push(`Unknown type "${type}" for argument "${name}".`);
}
}
}
return errors;
}
static listCommands() {
const commands = [];
const entries = getRegisteredSlashCommands();
for (const entry of entries) {
if (!entry || entry.isAlias) {
continue;
}
const CommandModule = entry.module;
if (!CommandModule) {
continue;
}
const commandName = typeof entry.canonicalName === 'string' && entry.canonicalName.trim()
? entry.canonicalName.trim()
: (typeof CommandModule.name === 'string' ? CommandModule.name : null);
if (!commandName) {
continue;
}
let description = '';
try {
const rawDescription = CommandModule.description;
description = typeof rawDescription === 'string' ? rawDescription : '';
} catch (_) {
description = '';
}
let usage;
try {
usage = typeof CommandModule.usage === 'string' ? CommandModule.usage : `/${commandName}`;
} catch (_) {
usage = `/${commandName}`;
}
commands.push({
name: commandName,
description,
usage
});
}
commands.sort((a, b) => a.name.localeCompare(b.name));
return commands;
}
}
module.exports = SlashCommandBase;