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.
Files changed (39) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +292 -0
  3. package/dist/module.d.mts +128 -0
  4. package/dist/module.json +9 -0
  5. package/dist/module.mjs +508 -0
  6. package/dist/runtime/client/components/TheHeader.css +1 -0
  7. package/dist/runtime/client/components/TheHeader.vue +25 -0
  8. package/dist/runtime/client/components/TheHeader.vue.d.ts +3 -0
  9. package/dist/runtime/client/pages/slash-commands.css +1 -0
  10. package/dist/runtime/client/pages/slash-commands.vue +429 -0
  11. package/dist/runtime/client/pages/slash-commands.vue.d.ts +4 -0
  12. package/dist/runtime/client/style.css +1 -0
  13. package/dist/runtime/client/uno.config.d.ts +2 -0
  14. package/dist/runtime/client/uno.config.js +10 -0
  15. package/dist/runtime/server/api/discord/slash-command/all.get.d.ts +2 -0
  16. package/dist/runtime/server/api/discord/slash-command/all.get.js +8 -0
  17. package/dist/runtime/server/api/discord/slash-command/register.post.d.ts +2 -0
  18. package/dist/runtime/server/api/discord/slash-command/register.post.js +6 -0
  19. package/dist/runtime/server/api/discord/slash-command/remote-all.get.d.ts +2 -0
  20. package/dist/runtime/server/api/discord/slash-command/remote-all.get.js +8 -0
  21. package/dist/runtime/server/api/discord/slash-command/remote-diff.post.d.ts +20 -0
  22. package/dist/runtime/server/api/discord/slash-command/remote-diff.post.js +8 -0
  23. package/dist/runtime/server/plugins/discord.d.ts +2 -0
  24. package/dist/runtime/server/plugins/discord.js +38 -0
  25. package/dist/runtime/server/tsconfig.json +3 -0
  26. package/dist/runtime/server/utils/client.d.ts +59 -0
  27. package/dist/runtime/server/utils/client.js +396 -0
  28. package/dist/runtime/server/utils/defineSlashCommand.d.ts +10 -0
  29. package/dist/runtime/server/utils/defineSlashCommand.js +3 -0
  30. package/dist/runtime/server/utils/describeCommand.d.ts +19 -0
  31. package/dist/runtime/server/utils/describeCommand.js +2 -0
  32. package/dist/runtime/server/utils/describeOption.d.ts +36 -0
  33. package/dist/runtime/server/utils/describeOption.js +2 -0
  34. package/dist/runtime/server/utils/reply.d.ts +9 -0
  35. package/dist/runtime/server/utils/reply.js +27 -0
  36. package/dist/runtime/server/utils/useDiscordClient.d.ts +5 -0
  37. package/dist/runtime/server/utils/useDiscordClient.js +11 -0
  38. package/dist/types.d.mts +9 -0
  39. package/package.json +78 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Jize Zou <https://github.com/zojize>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,292 @@
