nuxt-discord 0.0.17 → 0.2.0
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/README.md +249 -171
- package/dist/module.d.mts +146 -10
- package/dist/module.json +3 -3
- package/dist/module.mjs +1077 -348
- package/dist/runtime/acknowledgement.d.ts +11 -0
- package/dist/runtime/acknowledgement.js +156 -0
- package/dist/runtime/client/chat-theme.css +52 -0
- package/dist/runtime/client/components/ChatAvatar.d.vue.ts +6 -0
- package/dist/runtime/client/components/ChatAvatar.vue +11 -0
- package/dist/runtime/client/components/ChatAvatar.vue.d.ts +6 -0
- package/dist/runtime/client/components/CommandPicker.d.vue.ts +24 -0
- package/dist/runtime/client/components/CommandPicker.vue +67 -0
- package/dist/runtime/client/components/CommandPicker.vue.d.ts +24 -0
- package/dist/runtime/client/components/CommandReference.d.vue.ts +20 -0
- package/dist/runtime/client/components/CommandReference.vue +21 -0
- package/dist/runtime/client/components/CommandReference.vue.d.ts +20 -0
- package/dist/runtime/client/components/CommandTestBar.d.vue.ts +60 -0
- package/dist/runtime/client/components/CommandTestBar.vue +332 -0
- package/dist/runtime/client/components/CommandTestBar.vue.d.ts +60 -0
- package/dist/runtime/client/components/CommandTranscript.d.vue.ts +39 -0
- package/dist/runtime/client/components/CommandTranscript.vue +147 -0
- package/dist/runtime/client/components/CommandTranscript.vue.d.ts +39 -0
- package/dist/runtime/client/components/ProjectLogo.d.vue.ts +3 -0
- package/dist/runtime/client/components/ProjectLogo.vue +31 -0
- package/dist/runtime/client/components/ProjectLogo.vue.d.ts +3 -0
- package/dist/runtime/client/components/ReferenceContent.d.ts +26 -0
- package/dist/runtime/client/components/ReferenceContent.js +73 -0
- package/dist/runtime/client/components/TheHeader.d.vue.ts +3 -0
- package/dist/runtime/client/components/TheHeader.vue +53 -16
- package/dist/runtime/client/components/TheHeader.vue.d.ts +2 -2
- package/dist/runtime/client/components/appearance-select.d.ts +17 -0
- package/dist/runtime/client/components/appearance-select.js +15 -0
- package/dist/runtime/client/pages/activity.d.vue.ts +3 -0
- package/dist/runtime/client/pages/activity.vue +202 -0
- package/dist/runtime/client/pages/activity.vue.d.ts +3 -0
- package/dist/runtime/client/pages/context-menus.d.vue.ts +3 -0
- package/dist/runtime/client/pages/context-menus.vue +120 -0
- package/dist/runtime/client/pages/context-menus.vue.d.ts +3 -0
- package/dist/runtime/client/pages/listeners.d.vue.ts +3 -0
- package/dist/runtime/client/pages/listeners.vue +68 -0
- package/dist/runtime/client/pages/listeners.vue.d.ts +3 -0
- package/dist/runtime/client/pages/overview.d.vue.ts +3 -0
- package/dist/runtime/client/pages/overview.vue +280 -0
- package/dist/runtime/client/pages/overview.vue.d.ts +3 -0
- package/dist/runtime/client/pages/slash-commands.d.vue.ts +3 -0
- package/dist/runtime/client/pages/slash-commands.vue +185 -359
- package/dist/runtime/client/pages/slash-commands.vue.d.ts +2 -3
- package/dist/runtime/client/reference.css +46 -0
- package/dist/runtime/client/style.css +4 -1
- package/dist/runtime/client/useActivityLog.d.ts +7 -0
- package/dist/runtime/client/useActivityLog.js +76 -0
- package/dist/runtime/client/useCommandRegistry.d.ts +7 -0
- package/dist/runtime/client/useCommandRegistry.js +34 -0
- package/dist/runtime/client/useDashboardCatalog.d.ts +5 -0
- package/dist/runtime/client/useDashboardCatalog.js +9 -0
- package/dist/runtime/client/useDiscordCommandReference.d.ts +2 -0
- package/dist/runtime/client/useDiscordCommandReference.js +6 -0
- package/dist/runtime/dashboard/auth.d.ts +4 -0
- package/dist/runtime/dashboard/auth.js +55 -0
- package/dist/runtime/dashboard/oauth.d.ts +17 -0
- package/dist/runtime/dashboard/oauth.js +172 -0
- package/dist/runtime/dashboard/origin.d.ts +3 -0
- package/dist/runtime/dashboard/origin.js +15 -0
- package/dist/runtime/dashboard/permissions.d.ts +4 -0
- package/dist/runtime/dashboard/permissions.js +84 -0
- package/dist/runtime/dashboard/signin.d.ts +3 -0
- package/dist/runtime/dashboard/signin.js +42 -0
- package/dist/runtime/diagnostics.d.ts +4 -0
- package/dist/runtime/diagnostics.js +24 -0
- package/dist/runtime/discordInvocation.d.ts +14 -0
- package/dist/runtime/discordInvocation.js +28 -0
- package/dist/runtime/middleware.d.ts +1 -0
- package/dist/runtime/middleware.js +12 -0
- package/dist/runtime/reference/access.d.ts +6 -0
- package/dist/runtime/reference/access.js +143 -0
- package/dist/runtime/reference/catalog.d.ts +2 -0
- package/dist/runtime/reference/catalog.js +9 -0
- package/dist/runtime/reference/document.d.ts +2 -0
- package/dist/runtime/reference/document.js +22 -0
- package/dist/runtime/reference/hosted.d.ts +2 -0
- package/dist/runtime/reference/hosted.js +16 -0
- package/dist/runtime/reference/oauth.d.ts +2 -0
- package/dist/runtime/reference/oauth.js +4 -0
- package/dist/runtime/registration.d.ts +65 -0
- package/dist/runtime/registration.js +354 -0
- package/dist/runtime/server/api/discord/activity.get.d.ts +2 -0
- package/dist/runtime/server/api/discord/activity.get.js +6 -0
- package/dist/runtime/server/api/discord/catalog.get.d.ts +2 -0
- package/dist/runtime/server/api/discord/catalog.get.js +3 -0
- package/dist/runtime/server/api/discord/slash-command/autocomplete.post.d.ts +2 -0
- package/dist/runtime/server/api/discord/slash-command/autocomplete.post.js +14 -0
- package/dist/runtime/server/api/discord/slash-command/register.post.d.ts +1 -1
- package/dist/runtime/server/api/discord/slash-command/register.post.js +1 -1
- package/dist/runtime/server/api/discord/slash-command/remote-all.get.d.ts +1 -1
- package/dist/runtime/server/api/discord/slash-command/remote-diff.post.d.ts +1 -238
- package/dist/runtime/server/api/discord/slash-command/remote-diff.post.js +3 -1
- package/dist/runtime/server/api/discord/slash-command/test.post.d.ts +20 -0
- package/dist/runtime/server/api/discord/slash-command/test.post.js +19 -0
- package/dist/runtime/server/plugins/discord.js +95 -31
- package/dist/runtime/server/utils/client.d.ts +59 -246
- package/dist/runtime/server/utils/client.js +413 -451
- package/dist/runtime/server/utils/defineContextMenu.d.ts +49 -0
- package/dist/runtime/server/utils/defineContextMenu.js +12 -0
- package/dist/runtime/server/utils/defineListener.d.ts +24 -0
- package/dist/runtime/server/utils/defineListener.js +7 -0
- package/dist/runtime/server/utils/defineMiddleware.d.ts +29 -0
- package/dist/runtime/server/utils/defineMiddleware.js +24 -0
- package/dist/runtime/server/utils/describeCommand.d.ts +2 -0
- package/dist/runtime/server/utils/describeOption.d.ts +1 -1
- package/dist/runtime/server/utils/messageSender.d.ts +37 -0
- package/dist/runtime/server/utils/messageSender.js +149 -0
- package/dist/runtime/server/utils/middleware/cooldown.d.ts +1 -0
- package/dist/runtime/server/utils/middleware/cooldown.js +39 -0
- package/dist/runtime/server/utils/middleware/guildOnly.d.ts +1 -0
- package/dist/runtime/server/utils/middleware/guildOnly.js +6 -0
- package/dist/runtime/server/utils/middleware/ownerOnly.d.ts +1 -0
- package/dist/runtime/server/utils/middleware/ownerOnly.js +10 -0
- package/dist/runtime/server/utils/middleware/requireRole.d.ts +1 -0
- package/dist/runtime/server/utils/middleware/requireRole.js +15 -0
- package/dist/runtime/server/utils/reply.d.ts +13 -3
- package/dist/runtime/server/utils/reply.js +111 -162
- package/dist/runtime/server/utils/simulateInteraction.d.ts +20 -0
- package/dist/runtime/server/utils/simulateInteraction.js +127 -0
- package/dist/runtime/server/utils/useTextChannel.d.ts +56 -0
- package/dist/runtime/server/utils/useTextChannel.js +105 -0
- package/dist/types.d.mts +11 -1
- package/dist/types.d.ts +11 -1
- package/package.json +62 -44
- package/dist/runtime/client/components/TheHeader.css +0 -1
- package/dist/runtime/client/pages/slash-commands.css +0 -1
- package/dist/runtime/client/uno.config.d.ts +0 -2
- package/dist/runtime/client/uno.config.js +0 -10
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { MessageContextMenuCommandInteraction, UserContextMenuCommandInteraction } from 'discord.js';
|
|
2
|
+
interface MenuOptions {
|
|
3
|
+
middleware?: import('./defineMiddleware').MiddlewareDefinition[];
|
|
4
|
+
}
|
|
5
|
+
export type ContextMenuDefinition = ({
|
|
6
|
+
type: 'user';
|
|
7
|
+
name?: string;
|
|
8
|
+
execute: (interaction: UserContextMenuCommandInteraction) => unknown;
|
|
9
|
+
} | {
|
|
10
|
+
type: 'message';
|
|
11
|
+
name?: string;
|
|
12
|
+
execute: (interaction: MessageContextMenuCommandInteraction) => unknown;
|
|
13
|
+
}) & {
|
|
14
|
+
guilds?: true | string[];
|
|
15
|
+
middlewareEntries?: MenuOptions['middleware'];
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Define a user context menu command (right-click on a user).
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* // Name from first argument
|
|
22
|
+
* export default defineUserContextMenu('User Info', async (interaction) => {
|
|
23
|
+
* await interaction.reply(interaction.targetUser.tag)
|
|
24
|
+
* })
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // Name from @name JSDoc tag or filename
|
|
28
|
+
* export default defineUserContextMenu(async (interaction) => {
|
|
29
|
+
* await interaction.reply(interaction.targetUser.tag)
|
|
30
|
+
* })
|
|
31
|
+
*/
|
|
32
|
+
export declare function defineUserContextMenu(nameOrExecute: string | ((interaction: UserContextMenuCommandInteraction) => unknown), execute?: (interaction: UserContextMenuCommandInteraction) => unknown, options?: MenuOptions): ContextMenuDefinition;
|
|
33
|
+
/**
|
|
34
|
+
* Define a message context menu command (right-click on a message).
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* // Name from first argument
|
|
38
|
+
* export default defineMessageContextMenu('Bookmark', async (interaction) => {
|
|
39
|
+
* await interaction.reply(interaction.targetMessage.url)
|
|
40
|
+
* })
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* // Name from @name JSDoc tag or filename
|
|
44
|
+
* export default defineMessageContextMenu(async (interaction) => {
|
|
45
|
+
* await interaction.reply(interaction.targetMessage.url)
|
|
46
|
+
* })
|
|
47
|
+
*/
|
|
48
|
+
export declare function defineMessageContextMenu(nameOrExecute: string | ((interaction: MessageContextMenuCommandInteraction) => unknown), execute?: (interaction: MessageContextMenuCommandInteraction) => unknown, options?: MenuOptions): ContextMenuDefinition;
|
|
49
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function defineUserContextMenu(nameOrExecute, execute, options) {
|
|
2
|
+
if (typeof nameOrExecute === "string") {
|
|
3
|
+
return { type: "user", name: nameOrExecute, execute, ...options?.middleware && { middlewareEntries: options.middleware } };
|
|
4
|
+
}
|
|
5
|
+
return { type: "user", execute: nameOrExecute };
|
|
6
|
+
}
|
|
7
|
+
export function defineMessageContextMenu(nameOrExecute, execute, options) {
|
|
8
|
+
if (typeof nameOrExecute === "string") {
|
|
9
|
+
return { type: "message", name: nameOrExecute, execute, ...options?.middleware && { middlewareEntries: options.middleware } };
|
|
10
|
+
}
|
|
11
|
+
return { type: "message", execute: nameOrExecute };
|
|
12
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ClientEvents } from 'discord.js';
|
|
2
|
+
export interface ListenerDefinition<E extends keyof ClientEvents = keyof ClientEvents> {
|
|
3
|
+
event: E;
|
|
4
|
+
execute: (...args: ClientEvents[E]) => void | Promise<void>;
|
|
5
|
+
once?: boolean;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Define a Discord event listener.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* // discord/listeners/welcome.ts
|
|
12
|
+
* export default defineListener('guildMemberAdd', (member) => {
|
|
13
|
+
* member.guild.systemChannel?.send(`Welcome ${member}!`)
|
|
14
|
+
* })
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* // discord/listeners/ready.ts
|
|
18
|
+
* export default defineListener('clientReady', (client) => {
|
|
19
|
+
* console.log(`Logged in as ${client.user.tag}`)
|
|
20
|
+
* }, { once: true })
|
|
21
|
+
*/
|
|
22
|
+
export declare function defineListener<E extends keyof ClientEvents>(event: E, execute: (...args: ClientEvents[E]) => void | Promise<void>, options?: {
|
|
23
|
+
once?: boolean;
|
|
24
|
+
}): ListenerDefinition<E>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Interaction } from 'discord.js';
|
|
2
|
+
import type { SlashCommandRuntime, SlashCommandSubcommandRuntime } from '../../../types.js';
|
|
3
|
+
export interface MiddlewareOpts {
|
|
4
|
+
interaction: Interaction;
|
|
5
|
+
command?: SlashCommandRuntime | SlashCommandSubcommandRuntime;
|
|
6
|
+
type: 'command' | 'context-menu';
|
|
7
|
+
next: <T extends Record<string, unknown> = Record<string, never>>(ctx?: T) => Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
export type MiddlewareFn = (opts: MiddlewareOpts, options?: Record<string, unknown>) => void | Promise<void>;
|
|
10
|
+
export interface MiddlewareDefinition {
|
|
11
|
+
name: string;
|
|
12
|
+
fn: MiddlewareFn;
|
|
13
|
+
}
|
|
14
|
+
export declare class MiddlewareError extends Error {
|
|
15
|
+
reason: string;
|
|
16
|
+
identifier?: string | undefined;
|
|
17
|
+
constructor(reason: string, identifier?: string | undefined);
|
|
18
|
+
}
|
|
19
|
+
export declare function defineMiddleware(name: string, fn: MiddlewareFn): MiddlewareDefinition;
|
|
20
|
+
export declare function setMiddlewareContext(ctx: Record<string, unknown> | null): void;
|
|
21
|
+
export declare function useMiddlewareContext<T extends Record<string, unknown> = Record<string, unknown>>(): T;
|
|
22
|
+
/**
|
|
23
|
+
* Compiler macro — stripped at build time by the transform plugin.
|
|
24
|
+
* At build time: registers the middleware on the command's metadata.
|
|
25
|
+
* At runtime: returns the accumulated middleware context.
|
|
26
|
+
*
|
|
27
|
+
* Usage: `useMiddleware(myMiddleware)` or `useMiddleware(cooldown, { seconds: 5 })`
|
|
28
|
+
*/
|
|
29
|
+
export declare function useMiddleware<T extends MiddlewareDefinition>(_middleware: T, _options?: Record<string, unknown>): Record<string, unknown>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { currentInvocation, setInvocationFields } from "../../discordInvocation.js";
|
|
2
|
+
export class MiddlewareError extends Error {
|
|
3
|
+
constructor(reason, identifier) {
|
|
4
|
+
super(reason);
|
|
5
|
+
this.reason = reason;
|
|
6
|
+
this.identifier = identifier;
|
|
7
|
+
this.name = "MiddlewareError";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export function defineMiddleware(name, fn) {
|
|
11
|
+
return { name, fn };
|
|
12
|
+
}
|
|
13
|
+
export function setMiddlewareContext(ctx) {
|
|
14
|
+
setInvocationFields({ middleware: ctx });
|
|
15
|
+
}
|
|
16
|
+
export function useMiddlewareContext() {
|
|
17
|
+
const currentMiddlewareContext = currentInvocation()?.middleware;
|
|
18
|
+
if (!currentMiddlewareContext)
|
|
19
|
+
throw new Error("useMiddlewareContext() called outside of a command execution context");
|
|
20
|
+
return currentMiddlewareContext;
|
|
21
|
+
}
|
|
22
|
+
export function useMiddleware(_middleware, _options) {
|
|
23
|
+
return currentInvocation()?.middleware ?? {};
|
|
24
|
+
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export interface DescribeCommandOptions {
|
|
2
2
|
/** The name of the command. */
|
|
3
3
|
name?: string;
|
|
4
|
+
/** Visibility of a slow command's automatic acknowledgement, or manual to opt out. */
|
|
5
|
+
defer?: import('../../acknowledgement').DeferPolicy;
|
|
4
6
|
/** A brief description of the command. */
|
|
5
7
|
description?: string;
|
|
6
8
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { APIApplicationCommandOptionChoice, ApplicationCommandOptionChoiceData, AutocompleteInteraction } from 'discord.js';
|
|
2
|
-
import type { SlashCommandOptionType } from '../../../types.js';
|
|
2
|
+
import type { integer, SlashCommandOptionType } from '../../../types.js';
|
|
3
3
|
export interface DescribeOptionOptionsBase {
|
|
4
4
|
/** The name of the command. */
|
|
5
5
|
name?: string;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { BaseMessageOptions, ButtonComponentData, ButtonInteraction, InteractionCallbackResponse, InteractionReplyOptions, InteractionResponse, Message, MessageCollectorOptionsParams, MessageCreateOptions } from 'discord.js';
|
|
2
|
+
import type { MaybeRef } from 'vue';
|
|
3
|
+
import { ComponentType } from 'discord.js';
|
|
4
|
+
export type File = NonNullable<BaseMessageOptions['files']>[number];
|
|
5
|
+
export type ButtonCollectorOptions = Partial<Omit<MessageCollectorOptionsParams<ComponentType.Button>, 'componentType'>>;
|
|
6
|
+
export type MaybeRefObject<T> = T extends any ? {
|
|
7
|
+
[K in keyof T]: MaybeRef<T[K]>;
|
|
8
|
+
} : never;
|
|
9
|
+
export type DistributiveOmit<T, K extends keyof T> = T extends any ? Omit<T, K> : never;
|
|
10
|
+
export type SentMessage = Message | InteractionResponse | InteractionCallbackResponse;
|
|
11
|
+
export interface MessageSenderConfig {
|
|
12
|
+
/** Sends the message with the final (raw, unwrapped) options. Returns the sent message. */
|
|
13
|
+
send: (opts: MessageCreateOptions) => Promise<SentMessage>;
|
|
14
|
+
/** Edits a previously-sent message with the new (raw, unwrapped) options. */
|
|
15
|
+
edit: (msg: SentMessage, opts: MessageCreateOptions) => Promise<SentMessage>;
|
|
16
|
+
}
|
|
17
|
+
export type ButtonHandlerArgs = [
|
|
18
|
+
label: MaybeRef<string>,
|
|
19
|
+
handler: (interaction: ButtonInteraction) => void | Promise<void>,
|
|
20
|
+
options?: Partial<MaybeRefObject<DistributiveOmit<ButtonComponentData, 'label' | 'type'>>> & {
|
|
21
|
+
defer?: boolean;
|
|
22
|
+
hide?: MaybeRef<boolean>;
|
|
23
|
+
collector?: ButtonCollectorOptions;
|
|
24
|
+
}
|
|
25
|
+
];
|
|
26
|
+
export interface SendOptions extends Omit<InteractionReplyOptions, 'content' | 'files' | 'flags'> {
|
|
27
|
+
flags?: MessageCreateOptions['flags'];
|
|
28
|
+
files?: MaybeRef<readonly MaybeRef<File>[]>;
|
|
29
|
+
buttons?: ButtonHandlerArgs[];
|
|
30
|
+
}
|
|
31
|
+
export declare function getButtonComponent(buttons: ButtonHandlerArgs[]): NonNullable<BaseMessageOptions['components']>;
|
|
32
|
+
export declare function deepToRaw<T extends Record<string, any>>(sourceObj: T): T;
|
|
33
|
+
/**
|
|
34
|
+
* Sends a message and watches the reactive options for changes, editing on update.
|
|
35
|
+
* Returns the message promise. Sets up button collectors automatically.
|
|
36
|
+
*/
|
|
37
|
+
export declare function sendReactiveMessage(config: MessageSenderConfig, content: MaybeRef<string> | undefined, opts?: SendOptions): Promise<SentMessage>;
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ComponentType, MessageFlags } from "discord.js";
|
|
3
|
+
import { computed, isRef, reactive, toRaw, toValue, watch } from "vue";
|
|
4
|
+
import { logDiscordFailure } from "../../diagnostics.js";
|
|
5
|
+
import { bindInvocation, onInvocationDispose } from "../../discordInvocation.js";
|
|
6
|
+
export function getButtonComponent(buttons) {
|
|
7
|
+
const rows = [];
|
|
8
|
+
for (const [label, , options] of buttons) {
|
|
9
|
+
if (toValue(options?.hide))
|
|
10
|
+
continue;
|
|
11
|
+
if (!rows.length || rows.at(-1).components.length === 5) {
|
|
12
|
+
if (rows.length === 5)
|
|
13
|
+
break;
|
|
14
|
+
rows.push(new ActionRowBuilder());
|
|
15
|
+
}
|
|
16
|
+
const { hide: _hide, collector: _collector, defer: _defer, ...fields } = options ?? {};
|
|
17
|
+
const data = Object.fromEntries(Object.entries(fields).map(([key, value]) => [key, toValue(value)]));
|
|
18
|
+
const style = toValue(options?.style) ?? ButtonStyle.Primary;
|
|
19
|
+
if (style === ButtonStyle.Link || style === ButtonStyle.Premium)
|
|
20
|
+
delete data.customId;
|
|
21
|
+
else
|
|
22
|
+
data.customId ??= randomUUID();
|
|
23
|
+
rows.at(-1).addComponents(new ButtonBuilder({ ...data, style, label: toValue(label), type: ComponentType.Button }));
|
|
24
|
+
}
|
|
25
|
+
return rows;
|
|
26
|
+
}
|
|
27
|
+
export function deepToRaw(sourceObj) {
|
|
28
|
+
const copies = /* @__PURE__ */ new WeakMap();
|
|
29
|
+
function unwrap(value) {
|
|
30
|
+
if (isRef(value))
|
|
31
|
+
return unwrap(value.value);
|
|
32
|
+
if (!value || typeof value !== "object")
|
|
33
|
+
return value;
|
|
34
|
+
const raw = toRaw(value);
|
|
35
|
+
const prototype = Object.getPrototypeOf(raw);
|
|
36
|
+
if (!Array.isArray(raw) && prototype !== Object.prototype && prototype !== null)
|
|
37
|
+
return raw;
|
|
38
|
+
if (copies.has(raw))
|
|
39
|
+
return copies.get(raw);
|
|
40
|
+
const copy = Array.isArray(raw) ? [] : Object.create(prototype);
|
|
41
|
+
copies.set(raw, copy);
|
|
42
|
+
for (const key of Object.keys(value))
|
|
43
|
+
copy[key] = unwrap(value[key]);
|
|
44
|
+
return copy;
|
|
45
|
+
}
|
|
46
|
+
return unwrap(sourceObj);
|
|
47
|
+
}
|
|
48
|
+
function registerButtonCollectors(msg, buttons) {
|
|
49
|
+
const newCollectors = [];
|
|
50
|
+
for (const [, buttonHandler, options] of buttons) {
|
|
51
|
+
if (!options || !("customId" in options))
|
|
52
|
+
continue;
|
|
53
|
+
const { filter, ...collectorOptions } = options.collector ?? {};
|
|
54
|
+
const target = "resource" in msg ? msg.resource?.message : msg;
|
|
55
|
+
if (!target)
|
|
56
|
+
throw new Error("Discord returned no message for the component collector");
|
|
57
|
+
const collector = target.createMessageComponentCollector({
|
|
58
|
+
...collectorOptions,
|
|
59
|
+
componentType: ComponentType.Button,
|
|
60
|
+
filter: async (click, ...args) => {
|
|
61
|
+
if (click.customId !== toValue(options.customId) || toValue(options.hide))
|
|
62
|
+
return false;
|
|
63
|
+
try {
|
|
64
|
+
return await filter?.(click, ...args) ?? true;
|
|
65
|
+
} catch (error) {
|
|
66
|
+
logDiscordFailure("Discord button filter failed:", error);
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
newCollectors.push(collector);
|
|
72
|
+
const invoke = bindInvocation(buttonHandler);
|
|
73
|
+
collector.on("collect", async (buttonInteraction) => {
|
|
74
|
+
try {
|
|
75
|
+
if (options.defer ?? true)
|
|
76
|
+
await buttonInteraction.deferUpdate();
|
|
77
|
+
await invoke(buttonInteraction);
|
|
78
|
+
} catch (error) {
|
|
79
|
+
logDiscordFailure("Discord button handler failed:", error);
|
|
80
|
+
const payload = { content: "An error occurred.", flags: MessageFlags.Ephemeral };
|
|
81
|
+
const report = buttonInteraction.deferred || buttonInteraction.replied ? buttonInteraction.followUp(payload) : buttonInteraction.reply(payload);
|
|
82
|
+
await report.catch((error2) => logDiscordFailure("Could not acknowledge failed button:", error2));
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return newCollectors;
|
|
87
|
+
}
|
|
88
|
+
export function sendReactiveMessage(config, content, opts = {}) {
|
|
89
|
+
const { buttons: definitions = [], files, flags, ...options } = opts;
|
|
90
|
+
const buttons = definitions.map(([label, handler, options2]) => [
|
|
91
|
+
label,
|
|
92
|
+
handler,
|
|
93
|
+
{ customId: randomUUID(), ...options2 }
|
|
94
|
+
]);
|
|
95
|
+
const buttonComponents = computed(() => [...options.components ?? [], ...getButtonComponent(buttons)]);
|
|
96
|
+
let collectors = [];
|
|
97
|
+
const messageOptions = reactive({
|
|
98
|
+
...options,
|
|
99
|
+
...content !== void 0 ? { content } : {},
|
|
100
|
+
...buttons.length ? { components: buttonComponents } : {},
|
|
101
|
+
...flags != null ? { flags } : {},
|
|
102
|
+
...files != null ? { files } : {}
|
|
103
|
+
});
|
|
104
|
+
let active = true;
|
|
105
|
+
let pending = false;
|
|
106
|
+
let editing = false;
|
|
107
|
+
const snapshot = computed(() => deepToRaw(messageOptions));
|
|
108
|
+
const stop = watch(snapshot, () => {
|
|
109
|
+
pending = true;
|
|
110
|
+
void flushEdits();
|
|
111
|
+
});
|
|
112
|
+
function dispose() {
|
|
113
|
+
active = false;
|
|
114
|
+
pending = false;
|
|
115
|
+
stop();
|
|
116
|
+
for (const collector of collectors)
|
|
117
|
+
collector.stop();
|
|
118
|
+
}
|
|
119
|
+
onInvocationDispose(dispose);
|
|
120
|
+
const messagePromise = config.send(snapshot.value).then((msg) => {
|
|
121
|
+
if (active)
|
|
122
|
+
collectors = registerButtonCollectors(msg, buttons);
|
|
123
|
+
return msg;
|
|
124
|
+
}).catch((error) => {
|
|
125
|
+
dispose();
|
|
126
|
+
throw error;
|
|
127
|
+
});
|
|
128
|
+
async function flushEdits() {
|
|
129
|
+
if (editing || !active)
|
|
130
|
+
return;
|
|
131
|
+
editing = true;
|
|
132
|
+
try {
|
|
133
|
+
const msg = await messagePromise.catch(() => null);
|
|
134
|
+
while (pending) {
|
|
135
|
+
if (!active || !msg)
|
|
136
|
+
return;
|
|
137
|
+
pending = false;
|
|
138
|
+
try {
|
|
139
|
+
await config.edit(msg, snapshot.value);
|
|
140
|
+
} catch (error) {
|
|
141
|
+
logDiscordFailure("Could not edit reactive Discord message:", error);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
} finally {
|
|
145
|
+
editing = false;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return messagePromise;
|
|
149
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const cooldown: import("../defineMiddleware.js").MiddlewareDefinition;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { defineMiddleware, MiddlewareError } from "../defineMiddleware.js";
|
|
2
|
+
const cooldowns = /* @__PURE__ */ new Map();
|
|
3
|
+
let sweep;
|
|
4
|
+
function scheduleSweep() {
|
|
5
|
+
if (sweep)
|
|
6
|
+
return;
|
|
7
|
+
sweep = setTimeout(() => {
|
|
8
|
+
const now = Date.now();
|
|
9
|
+
for (const [key, expiresAt] of cooldowns) {
|
|
10
|
+
if (expiresAt <= now)
|
|
11
|
+
cooldowns.delete(key);
|
|
12
|
+
}
|
|
13
|
+
sweep = void 0;
|
|
14
|
+
if (cooldowns.size)
|
|
15
|
+
scheduleSweep();
|
|
16
|
+
}, 6e4);
|
|
17
|
+
sweep.unref();
|
|
18
|
+
}
|
|
19
|
+
export const cooldown = defineMiddleware("cooldown", ({ interaction, command, type, next }, options) => {
|
|
20
|
+
const seconds = options?.seconds ?? 5;
|
|
21
|
+
if (typeof seconds !== "number" || !Number.isFinite(seconds) || seconds < 0)
|
|
22
|
+
throw new MiddlewareError("Cooldown duration must be a finite, nonnegative number.", "cooldown");
|
|
23
|
+
if (seconds === 0)
|
|
24
|
+
return next();
|
|
25
|
+
const scope = options?.scope ?? "user";
|
|
26
|
+
const scopeId = scope === "guild" ? interaction.guildId : scope === "channel" ? interaction.channelId : interaction.user.id;
|
|
27
|
+
const commandName = command?.path ?? ("commandName" in interaction ? interaction.commandName : command?.name);
|
|
28
|
+
const commandType = "commandType" in interaction ? interaction.commandType : type;
|
|
29
|
+
const key = JSON.stringify([interaction.applicationId, commandType, commandName, scope, scopeId ?? interaction.user.id]);
|
|
30
|
+
const now = Date.now();
|
|
31
|
+
const expiresAt = cooldowns.get(key);
|
|
32
|
+
if (expiresAt && now < expiresAt) {
|
|
33
|
+
const remaining = Math.ceil((expiresAt - now) / 1e3);
|
|
34
|
+
throw new MiddlewareError(`Please wait ${remaining}s before using this command again.`, "cooldown");
|
|
35
|
+
}
|
|
36
|
+
cooldowns.set(key, now + seconds * 1e3);
|
|
37
|
+
scheduleSweep();
|
|
38
|
+
return next();
|
|
39
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const guildOnly: import("../defineMiddleware.js").MiddlewareDefinition;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { defineMiddleware, MiddlewareError } from "../defineMiddleware.js";
|
|
2
|
+
export const guildOnly = defineMiddleware("guildOnly", ({ interaction, next }) => {
|
|
3
|
+
if (!interaction.guildId)
|
|
4
|
+
throw new MiddlewareError("This command can only be used in a server.", "guildOnly");
|
|
5
|
+
return next();
|
|
6
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const ownerOnly: import("../defineMiddleware.js").MiddlewareDefinition;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import process from "node:process";
|
|
2
|
+
import { defineMiddleware, MiddlewareError } from "../defineMiddleware.js";
|
|
3
|
+
export const ownerOnly = defineMiddleware("ownerOnly", ({ interaction, next }) => {
|
|
4
|
+
const ownerId = process.env.DISCORD_OWNER_ID;
|
|
5
|
+
if (!ownerId)
|
|
6
|
+
throw new MiddlewareError("DISCORD_OWNER_ID environment variable is not set.", "ownerOnly");
|
|
7
|
+
if (interaction.user.id !== ownerId)
|
|
8
|
+
throw new MiddlewareError("This command can only be used by the bot owner.", "ownerOnly");
|
|
9
|
+
return next();
|
|
10
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const requireRole: import("../defineMiddleware.js").MiddlewareDefinition;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { defineMiddleware, MiddlewareError } from "../defineMiddleware.js";
|
|
2
|
+
export const requireRole = defineMiddleware("requireRole", ({ interaction, next }, options) => {
|
|
3
|
+
const roleId = options?.roleId;
|
|
4
|
+
if (!roleId)
|
|
5
|
+
throw new MiddlewareError("requireRole middleware requires a roleId option.", "requireRole");
|
|
6
|
+
const member = interaction.member;
|
|
7
|
+
if (!member)
|
|
8
|
+
throw new MiddlewareError("This command can only be used in a server.", "requireRole");
|
|
9
|
+
const roleIds = Array.isArray(roleId) ? roleId : [roleId];
|
|
10
|
+
const roles = member.roles;
|
|
11
|
+
const hasRole = roleIds.some((id) => Array.isArray(roles) ? roles.includes(id) : roles.cache.has(id));
|
|
12
|
+
if (!hasRole)
|
|
13
|
+
throw new MiddlewareError("You do not have the required role.", "requireRole");
|
|
14
|
+
return next();
|
|
15
|
+
});
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type { BaseMessageOptions, ButtonComponentData, ButtonInteraction, InteractionCallbackResponse, InteractionReplyOptions, InteractionResponse, Message, MessageCollectorOptionsParams } from 'discord.js';
|
|
1
|
+
import type { BaseMessageOptions, ButtonComponentData, ButtonInteraction, ComponentType, InteractionCallbackResponse, InteractionReplyOptions, InteractionResponse, Message, MessageCollectorOptionsParams, ModalSubmitInteraction } from 'discord.js';
|
|
2
2
|
import type { MaybeRef } from 'vue';
|
|
3
3
|
import type { SlashCommandCustomReturnHandler } from '../../../types.js';
|
|
4
|
-
import { ComponentType } from 'discord.js';
|
|
5
4
|
type OmitPreservingCallSignature<T, K extends keyof T = keyof T> = Omit<T, K> & (T extends (...args: infer R) => infer S ? (...args: R) => S : unknown);
|
|
6
5
|
type File = NonNullable<BaseMessageOptions['files']>[number];
|
|
7
6
|
type ButtonCollectorOptions = Partial<Omit<MessageCollectorOptionsParams<ComponentType.Button>, 'componentType'>>;
|
|
@@ -30,7 +29,18 @@ type ReplyFunction = SlashCommandCustomReturnHandler & {
|
|
|
30
29
|
}): ReplyFunction;
|
|
31
30
|
};
|
|
32
31
|
send: ReplyFunction;
|
|
32
|
+
modal: (title: string, fields: Record<string, ModalFieldOptions | 'short' | 'paragraph'>, onSubmit: (values: Record<string, string>, interaction: ModalSubmitInteraction) => unknown, options?: {
|
|
33
|
+
timeout?: number;
|
|
34
|
+
}) => SlashCommandCustomReturnHandler;
|
|
33
35
|
};
|
|
36
|
+
export interface ModalFieldOptions {
|
|
37
|
+
style?: 'short' | 'paragraph';
|
|
38
|
+
label?: string;
|
|
39
|
+
placeholder?: string;
|
|
40
|
+
required?: boolean;
|
|
41
|
+
value?: string;
|
|
42
|
+
minLength?: number;
|
|
43
|
+
maxLength?: number;
|
|
44
|
+
}
|
|
34
45
|
export declare const reply: ReplyFunction;
|
|
35
|
-
export declare function deepToRaw<T extends Record<string, any>>(sourceObj: T): T;
|
|
36
46
|
export {};
|