nuxt-discord 0.0.11 → 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.11",
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) {
@@ -415,8 +418,8 @@ async function generateDynamicCommandBuild(file, config, ctx) {
415
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,7 @@ export class DiscordClient {
161
161
  }
162
162
  try {
163
163
  if (typeof result === "string") {
164
- if (interaction.deferred) {
164
+ if (interaction.deferred || interaction.replied) {
165
165
  return interaction.editReply({ content: result });
166
166
  } else {
167
167
  return interaction.reply({ content: result });
@@ -344,7 +344,6 @@ export class DiscordClient {
344
344
  this.#nitro.hooks.callHook("discord:client:error", {
345
345
  type: "SlashCommandRegistrationError",
346
346
  client: this,
347
- interaction: currentInteraction,
348
347
  command: result.reason.command,
349
348
  error: result.reason.error
350
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,15 +1,22 @@
1
- import type { InteractionEditReplyOptions, InteractionReplyOptions } from 'discord.js';
2
- import type { SlashCommandCustomReturnHandler } from '~/src/types';
1
+ import type { BaseMessageOptions, InteractionEditReplyOptions, InteractionReplyOptions } from 'discord.js';
2
+ import type { SlashCommandCustomReturnHandler } from '../../../types.js';
3
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
- interface ReplyFunction {
5
- (text: string, options?: InteractionReplyOptions): SlashCommandCustomReturnHandler;
4
+ type File = NonNullable<BaseMessageOptions['files']>[number];
5
+ type ReplyFunction = SlashCommandCustomReturnHandler & {
6
+ (text?: string, options?: InteractionReplyOptions): SlashCommandCustomReturnHandler;
6
7
  edit: EditReplyFunction;
7
8
  ephemeral: OmitPreservingCallSignature<ReplyFunction, 'edit' | 'ephemeral'>;
8
9
  flags: (flags: InteractionReplyOptions['flags']) => OmitPreservingCallSignature<ReplyFunction, 'edit'>;
9
- }
10
- interface EditReplyFunction {
11
- (text: string, options?: InteractionEditReplyOptions): SlashCommandCustomReturnHandler;
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;
12
16
  flags: (flags: InteractionEditReplyOptions['flags']) => EditReplyFunction;
13
- }
17
+ file: (file: File) => EditReplyFunction;
18
+ files: (files: File[]) => EditReplyFunction;
19
+ send: EditReplyFunction;
20
+ };
14
21
  export declare const reply: ReplyFunction;
15
22
  export {};
@@ -1,18 +1,34 @@
1
1
  import { MessageFlags } from "discord.js";
2
2
  function createReplyFunction({ editReply, ...defaultOptions } = {}) {
3
- const reply2 = (text, options) => async (interaction) => {
4
- if (editReply) {
5
- await interaction.editReply({
6
- content: text,
7
- ...defaultOptions,
8
- ...options
9
- });
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
+ };
10
21
  } else {
11
- await interaction.reply({
12
- content: text,
13
- ...defaultOptions,
14
- ...options
15
- });
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
+ }
16
32
  }
17
33
  };
18
34
  Object.defineProperties(reply2, {
@@ -39,6 +55,25 @@ function createReplyFunction({ editReply, ...defaultOptions } = {}) {
39
55
  editReply: true
40
56
  });
41
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
42
77
  }
43
78
  });
44
79
  return reply2;
@@ -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.11",
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
  }