nuxt-discord 0.0.14 → 0.0.16

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.14",
4
+ "version": "0.0.16",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.1",
7
7
  "unbuild": "3.5.0"
package/dist/module.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { createResolver, installModule, extendPages, updateTemplates, addVitePlugin, isIgnored, addServerTemplate, addTemplate, addTypeTemplate, defineNuxtModule, addServerScanDir, addImports } from '@nuxt/kit';
1
+ import { createResolver, installModule, extendPages, updateTemplates, addVitePlugin, isIgnored, resolveAlias, 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
4
  import fs, { globSync, readFileSync } from 'node:fs';
@@ -629,8 +629,17 @@ async function generateDynamicCommandBuild(file, config, ctx) {
629
629
  bundle = await rollup({
630
630
  ...config,
631
631
  input: file,
632
- // externalize all modules except the command file itself, excluding .ts files
633
- external: (id) => !id.includes(file) && !id.endsWith(".ts")
632
+ plugins: [
633
+ config.plugins,
634
+ {
635
+ name: "discord:resolve-alias-and-externalize",
636
+ resolveId: (id) => {
637
+ if (!id.includes(file) && !id.endsWith(".ts")) {
638
+ return { id: resolveAlias(id), external: true };
639
+ }
640
+ }
641
+ }
642
+ ]
634
643
  });
635
644
  const { output } = await bundle.generate({ ...config.output, sourcemap: false });
636
645
  fs.mkdirSync(path.join(ctx.nuxt.options.buildDir, "discord", "commands"), { recursive: true });
@@ -1,7 +1,6 @@
1
1
  <script setup>
2
2
  import { computed, ref, useFetch, useRuntimeConfig, watchEffect } from "#imports";
3
3
  import { useWebSocket } from "@vueuse/core";
4
- import slashCommands from "discord/slashCommands";
5
4
  import TheHeader from "../components/TheHeader.vue";
6
5
  import "../style.css";
7
6
  import "./slash-commands.css";
@@ -70,7 +69,7 @@ function getRemoteId(command) {
70
69
  }
71
70
  return void 0;
72
71
  }
73
- const commands = ref(slashCommands);
72
+ const commands = ref([]);
74
73
  const {
75
74
  data: diff,
76
75
  refresh: refreshDiff
@@ -1,8 +1,14 @@
1
- import type { BaseMessageOptions, InteractionCallbackResponse, InteractionReplyOptions, InteractionResponse, Message } from 'discord.js';
1
+ import type { BaseMessageOptions, ButtonComponentData, ButtonInteraction, InteractionCallbackResponse, InteractionReplyOptions, InteractionResponse, Message, MessageCollectorOptionsParams } from 'discord.js';
2
2
  import type { MaybeRef } from 'vue';
3
3
  import type { SlashCommandCustomReturnHandler } from '../../../types.js';
4
+ import { ComponentType } from 'discord.js';
4
5
  type OmitPreservingCallSignature<T, K extends keyof T = keyof T> = Omit<T, K> & (T extends (...args: infer R) => infer S ? (...args: R) => S : unknown);
5
6
  type File = NonNullable<BaseMessageOptions['files']>[number];
7
+ type ButtonCollectorOptions = Partial<Omit<MessageCollectorOptionsParams<ComponentType.Button>, 'componentType'>>;
8
+ type MaybeRefObject<T> = T extends any ? {
9
+ [K in keyof T]: MaybeRef<T[K]>;
10
+ } : never;
11
+ type DistributiveOmit<T, K extends keyof T> = T extends any ? Omit<T, K> : never;
6
12
  type ReplyFunction = SlashCommandCustomReturnHandler & {
7
13
  (text?: MaybeRef<string>, options?: InteractionReplyOptions): SlashCommandCustomReturnHandler & {
8
14
  [Symbol.iterator]: () => {
@@ -16,7 +22,15 @@ type ReplyFunction = SlashCommandCustomReturnHandler & {
16
22
  flags: (flags: MaybeRef<InteractionReplyOptions['flags']>) => ReplyFunction;
17
23
  file: (file: MaybeRef<File>) => ReplyFunction;
18
24
  files: (files: MaybeRef<File[]>) => ReplyFunction;
25
+ button: {
26
+ (label: MaybeRef<string>, handler: (interaction: ButtonInteraction) => void | Promise<void>, options?: Partial<MaybeRefObject<DistributiveOmit<ButtonComponentData, 'label' | 'type'>>> & {
27
+ defer?: boolean;
28
+ hide?: MaybeRef<boolean>;
29
+ collector?: ButtonCollectorOptions;
30
+ }): ReplyFunction;
31
+ };
19
32
  send: ReplyFunction;
20
33
  };
21
34
  export declare const reply: ReplyFunction;
35
+ export declare function deepToRaw<T extends Record<string, any>>(sourceObj: T): T;
22
36
  export {};
@@ -1,20 +1,58 @@
1
- import { MessageFlags } from "discord.js";
2
- import { isRef, reactive, watch } from "vue";
3
- function createReplyFunction({ ...defaultOptions } = {}) {
1
+ import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ComponentType, MessageFlags } from "discord.js";
2
+ import { computed, isProxy, isReactive, isRef, reactive, toRaw, toValue, watch } from "vue";
3
+ import { logger } from "../../logger.js";
4
+ function createReplyFunction(defaultOptions = {}) {
4
5
  const reply2 = (...args) => {
5
- if (typeof args[0] === "string" || typeof args[0] === "undefined" || isRef(args[0]) || typeof args[0] === "function") {
6
+ const { buttons, ...restOptions } = defaultOptions;
7
+ const buttonComponents = computed(() => getButtonComponent(buttons ?? []));
8
+ let buttonInteractionCollectors = [];
9
+ function registerButtonCollectors(msg) {
10
+ for (const collector of buttonInteractionCollectors) {
11
+ collector.stop();
12
+ }
13
+ buttonInteractionCollectors = [];
14
+ for (const [_, buttonHandler, options] of buttons ?? []) {
15
+ let collectorOptions;
16
+ if (options && "collector" in options && options.collector) {
17
+ collectorOptions = options.collector;
18
+ }
19
+ if ("resource" in msg) {
20
+ buttonInteractionCollectors.push(msg.resource.message.createMessageComponentCollector({
21
+ componentType: ComponentType.Button,
22
+ ...collectorOptions
23
+ }));
24
+ } else {
25
+ buttonInteractionCollectors.push(msg.createMessageComponentCollector({
26
+ componentType: ComponentType.Button,
27
+ ...collectorOptions
28
+ }));
29
+ }
30
+ buttonInteractionCollectors[buttonInteractionCollectors.length - 1].on("collect", async (buttonInteraction) => {
31
+ if (options?.defer ?? true)
32
+ buttonInteraction.deferUpdate();
33
+ await buttonHandler(buttonInteraction);
34
+ });
35
+ }
36
+ }
37
+ if (typeof args[0] === "string" || typeof args[0] === "undefined" || isRef(args[0])) {
6
38
  const [text, options] = args;
7
39
  let message;
8
40
  const replyOptions = reactive({
9
41
  content: text,
10
- ...defaultOptions,
42
+ ...buttonComponents.value.length > 0 ? { components: buttonComponents } : {},
43
+ ...restOptions,
11
44
  ...options
12
45
  });
13
46
  const handler = (interaction) => {
14
47
  const method = interaction.replied ? "followUp" : interaction.deferred ? "editReply" : "reply";
15
- message = interaction[method](
16
- replyOptions
17
- ).catch((err) => err);
48
+ const opts = deepToRaw(replyOptions);
49
+ message = interaction[method](opts).then((msg) => {
50
+ registerButtonCollectors(msg);
51
+ return msg;
52
+ }).catch((err) => {
53
+ logger.error(err);
54
+ return err;
55
+ });
18
56
  return message;
19
57
  };
20
58
  Object.defineProperty(handler, Symbol.iterator, {
@@ -29,26 +67,35 @@ function createReplyFunction({ ...defaultOptions } = {}) {
29
67
  })
30
68
  });
31
69
  watch(replyOptions, async (options2) => {
32
- const msg = await message;
33
- if ("edit" in msg)
34
- msg.edit(options2);
70
+ let msg = await message;
71
+ if ("resource" in msg)
72
+ msg = await msg.resource.message.edit(deepToRaw(options2));
35
73
  else
36
- msg.resource?.message?.edit(options2);
74
+ msg = await msg.edit(deepToRaw(options2));
75
+ registerButtonCollectors(msg);
37
76
  }, { deep: true, flush: "sync" });
38
77
  return handler;
39
78
  } else {
40
79
  const interaction = args[0];
41
80
  const method = interaction.replied ? "followUp" : interaction.deferred ? "editReply" : "reply";
42
- const options = reactive(defaultOptions);
43
- const messagePromise = interaction[method](options).catch((err) => err);
81
+ const options = reactive({
82
+ ...buttonComponents.value.length > 0 ? { components: buttonComponents } : {},
83
+ ...restOptions
84
+ });
85
+ const messagePromise = interaction[method](deepToRaw(options)).then((msg) => {
86
+ registerButtonCollectors(msg);
87
+ return msg;
88
+ }).catch((err) => {
89
+ logger.error(err);
90
+ return err;
91
+ });
44
92
  watch(options, async (opts) => {
45
- const message = await messagePromise;
46
- if (!message)
47
- return;
48
- if ("edit" in message)
49
- message.edit(opts);
93
+ let msg = await messagePromise;
94
+ if ("resource" in msg)
95
+ msg = await msg.resource.message.edit(deepToRaw(opts));
50
96
  else
51
- message.resource?.message?.edit(opts);
97
+ msg = await msg.edit(deepToRaw(opts));
98
+ registerButtonCollectors(msg);
52
99
  }, { deep: true, flush: "sync" });
53
100
  return {
54
101
  [Symbol.iterator]: () => ({
@@ -70,33 +117,102 @@ function createReplyFunction({ ...defaultOptions } = {}) {
70
117
  }
71
118
  },
72
119
  flags: {
73
- get() {
74
- return (flags) => createReplyFunction({
75
- ...defaultOptions,
76
- flags
77
- });
78
- }
120
+ value: (flags) => createReplyFunction({
121
+ ...defaultOptions,
122
+ flags
123
+ })
79
124
  },
80
125
  file: {
81
- get() {
82
- return (file) => createReplyFunction({
83
- ...defaultOptions,
84
- files: defaultOptions.files != null ? [...defaultOptions.files, file] : [file]
85
- });
86
- }
126
+ value: (file) => createReplyFunction({
127
+ ...defaultOptions,
128
+ files: defaultOptions.files != null ? [...defaultOptions.files, file] : [file]
129
+ })
87
130
  },
88
131
  files: {
89
- get() {
90
- return (files) => createReplyFunction({
91
- ...defaultOptions,
92
- files: defaultOptions.files != null ? [...defaultOptions.files, ...files] : files
93
- });
94
- }
132
+ value: (files) => createReplyFunction({
133
+ ...defaultOptions,
134
+ files: defaultOptions.files != null ? [...defaultOptions.files, ...files] : files
135
+ })
95
136
  },
96
137
  send: {
97
138
  value: reply2
139
+ },
140
+ button: {
141
+ value: (...args) => {
142
+ return createReplyFunction({
143
+ ...defaultOptions,
144
+ buttons: [
145
+ ...defaultOptions.buttons ?? [],
146
+ args
147
+ ]
148
+ });
149
+ }
98
150
  }
99
151
  });
100
152
  return reply2;
101
153
  }
102
154
  export const reply = createReplyFunction();
155
+ function getButtonComponent(buttons) {
156
+ if (buttons.length === 0)
157
+ return [];
158
+ let row = new ActionRowBuilder();
159
+ const rows = [row];
160
+ const it = Iterator.from(buttons).filter(([, , options]) => !toValue(options?.hide ?? false)).map(([label, , options]) => {
161
+ if (options == null) {
162
+ options = {};
163
+ }
164
+ if (toValue(options.style) == null) {
165
+ options.style = ButtonStyle.Primary;
166
+ }
167
+ if (options.style === ButtonStyle.Primary) {
168
+ if (!("customId" in options) || options.customId == null) {
169
+ options.customId = `button-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`;
170
+ }
171
+ }
172
+ return new ButtonBuilder(
173
+ Object.fromEntries(
174
+ Object.entries({
175
+ label,
176
+ ...options,
177
+ type: ComponentType.Button
178
+ }).map(([key, value]) => [key, toValue(value)])
179
+ )
180
+ );
181
+ });
182
+ for (let i = 0; i < 5; i++) {
183
+ row.addComponents(...it.take(5));
184
+ if (row.components.length >= 5) {
185
+ row = new ActionRowBuilder();
186
+ rows.push(row);
187
+ }
188
+ }
189
+ if (row.components.length === 0)
190
+ rows.pop();
191
+ return rows;
192
+ }
193
+ export function deepToRaw(sourceObj) {
194
+ const cycleDetector = /* @__PURE__ */ new WeakSet();
195
+ const objectIterator = (input) => {
196
+ if (cycleDetector.has(input)) {
197
+ return input;
198
+ }
199
+ if (Array.isArray(input)) {
200
+ cycleDetector.add(input);
201
+ return input.map((item) => objectIterator(item));
202
+ }
203
+ if (isRef(input) || isReactive(input) || isProxy(input)) {
204
+ cycleDetector.add(input);
205
+ return objectIterator(isRef(input) ? toValue(input) : toRaw(input));
206
+ }
207
+ if (input && typeof input === "object") {
208
+ cycleDetector.add(input);
209
+ const copy = Object.fromEntries(
210
+ Object.entries(input).map(([key, value]) => [key, objectIterator(value)])
211
+ );
212
+ Object.setPrototypeOf(copy, Object.getPrototypeOf(input));
213
+ return copy;
214
+ }
215
+ return input;
216
+ };
217
+ return objectIterator(sourceObj);
218
+ }
@@ -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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-discord",
3
3
  "type": "module",
4
- "version": "0.0.14",
4
+ "version": "0.0.16",
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",
@@ -16,7 +16,11 @@
16
16
  ".": {
17
17
  "types": "./dist/module.d.mts",
18
18
  "import": "./dist/module.mjs"
19
- }
19
+ },
20
+ "./types": {
21
+ "types": "./dist/types.d.mts"
22
+ },
23
+ "./*": "./*"
20
24
  },
21
25
  "main": "./dist/module.mjs",
22
26
  "types": "./dist/module.d.mts",
@@ -34,37 +38,37 @@
34
38
  "discord.js": "^14.21.0"
35
39
  },
36
40
  "dependencies": {
37
- "@nuxt/kit": "^3.17.5",
41
+ "@nuxt/kit": "^3.17.7",
38
42
  "@nuxt/ui": "^3.2.0",
39
43
  "ansis": "^4.1.0",
40
44
  "chokidar": "^4.0.3",
41
45
  "defu": "^6.1.4",
42
46
  "knitwork": "^1.2.0",
43
47
  "listhen": "^1.9.0",
44
- "rollup": "^4.44.1",
45
- "simple-icons": "^15.3.0",
46
- "unocss": "^66.3.2",
47
- "ws": "^8.18.2"
48
+ "rollup": "^4.45.1",
49
+ "simple-icons": "^15.6.0",
50
+ "unocss": "^66.3.3",
51
+ "ws": "^8.18.3"
48
52
  },
49
53
  "devDependencies": {
50
- "@antfu/eslint-config": "^4.16.1",
51
- "@nuxt/devtools": "^2.5.0",
52
- "@nuxt/eslint-config": "^1.4.1",
54
+ "@antfu/eslint-config": "^4.17.0",
55
+ "@nuxt/devtools": "^2.6.2",
56
+ "@nuxt/eslint-config": "^1.6.0",
53
57
  "@nuxt/module-builder": "^1.0.1",
54
- "@nuxt/schema": "^3.17.5",
55
- "@nuxt/test-utils": "^3.19.1",
56
- "@types/node": "^24.0.4",
57
- "@unocss/eslint-plugin": "^66.3.2",
58
- "@vueuse/core": "^13.4.0",
59
- "changelogen": "^0.6.1",
60
- "eslint": "^9.29.0",
58
+ "@nuxt/schema": "^3.17.7",
59
+ "@nuxt/test-utils": "^3.19.2",
60
+ "@types/node": "^24.0.15",
61
+ "@unocss/eslint-plugin": "^66.3.3",
62
+ "@vueuse/core": "^13.5.0",
63
+ "changelogen": "^0.6.2",
64
+ "eslint": "^9.31.0",
61
65
  "lint-staged": "^16.1.2",
62
- "nuxt": "^3.17.5",
66
+ "nuxt": "^3.17.7",
63
67
  "simple-git-hooks": "^2.13.0",
64
68
  "typescript": "~5.8.3",
65
69
  "unbuild": "^3.5.0",
66
70
  "vitest": "^3.2.4",
67
- "vue-tsc": "^2.2.10"
71
+ "vue-tsc": "^2.2.12"
68
72
  },
69
73
  "resolutions": {
70
74
  "nuxt-discord": "workspace:*"
@@ -77,6 +81,7 @@
77
81
  },
78
82
  "scripts": {
79
83
  "dev": "pnpm run dev:prepare && nuxi dev playground",
84
+ "dev:debug": "pnpm run dev:prepare && NODE_OPTIONS='--inspect' nuxi dev playground",
80
85
  "dev:build": "nuxi build playground",
81
86
  "dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
82
87
  "release": "pnpm run lint && pnpm run test && pnpm run prepack && changelogen --release && pnpm publish && git push --follow-tags",