nuxt-discord 0.0.12 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -9
- package/dist/module.d.mts +10 -5
- package/dist/module.json +1 -1
- package/dist/module.mjs +269 -51
- package/dist/runtime/client/pages/slash-commands.vue +16 -3
- package/dist/runtime/server/utils/client.d.ts +6 -4
- package/dist/runtime/server/utils/client.js +123 -49
- package/dist/runtime/server/utils/describeOption.d.ts +41 -8
- package/dist/runtime/server/utils/reply.d.ts +14 -14
- package/dist/runtime/server/utils/reply.js +54 -33
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -132,8 +132,6 @@ export default (name: string, style: string) => {
|
|
|
132
132
|
}
|
|
133
133
|
```
|
|
134
134
|
|
|
135
|
-
> **Coming Soon:** Support choices inferred from Typescript literal union types
|
|
136
|
-
|
|
137
135
|
## Web Interface
|
|
138
136
|
|
|
139
137
|
Access the command management interface at `/discord/slash-commands` in your application. The interface provides:
|
|
@@ -255,7 +253,7 @@ Here's what's planned for future releases:
|
|
|
255
253
|
- [ ] Better JSDocs support JSDoc comments
|
|
256
254
|
- [ ] Localization support
|
|
257
255
|
- [ ] File-based subcommands
|
|
258
|
-
- [
|
|
256
|
+
- [x] Auto-complete API
|
|
259
257
|
- [ ] Guild-specific commands
|
|
260
258
|
- [ ] Nuxt-devtools integration
|
|
261
259
|
- [ ] Unit tests
|
|
@@ -272,12 +270,7 @@ Here's what's planned for future releases:
|
|
|
272
270
|
|
|
273
271
|
## Contribution
|
|
274
272
|
|
|
275
|
-
|
|
276
|
-
<summary>Local development</summary>
|
|
277
|
-
|
|
278
|
-
Refer to the official [Module Author Guide](https://nuxt.com/docs/guide/going-further/modules)
|
|
279
|
-
|
|
280
|
-
</details>
|
|
273
|
+
Refer to the official [Module Author Guide](https://nuxt.com/docs/guide/going-further/modules)
|
|
281
274
|
|
|
282
275
|
## Acknowledgement
|
|
283
276
|
|
package/dist/module.d.mts
CHANGED
|
@@ -3,9 +3,10 @@ import * as __runtime_logger from '../dist/runtime/logger.js';
|
|
|
3
3
|
import * as _nuxt_kit from '@nuxt/kit';
|
|
4
4
|
import * as nuxt_schema from 'nuxt/schema';
|
|
5
5
|
import { DiscordClient } from '/Users/jeffz/Dev/nuxt-discord/dist/runtime/server/utils/client.js';
|
|
6
|
-
import { IntegerOption, NumberOption, StringOption } from '../dist/runtime/server/utils/describeOption.js';
|
|
6
|
+
import { IntegerOption, NumberOption, StringOption, DescribeOptionOptions } from '../dist/runtime/server/utils/describeOption.js';
|
|
7
7
|
import { ClientOptions, ApplicationCommandOptionType, ChatInputCommandInteraction } from 'discord.js';
|
|
8
8
|
import { ListenOptions } from 'listhen';
|
|
9
|
+
import { MaybeRef } from 'vue';
|
|
9
10
|
|
|
10
11
|
interface NuxtDiscordOptions {
|
|
11
12
|
/**
|
|
@@ -92,6 +93,7 @@ interface SlashCommandOptionBase {
|
|
|
92
93
|
description: string;
|
|
93
94
|
type: ApplicationCommandOptionType;
|
|
94
95
|
required: boolean;
|
|
96
|
+
hasAutocomplete: boolean;
|
|
95
97
|
}
|
|
96
98
|
interface SlashCommandIntegerOption extends SlashCommandOptionBase, Omit<IntegerOption, 'name' | 'description'> {
|
|
97
99
|
type: ApplicationCommandOptionType.Integer;
|
|
@@ -106,19 +108,22 @@ interface SlashCommandBooleanOption extends SlashCommandOptionBase {
|
|
|
106
108
|
type: ApplicationCommandOptionType.Boolean;
|
|
107
109
|
}
|
|
108
110
|
type SlashCommandOption = SlashCommandIntegerOption | SlashCommandNumberOption | SlashCommandStringOption | SlashCommandBooleanOption;
|
|
109
|
-
type SlashCommandCustomReturnHandler = (this: DiscordClient, interaction: ChatInputCommandInteraction, client: DiscordClient) =>
|
|
110
|
-
type SlashCommandReturnType = void | string | SlashCommandCustomReturnHandler | Promise<SlashCommandReturnType> | Generator<SlashCommandReturnType> | AsyncGenerator<SlashCommandReturnType>;
|
|
111
|
+
type SlashCommandCustomReturnHandler = (this: DiscordClient, interaction: ChatInputCommandInteraction, client: DiscordClient) => unknown;
|
|
112
|
+
type SlashCommandReturnType = void | MaybeRef<string> | SlashCommandCustomReturnHandler | Promise<SlashCommandReturnType> | Generator<SlashCommandReturnType> | AsyncGenerator<SlashCommandReturnType>;
|
|
111
113
|
interface SlashCommand {
|
|
112
114
|
name: string;
|
|
113
115
|
description: string;
|
|
114
116
|
path: string;
|
|
115
|
-
options: SlashCommandOption
|
|
117
|
+
options: (SlashCommandOption & {
|
|
118
|
+
varname: string;
|
|
119
|
+
})[];
|
|
116
120
|
}
|
|
117
121
|
interface SlashCommandRuntime extends SlashCommand {
|
|
118
122
|
/** if execute is not defined load must be defined */
|
|
119
|
-
load?: () => Promise<SlashCommandRuntime
|
|
123
|
+
load?: () => Promise<Pick<SlashCommandRuntime, 'execute' | 'optionMacros'>>;
|
|
120
124
|
execute?: (...args: (SlashCommandOptionType | undefined)[]) => SlashCommandReturnType;
|
|
121
125
|
id?: string;
|
|
126
|
+
optionMacros?: Record<string, DescribeOptionOptions>;
|
|
122
127
|
}
|
|
123
128
|
interface NuxtDiscordContext {
|
|
124
129
|
nuxt: nuxt_schema.Nuxt;
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import { ApplicationCommandOptionType, GatewayIntentBits } from 'discord.js';
|
|
|
3
3
|
import { logger } from '../dist/runtime/logger.js';
|
|
4
4
|
import fs, { globSync, readFileSync } from 'node:fs';
|
|
5
5
|
import path from 'node:path';
|
|
6
|
-
import ts from 'typescript';
|
|
6
|
+
import ts, { isFunctionExpression, isArrowFunction } from 'typescript';
|
|
7
7
|
import chokidar from 'chokidar';
|
|
8
8
|
import { listen } from 'listhen';
|
|
9
9
|
import { rollup } from 'rollup';
|
|
@@ -94,6 +94,8 @@ const macros = {
|
|
|
94
94
|
const macroOptions = parseLiteral(node.arguments[1]);
|
|
95
95
|
const validOptionKeys = ["name", "description", "min", "max", "minLength", "maxLength", "choices"];
|
|
96
96
|
const isValidOptionKey = (key) => validOptionKeys.includes(key);
|
|
97
|
+
Reflect.deleteProperty(macroOptions, "autocomplete");
|
|
98
|
+
option.hasAutocomplete = node.arguments[1].properties.find((prop) => ts.isPropertyAssignment(prop) && ts.isIdentifier(prop.name) && prop.name.escapedText === "autocomplete" || ts.isMethodDeclaration(prop) && ts.isIdentifier(prop.name) && prop.name.escapedText === "autocomplete") != null;
|
|
97
99
|
for (const key in macroOptions) {
|
|
98
100
|
if (!isValidOptionKey(key)) {
|
|
99
101
|
ctx.logger.warn(`describeOption: unknown option key "${key}" for option "${name}" in command "${command.name}"`);
|
|
@@ -147,30 +149,25 @@ function parseLiteral(node) {
|
|
|
147
149
|
return void 0;
|
|
148
150
|
}
|
|
149
151
|
|
|
150
|
-
function collectSlashCommands(ctx
|
|
152
|
+
function collectSlashCommands(ctx) {
|
|
151
153
|
const commandFiles = globSync(`${ctx.resolve.root(ctx.options.dir, "commands")}/**/*.ts`);
|
|
152
|
-
|
|
154
|
+
ctx.slashCommands = [];
|
|
153
155
|
for (const file of commandFiles) {
|
|
154
156
|
const command = processCommandFile(ctx, file);
|
|
155
157
|
if (command) {
|
|
156
|
-
|
|
158
|
+
ctx.slashCommands.push(command);
|
|
157
159
|
}
|
|
158
160
|
}
|
|
159
161
|
}
|
|
160
162
|
function processCommandFile(ctx, file) {
|
|
161
|
-
const sourceFile = ts.createSourceFile(
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
170
|
-
} else if (ts.isFunctionDeclaration(node) && !!(ts.getCombinedModifierFlags(node) & ts.ModifierFlags.ExportDefault)) {
|
|
171
|
-
commandDefinition = node;
|
|
172
|
-
}
|
|
173
|
-
});
|
|
163
|
+
const sourceFile = ts.createSourceFile(
|
|
164
|
+
file,
|
|
165
|
+
readFileSync(file).toString(),
|
|
166
|
+
ts.ScriptTarget.Latest,
|
|
167
|
+
/* setParentNodes */
|
|
168
|
+
true
|
|
169
|
+
);
|
|
170
|
+
const commandDefinition = extractCommandDefinition(sourceFile);
|
|
174
171
|
if (!commandDefinition) {
|
|
175
172
|
ctx.logger.warn(`No command definition found for ${file}`);
|
|
176
173
|
return;
|
|
@@ -193,39 +190,153 @@ function processCommandFile(ctx, file) {
|
|
|
193
190
|
ctx.logger.warn(`No body found for command definition in ${file}`);
|
|
194
191
|
return;
|
|
195
192
|
}
|
|
193
|
+
const whichLiteral = (node) => {
|
|
194
|
+
if (ts.isLiteralTypeNode(node)) {
|
|
195
|
+
if (ts.isStringLiteral(node.literal)) {
|
|
196
|
+
return "string";
|
|
197
|
+
}
|
|
198
|
+
if (ts.isNumericLiteral(node.literal)) {
|
|
199
|
+
return "number";
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
const getType = (type) => {
|
|
204
|
+
if (!type) {
|
|
205
|
+
ctx.logger.warn(`No type found for slash command option in ${file}, defaulting to string`);
|
|
206
|
+
return { type: "string" };
|
|
207
|
+
}
|
|
208
|
+
if (ts.isUnionTypeNode(type)) {
|
|
209
|
+
const types = new Set(type.types.map((t) => whichLiteral(t)));
|
|
210
|
+
if (types.size !== 1) {
|
|
211
|
+
ctx.logger.warn(`Union type with multiple conflicting types found in ${file}, defaulting to string`);
|
|
212
|
+
return { type: "string" };
|
|
213
|
+
}
|
|
214
|
+
const typeName2 = types.values().next().value;
|
|
215
|
+
if (!(typeName2 in typeIdentifierToEnum)) {
|
|
216
|
+
ctx.logger.warn(`Unrecognizable type ${type.getText(sourceFile)}, defaulting to string`);
|
|
217
|
+
return { type: "string" };
|
|
218
|
+
}
|
|
219
|
+
return {
|
|
220
|
+
type: typeName2,
|
|
221
|
+
choices: typeName2 === "string" ? type.types.map((t) => t.getText(sourceFile).slice(1, -1)) : type.types.map((t) => Number(t.getText(sourceFile)))
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
if (ts.isLiteralTypeNode(type)) {
|
|
225
|
+
const typeName2 = whichLiteral(type);
|
|
226
|
+
if (!typeName2) {
|
|
227
|
+
ctx.logger.warn(`Unrecognizable literal type ${type.getText(sourceFile)}, defaulting to string`);
|
|
228
|
+
return { type: "string" };
|
|
229
|
+
}
|
|
230
|
+
if (!(typeName2 in typeIdentifierToEnum)) {
|
|
231
|
+
ctx.logger.warn(`Unrecognizable literal type ${type.getText(sourceFile)}, defaulting to string`);
|
|
232
|
+
return { type: "string" };
|
|
233
|
+
}
|
|
234
|
+
return {
|
|
235
|
+
type: typeName2,
|
|
236
|
+
choices: typeName2 === "string" ? [type.literal.getText(sourceFile).slice(1, -1)] : [Number(type.literal.getText(sourceFile))]
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
const typeName = type.getText(sourceFile);
|
|
240
|
+
if (typeName && typeName in typeIdentifierToEnum) {
|
|
241
|
+
return { type: typeName };
|
|
242
|
+
}
|
|
243
|
+
ctx.logger.warn(`Unknown slash command option type: ${type.getText(sourceFile)} in ${file}, defaulting to string`);
|
|
244
|
+
return { type: "string" };
|
|
245
|
+
};
|
|
196
246
|
for (const param of commandDefinition.parameters) {
|
|
197
247
|
const name2 = param.name.getText(sourceFile);
|
|
198
|
-
let type = param.type
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
248
|
+
let { type, choices } = getType(param.type);
|
|
249
|
+
const jsDocTagIdx = jsDocTags.findIndex((tag) => ts.isJSDocParameterTag(tag) && tag.name.getText() === name2);
|
|
250
|
+
const findModifiers = (modifiers2) => {
|
|
251
|
+
let idx = jsDocTagIdx;
|
|
252
|
+
const ret = {};
|
|
253
|
+
while (idx >= 0) {
|
|
254
|
+
const tag = jsDocTags[idx];
|
|
255
|
+
if (modifiers2.includes(tag.tagName.escapedText) && tag.comment) {
|
|
256
|
+
ret[tag.tagName.escapedText] = JSON.parse(tag.comment.toString());
|
|
257
|
+
} else {
|
|
258
|
+
return ret;
|
|
259
|
+
}
|
|
260
|
+
idx -= 1;
|
|
261
|
+
}
|
|
262
|
+
return ret;
|
|
263
|
+
};
|
|
264
|
+
const jsdocDescription = jsDocTagIdx !== -1 ? jsDocTags[jsDocTagIdx].comment?.toString() ?? "" : "";
|
|
265
|
+
const modifiersMap = {
|
|
266
|
+
string: ["minLength", "maxLength", "choices"],
|
|
267
|
+
number: ["min", "max", "choices"],
|
|
268
|
+
integer: ["min", "max", "choices"],
|
|
269
|
+
boolean: []
|
|
270
|
+
};
|
|
271
|
+
const modifiers = findModifiers(modifiersMap[type]);
|
|
272
|
+
choices = choices ?? modifiers.choices;
|
|
204
273
|
command.options.push({
|
|
205
274
|
name: name2,
|
|
206
275
|
// TODO: remove this type assertion
|
|
207
276
|
type: typeIdentifierToEnum[type],
|
|
208
277
|
description: jsdocDescription,
|
|
209
|
-
required: !param.questionToken
|
|
278
|
+
required: !param.questionToken,
|
|
279
|
+
varname: name2,
|
|
280
|
+
hasAutocomplete: false,
|
|
281
|
+
...modifiers,
|
|
282
|
+
...choices ? {
|
|
283
|
+
choices: choices.map((choice) => {
|
|
284
|
+
if (typeof choice === "string") {
|
|
285
|
+
return { name: choice, value: choice };
|
|
286
|
+
}
|
|
287
|
+
return { name: String(choice), value: choice };
|
|
288
|
+
})
|
|
289
|
+
// hmm... is it possible to not use `any` here?
|
|
290
|
+
} : {}
|
|
210
291
|
});
|
|
211
292
|
}
|
|
293
|
+
const [macroCalls, outOfRangMacroCalls] = extractMacroCalls(commandDefinition.body);
|
|
294
|
+
for (const macroCall of macroCalls) {
|
|
295
|
+
const macroName = macroCall.expression.escapedText;
|
|
296
|
+
macros[macroName](ctx, macroCall, command);
|
|
297
|
+
}
|
|
298
|
+
if (outOfRangMacroCalls.length > 0) {
|
|
299
|
+
ctx.logger.warn(`Macros in file ${file} must be called at the top of the file`);
|
|
300
|
+
}
|
|
301
|
+
return command;
|
|
302
|
+
}
|
|
303
|
+
function extractCommandDefinition(sourceFile) {
|
|
304
|
+
let commandDefinition;
|
|
305
|
+
ts.forEachChild(sourceFile, (node) => {
|
|
306
|
+
if (ts.isExportAssignment(node)) {
|
|
307
|
+
if (ts.isArrowFunction(node.expression)) {
|
|
308
|
+
commandDefinition = node.expression;
|
|
309
|
+
} else if (ts.isCallExpression(node.expression) && ts.isIdentifier(node.expression.expression) && node.expression.expression.escapedText === "defineSlashCommand" && (ts.isArrowFunction(node.expression.arguments[0]) || ts.isFunctionExpression(node.expression.arguments[0]))) {
|
|
310
|
+
commandDefinition = node.expression.arguments[0];
|
|
311
|
+
}
|
|
312
|
+
} else if (ts.isFunctionDeclaration(node) && !!(ts.getCombinedModifierFlags(node) & ts.ModifierFlags.ExportDefault)) {
|
|
313
|
+
commandDefinition = node;
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
return commandDefinition;
|
|
317
|
+
}
|
|
318
|
+
function extractMacroCalls(definitionBody) {
|
|
212
319
|
let macrosEnd = false;
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
320
|
+
const macroCalls = [];
|
|
321
|
+
const outOfRangMacroCalls = [];
|
|
322
|
+
ts.forEachChild(definitionBody, (node) => {
|
|
323
|
+
if (isMacroCallExpression(node)) {
|
|
216
324
|
if (macrosEnd) {
|
|
217
|
-
|
|
218
|
-
ctx.logger.warn(`${node.expression.expression.escapedText} macro must be called at the top in file ${file}`);
|
|
219
|
-
warned = true;
|
|
220
|
-
}
|
|
325
|
+
outOfRangMacroCalls.push(node.expression);
|
|
221
326
|
return;
|
|
222
327
|
}
|
|
223
|
-
|
|
328
|
+
macroCalls.push(node.expression);
|
|
224
329
|
} else {
|
|
225
330
|
macrosEnd = true;
|
|
226
331
|
}
|
|
227
332
|
});
|
|
228
|
-
return
|
|
333
|
+
return [
|
|
334
|
+
macroCalls,
|
|
335
|
+
outOfRangMacroCalls
|
|
336
|
+
];
|
|
337
|
+
}
|
|
338
|
+
function isMacroCallExpression(node) {
|
|
339
|
+
return ts.isExpressionStatement(node) && ts.isCallExpression(node.expression) && ts.isIdentifier(node.expression.expression) && node.expression.expression.escapedText in macros;
|
|
229
340
|
}
|
|
230
341
|
function getJSDocTags(node) {
|
|
231
342
|
const tags = ts.getJSDocTags(node);
|
|
@@ -244,6 +355,104 @@ function getJSDocTags(node) {
|
|
|
244
355
|
return [];
|
|
245
356
|
}
|
|
246
357
|
|
|
358
|
+
const TransformPlugin = (ctx) => {
|
|
359
|
+
const commandsDir = ctx.resolve.root(ctx.options.dir, "commands");
|
|
360
|
+
return {
|
|
361
|
+
name: "transform-slash-commands",
|
|
362
|
+
async load(id) {
|
|
363
|
+
if (!id.startsWith(commandsDir)) {
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
try {
|
|
367
|
+
const sourceFile = ts.createSourceFile(
|
|
368
|
+
id,
|
|
369
|
+
fs.readFileSync(id, "utf-8"),
|
|
370
|
+
ts.ScriptTarget.Latest,
|
|
371
|
+
/* setParentNodes */
|
|
372
|
+
true
|
|
373
|
+
);
|
|
374
|
+
let commandDefinition = extractCommandDefinition(sourceFile);
|
|
375
|
+
if (!commandDefinition || !commandDefinition.body) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
const transformer = (context) => {
|
|
379
|
+
return (sourceFile2) => {
|
|
380
|
+
const visitor = (node) => {
|
|
381
|
+
if (ts.isExportAssignment(node) || ts.isFunctionDeclaration(node) && !!(ts.getCombinedModifierFlags(node) & ts.ModifierFlags.ExportDefault)) {
|
|
382
|
+
if (ts.isFunctionDeclaration(commandDefinition)) {
|
|
383
|
+
commandDefinition = ts.factory.createFunctionExpression(
|
|
384
|
+
ts.getModifiers(commandDefinition)?.filter((m) => m.kind !== ts.SyntaxKind.ExportKeyword && m.kind !== ts.SyntaxKind.DefaultKeyword),
|
|
385
|
+
commandDefinition.asteriskToken,
|
|
386
|
+
commandDefinition.name,
|
|
387
|
+
commandDefinition.typeParameters,
|
|
388
|
+
commandDefinition.parameters,
|
|
389
|
+
commandDefinition.type,
|
|
390
|
+
commandDefinition.body
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
commandDefinition = commandDefinition;
|
|
394
|
+
const [macroCalls] = extractMacroCalls(commandDefinition.body);
|
|
395
|
+
if (ts.isBlock(commandDefinition.body)) {
|
|
396
|
+
if (isFunctionExpression(commandDefinition)) {
|
|
397
|
+
commandDefinition = ts.factory.createFunctionExpression(
|
|
398
|
+
commandDefinition.modifiers,
|
|
399
|
+
commandDefinition.asteriskToken,
|
|
400
|
+
commandDefinition.name,
|
|
401
|
+
commandDefinition.typeParameters,
|
|
402
|
+
commandDefinition.parameters,
|
|
403
|
+
commandDefinition.type,
|
|
404
|
+
ts.factory.createBlock(
|
|
405
|
+
commandDefinition.body.statements.filter((s) => !isMacroCallExpression(s)),
|
|
406
|
+
true
|
|
407
|
+
)
|
|
408
|
+
);
|
|
409
|
+
} else if (isArrowFunction(commandDefinition)) {
|
|
410
|
+
commandDefinition = ts.factory.createArrowFunction(
|
|
411
|
+
commandDefinition.modifiers,
|
|
412
|
+
commandDefinition.typeParameters,
|
|
413
|
+
commandDefinition.parameters,
|
|
414
|
+
commandDefinition.type,
|
|
415
|
+
commandDefinition.equalsGreaterThanToken,
|
|
416
|
+
ts.factory.createBlock(
|
|
417
|
+
commandDefinition.body.statements.filter((s) => !isMacroCallExpression(s)),
|
|
418
|
+
true
|
|
419
|
+
)
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
return ts.factory.createExportAssignment(
|
|
424
|
+
[],
|
|
425
|
+
false,
|
|
426
|
+
ts.factory.createObjectLiteralExpression([
|
|
427
|
+
ts.factory.createPropertyAssignment("execute", commandDefinition),
|
|
428
|
+
ts.factory.createPropertyAssignment(
|
|
429
|
+
"optionMacros",
|
|
430
|
+
ts.factory.createObjectLiteralExpression(
|
|
431
|
+
macroCalls.filter((m) => m.expression.escapedText === "describeOption" && ts.isIdentifier(m.arguments[0]) && m.arguments.length > 1).map((m) => ts.factory.createPropertyAssignment(m.arguments[0].escapedText, m.arguments[1]))
|
|
432
|
+
)
|
|
433
|
+
)
|
|
434
|
+
])
|
|
435
|
+
);
|
|
436
|
+
}
|
|
437
|
+
return ts.visitEachChild(node, visitor, context);
|
|
438
|
+
};
|
|
439
|
+
return ts.visitNode(sourceFile2, visitor);
|
|
440
|
+
};
|
|
441
|
+
};
|
|
442
|
+
const res = ts.transform(sourceFile, [transformer]);
|
|
443
|
+
const printer = ts.createPrinter({
|
|
444
|
+
newLine: ts.NewLineKind.LineFeed,
|
|
445
|
+
removeComments: false
|
|
446
|
+
});
|
|
447
|
+
const transformed = printer.printFile(res.transformed[0]);
|
|
448
|
+
return transformed;
|
|
449
|
+
} catch {
|
|
450
|
+
return null;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
};
|
|
455
|
+
|
|
247
456
|
function prepareHMR(ctx) {
|
|
248
457
|
const { nuxt, options } = ctx;
|
|
249
458
|
if (nuxt.options.dev) {
|
|
@@ -266,8 +475,15 @@ function prepareHMR(ctx) {
|
|
|
266
475
|
nitroIgnored.push(existingNitroIgnored);
|
|
267
476
|
}
|
|
268
477
|
nitroConfig.watchOptions.ignored = nitroIgnored;
|
|
269
|
-
nitroConfig.hooks
|
|
270
|
-
|
|
478
|
+
const originalBeforeHook = nitroConfig.hooks?.["rollup:before"];
|
|
479
|
+
nitroConfig.hooks = {
|
|
480
|
+
...nitroConfig.hooks,
|
|
481
|
+
"rollup:before": (nitroCtx, config) => {
|
|
482
|
+
originalBeforeHook?.(nitroCtx, config);
|
|
483
|
+
config.plugins = [
|
|
484
|
+
config.plugins,
|
|
485
|
+
TransformPlugin(ctx)
|
|
486
|
+
];
|
|
271
487
|
rollupConfig = config;
|
|
272
488
|
}
|
|
273
489
|
};
|
|
@@ -296,12 +512,11 @@ function prepareHMR(ctx) {
|
|
|
296
512
|
};
|
|
297
513
|
listener.server.on("upgrade", websocket.serve);
|
|
298
514
|
websocket.on("full-update", (client) => {
|
|
299
|
-
|
|
300
|
-
collectSlashCommands(ctx, commands);
|
|
515
|
+
collectSlashCommands(ctx);
|
|
301
516
|
try {
|
|
302
517
|
client.send(JSON.stringify({
|
|
303
518
|
event: "full-update",
|
|
304
|
-
commands
|
|
519
|
+
commands: ctx.slashCommands
|
|
305
520
|
}));
|
|
306
521
|
} catch {
|
|
307
522
|
}
|
|
@@ -445,17 +660,17 @@ function prepareTemplates(ctx) {
|
|
|
445
660
|
filename: "discord/slashCommands",
|
|
446
661
|
getContents: () => getSlashCommandTemplateContent(
|
|
447
662
|
ctx,
|
|
448
|
-
/*
|
|
663
|
+
/* commandRuntime */
|
|
449
664
|
true
|
|
450
665
|
)
|
|
451
666
|
});
|
|
452
667
|
addTemplate({
|
|
453
668
|
filename: "discord/slashCommands",
|
|
454
|
-
getContents: () =>
|
|
669
|
+
getContents: () => getSlashCommandTemplateContent(
|
|
455
670
|
ctx,
|
|
456
|
-
/*
|
|
671
|
+
/* commandRuntime */
|
|
457
672
|
false
|
|
458
|
-
)
|
|
673
|
+
)
|
|
459
674
|
});
|
|
460
675
|
const typesTemplateDst = addTypeTemplate({
|
|
461
676
|
filename: "discord/types.d.ts",
|
|
@@ -495,20 +710,23 @@ declare global {
|
|
|
495
710
|
include: [typesTemplateDst]
|
|
496
711
|
});
|
|
497
712
|
}
|
|
498
|
-
function getSlashCommandTemplateContent(ctx,
|
|
499
|
-
const imports =
|
|
713
|
+
function getSlashCommandTemplateContent(ctx, commandRuntime) {
|
|
714
|
+
const imports = commandRuntime ? ctx.slashCommands.map(({ path }) => {
|
|
500
715
|
const name = genSafeVariableName(path);
|
|
501
716
|
return { name, code: genImport(path, [{ name: "default", as: name }]) };
|
|
502
717
|
}) : [];
|
|
503
718
|
return [
|
|
504
719
|
...imports.map(({ code }) => code),
|
|
505
|
-
`export default
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
720
|
+
`export default [
|
|
721
|
+
${ctx.slashCommands.map(({ options, ...rest }, i) => ` {
|
|
722
|
+
${Object.entries(rest).map(([key, value]) => `${key}: ${genString(value)}`).join(",\n ")},
|
|
723
|
+
|
|
724
|
+
options: ${genArrayFromRaw(options, void 0, { preserveTypes: true })},
|
|
725
|
+
${commandRuntime ? `
|
|
726
|
+
...${imports[i].name},
|
|
727
|
+
` : ""}
|
|
728
|
+
},
|
|
729
|
+
`).join("")}]`
|
|
512
730
|
].join("\n");
|
|
513
731
|
}
|
|
514
732
|
|
|
@@ -104,7 +104,7 @@ const commandsWithStatus = computed(() => {
|
|
|
104
104
|
name: cmd.name,
|
|
105
105
|
description: cmd.description,
|
|
106
106
|
path: "unknown",
|
|
107
|
-
options: cmd.options,
|
|
107
|
+
options: cmd.options ?? [],
|
|
108
108
|
status: "removed",
|
|
109
109
|
remote: cmd
|
|
110
110
|
})),
|
|
@@ -242,7 +242,6 @@ const statusClasses = {
|
|
|
242
242
|
/>
|
|
243
243
|
</div>
|
|
244
244
|
|
|
245
|
-
<!-- <UButton icon="i-lucide-circle" @click="test" /> -->
|
|
246
245
|
<UButton
|
|
247
246
|
v-if="!allSynced"
|
|
248
247
|
:icon="pendingSync ? 'i-lucide-refresh-ccw' : 'i-lucide-cloud-upload'"
|
|
@@ -331,6 +330,13 @@ const statusClasses = {
|
|
|
331
330
|
color="error"
|
|
332
331
|
size="sm"
|
|
333
332
|
/>
|
|
333
|
+
<UBadge
|
|
334
|
+
v-if="option.hasAutocomplete"
|
|
335
|
+
label="autocomplete"
|
|
336
|
+
variant="soft"
|
|
337
|
+
color="info"
|
|
338
|
+
size="sm"
|
|
339
|
+
/>
|
|
334
340
|
<!-- Restriction badges -->
|
|
335
341
|
<UBadge
|
|
336
342
|
v-if="'min' in option && option.min !== void 0"
|
|
@@ -363,8 +369,12 @@ const statusClasses = {
|
|
|
363
369
|
<!-- Choices dropdown -->
|
|
364
370
|
<UDropdownMenu
|
|
365
371
|
v-if="'choices' in option && option.choices && option.choices.length > 0"
|
|
366
|
-
:items="option.choices.map((choice) => ({
|
|
372
|
+
:items="option.choices.map((choice) => ({
|
|
373
|
+
label: choice.name,
|
|
374
|
+
value: typeof choice.value === 'string' ? `\'${choice.value}\'` : choice.value
|
|
375
|
+
}))"
|
|
367
376
|
:popper="{ placement: 'bottom-start' }"
|
|
377
|
+
:ui="{ itemTrailing: 'text-sm text-gray-500 dark:text-gray-400' }"
|
|
368
378
|
>
|
|
369
379
|
<UBadge
|
|
370
380
|
v-if="'choices' in option && option.choices && option.choices.length > 0"
|
|
@@ -374,6 +384,9 @@ const statusClasses = {
|
|
|
374
384
|
size="sm"
|
|
375
385
|
class="hover:cursor-pointer"
|
|
376
386
|
/>
|
|
387
|
+
<template #item-trailing="{ item: { value } }">
|
|
388
|
+
{{ value }}
|
|
389
|
+
</template>
|
|
377
390
|
</UDropdownMenu>
|
|
378
391
|
</div>
|
|
379
392
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { APIApplicationCommand, ChatInputCommandInteraction, RESTGetAPIApplicationCommandsResult, RESTPutAPIApplicationCommandsResult } from 'discord.js';
|
|
1
|
+
import type { APIApplicationCommand, ChatInputCommandInteraction, CommandInteraction, RESTGetAPIApplicationCommandsResult, RESTPutAPIApplicationCommandsResult } from 'discord.js';
|
|
2
2
|
import type { NuxtDiscordOptions, SlashCommandRuntime } from '../../../types.js';
|
|
3
3
|
import { Client as InternalClient } from 'discord.js';
|
|
4
4
|
export interface DiscordClientErrorBase {
|
|
@@ -7,17 +7,17 @@ export interface DiscordClientErrorBase {
|
|
|
7
7
|
}
|
|
8
8
|
export interface UnknownSlashCommandError extends DiscordClientErrorBase {
|
|
9
9
|
type: 'UnknownSlashCommandError';
|
|
10
|
-
interaction:
|
|
10
|
+
interaction: CommandInteraction;
|
|
11
11
|
}
|
|
12
12
|
export interface MissingRequiredOptionError extends DiscordClientErrorBase {
|
|
13
13
|
type: 'MissingRequiredOptionError';
|
|
14
|
-
interaction:
|
|
14
|
+
interaction: CommandInteraction;
|
|
15
15
|
command: SlashCommandRuntime;
|
|
16
16
|
option: string;
|
|
17
17
|
}
|
|
18
18
|
export interface SlashCommandExecutionError extends DiscordClientErrorBase {
|
|
19
19
|
type: 'SlashCommandExecutionError';
|
|
20
|
-
interaction:
|
|
20
|
+
interaction: CommandInteraction;
|
|
21
21
|
command: SlashCommandRuntime;
|
|
22
22
|
error: unknown;
|
|
23
23
|
}
|
|
@@ -34,6 +34,8 @@ export declare class DiscordClient {
|
|
|
34
34
|
constructor();
|
|
35
35
|
start(options: NuxtDiscordOptions['client']): Promise<undefined>;
|
|
36
36
|
stop(): Promise<void>;
|
|
37
|
+
startScope(): void;
|
|
38
|
+
stopScope(): void;
|
|
37
39
|
isReady(): boolean;
|
|
38
40
|
addSlashCommand(command: SlashCommandRuntime): void;
|
|
39
41
|
addSlashCommands(commands: SlashCommandRuntime[]): void;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import process from "node:process";
|
|
2
2
|
import { ApplicationCommandOptionType, Events, Client as InternalClient, REST, Routes, SlashCommandBuilder } from "discord.js";
|
|
3
3
|
import { useNitroApp } from "nitropack/runtime";
|
|
4
|
+
import { effectScope, isRef } from "vue";
|
|
4
5
|
import { logger } from "../../logger.js";
|
|
6
|
+
import { reply } from "./reply.js";
|
|
5
7
|
let currentInteraction = null;
|
|
6
8
|
export function useInteraction() {
|
|
7
9
|
return currentInteraction;
|
|
@@ -35,11 +37,8 @@ export class DiscordClient {
|
|
|
35
37
|
this.#clientOptions = { ...options };
|
|
36
38
|
await this.#nitro.hooks.callHook("discord:client:config", this.#clientOptions);
|
|
37
39
|
this.#client = new InternalClient(this.#clientOptions);
|
|
38
|
-
this.#client.on(Events.InteractionCreate, (interaction) =>
|
|
39
|
-
|
|
40
|
-
return;
|
|
41
|
-
this.#handleSlashCommand(interaction);
|
|
42
|
-
});
|
|
40
|
+
this.#client.on(Events.InteractionCreate, (interaction) => this.#handleInteraction(interaction));
|
|
41
|
+
this.startScope();
|
|
43
42
|
const { resolve, promise } = Promise.withResolvers();
|
|
44
43
|
this.#client.once(Events.ClientReady, (readyClient) => {
|
|
45
44
|
logger.log(`Discord client is ready! Logged in as ${readyClient.user?.tag}`);
|
|
@@ -49,11 +48,73 @@ export class DiscordClient {
|
|
|
49
48
|
await this.#client.login(this.#token);
|
|
50
49
|
return promise;
|
|
51
50
|
}
|
|
51
|
+
async #handleInteraction(interaction) {
|
|
52
|
+
if (!interaction.isAutocomplete() && !interaction.isChatInputCommand()) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const commandName = interaction.commandName;
|
|
56
|
+
const command = this.#slashCommands.find((cmd) => cmd.name === commandName);
|
|
57
|
+
if (!command) {
|
|
58
|
+
logger.warn(`Unknown slash command: ${commandName}`);
|
|
59
|
+
this.#nitro.hooks.callHook("discord:client:error", {
|
|
60
|
+
type: "UnknownSlashCommandError",
|
|
61
|
+
client: this,
|
|
62
|
+
interaction
|
|
63
|
+
});
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (!command.execute && command.load) {
|
|
67
|
+
try {
|
|
68
|
+
const dynamicCommand = await command.load();
|
|
69
|
+
Object.assign(command, dynamicCommand);
|
|
70
|
+
} catch (error) {
|
|
71
|
+
this.#nitro.hooks.callHook("discord:client:error", {
|
|
72
|
+
type: "SlashCommandExecutionError",
|
|
73
|
+
client: this,
|
|
74
|
+
interaction,
|
|
75
|
+
command,
|
|
76
|
+
error
|
|
77
|
+
});
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (!command.execute && !command.load) {
|
|
82
|
+
this.#nitro.hooks.callHook("discord:client:error", {
|
|
83
|
+
type: "SlashCommandExecutionError",
|
|
84
|
+
client: this,
|
|
85
|
+
interaction,
|
|
86
|
+
command,
|
|
87
|
+
error: new Error(`Slash command "${commandName}" cannot be executed or loaded`)
|
|
88
|
+
});
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (interaction.isAutocomplete()) {
|
|
92
|
+
await this.#handleAutocomplete(interaction, command);
|
|
93
|
+
} else if (interaction.isChatInputCommand()) {
|
|
94
|
+
await this.#handleSlashCommand(interaction, command);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
52
97
|
async stop() {
|
|
53
98
|
if (this.#client) {
|
|
54
99
|
await this.#client.destroy();
|
|
55
100
|
this.#client = null;
|
|
56
101
|
}
|
|
102
|
+
this.stopScope();
|
|
103
|
+
}
|
|
104
|
+
#scope;
|
|
105
|
+
startScope() {
|
|
106
|
+
if (this.#scope)
|
|
107
|
+
return;
|
|
108
|
+
this.#scope = effectScope(
|
|
109
|
+
/* detached */
|
|
110
|
+
true
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
stopScope() {
|
|
114
|
+
if (!this.#scope)
|
|
115
|
+
return;
|
|
116
|
+
this.#scope.stop();
|
|
117
|
+
this.#scope = void 0;
|
|
57
118
|
}
|
|
58
119
|
isReady() {
|
|
59
120
|
return this.#client?.isReady() ?? false;
|
|
@@ -86,18 +147,9 @@ export class DiscordClient {
|
|
|
86
147
|
getSlashCommand(name) {
|
|
87
148
|
return this.#slashCommands.find((cmd) => cmd.name === name);
|
|
88
149
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (!command) {
|
|
93
|
-
logger.warn(`Unknown slash command: ${name}`);
|
|
94
|
-
this.#nitro.hooks.callHook("discord:client:error", {
|
|
95
|
-
type: "UnknownSlashCommandError",
|
|
96
|
-
client: this,
|
|
97
|
-
interaction
|
|
98
|
-
});
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
150
|
+
#interactionScopes = /* @__PURE__ */ new WeakMap();
|
|
151
|
+
#commandFinalizationRegistry = new FinalizationRegistry((scope) => scope.stop());
|
|
152
|
+
async #handleSlashCommand(interaction, command) {
|
|
101
153
|
const args = [];
|
|
102
154
|
for (const option of command.options) {
|
|
103
155
|
const opt = interaction.options.get(option.name, option.required);
|
|
@@ -113,33 +165,22 @@ export class DiscordClient {
|
|
|
113
165
|
}
|
|
114
166
|
args.push(opt?.value);
|
|
115
167
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
command,
|
|
122
|
-
error: new Error(`Slash command "${name}" cannot be executed or loaded`)
|
|
123
|
-
});
|
|
124
|
-
return;
|
|
125
|
-
} else if (!command.execute && command.load) {
|
|
126
|
-
try {
|
|
127
|
-
command.execute = await command.load();
|
|
128
|
-
} catch (error) {
|
|
129
|
-
this.#nitro.hooks.callHook("discord:client:error", {
|
|
130
|
-
type: "SlashCommandExecutionError",
|
|
131
|
-
client: this,
|
|
132
|
-
interaction,
|
|
133
|
-
command,
|
|
134
|
-
error
|
|
135
|
-
});
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
168
|
+
let scope;
|
|
169
|
+
if (this.#scope) {
|
|
170
|
+
this.#scope.run(() => scope = effectScope());
|
|
171
|
+
} else {
|
|
172
|
+
scope = effectScope();
|
|
138
173
|
}
|
|
174
|
+
this.#interactionScopes.set(interaction, scope);
|
|
175
|
+
if (import.meta.dev)
|
|
176
|
+
this.#commandFinalizationRegistry.register(command, scope);
|
|
139
177
|
try {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
178
|
+
let result;
|
|
179
|
+
scope.run(() => {
|
|
180
|
+
currentInteraction = interaction;
|
|
181
|
+
result = command.execute(...args);
|
|
182
|
+
currentInteraction = null;
|
|
183
|
+
});
|
|
143
184
|
if (this.#clientOptions?.deferOnPromise && isPromise(result)) {
|
|
144
185
|
await interaction.deferReply();
|
|
145
186
|
}
|
|
@@ -160,15 +201,15 @@ export class DiscordClient {
|
|
|
160
201
|
return;
|
|
161
202
|
}
|
|
162
203
|
try {
|
|
163
|
-
if (typeof result === "string") {
|
|
164
|
-
|
|
165
|
-
|
|
204
|
+
if (typeof result === "string" || isRef(result)) {
|
|
205
|
+
return this.#handleSlashCommandReturn(reply(result), interaction, command);
|
|
206
|
+
} else if (typeof result === "function") {
|
|
207
|
+
const scope = this.#interactionScopes.get(interaction);
|
|
208
|
+
if (!scope) {
|
|
209
|
+
return result.call(this, interaction, this);
|
|
166
210
|
} else {
|
|
167
|
-
return
|
|
211
|
+
return scope.run(() => result.call(this, interaction, this));
|
|
168
212
|
}
|
|
169
|
-
} else if (typeof result === "function") {
|
|
170
|
-
const newResult = result.call(this, interaction, this);
|
|
171
|
-
return this.#handleSlashCommandReturn(newResult, interaction, command);
|
|
172
213
|
} else if (Symbol.iterator in result && typeof result[Symbol.iterator] === "function") {
|
|
173
214
|
const gen = result[Symbol.iterator]();
|
|
174
215
|
let next = gen.next();
|
|
@@ -202,6 +243,29 @@ export class DiscordClient {
|
|
|
202
243
|
});
|
|
203
244
|
}
|
|
204
245
|
}
|
|
246
|
+
async #handleAutocomplete(interaction, command) {
|
|
247
|
+
const commandName = interaction.commandName;
|
|
248
|
+
if (!command) {
|
|
249
|
+
logger.warn(`Unknown slash command: ${commandName}`);
|
|
250
|
+
this.#nitro.hooks.callHook("discord:client:error", {
|
|
251
|
+
type: "UnknownSlashCommandError",
|
|
252
|
+
client: this,
|
|
253
|
+
interaction
|
|
254
|
+
});
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
const focusedOption = interaction.options.getFocused(true);
|
|
258
|
+
const varname = command.options.find((opt) => opt.name === focusedOption.name)?.varname;
|
|
259
|
+
if (!varname) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
const autocompleteFunction = command.optionMacros?.[varname]?.autocomplete;
|
|
263
|
+
if (!autocompleteFunction) {
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
const choices = await autocompleteFunction(focusedOption.value, interaction);
|
|
267
|
+
await interaction.respond(choices);
|
|
268
|
+
}
|
|
205
269
|
#getSlashCommandBuilder(command) {
|
|
206
270
|
const builder = new SlashCommandBuilder().setName(command.name).setDescription(command.description);
|
|
207
271
|
for (const option of command.options) {
|
|
@@ -217,6 +281,9 @@ export class DiscordClient {
|
|
|
217
281
|
opt = opt.setMaxLength(option.maxLength);
|
|
218
282
|
if (option.choices)
|
|
219
283
|
opt = opt.addChoices(...option.choices);
|
|
284
|
+
if (option.hasAutocomplete) {
|
|
285
|
+
opt = opt.setAutocomplete(true);
|
|
286
|
+
}
|
|
220
287
|
return opt;
|
|
221
288
|
});
|
|
222
289
|
break;
|
|
@@ -231,6 +298,8 @@ export class DiscordClient {
|
|
|
231
298
|
opt = opt.setMaxValue(option.max);
|
|
232
299
|
if (option.choices)
|
|
233
300
|
opt = opt.addChoices(...option.choices);
|
|
301
|
+
if (option.hasAutocomplete)
|
|
302
|
+
opt = opt.setAutocomplete(true);
|
|
234
303
|
return opt;
|
|
235
304
|
});
|
|
236
305
|
break;
|
|
@@ -245,6 +314,8 @@ export class DiscordClient {
|
|
|
245
314
|
opt = opt.setMaxValue(option.max);
|
|
246
315
|
if (option.choices)
|
|
247
316
|
opt = opt.addChoices(...option.choices);
|
|
317
|
+
if (option.hasAutocomplete)
|
|
318
|
+
opt = opt.setAutocomplete(true);
|
|
248
319
|
return opt;
|
|
249
320
|
});
|
|
250
321
|
break;
|
|
@@ -394,6 +465,9 @@ function optionEqual(a, b) {
|
|
|
394
465
|
return false;
|
|
395
466
|
}
|
|
396
467
|
}
|
|
468
|
+
if (a.hasAutocomplete !== !!("autocomplete" in b && b.autocomplete)) {
|
|
469
|
+
return false;
|
|
470
|
+
}
|
|
397
471
|
return true;
|
|
398
472
|
}
|
|
399
473
|
export function isPromise(value) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { APIApplicationCommandOptionChoice } from 'discord.js';
|
|
1
|
+
import type { APIApplicationCommandOptionChoice, ApplicationCommandOptionChoiceData, AutocompleteInteraction } from 'discord.js';
|
|
2
2
|
import type { SlashCommandOptionType } from '../../../types.js';
|
|
3
3
|
export interface DescribeOptionOptionsBase {
|
|
4
4
|
/** The name of the command. */
|
|
@@ -6,31 +6,64 @@ export interface DescribeOptionOptionsBase {
|
|
|
6
6
|
/** A brief description of the command. */
|
|
7
7
|
description?: string;
|
|
8
8
|
}
|
|
9
|
-
|
|
9
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
10
|
+
interface AutocompleteFunction {
|
|
11
|
+
(value: string, interaction: AutocompleteInteraction): MaybePromise<ApplicationCommandOptionChoiceData[]>;
|
|
12
|
+
}
|
|
13
|
+
export interface IntegerOption<T extends number = number> extends DescribeOptionOptionsBase {
|
|
10
14
|
/** The minimum value for the integer option. */
|
|
11
15
|
min?: number;
|
|
12
16
|
/** The maximum value for the integer option. */
|
|
13
17
|
max?: number;
|
|
14
18
|
/** An array of choices for the integer option. */
|
|
15
|
-
choices?: APIApplicationCommandOptionChoice<
|
|
19
|
+
choices?: APIApplicationCommandOptionChoice<T>[];
|
|
20
|
+
/** A function to handle autocomplete for the integer option. */
|
|
21
|
+
autocomplete?: AutocompleteFunction;
|
|
16
22
|
}
|
|
17
|
-
export interface NumberOption extends DescribeOptionOptionsBase {
|
|
23
|
+
export interface NumberOption<T extends number = number> extends DescribeOptionOptionsBase {
|
|
18
24
|
/** The minimum value for the number option. */
|
|
19
25
|
min?: number;
|
|
20
26
|
/** The maximum value for the number option. */
|
|
21
27
|
max?: number;
|
|
22
28
|
/** An array of choices for the number option. */
|
|
23
|
-
choices?: APIApplicationCommandOptionChoice<
|
|
29
|
+
choices?: APIApplicationCommandOptionChoice<T>[];
|
|
30
|
+
/** A function to handle autocomplete for the number option. */
|
|
31
|
+
autocomplete?: AutocompleteFunction;
|
|
24
32
|
}
|
|
25
|
-
export interface StringOption extends DescribeOptionOptionsBase {
|
|
33
|
+
export interface StringOption<T extends string = string> extends DescribeOptionOptionsBase {
|
|
26
34
|
/** The minimum length of the string option. */
|
|
27
35
|
minLength?: number;
|
|
28
36
|
/** The maximum length of the string option. */
|
|
29
37
|
maxLength?: number;
|
|
30
38
|
/** An array of choices for the string option. */
|
|
31
|
-
choices?: APIApplicationCommandOptionChoice<
|
|
39
|
+
choices?: APIApplicationCommandOptionChoice<T>[];
|
|
40
|
+
/** A function to handle autocomplete for the string option. */
|
|
41
|
+
autocomplete?: AutocompleteFunction;
|
|
32
42
|
}
|
|
33
43
|
export type DescribeOptionOptions = IntegerOption | NumberOption | StringOption;
|
|
44
|
+
export type NarrowedDescribeOptionOptions<T extends SlashCommandOptionType> = T extends integer ? IntegerOption<T> : T extends number ? NumberOption<T> : T extends string ? StringOption<T> : never;
|
|
34
45
|
/**
|
|
46
|
+
* A compiler macro that describes a slash command option. Used for defining
|
|
47
|
+
* metadata for registering slash command options. Options defined here will
|
|
48
|
+
* take higher precedence over the inferred ones from the function signature
|
|
49
|
+
* and JSDoc tags.
|
|
50
|
+
*
|
|
51
|
+
* @param _option - The type of the option, used for type inference.
|
|
52
|
+
* @param _options - The options for the command option.
|
|
53
|
+
* @example
|
|
54
|
+
* // this will describe the name option as a string with a minimum length of 1 and a maximum length of 32
|
|
55
|
+
* describeOption(name, {
|
|
56
|
+
* minLength: 1,
|
|
57
|
+
* maxLength: 32,
|
|
58
|
+
* })
|
|
59
|
+
* // this will describe the style option as a string with three choices
|
|
60
|
+
* describeOption(style, {
|
|
61
|
+
* choices: [
|
|
62
|
+
* { name: 'Formal', value: 'formal' },
|
|
63
|
+
* { name: 'Casual', value: 'casual' },
|
|
64
|
+
* { name: 'Enthusiastic', value: 'enthusiastic' },
|
|
65
|
+
* ],
|
|
66
|
+
* })
|
|
35
67
|
*/
|
|
36
|
-
export declare function describeOption<const T extends SlashCommandOptionType>(_option: T | undefined, _options: T
|
|
68
|
+
export declare function describeOption<const T extends SlashCommandOptionType>(_option: T | undefined, _options: NarrowedDescribeOptionOptions<T>): void;
|
|
69
|
+
export {};
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import type { BaseMessageOptions,
|
|
1
|
+
import type { BaseMessageOptions, InteractionCallbackResponse, InteractionReplyOptions, InteractionResponse, Message } from 'discord.js';
|
|
2
|
+
import type { MaybeRef } from 'vue';
|
|
2
3
|
import type { SlashCommandCustomReturnHandler } from '../../../types.js';
|
|
3
4
|
type OmitPreservingCallSignature<T, K extends keyof T = keyof T> = Omit<T, K> & (T extends (...args: infer R) => infer S ? (...args: R) => S : unknown);
|
|
4
5
|
type File = NonNullable<BaseMessageOptions['files']>[number];
|
|
5
6
|
type ReplyFunction = SlashCommandCustomReturnHandler & {
|
|
6
|
-
(text?: string
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
(text?: MaybeRef<string>, options?: InteractionReplyOptions): SlashCommandCustomReturnHandler & {
|
|
8
|
+
[Symbol.iterator]: () => {
|
|
9
|
+
next: (...args: ReadonlyArray<any>) => IteratorResult<ReplyFunction, Message | InteractionResponse | InteractionCallbackResponse>;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
[Symbol.iterator]: () => {
|
|
13
|
+
next: (...args: ReadonlyArray<any>) => IteratorResult<never, Promise<Message | InteractionResponse | InteractionCallbackResponse>>;
|
|
14
|
+
};
|
|
15
|
+
ephemeral: OmitPreservingCallSignature<ReplyFunction, 'ephemeral'>;
|
|
16
|
+
flags: (flags: MaybeRef<InteractionReplyOptions['flags']>) => ReplyFunction;
|
|
17
|
+
file: (file: MaybeRef<File>) => ReplyFunction;
|
|
18
|
+
files: (files: MaybeRef<File[]>) => ReplyFunction;
|
|
12
19
|
send: ReplyFunction;
|
|
13
20
|
};
|
|
14
|
-
type EditReplyFunction = SlashCommandCustomReturnHandler & {
|
|
15
|
-
(text?: string, options?: InteractionEditReplyOptions): SlashCommandCustomReturnHandler;
|
|
16
|
-
flags: (flags: InteractionEditReplyOptions['flags']) => EditReplyFunction;
|
|
17
|
-
file: (file: File) => EditReplyFunction;
|
|
18
|
-
files: (files: File[]) => EditReplyFunction;
|
|
19
|
-
send: EditReplyFunction;
|
|
20
|
-
};
|
|
21
21
|
export declare const reply: ReplyFunction;
|
|
22
22
|
export {};
|
|
@@ -1,34 +1,63 @@
|
|
|
1
1
|
import { MessageFlags } from "discord.js";
|
|
2
|
-
|
|
2
|
+
import { isRef, reactive, watch } from "vue";
|
|
3
|
+
function createReplyFunction({ ...defaultOptions } = {}) {
|
|
3
4
|
const reply2 = (...args) => {
|
|
4
|
-
if (typeof args[0] === "string" || typeof args[0] === "undefined") {
|
|
5
|
+
if (typeof args[0] === "string" || typeof args[0] === "undefined" || isRef(args[0]) || typeof args[0] === "function") {
|
|
5
6
|
const [text, options] = args;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
});
|
|
19
|
-
}
|
|
7
|
+
let message;
|
|
8
|
+
const replyOptions = reactive({
|
|
9
|
+
content: text,
|
|
10
|
+
...defaultOptions,
|
|
11
|
+
...options
|
|
12
|
+
});
|
|
13
|
+
const handler = (interaction) => {
|
|
14
|
+
const method = interaction.replied ? "followUp" : interaction.deferred ? "editReply" : "reply";
|
|
15
|
+
message = interaction[method](
|
|
16
|
+
replyOptions
|
|
17
|
+
).catch((err) => err);
|
|
18
|
+
return message;
|
|
20
19
|
};
|
|
20
|
+
Object.defineProperty(handler, Symbol.iterator, {
|
|
21
|
+
value: () => ({
|
|
22
|
+
next: () => message !== void 0 ? {
|
|
23
|
+
done: true,
|
|
24
|
+
value: message
|
|
25
|
+
} : {
|
|
26
|
+
done: false,
|
|
27
|
+
value: handler
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
});
|
|
31
|
+
watch(replyOptions, async (options2) => {
|
|
32
|
+
const msg = await message;
|
|
33
|
+
if ("edit" in msg)
|
|
34
|
+
msg.edit(options2);
|
|
35
|
+
else
|
|
36
|
+
msg.resource?.message?.edit(options2);
|
|
37
|
+
}, { deep: true, flush: "sync" });
|
|
38
|
+
return handler;
|
|
21
39
|
} else {
|
|
22
40
|
const interaction = args[0];
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
41
|
+
const method = interaction.replied ? "followUp" : interaction.deferred ? "editReply" : "reply";
|
|
42
|
+
const options = reactive(defaultOptions);
|
|
43
|
+
const messagePromise = interaction[method](options).catch((err) => err);
|
|
44
|
+
watch(options, async (opts) => {
|
|
45
|
+
const message = await messagePromise;
|
|
46
|
+
if (!message)
|
|
47
|
+
return;
|
|
48
|
+
if ("edit" in message)
|
|
49
|
+
message.edit(opts);
|
|
50
|
+
else
|
|
51
|
+
message.resource?.message?.edit(opts);
|
|
52
|
+
}, { deep: true, flush: "sync" });
|
|
53
|
+
return {
|
|
54
|
+
[Symbol.iterator]: () => ({
|
|
55
|
+
next: () => ({
|
|
56
|
+
done: true,
|
|
57
|
+
value: messagePromise
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
};
|
|
32
61
|
}
|
|
33
62
|
};
|
|
34
63
|
Object.defineProperties(reply2, {
|
|
@@ -48,14 +77,6 @@ function createReplyFunction({ editReply, ...defaultOptions } = {}) {
|
|
|
48
77
|
});
|
|
49
78
|
}
|
|
50
79
|
},
|
|
51
|
-
edit: {
|
|
52
|
-
get() {
|
|
53
|
-
return createReplyFunction({
|
|
54
|
-
...defaultOptions,
|
|
55
|
-
editReply: true
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
80
|
file: {
|
|
60
81
|
get() {
|
|
61
82
|
return (file) => createReplyFunction({
|
package/package.json
CHANGED