1
+ # <span style="color:oklch(72.3% 0.219 149.579)">Nuxt</span> <span style="color:#5865F2">Discord</span>
2
+
3
+ [![npm version][npm-version-src]][npm-version-href]
4
+ [![License][license-src]][license-href]
5
+ [![Nuxt][nuxt-src]][nuxt-href]
6
+
7
+ A Nuxt module for building Discord bots with slash commands, featuring hot module replacement, automatic command registration, and web interface for command management.
8
+
9
+ ## Features
10
+
11
+ - 🤖 **Discord Bot Integration** - Seamlessly integrate Discord.js with your Nuxt application
12
+ - ⚡ **Slash Commands** - Type-safe slash command definitions with automatic registration
13
+ - 🔥 **Hot Module Replacement** - Real-time command updates during development
14
+ - 🎯 **Auto-sync** - Automatically sync commands with Discord's servers
15
+ - 🖥️ **Web Interface** - Beautiful UI for managing and viewing slash commands
16
+ - 🔄 **Real-time Updates** - WebSocket-based live updates in development
17
+
18
+ ## Quick Setup
19
+
20
+ Install the module to your Nuxt application:
21
+
22
+ ```bash
23
+ npm install nuxt-discord discord.js
24
+ ```
25
+
26
+ Add the module to your `nuxt.config.ts`:
27
+
28
+ ```ts
29
+ export default defineNuxtConfig({
30
+ modules: ['nuxt-discord'],
31
+ discord: {
32
+ // Discord bot configuration
33
+ intents: ['Guilds'], // Discord gateway intents
34
+ dir: 'discord', // Directory for Discord-related files
35
+ autoStart: true, // Auto-start the bot on server startup
36
+ watch: {
37
+ enabled: true, // Enable HMR for commands
38
+ sync: {
39
+ debounce: 1000 // Sync delay in milliseconds
40
+ }
41
+ }
42
+ }
43
+ })
44
+ ```
45
+
46
+ Set up your environment variables:
47
+
48
+ ```bash
49
+ # .env
50
+ DISCORD_TOKEN=your_bot_token_here
51
+ DISCORD_CLIENT_ID=your_application_id_here
52
+ ```
53
+
54
+ ## Creating Slash Commands
55
+
56
+ Create slash commands in the `/discord/commands` directory:
57
+
58
+ ```ts
59
+ // discord/commands/ping.ts
60
+ /**
61
+ * @name ping
62
+ * @description A simple ping command that responds with 'pong!'
63
+ */
64
+ export default function ping() {
65
+ return 'pong!'
66
+ }
67
+ ```
68
+
69
+ ### Commands with Parameters
70
+
71
+ ```ts
72
+ // discord/commands/add.ts
73
+ /**
74
+ * @name add
75
+ * @description Adds two numbers together
76
+ * @param a The first number to add
77
+ * @param b The second number to add
78
+ */
79
+ export default (a: number, b: number) => {
80
+ // Add parameter validation
81
+ describeOption(a, {
82
+ min: -100,
83
+ max: 100,
84
+ })
85
+
86
+ describeOption(b, {
87
+ min: -100,
88
+ max: 100,
89
+ })
90
+
91
+ return `The sum of ${a} and ${b} is ${a + b}!`
92
+ }
93
+ ```
94
+
95
+ ### String Parameters with Choices
96
+
97
+ ```ts
98
+ // discord/commands/greet.ts
99
+ /**
100
+ * @name greet
101
+ * @description Greet someone with a custom message
102
+ * @param name The person to greet
103
+ * @param style The greeting style
104
+ */
105
+ export default (name: string, style: string) => {
106
+ describeOption(name, {
107
+ minLength: 1,
108
+ maxLength: 32,
109
+ })
110
+
111
+ describeOption(style, {
112
+ choices: [
113
+ { name: 'Formal', value: 'formal' },
114
+ { name: 'Casual', value: 'casual' },
115
+ { name: 'Enthusiastic', value: 'enthusiastic' },
116
+ ],
117
+ })
118
+
119
+ const greetings = {
120
+ formal: `Good day, ${name}.`,
121
+ casual: `Hey ${name}!`,
122
+ enthusiastic: `HELLO THERE ${name.toUpperCase()}!!! 🎉`,
123
+ } as const
124
+
125
+ return greetings[style as keyof typeof greetings]
126
+ }
127
+ ```
128
+
129
+ > **Coming Soon:** Support choices inferred from Typescript literal union types
130
+
131
+ ## Web Interface
132
+
133
+ Access the command management interface at `/discord/slash-commands` in your application. The interface provides:
134
+
135
+ - 📋 **Command Overview** - View all registered commands with their parameters
136
+ - 🏷️ **Parameter Details** - See type information, validation rules, and constraints
137
+ - 🔄 **Sync Commands** - Monitor local commands' synced status with server
138
+ - ➕ **Register Commands** - Easily register newly added commands
139
+ - ⚡ **Live Updates** - Real-time updates during development
140
+
141
+ > **Coming Soon:** Live command execution/testing, i18n support, logging dashboard, and more fine-grained client control
142
+
143
+ ## Advanced Usage & Customization
144
+
145
+ The module is highly customizable and provides access to the internal Discord client and runtime hooks for advanced use cases.
146
+
147
+ ### Using the Discord Client in Server
148
+
149
+ ```ts
150
+ import { useDiscordClient } from '#imports'
151
+
152
+ export default async () => {
153
+ const client = useDiscordClient()
154
+
155
+ // Access the internal Discord.js client
156
+ const discordJS = client.internalClient
157
+ const guilds = discordJS?.guilds.cache.size || 0
158
+
159
+ return `Bot is in ${guilds} servers!`
160
+ }
161
+ ```
162
+
163
+ ### Runtime Hooks
164
+
165
+ The module provides several hooks for customizing bot behavior:
166
+
167
+ ```ts
168
+ // nitro/plugins/discord-config.ts
169
+ export default defineNitroPlugin(async (nitro) => {
170
+ // Configure Discord client options
171
+ nitro.hooks.hook('discord:client:config', (options) => {
172
+ options.presence = {
173
+ status: 'online',
174
+ activities: [{
175
+ name: 'Powered by Nuxt',
176
+ }]
177
+ }
178
+ })
179
+
180
+ // Handle client ready event
181
+ nitro.hooks.hook('discord:client:ready', (client) => {
182
+ console.log('Discord bot is ready!')
183
+ })
184
+
185
+ // Handle client errors
186
+ nitro.hooks.hook('discord:client:error', (error) => {
187
+ console.error('Discord error:', error)
188
+ })
189
+ })
190
+ ```
191
+
192
+ ### Custom Response Handling
193
+
194
+ ```ts
195
+ export default (message: string) => {
196
+ // Return different response types
197
+ return reply.ephemeral(`This is private: ${message}`) // Only visible to user
198
+ // or
199
+ return `Public response: ${message}` // Visible to everyone
200
+ }
201
+ ```
202
+
203
+ > Refer to [/src/types.ts](/src/types.ts) for detailed type definitions for how you can define commands return types for advanced behavior.
204
+
205
+ ## Configuration Options
206
+
207
+ ```ts
208
+ // nuxt.config.ts
209
+ export default defineNuxtConfig({
210
+ discord: {
211
+ intents: [
212
+ 'Guilds',
213
+ 'GuildMessages'
214
+ ],
215
+ dir: 'discord',
216
+ autoStart: true,
217
+ watch: {
218
+ enabled: true,
219
+ port: 4222,
220
+ sync: {
221
+ debounce: 1000
222
+ }
223
+ }
224
+ }
225
+ })
226
+ ```
227
+
228
+ > Use the 'nitro:client:config' hook to configure other options passed to the Discord client.
229
+
230
+ ## Roadmap
231
+
232
+ Here's what's planned for future releases:
233
+
234
+ ### Core Features
235
+
236
+ - [ ] Full slash command options support
237
+ - [x] String type
238
+ - [x] Number type
239
+ - [x] Integer type
240
+ - [x] Boolean type
241
+ - [ ] Role type
242
+ - [ ] User type
243
+ - [ ] Mentionable type
244
+ - [ ] Attachment type
245
+ - [ ] Better JSDocs support JSDoc comments
246
+ - [ ] Localization support
247
+ - [ ] File-based subcommands
248
+ - [ ] Auto-complete API
249
+ - [ ] Guild-specific commands
250
+ - [ ] Nuxt-devtools integration
251
+ - [ ] Unit tests
252
+
253
+ ### Web Interface Enhancements
254
+
255
+ - [ ] Live command execution
256
+ - [ ] Command testing interface
257
+ - [ ] i18n management
258
+ - [ ] Logging dashboard
259
+ - [ ] Detailed client control panel
260
+ - [ ] Command analytics
261
+ - [ ] Fine grained diff display
262
+
263
+ ## Contribution
264
+
265
+ <details>
266
+ <summary>Local development</summary>
267
+
268
+ Refer to the official [Module Author Guide](https://nuxt.com/docs/guide/going-further/modules)
269
+
270
+ </details>
271
+
272
+ ## Acknowledgement
273
+
274
+ - Generated from the [module starter template](https://github.com/nuxt/starter/tree/module)
275
+ - Implementation details and best practices are inspired by
276
+ - [@nuxt/content](https://github.com/nuxt/content)
277
+ - [@nuxtjs/i18n](https://github.com/nuxt-modules/i18n)
278
+ - [@nuxt/devtools](https://github.com/nuxt/devtools)
279
+ - UI components are based on [Nuxt UI](https://nuxtui.com) and [UnoCSS](https://unocss.dev/)
280
+
281
+ ## License
282
+
283
+ [MIT](./LICENSE) - Made with 💚
284
+
285
+ [npm-version-src]: https://img.shields.io/npm/v/nuxt-discord/latest.svg?style=flat&colorA=18181B&colorB=28CF8D
286
+ [npm-version-href]: https://npmjs.com/package/nuxt-discord
287
+
288
+ [license-src]: https://img.shields.io/github/license/zojize/nuxt-discord.svg?style=flat&colorA=18181B&colorB=28CF8D
289
+ [license-href]: https://github.com/zojize/nuxt-discord/blob/main/LICENSE
290
+
291
+ [nuxt-src]: https://img.shields.io/badge/Nuxt-18181B?logo=nuxt.js
292
+ [nuxt-href]: https://nuxt.com
@@ -0,0 +1,128 @@
1
+ import * as _nuxt_schema from '@nuxt/schema';
2
+ import * as _nuxt_kit from '@nuxt/kit';
3
+ import * as nuxt_schema from 'nuxt/schema';
4
+ import { DiscordClient } from '/Users/jeffz/Dev/nuxt-discord/dist/runtime/server/utils/client.js';
5
+ import { IntegerOption, NumberOption, StringOption } from '../dist/runtime/server/utils/describeOption.js';
6
+ import { ClientOptions, ApplicationCommandOptionType, ChatInputCommandInteraction } from 'discord.js';
7
+ import { ListenOptions } from 'listhen';
8
+
9
+ interface NuxtDiscordOptions {
10
+ /**
11
+ * The intents to use for the Discord client. Use
12
+ * the 'nitro:client:config' hook to configure other
13
+ * options passed to the Discord client.
14
+ *
15
+ * @default [GatewayIntentBits.Guilds]
16
+ */
17
+ intents: ClientOptions['intents'];
18
+ /**
19
+ * The directory to scan for nuxt-discord related
20
+ * files, such as slash commands.
21
+ *
22
+ * @default 'discord'
23
+ */
24
+ dir: string;
25
+ /**
26
+ * Start the Discord client on init.
27
+ *
28
+ * @default true
29
+ */
30
+ autoStart: boolean;
31
+ /**
32
+ * HMR options
33
+ */
34
+ watch: {
35
+ /**
36
+ * Enable or disable HMR for slash commands.
37
+ *
38
+ * @default true
39
+ */
40
+ enabled?: boolean;
41
+ } & {
42
+ /**
43
+ * Whether to automatically register and update
44
+ * slash commands on the Discord client.
45
+ */
46
+ sync?: false | {
47
+ /**
48
+ * Milliseconds to debounce before updating
49
+ * on HMR events.
50
+ *
51
+ * @default 1000
52
+ */
53
+ debounce?: number;
54
+ };
55
+ } & Partial<ListenOptions>;
56
+ }
57
+ interface DiscordRuntimeConfig {
58
+ intents: ClientOptions['intents'];
59
+ autoStart: boolean;
60
+ sync: NuxtDiscordOptions['watch']['sync'];
61
+ dir: string;
62
+ buildDir: string;
63
+ }
64
+ declare const slashCommandOptionTypeIdentifiers: readonly ["number", "integer", "string", "boolean"];
65
+ declare const typeIdentifierToEnum: {
66
+ readonly number: ApplicationCommandOptionType.Number;
67
+ readonly integer: ApplicationCommandOptionType.Integer;
68
+ readonly string: ApplicationCommandOptionType.String;
69
+ readonly boolean: ApplicationCommandOptionType.Boolean;
70
+ };
71
+ type SlashCommandOptionTypeIdentifier = (typeof slashCommandOptionTypeIdentifiers)[number];
72
+ declare const integerLabel: unique symbol;
73
+ /**
74
+ * An alias of the number type, represents the integer,
75
+ * option when used as a slash command argument,
76
+ */
77
+ type integer = number & {
78
+ [integerLabel]: never;
79
+ };
80
+ type SlashCommandOptionType = number | integer | string | boolean;
81
+ interface SlashCommandOptionBase {
82
+ name: string;
83
+ description: string;
84
+ type: ApplicationCommandOptionType;
85
+ required: boolean;
86
+ }
87
+ interface SlashCommandIntegerOption extends SlashCommandOptionBase, Omit<IntegerOption, 'name' | 'description'> {
88
+ type: ApplicationCommandOptionType.Integer;
89
+ }
90
+ interface SlashCommandNumberOption extends SlashCommandOptionBase, Omit<NumberOption, 'name' | 'description'> {
91
+ type: ApplicationCommandOptionType.Number;
92
+ }
93
+ interface SlashCommandStringOption extends SlashCommandOptionBase, Omit<StringOption, 'name' | 'description'> {
94
+ type: ApplicationCommandOptionType.String;
95
+ }
96
+ interface SlashCommandBooleanOption extends SlashCommandOptionBase {
97
+ type: ApplicationCommandOptionType.Boolean;
98
+ }
99
+ type SlashCommandOption = SlashCommandIntegerOption | SlashCommandNumberOption | SlashCommandStringOption | SlashCommandBooleanOption;
100
+ type SlashCommandCustomReturnHandler = (this: DiscordClient, interaction: ChatInputCommandInteraction, client: DiscordClient) => SlashCommandReturnType;
101
+ type SlashCommandReturnType = void | string | SlashCommandCustomReturnHandler | Promise<SlashCommandReturnType> | Generator<SlashCommandReturnType> | AsyncGenerator<SlashCommandReturnType>;
102
+ interface SlashCommand {
103
+ name: string;
104
+ description: string;
105
+ path: string;
106
+ options: SlashCommandOption[];
107
+ }
108
+ interface SlashCommandRuntime extends SlashCommand {
109
+ /** if execute is not defined load must be defined */
110
+ load?: () => Promise<SlashCommandRuntime['execute']>;
111
+ execute?: (...args: (SlashCommandOptionType | undefined)[]) => SlashCommandReturnType;
112
+ id?: string;
113
+ }
114
+ interface NuxtDiscordContext {
115
+ nuxt: nuxt_schema.Nuxt;
116
+ options: NuxtDiscordOptions;
117
+ slashCommands: SlashCommand[];
118
+ resolve: {
119
+ root: _nuxt_kit.Resolver['resolve'];
120
+ module: _nuxt_kit.Resolver['resolve'];
121
+ };
122
+ logger: ReturnType<typeof _nuxt_kit.useLogger>;
123
+ }
124
+
125
+ declare const _default: _nuxt_schema.NuxtModule<NuxtDiscordOptions, NuxtDiscordOptions, false>;
126
+
127
+ export { _default as default, slashCommandOptionTypeIdentifiers, typeIdentifierToEnum };
128
+ export type { DiscordRuntimeConfig, NuxtDiscordContext, NuxtDiscordOptions, SlashCommand, SlashCommandBooleanOption, SlashCommandCustomReturnHandler, SlashCommandIntegerOption, SlashCommandNumberOption, SlashCommandOption, SlashCommandOptionBase, SlashCommandOptionType, SlashCommandOptionTypeIdentifier, SlashCommandReturnType, SlashCommandRuntime, SlashCommandStringOption, integer };
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "nuxt-discord",
3
+ "configKey": "discord",
4
+ "version": "0.0.1",
5
+ "builder": {
6
+ "@nuxt/module-builder": "1.0.1",
7
+ "unbuild": "3.5.0"
8
+ }
9
+ }