nuxt-discord 0.0.1
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/LICENSE +21 -0
- package/README.md +292 -0
- package/dist/module.d.mts +128 -0
- package/dist/module.json +9 -0
- package/dist/module.mjs +508 -0
- package/dist/runtime/client/components/TheHeader.css +1 -0
- package/dist/runtime/client/components/TheHeader.vue +25 -0
- package/dist/runtime/client/components/TheHeader.vue.d.ts +3 -0
- package/dist/runtime/client/pages/slash-commands.css +1 -0
- package/dist/runtime/client/pages/slash-commands.vue +429 -0
- package/dist/runtime/client/pages/slash-commands.vue.d.ts +4 -0
- package/dist/runtime/client/style.css +1 -0
- package/dist/runtime/client/uno.config.d.ts +2 -0
- package/dist/runtime/client/uno.config.js +10 -0
- package/dist/runtime/server/api/discord/slash-command/all.get.d.ts +2 -0
- package/dist/runtime/server/api/discord/slash-command/all.get.js +8 -0
- package/dist/runtime/server/api/discord/slash-command/register.post.d.ts +2 -0
- package/dist/runtime/server/api/discord/slash-command/register.post.js +6 -0
- package/dist/runtime/server/api/discord/slash-command/remote-all.get.d.ts +2 -0
- package/dist/runtime/server/api/discord/slash-command/remote-all.get.js +8 -0
- package/dist/runtime/server/api/discord/slash-command/remote-diff.post.d.ts +20 -0
- package/dist/runtime/server/api/discord/slash-command/remote-diff.post.js +8 -0
- package/dist/runtime/server/plugins/discord.d.ts +2 -0
- package/dist/runtime/server/plugins/discord.js +38 -0
- package/dist/runtime/server/tsconfig.json +3 -0
- package/dist/runtime/server/utils/client.d.ts +59 -0
- package/dist/runtime/server/utils/client.js +396 -0
- package/dist/runtime/server/utils/defineSlashCommand.d.ts +10 -0
- package/dist/runtime/server/utils/defineSlashCommand.js +3 -0
- package/dist/runtime/server/utils/describeCommand.d.ts +19 -0
- package/dist/runtime/server/utils/describeCommand.js +2 -0
- package/dist/runtime/server/utils/describeOption.d.ts +36 -0
- package/dist/runtime/server/utils/describeOption.js +2 -0
- package/dist/runtime/server/utils/reply.d.ts +9 -0
- package/dist/runtime/server/utils/reply.js +27 -0
- package/dist/runtime/server/utils/useDiscordClient.d.ts +5 -0
- package/dist/runtime/server/utils/useDiscordClient.js +11 -0
- package/dist/types.d.mts +9 -0
- package/package.json +78 -0
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
import process from "node:process";
|
|
2
|
+
import ansis from "ansis";
|
|
3
|
+
import { ApplicationCommandOptionType, Events, Client as InternalClient, REST, Routes, SlashCommandBuilder } from "discord.js";
|
|
4
|
+
import { useNitroApp } from "nitropack/runtime";
|
|
5
|
+
let currentInteraction = null;
|
|
6
|
+
export function useInteraction() {
|
|
7
|
+
return currentInteraction;
|
|
8
|
+
}
|
|
9
|
+
const logger = {
|
|
10
|
+
// eslint-disable-next-line no-console
|
|
11
|
+
log: (...args) => console.log(ansis.hex("#5865F2").bold("[Discord]"), ...args),
|
|
12
|
+
warn: (...args) => console.warn(ansis.yellow.bold("[Discord]"), ...args),
|
|
13
|
+
error: (...args) => console.error(ansis.red.bold("[Discord]"), ...args)
|
|
14
|
+
};
|
|
15
|
+
export class DiscordClient {
|
|
16
|
+
#client = null;
|
|
17
|
+
get internalClient() {
|
|
18
|
+
return this.#client;
|
|
19
|
+
}
|
|
20
|
+
#slashCommands = [];
|
|
21
|
+
#nitro = useNitroApp();
|
|
22
|
+
#token;
|
|
23
|
+
#clientId;
|
|
24
|
+
#rest;
|
|
25
|
+
constructor() {
|
|
26
|
+
if (!process.env.DISCORD_TOKEN) {
|
|
27
|
+
throw new Error("DISCORD_TOKEN environment variable is not set");
|
|
28
|
+
}
|
|
29
|
+
this.#token = process.env.DISCORD_TOKEN;
|
|
30
|
+
this.#rest = new REST().setToken(this.#token);
|
|
31
|
+
if (!process.env.DISCORD_CLIENT_ID) {
|
|
32
|
+
throw new Error("DISCORD_CLIENT_ID environment variable is not set");
|
|
33
|
+
}
|
|
34
|
+
this.#clientId = process.env.DISCORD_CLIENT_ID;
|
|
35
|
+
this.#nitro.hooks.hook("discord:client:error", (error) => {
|
|
36
|
+
logger.error(error);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
async start(options) {
|
|
40
|
+
await this.#nitro.hooks.callHook("discord:client:config", options);
|
|
41
|
+
this.#client = new InternalClient(options);
|
|
42
|
+
this.#client.on(Events.InteractionCreate, (interaction) => {
|
|
43
|
+
if (!interaction.isChatInputCommand())
|
|
44
|
+
return;
|
|
45
|
+
this.#handleSlashCommand(interaction);
|
|
46
|
+
});
|
|
47
|
+
const { resolve, promise } = Promise.withResolvers();
|
|
48
|
+
this.#client.once(Events.ClientReady, (readyClient) => {
|
|
49
|
+
logger.log(`Discord client is ready! Logged in as ${readyClient.user?.tag}`);
|
|
50
|
+
this.#nitro.hooks.callHook("discord:client:ready", this);
|
|
51
|
+
resolve(void 0);
|
|
52
|
+
});
|
|
53
|
+
await this.#client.login(this.#token);
|
|
54
|
+
return promise;
|
|
55
|
+
}
|
|
56
|
+
async stop() {
|
|
57
|
+
if (this.#client) {
|
|
58
|
+
await this.#client.destroy();
|
|
59
|
+
this.#client = null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
isReady() {
|
|
63
|
+
return this.#client?.isReady() ?? false;
|
|
64
|
+
}
|
|
65
|
+
addSlashCommand(command) {
|
|
66
|
+
const existingCommandIndex = this.#slashCommands.findIndex((cmd) => cmd.name === command.name);
|
|
67
|
+
if (existingCommandIndex !== -1) {
|
|
68
|
+
this.#slashCommands[existingCommandIndex] = command;
|
|
69
|
+
} else {
|
|
70
|
+
this.#slashCommands.push(command);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
addSlashCommands(commands) {
|
|
74
|
+
for (const command of commands) {
|
|
75
|
+
this.addSlashCommand(command);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
removeSlashCommand(name) {
|
|
79
|
+
const index = this.#slashCommands.findIndex((cmd) => cmd.name === name);
|
|
80
|
+
if (index !== -1) {
|
|
81
|
+
this.#slashCommands.splice(index, 1);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
clearSlashCommands() {
|
|
85
|
+
this.#slashCommands = [];
|
|
86
|
+
}
|
|
87
|
+
getSlashCommands() {
|
|
88
|
+
return this.#slashCommands;
|
|
89
|
+
}
|
|
90
|
+
getSlashCommand(name) {
|
|
91
|
+
return this.#slashCommands.find((cmd) => cmd.name === name);
|
|
92
|
+
}
|
|
93
|
+
async #handleSlashCommand(interaction) {
|
|
94
|
+
const name = interaction.commandName;
|
|
95
|
+
const command = this.#slashCommands.find((cmd) => cmd.name === name);
|
|
96
|
+
if (!command) {
|
|
97
|
+
logger.warn(`Unknown slash command: ${name}`);
|
|
98
|
+
this.#nitro.hooks.callHook("discord:client:error", {
|
|
99
|
+
type: "UnknownSlashCommandError",
|
|
100
|
+
client: this,
|
|
101
|
+
interaction
|
|
102
|
+
});
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const args = [];
|
|
106
|
+
for (const option of command.options) {
|
|
107
|
+
const opt = interaction.options.get(option.name, option.required);
|
|
108
|
+
if (option.required && opt == null) {
|
|
109
|
+
this.#nitro.hooks.callHook("discord:client:error", {
|
|
110
|
+
type: "MissingRequiredOptionError",
|
|
111
|
+
client: this,
|
|
112
|
+
interaction,
|
|
113
|
+
command,
|
|
114
|
+
option: option.name
|
|
115
|
+
});
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
args.push(opt?.value);
|
|
119
|
+
}
|
|
120
|
+
if (!command.execute && !command.load) {
|
|
121
|
+
this.#nitro.hooks.callHook("discord:client:error", {
|
|
122
|
+
type: "SlashCommandExecutionError",
|
|
123
|
+
client: this,
|
|
124
|
+
interaction,
|
|
125
|
+
command,
|
|
126
|
+
error: new Error(`Slash command "${name}" cannot be executed or loaded`)
|
|
127
|
+
});
|
|
128
|
+
return;
|
|
129
|
+
} else if (!command.execute && command.load) {
|
|
130
|
+
try {
|
|
131
|
+
command.execute = await command.load();
|
|
132
|
+
} catch (error) {
|
|
133
|
+
this.#nitro.hooks.callHook("discord:client:error", {
|
|
134
|
+
type: "SlashCommandExecutionError",
|
|
135
|
+
client: this,
|
|
136
|
+
interaction,
|
|
137
|
+
command,
|
|
138
|
+
error
|
|
139
|
+
});
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
currentInteraction = interaction;
|
|
145
|
+
const result = await command.execute(...args);
|
|
146
|
+
currentInteraction = null;
|
|
147
|
+
this.#handleSlashCommandReturn(result, interaction, command);
|
|
148
|
+
} catch (error) {
|
|
149
|
+
this.#nitro.hooks.callHook("discord:client:error", {
|
|
150
|
+
type: "SlashCommandExecutionError",
|
|
151
|
+
client: this,
|
|
152
|
+
interaction,
|
|
153
|
+
command,
|
|
154
|
+
error
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
async #handleSlashCommandReturn(result, interaction, command) {
|
|
159
|
+
if (!result) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
try {
|
|
163
|
+
if (typeof result === "string") {
|
|
164
|
+
return interaction.reply({ content: result });
|
|
165
|
+
} else if (typeof result === "function") {
|
|
166
|
+
const newResult = result.call(this, interaction, this);
|
|
167
|
+
return this.#handleSlashCommandReturn(await newResult, interaction, command);
|
|
168
|
+
} else if (Symbol.iterator in result && typeof result[Symbol.iterator] === "function") {
|
|
169
|
+
const gen = result[Symbol.iterator]();
|
|
170
|
+
let next = gen.next();
|
|
171
|
+
while (!next.done) {
|
|
172
|
+
next = gen.next(await this.#handleSlashCommandReturn(await next.value, interaction, command));
|
|
173
|
+
}
|
|
174
|
+
if (next.value != null) {
|
|
175
|
+
return this.#handleSlashCommandReturn(next.value, interaction, command);
|
|
176
|
+
}
|
|
177
|
+
} else if (Symbol.asyncIterator in result && typeof result[Symbol.asyncIterator] === "function") {
|
|
178
|
+
const gen = result[Symbol.asyncIterator]();
|
|
179
|
+
let next = await gen.next();
|
|
180
|
+
while (!next.done) {
|
|
181
|
+
next = await gen.next(await this.#handleSlashCommandReturn(await next.value, interaction, command));
|
|
182
|
+
}
|
|
183
|
+
if (next.value != null) {
|
|
184
|
+
return this.#handleSlashCommandReturn(next.value, interaction, command);
|
|
185
|
+
}
|
|
186
|
+
} else {
|
|
187
|
+
if (result != null) {
|
|
188
|
+
throw new Error(`Unexpected return type from slash command: ${typeof result}`);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
} catch (error) {
|
|
192
|
+
this.#nitro.hooks.callHook("discord:client:error", {
|
|
193
|
+
type: "SlashCommandExecutionError",
|
|
194
|
+
client: this,
|
|
195
|
+
interaction,
|
|
196
|
+
command,
|
|
197
|
+
error
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
#getSlashCommandBuilder(command) {
|
|
202
|
+
const builder = new SlashCommandBuilder().setName(command.name).setDescription(command.description);
|
|
203
|
+
for (const option of command.options) {
|
|
204
|
+
switch (option.type) {
|
|
205
|
+
case ApplicationCommandOptionType.String:
|
|
206
|
+
builder.addStringOption((opt) => {
|
|
207
|
+
opt.setName(option.name).setRequired(option.required);
|
|
208
|
+
if (option.description.length > 0)
|
|
209
|
+
opt = opt.setDescription(option.description);
|
|
210
|
+
if (option.minLength)
|
|
211
|
+
opt = opt.setMinLength(option.minLength);
|
|
212
|
+
if (option.maxLength)
|
|
213
|
+
opt = opt.setMaxLength(option.maxLength);
|
|
214
|
+
if (option.choices)
|
|
215
|
+
opt = opt.addChoices(...option.choices);
|
|
216
|
+
return opt;
|
|
217
|
+
});
|
|
218
|
+
break;
|
|
219
|
+
case ApplicationCommandOptionType.Integer:
|
|
220
|
+
builder.addIntegerOption((opt) => {
|
|
221
|
+
opt.setName(option.name).setRequired(option.required);
|
|
222
|
+
if (option.description.length > 0)
|
|
223
|
+
opt = opt.setDescription(option.description);
|
|
224
|
+
if (option.min)
|
|
225
|
+
opt = opt.setMinValue(option.min);
|
|
226
|
+
if (option.max)
|
|
227
|
+
opt = opt.setMaxValue(option.max);
|
|
228
|
+
if (option.choices)
|
|
229
|
+
opt = opt.addChoices(...option.choices);
|
|
230
|
+
return opt;
|
|
231
|
+
});
|
|
232
|
+
break;
|
|
233
|
+
case ApplicationCommandOptionType.Number:
|
|
234
|
+
builder.addNumberOption((opt) => {
|
|
235
|
+
opt.setName(option.name).setRequired(option.required);
|
|
236
|
+
if (option.description.length > 0)
|
|
237
|
+
opt = opt.setDescription(option.description);
|
|
238
|
+
if (option.min)
|
|
239
|
+
opt = opt.setMinValue(option.min);
|
|
240
|
+
if (option.max)
|
|
241
|
+
opt = opt.setMaxValue(option.max);
|
|
242
|
+
if (option.choices)
|
|
243
|
+
opt = opt.addChoices(...option.choices);
|
|
244
|
+
return opt;
|
|
245
|
+
});
|
|
246
|
+
break;
|
|
247
|
+
case ApplicationCommandOptionType.Boolean:
|
|
248
|
+
builder.addBooleanOption((opt) => {
|
|
249
|
+
opt.setName(option.name).setRequired(option.required);
|
|
250
|
+
if (option.description.length > 0)
|
|
251
|
+
opt = opt.setDescription(option.description);
|
|
252
|
+
return opt;
|
|
253
|
+
});
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return builder;
|
|
258
|
+
}
|
|
259
|
+
#cachedRemoteSlashCommands = null;
|
|
260
|
+
async getRemoteSlashCommands() {
|
|
261
|
+
this.#cachedRemoteSlashCommands = await this.#rest.get(Routes.applicationCommands(this.#clientId));
|
|
262
|
+
return this.#cachedRemoteSlashCommands;
|
|
263
|
+
}
|
|
264
|
+
async diffRemoteSlashCommands(localCommands = this.#slashCommands, forceRefresh = false) {
|
|
265
|
+
if (!this.#cachedRemoteSlashCommands || forceRefresh) {
|
|
266
|
+
await this.getRemoteSlashCommands();
|
|
267
|
+
}
|
|
268
|
+
const remoteCommands = this.#cachedRemoteSlashCommands;
|
|
269
|
+
const nameToCommands = localCommands.reduce((acc, command) => {
|
|
270
|
+
(acc[command.name] ??= []).push(command);
|
|
271
|
+
return acc;
|
|
272
|
+
}, {});
|
|
273
|
+
return localCommands.reduce((acc, command) => {
|
|
274
|
+
const remoteCommand = remoteCommands.find((cmd) => cmd.name === command.name);
|
|
275
|
+
if (nameToCommands[command.name].length > 1) {
|
|
276
|
+
acc.conflict.push(command);
|
|
277
|
+
} else if (!remoteCommand) {
|
|
278
|
+
acc.added.push(command);
|
|
279
|
+
} else if (!propsEqual(command, remoteCommand, [
|
|
280
|
+
"description"
|
|
281
|
+
/* TODO: more */
|
|
282
|
+
]) || (command.options?.length ?? 0) !== (remoteCommand.options?.length ?? 0) || (remoteCommand.options ?? []).some((opt, i) => !optionEqual(command.options[i], opt))) {
|
|
283
|
+
acc.changed.push({ local: command, remote: remoteCommand });
|
|
284
|
+
} else {
|
|
285
|
+
if (remoteCommand.id && !command.id) {
|
|
286
|
+
command.id = remoteCommand.id;
|
|
287
|
+
}
|
|
288
|
+
acc.synced.push({ local: command, remote: remoteCommand });
|
|
289
|
+
}
|
|
290
|
+
return acc;
|
|
291
|
+
}, {
|
|
292
|
+
added: [],
|
|
293
|
+
removed: remoteCommands.filter(({ name }) => !localCommands.some((cmd) => cmd.name === name)),
|
|
294
|
+
changed: [],
|
|
295
|
+
synced: [],
|
|
296
|
+
conflict: []
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
async registerSlashCommands() {
|
|
300
|
+
const diff = await this.diffRemoteSlashCommands();
|
|
301
|
+
if (diff.added.length === 0 && diff.removed.length === 0 && diff.changed.length === 0) {
|
|
302
|
+
logger.log("No changes to slash commands, skipping registration.");
|
|
303
|
+
return [];
|
|
304
|
+
}
|
|
305
|
+
this.#cachedRemoteSlashCommands = null;
|
|
306
|
+
if (diff.added.length > 0 || diff.removed.length > 0 || diff.changed.length > 2) {
|
|
307
|
+
try {
|
|
308
|
+
const result = await this.#rest.put(
|
|
309
|
+
Routes.applicationCommands(this.#clientId),
|
|
310
|
+
{ body: this.#slashCommands.map((command) => this.#getSlashCommandBuilder(command).toJSON()) }
|
|
311
|
+
);
|
|
312
|
+
result.forEach((command, i) => {
|
|
313
|
+
const localCommand = this.#slashCommands[i];
|
|
314
|
+
if (localCommand) {
|
|
315
|
+
localCommand.id = command.id;
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
return result;
|
|
319
|
+
} catch (error) {
|
|
320
|
+
this.#nitro.hooks.callHook("discord:client:error", {
|
|
321
|
+
type: "SlashCommandRegistrationError",
|
|
322
|
+
client: this,
|
|
323
|
+
error
|
|
324
|
+
});
|
|
325
|
+
return [];
|
|
326
|
+
}
|
|
327
|
+
} else {
|
|
328
|
+
const results = await Promise.allSettled(
|
|
329
|
+
diff.changed.map((command) => {
|
|
330
|
+
logger.log(`Updating slash command: ${command.local.name}`);
|
|
331
|
+
const builder = this.#getSlashCommandBuilder(command.local);
|
|
332
|
+
return this.#rest.patch(
|
|
333
|
+
Routes.applicationCommand(this.#clientId, command.remote.id),
|
|
334
|
+
{ body: builder.toJSON() }
|
|
335
|
+
);
|
|
336
|
+
})
|
|
337
|
+
);
|
|
338
|
+
results.forEach((result, i) => {
|
|
339
|
+
logger.log(result);
|
|
340
|
+
if (result.status === "rejected") {
|
|
341
|
+
this.#nitro.hooks.callHook("discord:client:error", {
|
|
342
|
+
type: "SlashCommandRegistrationError",
|
|
343
|
+
client: this,
|
|
344
|
+
interaction: currentInteraction,
|
|
345
|
+
command: result.reason.command,
|
|
346
|
+
error: result.reason.error
|
|
347
|
+
});
|
|
348
|
+
} else {
|
|
349
|
+
const command = diff.changed[i];
|
|
350
|
+
command.local.id = result.value.id;
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
return results;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
function propsEqual(a, b, props) {
|
|
358
|
+
for (const prop of props) {
|
|
359
|
+
if (a[prop] !== b[prop]) {
|
|
360
|
+
return false;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return true;
|
|
364
|
+
}
|
|
365
|
+
function optionEqual(a, b) {
|
|
366
|
+
if (a.name !== b.name || a.description !== b.description || a.required !== b.required || a.type !== b.type) {
|
|
367
|
+
return false;
|
|
368
|
+
}
|
|
369
|
+
if (!!("choices" in a && a.choices) !== !!("choices" in b && b.choices)) {
|
|
370
|
+
return false;
|
|
371
|
+
} else if ("choices" in a && a.choices && "choices" in b && b.choices) {
|
|
372
|
+
if (a.choices.length !== b.choices.length) {
|
|
373
|
+
return false;
|
|
374
|
+
}
|
|
375
|
+
for (let i = 0; i < a.choices.length; i++) {
|
|
376
|
+
if (a.choices[i].name !== b.choices[i].name || a.choices[i].value !== b.choices[i].value) {
|
|
377
|
+
return false;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
if (a.type === ApplicationCommandOptionType.Integer || a.type === ApplicationCommandOptionType.Number) {
|
|
382
|
+
if ("min" in a !== "min_value" in b) {
|
|
383
|
+
return false;
|
|
384
|
+
} else if ("min" in a && "min_value" in b && a.min !== b.min_value) {
|
|
385
|
+
return false;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
if (a.type === ApplicationCommandOptionType.String) {
|
|
389
|
+
if ("minLength" in a !== "min_length" in b) {
|
|
390
|
+
return false;
|
|
391
|
+
} else if ("minLength" in a && "min_length" in b && a.minLength !== b.min_length) {
|
|
392
|
+
return false;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return true;
|
|
396
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { SlashCommandOptionType, SlashCommandReturnType } from '~/src/types';
|
|
2
|
+
/**
|
|
3
|
+
* Defines a slash command function that is automatically registered
|
|
4
|
+
* by the Nuxt Discord module.
|
|
5
|
+
*
|
|
6
|
+
* @param command - The function that implements the slash command.
|
|
7
|
+
*/
|
|
8
|
+
export declare function defineSlashCommand(command: {
|
|
9
|
+
exec(...args: (SlashCommandOptionType | undefined)[]): SlashCommandReturnType;
|
|
10
|
+
}['exec']): (...args: (SlashCommandOptionType | undefined)[]) => SlashCommandReturnType;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface DescribeCommandOptions {
|
|
2
|
+
/** The name of the command. */
|
|
3
|
+
name?: string;
|
|
4
|
+
/** A brief description of the command. */
|
|
5
|
+
description?: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* A compiler macro that describes a command. Used for defining metadata
|
|
9
|
+
* for registering slash commands. Options defined here will take higher
|
|
10
|
+
* precedence over the inferred function name and JSDoc tags.
|
|
11
|
+
*
|
|
12
|
+
* @param _options - The options for the command.
|
|
13
|
+
* @example
|
|
14
|
+
* describeCommand({
|
|
15
|
+
* name: 'ping',
|
|
16
|
+
* description: 'A simple ping command that responds with "pong!"',
|
|
17
|
+
* })
|
|
18
|
+
*/
|
|
19
|
+
export declare function describeCommand(_options: DescribeCommandOptions): void;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { APIApplicationCommandOptionChoice } from 'discord.js';
|
|
2
|
+
import type { SlashCommandOptionType } from '~/src/types';
|
|
3
|
+
export interface DescribeOptionOptionsBase {
|
|
4
|
+
/** The name of the command. */
|
|
5
|
+
name?: string;
|
|
6
|
+
/** A brief description of the command. */
|
|
7
|
+
description?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface IntegerOption extends DescribeOptionOptionsBase {
|
|
10
|
+
/** The minimum value for the integer option. */
|
|
11
|
+
min?: number;
|
|
12
|
+
/** The maximum value for the integer option. */
|
|
13
|
+
max?: number;
|
|
14
|
+
/** An array of choices for the integer option. */
|
|
15
|
+
choices?: APIApplicationCommandOptionChoice<number>[];
|
|
16
|
+
}
|
|
17
|
+
export interface NumberOption extends DescribeOptionOptionsBase {
|
|
18
|
+
/** The minimum value for the number option. */
|
|
19
|
+
min?: number;
|
|
20
|
+
/** The maximum value for the number option. */
|
|
21
|
+
max?: number;
|
|
22
|
+
/** An array of choices for the number option. */
|
|
23
|
+
choices?: APIApplicationCommandOptionChoice<number>[];
|
|
24
|
+
}
|
|
25
|
+
export interface StringOption extends DescribeOptionOptionsBase {
|
|
26
|
+
/** The minimum length of the string option. */
|
|
27
|
+
minLength?: number;
|
|
28
|
+
/** The maximum length of the string option. */
|
|
29
|
+
maxLength?: number;
|
|
30
|
+
/** An array of choices for the string option. */
|
|
31
|
+
choices?: APIApplicationCommandOptionChoice<string>[];
|
|
32
|
+
}
|
|
33
|
+
export type DescribeOptionOptions = IntegerOption | NumberOption | StringOption;
|
|
34
|
+
/**
|
|
35
|
+
*/
|
|
36
|
+
export declare function describeOption<const T extends SlashCommandOptionType>(_option: T | undefined, _options: T extends integer ? IntegerOption : T extends number ? NumberOption : T extends string ? StringOption : DescribeOptionOptionsBase): void;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { InteractionReplyOptions } from 'discord.js';
|
|
2
|
+
import type { SlashCommandCustomReturnHandler } from '~/src/types';
|
|
3
|
+
interface ReplyFunction {
|
|
4
|
+
(text: string, options?: InteractionReplyOptions): SlashCommandCustomReturnHandler;
|
|
5
|
+
ephemeral: this;
|
|
6
|
+
flags: (flags: InteractionReplyOptions['flags']) => this;
|
|
7
|
+
}
|
|
8
|
+
export declare const reply: ReplyFunction;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { MessageFlags } from "discord.js";
|
|
2
|
+
function createReplyFunction(defaultOptions = {}) {
|
|
3
|
+
const reply2 = (text, options) => (interaction) => {
|
|
4
|
+
interaction.reply({
|
|
5
|
+
content: text,
|
|
6
|
+
...defaultOptions,
|
|
7
|
+
...options
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
return new Proxy(reply2, {
|
|
11
|
+
get(target, prop, receiver) {
|
|
12
|
+
if (prop === "ephemeral") {
|
|
13
|
+
return createReplyFunction({
|
|
14
|
+
...defaultOptions,
|
|
15
|
+
flags: defaultOptions.flags == null ? MessageFlags.Ephemeral : typeof defaultOptions.flags === "string" ? [defaultOptions.flags, "Ephemeral"] : Array.isArray(defaultOptions.flags) ? [...defaultOptions.flags, "Ephemeral"] : typeof defaultOptions.flags === "number" || typeof defaultOptions.flags === "bigint" ? defaultOptions.flags | MessageFlags.Ephemeral : MessageFlags.Ephemeral
|
|
16
|
+
});
|
|
17
|
+
} else if (prop === "flags") {
|
|
18
|
+
return (flags) => createReplyFunction({
|
|
19
|
+
...defaultOptions,
|
|
20
|
+
flags
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
return Reflect.get(target, prop, receiver);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
export const reply = createReplyFunction();
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createError } from "h3";
|
|
2
|
+
export function useDiscordClient() {
|
|
3
|
+
const client = globalThis[Symbol.for("discord-client")];
|
|
4
|
+
if (!client) {
|
|
5
|
+
throw createError({
|
|
6
|
+
statusCode: 500,
|
|
7
|
+
statusMessage: "Discord client not initialized"
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
return client;
|
|
11
|
+
}
|
package/dist/types.d.mts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { NuxtModule } from '@nuxt/schema'
|
|
2
|
+
|
|
3
|
+
import type { default as Module } from './module.mjs'
|
|
4
|
+
|
|
5
|
+
export type ModuleOptions = typeof Module extends NuxtModule<infer O> ? Partial<O> : Record<string, any>
|
|
6
|
+
|
|
7
|
+
export { default, type slashCommandOptionTypeIdentifiers, type typeIdentifierToEnum } from './module.mjs'
|
|
8
|
+
|
|
9
|
+
export { type DiscordRuntimeConfig, type NuxtDiscordContext, type NuxtDiscordOptions, type SlashCommand, type SlashCommandBooleanOption, type SlashCommandCustomReturnHandler, type SlashCommandIntegerOption, type SlashCommandNumberOption, type SlashCommandOption, type SlashCommandOptionBase, type SlashCommandOptionType, type SlashCommandOptionTypeIdentifier, type SlashCommandReturnType, type SlashCommandRuntime, type SlashCommandStringOption, type integer } from './module.mjs'
|
package/package.json
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nuxt-discord",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"description": "A Nuxt module for building Discord bots",
|
|
6
|
+
"author": "Jize Zou <jeffzou2001@gmail.com> (https://github.com/zojize)",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": "git+https://github.com/zojize/nuxt-discord.git",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"nuxt",
|
|
11
|
+
"nuxt-module",
|
|
12
|
+
"discord",
|
|
13
|
+
"discord-bot"
|
|
14
|
+
],
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/module.d.mts",
|
|
18
|
+
"import": "./dist/module.mjs"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"main": "./dist/module.mjs",
|
|
22
|
+
"types": "./dist/module.d.mts",
|
|
23
|
+
"typesVersions": {
|
|
24
|
+
"*": {
|
|
25
|
+
".": [
|
|
26
|
+
"./dist/types.d.mts"
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist"
|
|
32
|
+
],
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"discord.js": "^14.21.0"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@nuxt/kit": "^3.17.5",
|
|
38
|
+
"@nuxt/ui": "^3.2.0",
|
|
39
|
+
"ansis": "^4.1.0",
|
|
40
|
+
"defu": "^6.1.4",
|
|
41
|
+
"knitwork": "^1.2.0",
|
|
42
|
+
"listhen": "^1.9.0",
|
|
43
|
+
"simple-icons": "^15.3.0",
|
|
44
|
+
"unocss": "^66.3.2",
|
|
45
|
+
"ws": "^8.18.2"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@antfu/eslint-config": "^4.16.1",
|
|
49
|
+
"@nuxt/devtools": "^2.5.0",
|
|
50
|
+
"@nuxt/eslint-config": "^1.4.1",
|
|
51
|
+
"@nuxt/module-builder": "^1.0.1",
|
|
52
|
+
"@nuxt/schema": "^3.17.5",
|
|
53
|
+
"@nuxt/test-utils": "^3.19.1",
|
|
54
|
+
"@types/node": "^24.0.4",
|
|
55
|
+
"@unocss/eslint-plugin": "^66.3.2",
|
|
56
|
+
"@vueuse/core": "^13.4.0",
|
|
57
|
+
"changelogen": "^0.6.1",
|
|
58
|
+
"eslint": "^9.29.0",
|
|
59
|
+
"nuxt": "^3.17.5",
|
|
60
|
+
"typescript": "~5.8.3",
|
|
61
|
+
"unbuild": "^3.5.0",
|
|
62
|
+
"vitest": "^3.2.4",
|
|
63
|
+
"vue-tsc": "^2.2.10"
|
|
64
|
+
},
|
|
65
|
+
"resolutions": {
|
|
66
|
+
"nuxt-discord": "workspace:*"
|
|
67
|
+
},
|
|
68
|
+
"scripts": {
|
|
69
|
+
"dev": "pnpm run dev:prepare && nuxi dev playground",
|
|
70
|
+
"dev:build": "nuxi build playground",
|
|
71
|
+
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
|
|
72
|
+
"release": "pnpm run lint && pnpm run test && pnpm run prepack && changelogen --release && pnpm publish && git push --follow-tags",
|
|
73
|
+
"lint": "eslint .",
|
|
74
|
+
"test": "vitest run",
|
|
75
|
+
"test:watch": "vitest watch",
|
|
76
|
+
"test:types": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit"
|
|
77
|
+
}
|
|
78
|
+
}
|