nuxt-discord 0.0.10 → 0.0.12

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/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-discord",
3
3
  "configKey": "discord",
4
- "version": "0.0.10",
4
+ "version": "0.0.12",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.1",
7
7
  "unbuild": "3.5.0"
package/dist/module.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { createResolver, installModule, extendPages, updateTemplates, addVitePlugin, isIgnored, addServerTemplate, addTemplate, addTypeTemplate, defineNuxtModule, addServerScanDir, addImports } from '@nuxt/kit';
2
2
  import { ApplicationCommandOptionType, GatewayIntentBits } from 'discord.js';
3
3
  import { logger } from '../dist/runtime/logger.js';
4
- import { globSync, readFileSync, existsSync, mkdirSync, writeFileSync } from 'node:fs';
4
+ import fs, { globSync, readFileSync } from 'node:fs';
5
5
  import path from 'node:path';
6
6
  import ts from 'typescript';
7
7
  import chokidar from 'chokidar';
@@ -308,6 +308,9 @@ function prepareHMR(ctx) {
308
308
  });
309
309
  });
310
310
  }
311
+ const commandsBuildDir = path.join(ctx.nuxt.options.buildDir, "discord", "commands");
312
+ fs.rmSync(commandsBuildDir, { recursive: true, force: true });
313
+ fs.mkdirSync(commandsBuildDir, { recursive: true });
311
314
  chokidar.watch(ctx.resolve.root(ctx.options.dir, "commands"), { ignoreInitial: true }).on("all", async (event, path2) => {
312
315
  if (event === "all" || event === "error" || event === "raw") {
313
316
  return;
@@ -331,7 +334,7 @@ function prepareHMR(ctx) {
331
334
  websocket?.close();
332
335
  });
333
336
  const packageJson = ctx.resolve.module("../package.json");
334
- if (existsSync(packageJson) && JSON.parse(readFileSync(packageJson, "utf-8")).name === "nuxt-discord") {
337
+ if (fs.existsSync(packageJson) && JSON.parse(fs.readFileSync(packageJson, "utf-8")).name === "nuxt-discord") {
335
338
  (async () => {
336
339
  const { createGenerator } = await import('unocss');
337
340
  const config = (await import('../dist/runtime/client/uno.config.js')).default;
@@ -339,14 +342,14 @@ function prepareHMR(ctx) {
339
342
  const clientDir = ctx.resolve.module("runtime/client");
340
343
  const generateCSS = async (path2) => {
341
344
  if (path2.startsWith(clientDir) && path2.endsWith(".vue")) {
342
- const content = readFileSync(path2, "utf-8");
345
+ const content = fs.readFileSync(path2, "utf-8");
343
346
  const tokens = await generator.applyExtractors(content, path2);
344
347
  const { css } = await generator.generate(tokens);
345
- writeFileSync(path2.replace(".vue", ".css"), css, "utf-8");
348
+ fs.writeFileSync(path2.replace(".vue", ".css"), css, "utf-8");
346
349
  }
347
350
  };
348
351
  const globPattern = `${clientDir}/**/*.vue`;
349
- globSync(globPattern).forEach(generateCSS);
352
+ fs.globSync(globPattern).forEach(generateCSS);
350
353
  addVitePlugin({
351
354
  name: "dev-nuxt-discord-generate-css",
352
355
  configureServer(server) {
@@ -411,12 +414,12 @@ async function generateDynamicCommandBuild(file, config, ctx) {
411
414
  bundle = await rollup({
412
415
  ...config,
413
416
  input: file,
414
- // externalize all modules except the command file itself
415
- external: (id) => !id.includes(file)
417
+ // externalize all modules except the command file itself, excluding .ts files
418
+ external: (id) => !id.includes(file) && !id.endsWith(".ts")
416
419
  });
417
420
  const { output } = await bundle.generate({ ...config.output, sourcemap: false });
418
- mkdirSync(path.join(ctx.nuxt.options.buildDir, "discord", "commands"), { recursive: true });
419
- writeFileSync(file.replace(ctx.resolve.root(ctx.nuxt.options.rootDir), ctx.nuxt.options.buildDir).replace(".ts", ".mjs"), output[0].code, { encoding: "utf-8", flush: true });
421
+ fs.mkdirSync(path.join(ctx.nuxt.options.buildDir, "discord", "commands"), { recursive: true });
422
+ fs.writeFileSync(file.replace(ctx.resolve.root(ctx.nuxt.options.rootDir), ctx.nuxt.options.buildDir).replace(".ts", ".mjs"), output[0].code, { encoding: "utf-8", flush: true });
420
423
  } catch (error) {
421
424
  ctx.logger.error(`Error processing command file ${file}:`, error);
422
425
  return;
@@ -1,2 +1,4 @@
1
- declare const _default: any;
1
+ declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<{
2
+ commands: import("../../../../../types.js").SlashCommandRuntime[];
3
+ }>>;
2
4
  export default _default;
@@ -1,8 +1,9 @@
1
- import { defineEventHandler, useDiscordClient } from "#imports";
1
+ import { defineEventHandler } from "h3";
2
+ import { useDiscordClient } from "../../../utils/useDiscordClient.js";
2
3
  export default defineEventHandler(async () => {
3
4
  const client = useDiscordClient();
4
5
  const commands = client.getSlashCommands();
5
6
  return {
6
- commands: commands.map(({ execute, load, ...command }) => command)
7
+ commands
7
8
  };
8
9
  });
@@ -1,2 +1,2 @@
1
- declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<any>>;
1
+ declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<import("discord.js").RESTPutAPIApplicationCommandsResult | PromiseSettledResult<import("discord.js").APIApplicationCommand>[]>>;
2
2
  export default _default;
@@ -1,5 +1,5 @@
1
- import { useDiscordClient } from "#imports";
2
1
  import { defineEventHandler } from "h3";
2
+ import { useDiscordClient } from "../../../utils/useDiscordClient.js";
3
3
  export default defineEventHandler(async () => {
4
4
  const client = useDiscordClient();
5
5
  return client.registerSlashCommands();
@@ -1,2 +1,4 @@
1
- declare const _default: any;
1
+ declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<{
2
+ commands: import("discord.js").RESTGetAPIApplicationCommandsResult;
3
+ }>>;
2
4
  export default _default;
@@ -1,4 +1,5 @@
1
- import { defineEventHandler, useDiscordClient } from "#imports";
1
+ import { defineEventHandler } from "h3";
2
+ import { useDiscordClient } from "../../../utils/useDiscordClient.js";
2
3
  export default defineEventHandler(async () => {
3
4
  const client = useDiscordClient();
4
5
  const commands = await client.getRemoteSlashCommands();
@@ -1,5 +1,5 @@
1
1
  import type { APIApplicationCommand } from 'discord.js';
2
- import type { SlashCommand, SlashCommandRuntime } from '~/src/types';
2
+ import type { SlashCommand, SlashCommandRuntime } from '../../../../../types.js';
3
3
  declare const _default: import("h3").EventHandler<{
4
4
  body?: {
5
5
  commands?: SlashCommand[];
@@ -1,5 +1,5 @@
1
- import { useDiscordClient } from "#imports";
2
1
  import { defineEventHandler, readBody } from "h3";
2
+ import { useDiscordClient } from "../../../utils/useDiscordClient.js";
3
3
  export default defineEventHandler(async (event) => {
4
4
  const client = useDiscordClient();
5
5
  const body = await readBody(event);
@@ -1,5 +1,5 @@
1
1
  import type { APIApplicationCommand, ChatInputCommandInteraction, RESTGetAPIApplicationCommandsResult, RESTPutAPIApplicationCommandsResult } from 'discord.js';
2
- import type { NuxtDiscordOptions, SlashCommandRuntime } from '~/src/types';
2
+ import type { NuxtDiscordOptions, SlashCommandRuntime } from '../../../types.js';
3
3
  import { Client as InternalClient } from 'discord.js';
4
4
  export interface DiscordClientErrorBase {
5
5
  type: string;
@@ -161,7 +161,11 @@ export class DiscordClient {
161
161
  }
162
162
  try {
163
163
  if (typeof result === "string") {
164
- return interaction.reply({ content: result });
164
+ if (interaction.deferred || interaction.replied) {
165
+ return interaction.editReply({ content: result });
166
+ } else {
167
+ return interaction.reply({ content: result });
168
+ }
165
169
  } else if (typeof result === "function") {
166
170
  const newResult = result.call(this, interaction, this);
167
171
  return this.#handleSlashCommandReturn(newResult, interaction, command);
@@ -340,7 +344,6 @@ export class DiscordClient {
340
344
  this.#nitro.hooks.callHook("discord:client:error", {
341
345
  type: "SlashCommandRegistrationError",
342
346
  client: this,
343
- interaction: currentInteraction,
344
347
  command: result.reason.command,
345
348
  error: result.reason.error
346
349
  });
@@ -1,10 +1,8 @@
1
- import type { SlashCommandOptionType, SlashCommandReturnType } from '~/src/types';
1
+ import type { SlashCommandOptionType, SlashCommandReturnType } from '../../../types.js';
2
2
  /**
3
3
  * Defines a slash command function that is automatically registered
4
4
  * by the Nuxt Discord module.
5
5
  *
6
6
  * @param command - The function that implements the slash command.
7
7
  */
8
- export declare function defineSlashCommand(command: {
9
- exec(...args: (SlashCommandOptionType | undefined)[]): SlashCommandReturnType;
10
- }['exec']): (...args: (SlashCommandOptionType | undefined)[]) => SlashCommandReturnType;
8
+ export declare function defineSlashCommand<Args extends (SlashCommandOptionType | undefined)[]>(command: (...args: Args) => SlashCommandReturnType): (...args: Args) => SlashCommandReturnType;
@@ -1,5 +1,5 @@
1
1
  import type { APIApplicationCommandOptionChoice } from 'discord.js';
2
- import type { SlashCommandOptionType } from '~/src/types';
2
+ import type { SlashCommandOptionType } from '../../../types.js';
3
3
  export interface DescribeOptionOptionsBase {
4
4
  /** The name of the command. */
5
5
  name?: string;
@@ -1,9 +1,22 @@
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
- }
1
+ import type { BaseMessageOptions, InteractionEditReplyOptions, InteractionReplyOptions } from 'discord.js';
2
+ import type { SlashCommandCustomReturnHandler } from '../../../types.js';
3
+ type OmitPreservingCallSignature<T, K extends keyof T = keyof T> = Omit<T, K> & (T extends (...args: infer R) => infer S ? (...args: R) => S : unknown);
4
+ type File = NonNullable<BaseMessageOptions['files']>[number];
5
+ type ReplyFunction = SlashCommandCustomReturnHandler & {
6
+ (text?: string, options?: InteractionReplyOptions): SlashCommandCustomReturnHandler;
7
+ edit: EditReplyFunction;
8
+ ephemeral: OmitPreservingCallSignature<ReplyFunction, 'edit' | 'ephemeral'>;
9
+ flags: (flags: InteractionReplyOptions['flags']) => OmitPreservingCallSignature<ReplyFunction, 'edit'>;
10
+ file: (file: File) => OmitPreservingCallSignature<ReplyFunction, 'edit'>;
11
+ files: (files: File[]) => OmitPreservingCallSignature<ReplyFunction, 'edit'>;
12
+ send: ReplyFunction;
13
+ };
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
+ };
8
21
  export declare const reply: ReplyFunction;
9
22
  export {};
@@ -1,27 +1,81 @@
1
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
- });
2
+ function createReplyFunction({ editReply, ...defaultOptions } = {}) {
3
+ const reply2 = (...args) => {
4
+ if (typeof args[0] === "string" || typeof args[0] === "undefined") {
5
+ const [text, options] = args;
6
+ return async (interaction) => {
7
+ if (editReply || interaction.deferred || interaction.replied) {
8
+ await interaction.editReply({
9
+ content: text,
10
+ ...defaultOptions,
11
+ ...options
12
+ });
13
+ } else {
14
+ await interaction.reply({
15
+ content: text,
16
+ ...defaultOptions,
17
+ ...options
18
+ });
19
+ }
20
+ };
21
+ } else {
22
+ const interaction = args[0];
23
+ if (editReply || interaction.deferred || interaction.replied) {
24
+ return interaction.editReply({
25
+ ...defaultOptions
26
+ }).then(() => void 0);
27
+ } else {
28
+ return interaction.reply({
29
+ ...defaultOptions
30
+ }).then(() => void 0);
31
+ }
32
+ }
9
33
  };
10
- return new Proxy(reply2, {
11
- get(target, prop, receiver) {
12
- if (prop === "ephemeral") {
34
+ Object.defineProperties(reply2, {
35
+ ephemeral: {
36
+ get() {
13
37
  return createReplyFunction({
14
38
  ...defaultOptions,
15
39
  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
40
  });
17
- } else if (prop === "flags") {
41
+ }
42
+ },
43
+ flags: {
44
+ get() {
18
45
  return (flags) => createReplyFunction({
19
46
  ...defaultOptions,
20
47
  flags
21
48
  });
22
49
  }
23
- return Reflect.get(target, prop, receiver);
50
+ },
51
+ edit: {
52
+ get() {
53
+ return createReplyFunction({
54
+ ...defaultOptions,
55
+ editReply: true
56
+ });
57
+ }
58
+ },
59
+ file: {
60
+ get() {
61
+ return (file) => createReplyFunction({
62
+ ...defaultOptions,
63
+ files: defaultOptions.files != null ? [...defaultOptions.files, file] : [file]
64
+ });
65
+ }
66
+ },
67
+ files: {
68
+ get() {
69
+ return (files) => createReplyFunction({
70
+ ...defaultOptions,
71
+ files: defaultOptions.files != null ? [...defaultOptions.files, ...files] : files
72
+ });
73
+ }
74
+ },
75
+ send: {
76
+ value: reply2
24
77
  }
25
78
  });
79
+ return reply2;
26
80
  }
27
81
  export const reply = createReplyFunction();
@@ -1,4 +1,4 @@
1
- import type { DiscordClient } from '#imports';
1
+ import type { DiscordClient } from './client.js';
2
2
  /**
3
3
  * Retrieves the Discord client from the event context.
4
4
  */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-discord",
3
3
  "type": "module",
4
- "version": "0.0.10",
4
+ "version": "0.0.12",
5
5
  "description": "A Nuxt module for building Discord bots",
6
6
  "author": "Jize Zou <jeffzou2001@gmail.com> (https://github.com/zojize)",
7
7
  "license": "MIT",
@@ -83,6 +83,6 @@
83
83
  "lint": "eslint .",
84
84
  "test": "vitest run",
85
85
  "test:watch": "vitest watch",
86
- "test:types": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit"
86
+ "test:types": "vue-tsc --noEmit"
87
87
  }
88
88
  }