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
package/dist/module.mjs
ADDED
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
import { useLogger, createResolver, installModule, extendPages, updateTemplates, addVitePlugin, isIgnored, addServerTemplate, addTemplate, addTypeTemplate, defineNuxtModule, addServerScanDir, addImports } from '@nuxt/kit';
|
|
2
|
+
import { ApplicationCommandOptionType, GatewayIntentBits } from 'discord.js';
|
|
3
|
+
import { globSync, readFileSync, existsSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import ts from 'typescript';
|
|
5
|
+
import { listen } from 'listhen';
|
|
6
|
+
import { WebSocketServer } from 'ws';
|
|
7
|
+
import defu from 'defu';
|
|
8
|
+
import { genSafeVariableName, genImport, genArrayFromRaw, genString } from 'knitwork';
|
|
9
|
+
|
|
10
|
+
function createContext(options, nuxt) {
|
|
11
|
+
return {
|
|
12
|
+
nuxt,
|
|
13
|
+
options,
|
|
14
|
+
slashCommands: [],
|
|
15
|
+
resolve: {
|
|
16
|
+
root: createResolver(nuxt.options.rootDir).resolve,
|
|
17
|
+
module: createResolver(import.meta.url).resolve
|
|
18
|
+
},
|
|
19
|
+
logger: useLogger("nuxt-discord")
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function prepareClient(ctx) {
|
|
24
|
+
ctx.nuxt.options.css.push(ctx.resolve.module("runtime/client/style.css"));
|
|
25
|
+
await installModule("@nuxt/ui");
|
|
26
|
+
extendPages((pages) => {
|
|
27
|
+
pages.unshift(
|
|
28
|
+
{
|
|
29
|
+
name: "slash-commands",
|
|
30
|
+
path: "/discord/slash-commands",
|
|
31
|
+
file: ctx.resolve.module("./runtime/client/pages/slash-commands.vue"),
|
|
32
|
+
mode: "client"
|
|
33
|
+
}
|
|
34
|
+
);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const typeIdentifierToEnum = {
|
|
39
|
+
number: ApplicationCommandOptionType.Number,
|
|
40
|
+
integer: ApplicationCommandOptionType.Integer,
|
|
41
|
+
string: ApplicationCommandOptionType.String,
|
|
42
|
+
boolean: ApplicationCommandOptionType.Boolean
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const macros = {
|
|
46
|
+
describeCommand(ctx, node, command) {
|
|
47
|
+
if (node.arguments.length === 0 || !ts.isObjectLiteralExpression(node.arguments[0])) {
|
|
48
|
+
ctx.logger.warn("describeCommand macro requires a object literal as the first argument");
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const describeCommandOptions = ["name", "description"];
|
|
52
|
+
const isValidOption = (key) => describeCommandOptions.includes(key);
|
|
53
|
+
const options = {};
|
|
54
|
+
for (const prop of node.arguments[0].properties) {
|
|
55
|
+
if (ts.isPropertyAssignment(prop) && ts.isIdentifier(prop.name)) {
|
|
56
|
+
const key = prop.name.escapedText;
|
|
57
|
+
if (!isValidOption(key)) {
|
|
58
|
+
ctx.logger.warn(`Unknown describeCommand option: ${key}`);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
options[key] = parseLiteral(prop.initializer);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (options.name && !options.name.length) {
|
|
65
|
+
ctx.logger.warn("describeCommand: name is empty");
|
|
66
|
+
} else {
|
|
67
|
+
command.name = options.name ?? command.name;
|
|
68
|
+
}
|
|
69
|
+
command.description = options.description ?? command.description;
|
|
70
|
+
},
|
|
71
|
+
describeOption(ctx, node, command) {
|
|
72
|
+
if (node.arguments.length !== 2) {
|
|
73
|
+
ctx.logger.warn("describeOption macro requires exactly 2 arguments");
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (!ts.isIdentifier(node.arguments[0])) {
|
|
77
|
+
ctx.logger.warn("describeOption macro requires the first argument to be the option parameter");
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (!ts.isObjectLiteralExpression(node.arguments[1])) {
|
|
81
|
+
ctx.logger.warn("describeOption macro requires the second argument to be an object literal");
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const name = node.arguments[0].escapedText;
|
|
85
|
+
const option = command.options?.find((opt) => opt.name === name);
|
|
86
|
+
if (!option) {
|
|
87
|
+
ctx.logger.warn(`describeOption: option with name "${name}" not found in command "${command.name}"`);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const macroOptions = parseLiteral(node.arguments[1]);
|
|
91
|
+
const validOptionKeys = ["name", "description", "min", "max", "minLength", "maxLength", "choices"];
|
|
92
|
+
const isValidOptionKey = (key) => validOptionKeys.includes(key);
|
|
93
|
+
for (const key in macroOptions) {
|
|
94
|
+
if (!isValidOptionKey(key)) {
|
|
95
|
+
ctx.logger.warn(`describeOption: unknown option key "${key}" for option "${name}" in command "${command.name}"`);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const value = macroOptions[key];
|
|
99
|
+
if (value === void 0 || value === null) {
|
|
100
|
+
ctx.logger.warn(`describeOption: option "${name}" in command "${command.name}" has undefined or null value for key "${key}"`);
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
option[key] = value;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
function parseLiteral(node) {
|
|
108
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) {
|
|
109
|
+
return node.text;
|
|
110
|
+
} else if (ts.isNumericLiteral(node)) {
|
|
111
|
+
return Number.parseFloat(node.text);
|
|
112
|
+
} else if (ts.isPrefixUnaryExpression(node)) {
|
|
113
|
+
if (node.operator === ts.SyntaxKind.MinusToken) {
|
|
114
|
+
return -parseLiteral(node.operand);
|
|
115
|
+
} else if (node.operator === ts.SyntaxKind.PlusToken) {
|
|
116
|
+
return parseLiteral(node.operand);
|
|
117
|
+
} else if (node.operator === ts.SyntaxKind.TildeToken) {
|
|
118
|
+
return ~parseLiteral(node.operand);
|
|
119
|
+
} else if (node.operator === ts.SyntaxKind.ExclamationToken) {
|
|
120
|
+
return !parseLiteral(node.operand);
|
|
121
|
+
} else {
|
|
122
|
+
return void 0;
|
|
123
|
+
}
|
|
124
|
+
} else if (node.kind === ts.SyntaxKind.TrueKeyword || node.kind === ts.SyntaxKind.FalseKeyword) {
|
|
125
|
+
return node.kind === ts.SyntaxKind.TrueKeyword;
|
|
126
|
+
} else if (node.kind === ts.SyntaxKind.NullKeyword) {
|
|
127
|
+
return null;
|
|
128
|
+
} else if (node.kind === ts.SyntaxKind.UndefinedKeyword) {
|
|
129
|
+
return void 0;
|
|
130
|
+
} else if (ts.isParenthesizedExpression(node)) {
|
|
131
|
+
return parseLiteral(node.expression);
|
|
132
|
+
} else if (ts.isArrayLiteralExpression(node)) {
|
|
133
|
+
return node.elements.map(parseLiteral);
|
|
134
|
+
} else if (ts.isObjectLiteralExpression(node)) {
|
|
135
|
+
return node.properties.reduce((acc, prop) => {
|
|
136
|
+
if (ts.isPropertyAssignment(prop) && ts.isIdentifier(prop.name)) {
|
|
137
|
+
const key = prop.name.escapedText;
|
|
138
|
+
acc[key] = parseLiteral(prop.initializer);
|
|
139
|
+
}
|
|
140
|
+
return acc;
|
|
141
|
+
}, {});
|
|
142
|
+
}
|
|
143
|
+
return void 0;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function collectSlashCommands(ctx, out) {
|
|
147
|
+
const commandFiles = globSync(`${ctx.resolve.root(ctx.options.dir, "commands")}/**/*.ts`);
|
|
148
|
+
out ??= ctx.slashCommands;
|
|
149
|
+
for (const file of commandFiles) {
|
|
150
|
+
const command = processCommandFile(ctx, file);
|
|
151
|
+
if (command) {
|
|
152
|
+
out.push(command);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function processCommandFile(ctx, file) {
|
|
157
|
+
const sourceFile = ts.createSourceFile(file, readFileSync(file).toString(), ts.ScriptTarget.Latest, true);
|
|
158
|
+
let commandDefinition;
|
|
159
|
+
ts.forEachChild(sourceFile, (node) => {
|
|
160
|
+
if (ts.isExportAssignment(node)) {
|
|
161
|
+
if (ts.isArrowFunction(node.expression)) {
|
|
162
|
+
commandDefinition = node.expression;
|
|
163
|
+
} else if (ts.isCallExpression(node.expression) && ts.isIdentifier(node.expression.expression) && node.expression.expression.escapedText === "defineSlashCommand" && (ts.isArrowFunction(node.expression.arguments[0]) || ts.isFunctionExpression(node.expression.arguments[0]))) {
|
|
164
|
+
commandDefinition = node.expression.arguments[0];
|
|
165
|
+
}
|
|
166
|
+
} else if (ts.isFunctionDeclaration(node) && !!(ts.getCombinedModifierFlags(node) & ts.ModifierFlags.ExportDefault)) {
|
|
167
|
+
commandDefinition = node;
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
if (!commandDefinition) {
|
|
171
|
+
ctx.logger.warn(`No command definition found for ${file}`);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
const command = {
|
|
175
|
+
path: file,
|
|
176
|
+
options: []
|
|
177
|
+
};
|
|
178
|
+
if ((ts.isFunctionDeclaration(commandDefinition) || ts.isFunctionExpression(commandDefinition)) && !!commandDefinition.name && ts.isIdentifier(commandDefinition.name) && commandDefinition.name.escapedText) {
|
|
179
|
+
command.name = commandDefinition.name.escapedText;
|
|
180
|
+
}
|
|
181
|
+
const jsDocTags = getJSDocTags(commandDefinition);
|
|
182
|
+
const nameTag = jsDocTags.find((tag) => tag.tagName.escapedText === "name");
|
|
183
|
+
const descriptionTag = jsDocTags.find((tag) => tag.tagName.escapedText === "description");
|
|
184
|
+
command.name = nameTag?.comment?.toString() ?? command.name;
|
|
185
|
+
command.description = descriptionTag?.comment?.toString() ?? command.description;
|
|
186
|
+
if (!commandDefinition.body) {
|
|
187
|
+
ctx.logger.warn(`No body found for command definition in ${file}`);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
for (const param of commandDefinition.parameters) {
|
|
191
|
+
const name = param.name.getText(sourceFile);
|
|
192
|
+
let type = param.type?.getText(sourceFile);
|
|
193
|
+
if (!type || !(type in typeIdentifierToEnum)) {
|
|
194
|
+
ctx.logger.warn(`Unknown slash command option type: ${type} for parameter ${name} in ${file}, defaulting to string`);
|
|
195
|
+
type = "string";
|
|
196
|
+
}
|
|
197
|
+
const jsdocDescription = jsDocTags.find((tag) => ts.isJSDocParameterTag(tag) && tag.name.getText() === name)?.comment?.toString() ?? "";
|
|
198
|
+
command.options.push({
|
|
199
|
+
name,
|
|
200
|
+
// TODO: remove this type assertion
|
|
201
|
+
type: typeIdentifierToEnum[type],
|
|
202
|
+
description: jsdocDescription,
|
|
203
|
+
required: !param.questionToken
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
let macrosEnd = false;
|
|
207
|
+
let warned = false;
|
|
208
|
+
ts.forEachChild(commandDefinition.body, (node) => {
|
|
209
|
+
if (ts.isExpressionStatement(node) && ts.isCallExpression(node.expression) && ts.isIdentifier(node.expression.expression) && node.expression.expression.escapedText in macros) {
|
|
210
|
+
if (macrosEnd) {
|
|
211
|
+
if (!warned) {
|
|
212
|
+
ctx.logger.warn(`${node.expression.expression.escapedText} macro must be called at the top in file ${file}`);
|
|
213
|
+
warned = true;
|
|
214
|
+
}
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
macros[node.expression.expression.escapedText](ctx, node.expression, command);
|
|
218
|
+
} else {
|
|
219
|
+
macrosEnd = true;
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
return command;
|
|
223
|
+
}
|
|
224
|
+
function getJSDocTags(node) {
|
|
225
|
+
const tags = ts.getJSDocTags(node);
|
|
226
|
+
if (tags.length > 0) {
|
|
227
|
+
return tags;
|
|
228
|
+
}
|
|
229
|
+
if ("jsDoc" in node) {
|
|
230
|
+
const tags2 = node.jsDoc.tags;
|
|
231
|
+
if (tags2?.length) {
|
|
232
|
+
return tags2;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (node.parent) {
|
|
236
|
+
return getJSDocTags(node.parent);
|
|
237
|
+
}
|
|
238
|
+
return [];
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function prepareHMR(ctx) {
|
|
242
|
+
const { nuxt, options } = ctx;
|
|
243
|
+
if (nuxt.options.dev) {
|
|
244
|
+
const commandsDir = ctx.resolve.root(options.dir, "commands");
|
|
245
|
+
const ignoreCommandsDir = (file) => {
|
|
246
|
+
if (file.startsWith(commandsDir)) {
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
return isIgnored(file);
|
|
250
|
+
};
|
|
251
|
+
nuxt.hook("nitro:config", (nitroConfig) => {
|
|
252
|
+
nitroConfig.watchOptions ??= {};
|
|
253
|
+
const existingNitroIgnored = nitroConfig.watchOptions.ignored;
|
|
254
|
+
const nitroIgnored = [ignoreCommandsDir];
|
|
255
|
+
if (existingNitroIgnored != null) {
|
|
256
|
+
if (Array.isArray(existingNitroIgnored))
|
|
257
|
+
nitroIgnored.push(...existingNitroIgnored);
|
|
258
|
+
else
|
|
259
|
+
nitroIgnored.push(existingNitroIgnored);
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
nuxt.options.vite.server ??= {};
|
|
263
|
+
nuxt.options.vite.server.watch ??= {};
|
|
264
|
+
const existingViteIgnored = nuxt.options.vite.server.watch.ignored;
|
|
265
|
+
const viteIgnored = [ignoreCommandsDir];
|
|
266
|
+
if (existingViteIgnored != null) {
|
|
267
|
+
if (Array.isArray(existingViteIgnored)) {
|
|
268
|
+
viteIgnored.push(...existingViteIgnored);
|
|
269
|
+
} else {
|
|
270
|
+
viteIgnored.push(existingViteIgnored);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
nuxt.options.vite.server.watch.ignored = viteIgnored;
|
|
274
|
+
let websocket;
|
|
275
|
+
let listener;
|
|
276
|
+
const websocketOptions = options.watch || {};
|
|
277
|
+
if (websocketOptions.enabled) {
|
|
278
|
+
nuxt.hook("nitro:init", async (nitro) => {
|
|
279
|
+
websocket = createWebSocket();
|
|
280
|
+
listener = await listen(() => "Nuxt Discord", websocketOptions);
|
|
281
|
+
nitro.options.runtimeConfig.public.discord = {
|
|
282
|
+
wsUrl: listener.url.replace("http", "ws")
|
|
283
|
+
};
|
|
284
|
+
listener.server.on("upgrade", websocket.serve);
|
|
285
|
+
websocket.on("full-update", (client) => {
|
|
286
|
+
const commands = [];
|
|
287
|
+
collectSlashCommands(ctx, commands);
|
|
288
|
+
try {
|
|
289
|
+
client.send(JSON.stringify({
|
|
290
|
+
event: "full-update",
|
|
291
|
+
commands
|
|
292
|
+
}));
|
|
293
|
+
} catch {
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
nuxt.hook("builder:watch", async (event, path) => {
|
|
299
|
+
const fullPath = ctx.resolve.root(path);
|
|
300
|
+
if (fullPath.startsWith(commandsDir)) {
|
|
301
|
+
path = ctx.resolve.root(path);
|
|
302
|
+
const command = processCommandFile(ctx, path) ?? null;
|
|
303
|
+
websocket?.broadcast({ event, path, command });
|
|
304
|
+
await updateTemplates({
|
|
305
|
+
filter: (template) => template.filename === "discord/slashCommands"
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
nuxt.hook("close", () => {
|
|
310
|
+
listener?.server.close();
|
|
311
|
+
websocket?.close();
|
|
312
|
+
});
|
|
313
|
+
const packageJson = ctx.resolve.module("../package.json");
|
|
314
|
+
if (existsSync(packageJson) && JSON.parse(readFileSync(packageJson, "utf-8")).name === "nuxt-discord") {
|
|
315
|
+
(async () => {
|
|
316
|
+
const { createGenerator } = await import('unocss');
|
|
317
|
+
const config = (await import('../dist/runtime/client/uno.config.js')).default;
|
|
318
|
+
const generator = await createGenerator(config);
|
|
319
|
+
const clientDir = ctx.resolve.module("runtime/client");
|
|
320
|
+
const generateCSS = async (path) => {
|
|
321
|
+
if (path.startsWith(clientDir) && path.endsWith(".vue")) {
|
|
322
|
+
const content = readFileSync(path, "utf-8");
|
|
323
|
+
const tokens = await generator.applyExtractors(content, path);
|
|
324
|
+
const { css } = await generator.generate(tokens);
|
|
325
|
+
writeFileSync(path.replace(".vue", ".css"), css, "utf-8");
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
const globPattern = `${clientDir}/**/*.vue`;
|
|
329
|
+
globSync(globPattern).forEach(generateCSS);
|
|
330
|
+
addVitePlugin({
|
|
331
|
+
name: "dev-nuxt-discord-generate-css",
|
|
332
|
+
configureServer(server) {
|
|
333
|
+
server.watcher.add(globPattern);
|
|
334
|
+
server.watcher.on("change", generateCSS);
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
})();
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
function createWebSocket() {
|
|
342
|
+
const wss = new WebSocketServer({ noServer: true });
|
|
343
|
+
const serve = (req, socket = req.socket, head) => wss.handleUpgrade(req, socket, head, (client) => {
|
|
344
|
+
wss.emit("connection", client, req);
|
|
345
|
+
});
|
|
346
|
+
const broadcast = (data) => {
|
|
347
|
+
const message = JSON.stringify(data);
|
|
348
|
+
for (const client of wss.clients) {
|
|
349
|
+
try {
|
|
350
|
+
client.send(message);
|
|
351
|
+
} catch (err) {
|
|
352
|
+
console.error(err);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
const listeners = /* @__PURE__ */ new Map();
|
|
357
|
+
const on = (event, listener) => {
|
|
358
|
+
listeners.set(event, [...listeners.get(event) || [], listener]);
|
|
359
|
+
};
|
|
360
|
+
wss.on("connection", (client) => {
|
|
361
|
+
client.on("message", (data) => {
|
|
362
|
+
let message;
|
|
363
|
+
try {
|
|
364
|
+
message = JSON.parse(data.toString());
|
|
365
|
+
} catch {
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
const event = message.event;
|
|
369
|
+
const eventListeners = listeners.get(event) ?? [];
|
|
370
|
+
for (const listener of eventListeners) {
|
|
371
|
+
try {
|
|
372
|
+
listener(client, message);
|
|
373
|
+
} catch {
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
});
|
|
378
|
+
return {
|
|
379
|
+
serve,
|
|
380
|
+
broadcast,
|
|
381
|
+
on,
|
|
382
|
+
close: () => {
|
|
383
|
+
wss.clients.forEach((client) => client.close());
|
|
384
|
+
return new Promise((resolve) => wss.close(resolve));
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function prepareRuntimeConfig(ctx) {
|
|
390
|
+
ctx.nuxt.hook("nitro:config", (config) => {
|
|
391
|
+
config.runtimeConfig ??= {};
|
|
392
|
+
config.runtimeConfig.discord ??= {};
|
|
393
|
+
config.runtimeConfig.discord.intents = ctx.options.intents;
|
|
394
|
+
config.runtimeConfig.discord.autoStart = ctx.options.autoStart;
|
|
395
|
+
config.runtimeConfig.discord.sync = ctx.options.watch.sync ?? false;
|
|
396
|
+
config.runtimeConfig.discord.dir = ctx.resolve.root(ctx.options.dir);
|
|
397
|
+
config.runtimeConfig.discord.buildDir = ctx.nuxt.options.buildDir;
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function prepareTemplates(ctx) {
|
|
402
|
+
addServerTemplate({
|
|
403
|
+
filename: "discord/slashCommands",
|
|
404
|
+
getContents: () => getSlashCommandTemplateContent(
|
|
405
|
+
ctx,
|
|
406
|
+
/* importExecute */
|
|
407
|
+
true
|
|
408
|
+
)
|
|
409
|
+
});
|
|
410
|
+
addTemplate({
|
|
411
|
+
filename: "discord/slashCommands",
|
|
412
|
+
getContents: () => `${getSlashCommandTemplateContent(
|
|
413
|
+
ctx,
|
|
414
|
+
/* importExecute */
|
|
415
|
+
false
|
|
416
|
+
)}`
|
|
417
|
+
});
|
|
418
|
+
const typesTemplateDst = addTypeTemplate({
|
|
419
|
+
filename: "discord/types.d.ts",
|
|
420
|
+
// proposal-string-dedent when?
|
|
421
|
+
getContents: () => `
|
|
422
|
+
declare module 'discord/slashCommands' {
|
|
423
|
+
declare const slashCommands: import('nuxt-discord').SlashCommandRuntime[]
|
|
424
|
+
export default slashCommands
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
type integer = import('nuxt-discord').integer
|
|
428
|
+
|
|
429
|
+
declare global {
|
|
430
|
+
declare module 'nitropack/types' {
|
|
431
|
+
interface NitroRuntimeHooks {
|
|
432
|
+
'discord:client:config': (options: import('discord.js').ClientOptions) => void
|
|
433
|
+
'discord:client:ready': (client: DiscordClient) => void
|
|
434
|
+
'discord:client:error': (error: DiscordClientError) => void
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
declare module '@nuxt/schema' {
|
|
439
|
+
interface RuntimeConfig {
|
|
440
|
+
discord: import('nuxt-discord').DiscordRuntimeConfig
|
|
441
|
+
}
|
|
442
|
+
interface PublicRuntimeConfig {
|
|
443
|
+
discord: {
|
|
444
|
+
wsUrl?: string
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
`
|
|
450
|
+
}).dst;
|
|
451
|
+
ctx.nuxt.options.nitro.typescript ||= {};
|
|
452
|
+
ctx.nuxt.options.nitro.typescript.tsConfig = defu(ctx.nuxt.options.nitro.typescript.tsConfig, {
|
|
453
|
+
include: [typesTemplateDst]
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
function getSlashCommandTemplateContent(ctx, importExecute) {
|
|
457
|
+
const imports = importExecute ? ctx.slashCommands.map(({ path }) => {
|
|
458
|
+
const name = genSafeVariableName(path);
|
|
459
|
+
return { name, code: genImport(path, [{ name: "default", as: name }]) };
|
|
460
|
+
}) : [];
|
|
461
|
+
return [
|
|
462
|
+
...imports.map(({ code }) => code),
|
|
463
|
+
`export default ${genArrayFromRaw(
|
|
464
|
+
ctx.slashCommands.map(({ options, ...rest }, i) => ({
|
|
465
|
+
...Object.fromEntries(Object.entries(rest).map(([key, value]) => [key, genString(value)])),
|
|
466
|
+
options: genArrayFromRaw(options, void 0, { preserveTypes: true }),
|
|
467
|
+
...importExecute ? { execute: imports[i].name } : {}
|
|
468
|
+
}))
|
|
469
|
+
)}`
|
|
470
|
+
].join("\n");
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
const module = defineNuxtModule({
|
|
474
|
+
meta: {
|
|
475
|
+
name: "nuxt-discord",
|
|
476
|
+
configKey: "discord"
|
|
477
|
+
},
|
|
478
|
+
defaults: {
|
|
479
|
+
intents: [GatewayIntentBits.Guilds],
|
|
480
|
+
dir: "discord",
|
|
481
|
+
autoStart: true,
|
|
482
|
+
watch: {
|
|
483
|
+
enabled: true,
|
|
484
|
+
port: 4222,
|
|
485
|
+
showURL: false,
|
|
486
|
+
sync: {
|
|
487
|
+
debounce: 1e3
|
|
488
|
+
// milliseconds
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
},
|
|
492
|
+
setup(discordOptions, nuxt) {
|
|
493
|
+
const ctx = createContext(discordOptions, nuxt);
|
|
494
|
+
addServerScanDir(ctx.resolve.module("runtime/server"));
|
|
495
|
+
addImports([{
|
|
496
|
+
name: "defineSlashCommand",
|
|
497
|
+
as: "defineSlashCommand",
|
|
498
|
+
from: ctx.resolve.module("runtime/server/utils/defineSlashCommand")
|
|
499
|
+
}]);
|
|
500
|
+
prepareRuntimeConfig(ctx);
|
|
501
|
+
collectSlashCommands(ctx);
|
|
502
|
+
prepareTemplates(ctx);
|
|
503
|
+
prepareHMR(ctx);
|
|
504
|
+
prepareClient(ctx);
|
|
505
|
+
}
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
export { module as default };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
@supports ((-webkit-hyphens:none) and (not (margin-trim:inline))) or ((-moz-orient:inline) and (not (color:rgb(from red r g b)))){*,::backdrop,:after,:before{--un-border-opacity:100%;--un-divide-opacity:100%;--un-text-opacity:100%;--un-space-y-reverse:initial}}@property --un-backdrop-blur{syntax:"*";inherits:false}@property --un-backdrop-brightness{syntax:"*";inherits:false}@property --un-backdrop-contrast{syntax:"*";inherits:false}@property --un-backdrop-grayscale{syntax:"*";inherits:false}@property --un-backdrop-hue-rotate{syntax:"*";inherits:false}@property --un-backdrop-invert{syntax:"*";inherits:false}@property --un-backdrop-opacity{syntax:"*";inherits:false}@property --un-backdrop-saturate{syntax:"*";inherits:false}@property --un-backdrop-sepia{syntax:"*";inherits:false}:host,:root{--spacing:0.25rem;--fontWeight-bold:700;--default-transition-timingFunction:cubic-bezier(0.4,0,0.2,1);--default-transition-duration:150ms;--fontWeight-semibold:600;--fontWeight-medium:500;--colors-gray-900:oklch(21% 0.034 264.665);--colors-gray-600:oklch(44.6% 0.03 256.802);--colors-gray-700:oklch(37.3% 0.034 259.733);--colors-gray-500:oklch(55.1% 0.027 264.364);--colors-gray-400:oklch(70.7% 0.022 261.325);--text-3xl-fontSize:1.875rem;--text-3xl-lineHeight:2.25rem;--text-sm-fontSize:0.875rem;--text-sm-lineHeight:1.25rem;--text-lg-fontSize:1.125rem;--text-lg-lineHeight:1.75rem;--text-xs-fontSize:0.75rem;--text-xs-lineHeight:1rem;--text-2xl-fontSize:1.5rem;--text-2xl-lineHeight:2rem;--colors-white:#fff;--colors-gray-200:oklch(92.8% 0.006 264.531);--colors-gray-300:oklch(87.2% 0.01 258.338)}.container{width:100%}@media (min-width:40rem){.container{max-width:40rem}}@media (min-width:48rem){.container{max-width:48rem}}@media (min-width:64rem){.container{max-width:64rem}}@media (min-width:80rem){.container{max-width:80rem}}@media (min-width:96rem){.container{max-width:96rem}}.text-2xl{font-size:var(--text-2xl-fontSize);line-height:var(--un-leading,var(--text-2xl-lineHeight))}.font-bold{--un-font-weight:var(--fontWeight-bold);font-weight:var(--fontWeight-bold)}.mx-auto{margin-inline:auto}.px-6{padding-inline:calc(var(--spacing)*6)}.border-b{border-bottom-width:1px}.flex{display:flex}.gap-4{gap:calc(var(--spacing)*4)}.h-16{height:calc(var(--spacing)*16)}.w-full{width:100%}.items-center{align-items:center}.top-0{top:calc(var(--spacing)*0)}.justify-center{justify-content:center}.justify-between{justify-content:space-between}.sticky{position:sticky}.z-10{z-index:10}.backdrop-blur{--un-backdrop-blur:blur(8px);-webkit-backdrop-filter:var(--un-backdrop-blur,) var(--un-backdrop-brightness,) var(--un-backdrop-contrast,) var(--un-backdrop-grayscale,) var(--un-backdrop-hue-rotate,) var(--un-backdrop-invert,) var(--un-backdrop-opacity,) var(--un-backdrop-saturate,) var(--un-backdrop-sepia,);backdrop-filter:var(--un-backdrop-blur,) var(--un-backdrop-brightness,) var(--un-backdrop-contrast,) var(--un-backdrop-grayscale,) var(--un-backdrop-hue-rotate,) var(--un-backdrop-invert,) var(--un-backdrop-opacity,) var(--un-backdrop-saturate,) var(--un-backdrop-sepia,)}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
<script setup>
|
|
2
|
+
import { useDark } from "@vueuse/core";
|
|
3
|
+
import "./TheHeader.css";
|
|
4
|
+
const dark = useDark();
|
|
5
|
+
</script>
|
|
6
|
+
|
|
7
|
+
<template>
|
|
8
|
+
<header class="bg-default/75 border-default border-b flex h-16 w-full top-0 justify-center sticky z-10 backdrop-blur">
|
|
9
|
+
<div class="mx-auto px-6 container flex items-center justify-between">
|
|
10
|
+
<ULink to="/discord/slash-commands" :active="false">
|
|
11
|
+
<h1 class="text-2xl font-bold">
|
|
12
|
+
<span class="text-primary"> Nuxt </span>
|
|
13
|
+
<span class="text-secondary"> Discord </span>
|
|
14
|
+
</h1>
|
|
15
|
+
</ULink>
|
|
16
|
+
|
|
17
|
+
<div class="flex gap-4 items-center justify-center">
|
|
18
|
+
<USwitch v-model="dark" unchecked-icon="i-lucide-sun" checked-icon="i-lucide-moon" size="xl" />
|
|
19
|
+
<ULink to="http://github.com/zojize/nuxt-discord" target="_blank" external class="flex items-center">
|
|
20
|
+
<UIcon name="i-simple-icons:github" size="1.25rem" />
|
|
21
|
+
</ULink>
|
|
22
|
+
</div>
|
|
23
|
+
</div>
|
|
24
|
+
</header>
|
|
25
|
+
</template>
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import './TheHeader.css.js';
|
|
2
|
+
declare const _default: import("vue").DefineComponent<{}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
3
|
+
export default _default;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
@supports ((-webkit-hyphens:none) and (not (margin-trim:inline))) or ((-moz-orient:inline) and (not (color:rgb(from red r g b)))){*,::backdrop,:after,:before{--un-border-opacity:100%;--un-divide-opacity:100%;--un-text-opacity:100%;--un-space-y-reverse:initial}}@property --un-text-opacity{syntax:"<percentage>";inherits:false;initial-value:100%}@property --un-outline-style{syntax:"*";inherits:false;initial-value:solid}@property --un-border-opacity{syntax:"<percentage>";inherits:false;initial-value:100%}@property --un-inset-ring-color{syntax:"*";inherits:false}@property --un-inset-ring-shadow{syntax:"*";inherits:false;initial-value:0 0 #0000}@property --un-inset-shadow{syntax:"*";inherits:false;initial-value:0 0 #0000}@property --un-inset-shadow-color{syntax:"*";inherits:false}@property --un-ring-color{syntax:"*";inherits:false}@property --un-ring-inset{syntax:"*";inherits:false}@property --un-ring-offset-color{syntax:"*";inherits:false}@property --un-ring-offset-shadow{syntax:"*";inherits:false;initial-value:0 0 #0000}@property --un-ring-offset-width{syntax:"<length>";inherits:false;initial-value:0}@property --un-ring-shadow{syntax:"*";inherits:false;initial-value:0 0 #0000}@property --un-shadow{syntax:"*";inherits:false;initial-value:0 0 #0000}@property --un-shadow-color{syntax:"*";inherits:false}@property --un-space-y-reverse{syntax:"*";inherits:false;initial-value:0}@property --un-divide-opacity{syntax:"<percentage>";inherits:false;initial-value:100%}:host,:root{--spacing:0.25rem;--fontWeight-bold:700;--default-transition-timingFunction:cubic-bezier(0.4,0,0.2,1);--default-transition-duration:150ms;--fontWeight-semibold:600;--fontWeight-medium:500;--colors-gray-900:oklch(21% 0.034 264.665);--colors-gray-600:oklch(44.6% 0.03 256.802);--colors-gray-700:oklch(37.3% 0.034 259.733);--colors-gray-500:oklch(55.1% 0.027 264.364);--colors-gray-400:oklch(70.7% 0.022 261.325);--text-3xl-fontSize:1.875rem;--text-3xl-lineHeight:2.25rem;--text-sm-fontSize:0.875rem;--text-sm-lineHeight:1.25rem;--text-lg-fontSize:1.125rem;--text-lg-lineHeight:1.75rem;--text-xs-fontSize:0.75rem;--text-xs-lineHeight:1rem;--text-2xl-fontSize:1.5rem;--text-2xl-lineHeight:2rem;--colors-white:#fff;--colors-gray-200:oklch(92.8% 0.006 264.531);--colors-gray-300:oklch(87.2% 0.01 258.338)}.container{width:100%}@media (min-width:40rem){.container{max-width:40rem}}@media (min-width:48rem){.container{max-width:48rem}}@media (min-width:64rem){.container{max-width:64rem}}@media (min-width:80rem){.container{max-width:80rem}}@media (min-width:96rem){.container{max-width:96rem}}.text-3xl{font-size:var(--text-3xl-fontSize);line-height:var(--un-leading,var(--text-3xl-lineHeight))}.text-lg{font-size:var(--text-lg-fontSize);line-height:var(--un-leading,var(--text-lg-lineHeight))}.text-sm{font-size:var(--text-sm-fontSize);line-height:var(--un-leading,var(--text-sm-lineHeight))}.text-xs{font-size:var(--text-xs-fontSize);line-height:var(--un-leading,var(--text-xs-lineHeight))}.dark .dark\:text-gray-200{color:color-mix(in srgb,var(--colors-gray-200) var(--un-text-opacity),transparent)}.dark .dark\:text-gray-300{color:color-mix(in srgb,var(--colors-gray-300) var(--un-text-opacity),transparent)}.dark .dark\:text-gray-400,.text-gray-400{color:color-mix(in srgb,var(--colors-gray-400) var(--un-text-opacity),transparent)}.dark .dark\:text-white{color:color-mix(in srgb,var(--colors-white) var(--un-text-opacity),transparent)}.text-\[var\(--ui-error\)\]\!{color:color-mix(in oklab,var(--ui-error) var(--un-text-opacity),transparent)!important}.text-gray-500{color:color-mix(in srgb,var(--colors-gray-500) var(--un-text-opacity),transparent)}.text-gray-600{color:color-mix(in srgb,var(--colors-gray-600) var(--un-text-opacity),transparent)}.text-gray-700{color:color-mix(in srgb,var(--colors-gray-700) var(--un-text-opacity),transparent)}.text-gray-900{color:color-mix(in srgb,var(--colors-gray-900) var(--un-text-opacity),transparent)}.font-bold{--un-font-weight:var(--fontWeight-bold);font-weight:var(--fontWeight-bold)}.font-medium{--un-font-weight:var(--fontWeight-medium);font-weight:var(--fontWeight-medium)}.font-semibold{--un-font-weight:var(--fontWeight-semibold);font-weight:var(--fontWeight-semibold)}.mx-auto{margin-inline:auto}.my-6{margin-block:calc(var(--spacing)*6)}.mb-1{margin-bottom:calc(var(--spacing)*1)}.mb-16{margin-bottom:calc(var(--spacing)*16)}.mb-2{margin-bottom:calc(var(--spacing)*2)}.mb-3{margin-bottom:calc(var(--spacing)*3)}.mb-6{margin-bottom:calc(var(--spacing)*6)}.mt-4{margin-top:calc(var(--spacing)*4)}.px-6{padding-inline:calc(var(--spacing)*6)}.py-12{padding-block:calc(var(--spacing)*12)}.pt-2{padding-top:calc(var(--spacing)*2)}.text-center{text-align:center}.outline{outline-style:var(--un-outline-style);outline-width:1px}.border{border-width:1px}.border-\[var\(--ui-error\)\]\/50,.divide-\[var\(--ui-error\)\]\/50>:not(:last-child){border-color:color-mix(in oklab,var(--ui-error) 50%,transparent)}.border-\[var\(--ui-neutral\)\]\/50,.divide-\[var\(--ui-neutral\)\]\/50>:not(:last-child){border-color:color-mix(in oklab,var(--ui-neutral) 50%,transparent)}.border-\[var\(--ui-secondary\)\]\/50,.divide-\[var\(--ui-secondary\)\]\/50>:not(:last-child){border-color:color-mix(in oklab,var(--ui-secondary) 50%,transparent)}.border-\[var\(--ui-warning\)\]\/50,.divide-\[var\(--ui-warning\)\]\/50>:not(:last-child){border-color:color-mix(in oklab,var(--ui-warning) 50%,transparent)}.border-dashed{--un-border-style:dashed;border-style:dashed}.flex{display:flex}.flex-col{flex-direction:column}.flex-wrap{flex-wrap:wrap}.gap-2{gap:calc(var(--spacing)*2)}.gap-3{gap:calc(var(--spacing)*3)}.gap-4{gap:calc(var(--spacing)*4)}.grid{display:grid}.h-12{height:calc(var(--spacing)*12)}.h-4{height:calc(var(--spacing)*4)}.h-5{height:calc(var(--spacing)*5)}.w-12{width:calc(var(--spacing)*12)}.w-4{width:calc(var(--spacing)*4)}.w-5{width:calc(var(--spacing)*5)}.w-full{width:100%}.hover\:cursor-pointer:hover{cursor:pointer}.disabled\:hover\:cursor-not-allowed:hover:disabled{cursor:not-allowed}.ring-0{--un-ring-shadow:var(--un-ring-inset,) 0 0 0 calc(var(--un-ring-offset-width)) var(--un-ring-color,currentColor);box-shadow:var(--un-inset-shadow),var(--un-inset-ring-shadow),var(--un-ring-offset-shadow),var(--un-ring-shadow),var(--un-shadow)}.transition-shadow{transition-duration:var(--un-duration,var(--default-transition-duration));transition-property:box-shadow;transition-timing-function:var(--un-ease,var(--default-transition-timingFunction))}.duration-200{--un-duration:200ms;transition-duration:.2s}.items-center{align-items:center}.justify-between{justify-content:space-between}.overflow-hidden{overflow:hidden}@keyframes spin{0%{transform:rotate(0deg)}to{transform:rotate(1turn)}}.animate-spin{animation:spin 1s linear infinite}.space-y-1>:not(:last-child){--un-space-y-reverse:0;margin-block-end:calc(var(--spacing)*1*(1 - var(--un-space-y-reverse)));margin-block-start:calc(var(--spacing)*1*var(--un-space-y-reverse))}.divide-dashed>:not(:last-child){border-style:dashed}@supports (color:color-mix(in lab,red,red)){.dark .dark\:text-gray-200{color:color-mix(in oklab,var(--colors-gray-200) var(--un-text-opacity),transparent)}.dark .dark\:text-gray-300{color:color-mix(in oklab,var(--colors-gray-300) var(--un-text-opacity),transparent)}.dark .dark\:text-gray-400{color:color-mix(in oklab,var(--colors-gray-400) var(--un-text-opacity),transparent)}.dark .dark\:text-white{color:color-mix(in oklab,var(--colors-white) var(--un-text-opacity),transparent)}.text-gray-400{color:color-mix(in oklab,var(--colors-gray-400) var(--un-text-opacity),transparent)}.text-gray-500{color:color-mix(in oklab,var(--colors-gray-500) var(--un-text-opacity),transparent)}.text-gray-600{color:color-mix(in oklab,var(--colors-gray-600) var(--un-text-opacity),transparent)}.text-gray-700{color:color-mix(in oklab,var(--colors-gray-700) var(--un-text-opacity),transparent)}.text-gray-900{color:color-mix(in oklab,var(--colors-gray-900) var(--un-text-opacity),transparent)}}@media (min-width:40rem){.sm\:grid-cols-2{grid-template-columns:repeat(2,minmax(0,1fr))}}@media (min-width:64rem){.lg\:grid-cols-3{grid-template-columns:repeat(3,minmax(0,1fr))}}
|