mini-interactions 0.0.0-stage → 0.16.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/LICENSE +21 -0
- package/MIGRATION.md +126 -0
- package/README.md +303 -2
- package/dist/bot.d.ts +113 -0
- package/dist/bot.js +293 -0
- package/dist/builders/ActionRowBuilder.d.ts +22 -0
- package/dist/builders/ActionRowBuilder.js +33 -0
- package/dist/builders/AutomodRuleBuilder.d.ts +45 -0
- package/dist/builders/AutomodRuleBuilder.js +151 -0
- package/dist/builders/ButtonBuilder.d.ts +53 -0
- package/dist/builders/ButtonBuilder.js +124 -0
- package/dist/builders/ChannelSelectMenuBuilder.d.ts +53 -0
- package/dist/builders/ChannelSelectMenuBuilder.js +105 -0
- package/dist/builders/CheckboxBuilder.d.ts +20 -0
- package/dist/builders/CheckboxBuilder.js +20 -0
- package/dist/builders/CheckboxGroupBuilder.d.ts +25 -0
- package/dist/builders/CheckboxGroupBuilder.js +48 -0
- package/dist/builders/EmbedBuilder.d.ts +33 -0
- package/dist/builders/EmbedBuilder.js +103 -0
- package/dist/builders/FileUploadBuilder.d.ts +17 -0
- package/dist/builders/FileUploadBuilder.js +22 -0
- package/dist/builders/LabelBuilder.d.ts +16 -0
- package/dist/builders/LabelBuilder.js +21 -0
- package/dist/builders/MiniContainerBuilder.d.ts +131 -0
- package/dist/builders/MiniContainerBuilder.js +347 -0
- package/dist/builders/ModalBuilder.d.ts +16 -0
- package/dist/builders/ModalBuilder.js +36 -0
- package/dist/builders/ModalChannelSelectMenuBuilder.d.ts +29 -0
- package/dist/builders/ModalChannelSelectMenuBuilder.js +25 -0
- package/dist/builders/ModalMentionableSelectMenuBuilder.d.ts +53 -0
- package/dist/builders/ModalMentionableSelectMenuBuilder.js +87 -0
- package/dist/builders/ModalRoleSelectMenuBuilder.d.ts +27 -0
- package/dist/builders/ModalRoleSelectMenuBuilder.js +24 -0
- package/dist/builders/ModalStringSelectMenuBuilder.d.ts +62 -0
- package/dist/builders/ModalStringSelectMenuBuilder.js +120 -0
- package/dist/builders/ModalUserSelectMenuBuilder.d.ts +53 -0
- package/dist/builders/ModalUserSelectMenuBuilder.js +96 -0
- package/dist/builders/RadioBuilder.d.ts +22 -0
- package/dist/builders/RadioBuilder.js +36 -0
- package/dist/builders/RoleSelectMenuBuilder.d.ts +47 -0
- package/dist/builders/RoleSelectMenuBuilder.js +92 -0
- package/dist/builders/StringSelectMenuBuilder.d.ts +52 -0
- package/dist/builders/StringSelectMenuBuilder.js +104 -0
- package/dist/builders/StringSelectMenuOptionBuilder.d.ts +46 -0
- package/dist/builders/StringSelectMenuOptionBuilder.js +106 -0
- package/dist/builders/TextInputBuilder.d.ts +23 -0
- package/dist/builders/TextInputBuilder.js +26 -0
- package/dist/builders/index.d.ts +44 -0
- package/dist/builders/index.js +22 -0
- package/dist/builders/shared.d.ts +8 -0
- package/dist/builders/shared.js +11 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +71 -0
- package/dist/commands/CommandBuilder.d.ts +278 -0
- package/dist/commands/CommandBuilder.js +687 -0
- package/dist/commands/ContextMenuCommandBuilder.d.ts +94 -0
- package/dist/commands/ContextMenuCommandBuilder.js +145 -0
- package/dist/core/http/DiscordRestClient.d.ts +400 -0
- package/dist/core/http/DiscordRestClient.js +988 -0
- package/dist/core/interactions/InteractionContext.d.ts +33 -0
- package/dist/core/interactions/InteractionContext.js +61 -0
- package/dist/core/interactions/InteractionVerifier.d.ts +8 -0
- package/dist/core/interactions/InteractionVerifier.js +9 -0
- package/dist/core/messages/DiscordSentMessage.d.ts +32 -0
- package/dist/core/messages/DiscordSentMessage.js +64 -0
- package/dist/core/messages/message-payloads.d.ts +94 -0
- package/dist/core/messages/message-payloads.js +99 -0
- package/dist/core/webhooks/DiscordWebhook.d.ts +14 -0
- package/dist/core/webhooks/DiscordWebhook.js +69 -0
- package/dist/database/MiniDataBuilder.d.ts +61 -0
- package/dist/database/MiniDataBuilder.js +98 -0
- package/dist/database/MiniDatabase.d.ts +67 -0
- package/dist/database/MiniDatabase.js +222 -0
- package/dist/database/MiniDatabaseBuilder.d.ts +58 -0
- package/dist/database/MiniDatabaseBuilder.js +84 -0
- package/dist/database/index.d.ts +5 -0
- package/dist/database/index.js +3 -0
- package/dist/identity/ApplicationIdentityProfile.d.ts +188 -0
- package/dist/identity/ApplicationIdentityProfile.js +207 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +34 -0
- package/dist/lobby/Lobby.d.ts +150 -0
- package/dist/lobby/Lobby.js +90 -0
- package/dist/node/MiniInteraction.d.ts +199 -0
- package/dist/node/MiniInteraction.js +861 -0
- package/dist/node/index.d.ts +2 -0
- package/dist/node/index.js +1 -0
- package/dist/oauth2/DiscordOAuth.d.ts +49 -0
- package/dist/oauth2/DiscordOAuth.js +113 -0
- package/dist/oauth2/OAuth2Builder.d.ts +296 -0
- package/dist/oauth2/OAuth2Builder.js +435 -0
- package/dist/oauth2/OAuth2Scopes.d.ts +285 -0
- package/dist/oauth2/OAuth2Scopes.js +438 -0
- package/dist/oauth2/OAuthTokenStorage.d.ts +37 -0
- package/dist/oauth2/OAuthTokenStorage.js +109 -0
- package/dist/oauth2/index.d.ts +8 -0
- package/dist/oauth2/index.js +4 -0
- package/dist/router/AutocompleteContext.d.ts +36 -0
- package/dist/router/AutocompleteContext.js +82 -0
- package/dist/router/InteractionRouter.d.ts +82 -0
- package/dist/router/InteractionRouter.js +225 -0
- package/dist/types/ButtonStyle.d.ts +16 -0
- package/dist/types/ButtonStyle.js +17 -0
- package/dist/types/ChannelType.d.ts +2 -0
- package/dist/types/ChannelType.js +2 -0
- package/dist/types/Commands.d.ts +35 -0
- package/dist/types/Commands.js +1 -0
- package/dist/types/ComponentTypes.d.ts +19 -0
- package/dist/types/ComponentTypes.js +1 -0
- package/dist/types/InteractionFlags.d.ts +5 -0
- package/dist/types/InteractionFlags.js +6 -0
- package/dist/types/PermissionFlags.d.ts +56 -0
- package/dist/types/PermissionFlags.js +5 -0
- package/dist/types/RoleConnectionMetadata.d.ts +14 -0
- package/dist/types/RoleConnectionMetadata.js +1 -0
- package/dist/types/RoleConnectionMetadataTypes.d.ts +11 -0
- package/dist/types/RoleConnectionMetadataTypes.js +12 -0
- package/dist/types/SeparatorSpacingSize.d.ts +2 -0
- package/dist/types/SeparatorSpacingSize.js +2 -0
- package/dist/types/checkbox.d.ts +16 -0
- package/dist/types/checkbox.js +12 -0
- package/dist/types/discord.d.ts +2 -0
- package/dist/types/discord.js +1 -0
- package/dist/types/radio.d.ts +12 -0
- package/dist/types/radio.js +10 -0
- package/dist/types/validation.d.ts +14 -0
- package/dist/types/validation.js +40 -0
- package/dist/utils/CommandInteractionOptions.d.ts +66 -0
- package/dist/utils/CommandInteractionOptions.js +364 -0
- package/dist/utils/ContextMenuInteraction.d.ts +76 -0
- package/dist/utils/ContextMenuInteraction.js +152 -0
- package/dist/utils/MessageComponentInteraction.d.ts +148 -0
- package/dist/utils/MessageComponentInteraction.js +259 -0
- package/dist/utils/ModalSubmitInteraction.d.ts +86 -0
- package/dist/utils/ModalSubmitInteraction.js +267 -0
- package/dist/utils/cdn.d.ts +30 -0
- package/dist/utils/cdn.js +60 -0
- package/dist/utils/formatting.d.ts +76 -0
- package/dist/utils/formatting.js +132 -0
- package/dist/utils/interactionMessageHelpers.d.ts +40 -0
- package/dist/utils/interactionMessageHelpers.js +118 -0
- package/dist/utils/localization.d.ts +25 -0
- package/dist/utils/localization.js +38 -0
- package/dist/utils/snowflake.d.ts +19 -0
- package/dist/utils/snowflake.js +32 -0
- package/dist/webhooks/WebhookEvent.d.ts +229 -0
- package/dist/webhooks/WebhookEvent.js +59 -0
- package/dist/webhooks/WebhookEventRouter.d.ts +123 -0
- package/dist/webhooks/WebhookEventRouter.js +160 -0
- package/dist/webhooks/WebhookEventVerifier.d.ts +33 -0
- package/dist/webhooks/WebhookEventVerifier.js +37 -0
- package/dist/webhooks/index.d.ts +6 -0
- package/dist/webhooks/index.js +3 -0
- package/package.json +93 -4
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { APIInteractionResponse, APIInteractionResponseCallbackData, APIModalInteractionResponseCallbackData } from "discord-api-types/v10";
|
|
2
|
+
import type { ParsedInteraction } from "../../types/discord.js";
|
|
3
|
+
import { DiscordRestClient } from "../http/DiscordRestClient.js";
|
|
4
|
+
import type { DiscordSentMessage } from "../messages/DiscordSentMessage.js";
|
|
5
|
+
import type { BaseDiscordMessageOptions, DiscordSendMessageOptions } from "../messages/message-payloads.js";
|
|
6
|
+
export type InteractionContextOptions = {
|
|
7
|
+
interaction: ParsedInteraction;
|
|
8
|
+
rest?: DiscordRestClient;
|
|
9
|
+
autoAck?: {
|
|
10
|
+
enabled: boolean;
|
|
11
|
+
delayMs?: number;
|
|
12
|
+
};
|
|
13
|
+
onDiagnostic?: (message: string) => void;
|
|
14
|
+
};
|
|
15
|
+
export declare class InteractionContext {
|
|
16
|
+
private readonly options;
|
|
17
|
+
private responded;
|
|
18
|
+
private autoAckTimer?;
|
|
19
|
+
constructor(options: InteractionContextOptions);
|
|
20
|
+
reply(data: APIInteractionResponseCallbackData): APIInteractionResponse;
|
|
21
|
+
deferReply(ephemeral?: boolean): APIInteractionResponse;
|
|
22
|
+
showModal(data: APIModalInteractionResponseCallbackData): APIInteractionResponse;
|
|
23
|
+
editReply(body: BaseDiscordMessageOptions): Promise<DiscordSentMessage>;
|
|
24
|
+
followUp(body: BaseDiscordMessageOptions): Promise<DiscordSentMessage>;
|
|
25
|
+
/** Deletes the original interaction response. */
|
|
26
|
+
deleteOriginal(): Promise<void>;
|
|
27
|
+
/** Deletes a follow-up message sent for this interaction. */
|
|
28
|
+
deleteFollowup(messageId: string): Promise<void>;
|
|
29
|
+
send(body: DiscordSendMessageOptions): Promise<DiscordSentMessage>;
|
|
30
|
+
get hasResponded(): boolean;
|
|
31
|
+
private requireRest;
|
|
32
|
+
private clearAutoAck;
|
|
33
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export class InteractionContext {
|
|
2
|
+
options;
|
|
3
|
+
responded = false;
|
|
4
|
+
autoAckTimer;
|
|
5
|
+
constructor(options) {
|
|
6
|
+
this.options = options;
|
|
7
|
+
if (options.autoAck?.enabled) {
|
|
8
|
+
const delay = options.autoAck.delayMs ?? 2000;
|
|
9
|
+
this.autoAckTimer = setTimeout(() => {
|
|
10
|
+
if (!this.responded) {
|
|
11
|
+
options.onDiagnostic?.(`[InteractionContext] auto-ack triggered for ${this.options.interaction.id}`);
|
|
12
|
+
}
|
|
13
|
+
}, delay);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
reply(data) {
|
|
17
|
+
this.responded = true;
|
|
18
|
+
this.clearAutoAck();
|
|
19
|
+
return { type: 4, data };
|
|
20
|
+
}
|
|
21
|
+
deferReply(ephemeral = false) {
|
|
22
|
+
this.responded = true;
|
|
23
|
+
this.clearAutoAck();
|
|
24
|
+
return { type: 5, data: ephemeral ? { flags: 64 } : undefined };
|
|
25
|
+
}
|
|
26
|
+
showModal(data) {
|
|
27
|
+
this.responded = true;
|
|
28
|
+
this.clearAutoAck();
|
|
29
|
+
return { type: 9, data };
|
|
30
|
+
}
|
|
31
|
+
editReply(body) {
|
|
32
|
+
return this.requireRest().editOriginalMessage(this.options.interaction.token, body);
|
|
33
|
+
}
|
|
34
|
+
followUp(body) {
|
|
35
|
+
return this.requireRest().createFollowupMessage(this.options.interaction.token, body);
|
|
36
|
+
}
|
|
37
|
+
/** Deletes the original interaction response. */
|
|
38
|
+
deleteOriginal() {
|
|
39
|
+
return this.requireRest().deleteOriginal(this.options.interaction.token);
|
|
40
|
+
}
|
|
41
|
+
/** Deletes a follow-up message sent for this interaction. */
|
|
42
|
+
deleteFollowup(messageId) {
|
|
43
|
+
return this.requireRest().deleteFollowup(this.options.interaction.token, messageId);
|
|
44
|
+
}
|
|
45
|
+
send(body) {
|
|
46
|
+
return this.requireRest().sendMessage(body);
|
|
47
|
+
}
|
|
48
|
+
get hasResponded() {
|
|
49
|
+
return this.responded;
|
|
50
|
+
}
|
|
51
|
+
requireRest() {
|
|
52
|
+
if (!this.options.rest) {
|
|
53
|
+
throw new Error("[InteractionContext] A Discord REST client is required for follow-up messages. Provide applicationId and token when creating the bot.");
|
|
54
|
+
}
|
|
55
|
+
return this.options.rest;
|
|
56
|
+
}
|
|
57
|
+
clearAutoAck() {
|
|
58
|
+
if (this.autoAckTimer)
|
|
59
|
+
clearTimeout(this.autoAckTimer);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { APIInteraction } from 'discord-api-types/v10';
|
|
2
|
+
export type VerifyInteractionRequest = {
|
|
3
|
+
body: string | Uint8Array;
|
|
4
|
+
signature: string;
|
|
5
|
+
timestamp: string;
|
|
6
|
+
publicKey: string;
|
|
7
|
+
};
|
|
8
|
+
export declare function verifyAndParseInteraction(request: VerifyInteractionRequest): Promise<APIInteraction>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { verifyKey } from 'discord-interactions';
|
|
2
|
+
export async function verifyAndParseInteraction(request) {
|
|
3
|
+
const valid = await verifyKey(request.body, request.signature, request.timestamp, request.publicKey);
|
|
4
|
+
if (!valid) {
|
|
5
|
+
throw new Error('[InteractionVerifier] invalid interaction signature');
|
|
6
|
+
}
|
|
7
|
+
const bodyText = typeof request.body === 'string' ? request.body : new TextDecoder().decode(request.body);
|
|
8
|
+
return JSON.parse(bodyText);
|
|
9
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { APIChannel, APIMessage } from "discord-api-types/v10";
|
|
2
|
+
import type { DiscordRestClient } from "../http/DiscordRestClient.js";
|
|
3
|
+
import type { BaseDiscordMessageOptions, DiscordReaction, DiscordStartThreadOptions } from "./message-payloads.js";
|
|
4
|
+
/**
|
|
5
|
+
* Wrapper around a message returned by the API with chainable helpers for
|
|
6
|
+
* editing, deleting, replying, reacting and threading.
|
|
7
|
+
*/
|
|
8
|
+
export declare class DiscordSentMessage {
|
|
9
|
+
private readonly rest;
|
|
10
|
+
readonly raw: APIMessage;
|
|
11
|
+
constructor(rest: DiscordRestClient, raw: APIMessage);
|
|
12
|
+
get id(): string;
|
|
13
|
+
get channelId(): string;
|
|
14
|
+
startThread(options: Omit<DiscordStartThreadOptions, "channelId" | "messageId">): Promise<APIChannel>;
|
|
15
|
+
react(reaction: DiscordReaction): Promise<this>;
|
|
16
|
+
/** Edits this message. Requires the bot to be the author (or manage messages for system flags). */
|
|
17
|
+
edit(options: BaseDiscordMessageOptions): Promise<DiscordSentMessage>;
|
|
18
|
+
/** Deletes this message. */
|
|
19
|
+
delete(reason?: string): Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* Sends a new message in the same channel replying to this one.
|
|
22
|
+
* Accepts either plain message options or a bare string as shorthand for content.
|
|
23
|
+
*/
|
|
24
|
+
reply(options: BaseDiscordMessageOptions | string): Promise<DiscordSentMessage>;
|
|
25
|
+
/** Pins this message in its channel. */
|
|
26
|
+
pin(reason?: string): Promise<void>;
|
|
27
|
+
/** Unpins this message from its channel. */
|
|
28
|
+
unpin(reason?: string): Promise<void>;
|
|
29
|
+
/** Publishes this message in an announcement channel to followers. */
|
|
30
|
+
crosspost(): Promise<APIMessage>;
|
|
31
|
+
toJSON(): APIMessage;
|
|
32
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wrapper around a message returned by the API with chainable helpers for
|
|
3
|
+
* editing, deleting, replying, reacting and threading.
|
|
4
|
+
*/
|
|
5
|
+
export class DiscordSentMessage {
|
|
6
|
+
rest;
|
|
7
|
+
raw;
|
|
8
|
+
constructor(rest, raw) {
|
|
9
|
+
this.rest = rest;
|
|
10
|
+
this.raw = raw;
|
|
11
|
+
}
|
|
12
|
+
get id() {
|
|
13
|
+
return this.raw.id;
|
|
14
|
+
}
|
|
15
|
+
get channelId() {
|
|
16
|
+
return this.raw.channel_id;
|
|
17
|
+
}
|
|
18
|
+
async startThread(options) {
|
|
19
|
+
return this.rest.startThread({
|
|
20
|
+
channelId: this.channelId,
|
|
21
|
+
messageId: this.id,
|
|
22
|
+
...options,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
async react(reaction) {
|
|
26
|
+
await this.rest.addReaction(this.channelId, this.id, reaction);
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
/** Edits this message. Requires the bot to be the author (or manage messages for system flags). */
|
|
30
|
+
async edit(options) {
|
|
31
|
+
return this.rest.editMessage(this.channelId, this.id, options);
|
|
32
|
+
}
|
|
33
|
+
/** Deletes this message. */
|
|
34
|
+
async delete(reason) {
|
|
35
|
+
await this.rest.deleteMessage(this.channelId, this.id, reason);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Sends a new message in the same channel replying to this one.
|
|
39
|
+
* Accepts either plain message options or a bare string as shorthand for content.
|
|
40
|
+
*/
|
|
41
|
+
reply(options) {
|
|
42
|
+
const resolved = typeof options === "string" ? { content: options } : options;
|
|
43
|
+
return this.rest.sendMessage({
|
|
44
|
+
...resolved,
|
|
45
|
+
channelId: this.channelId,
|
|
46
|
+
messageReference: resolved.messageReference ?? { messageId: this.id },
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/** Pins this message in its channel. */
|
|
50
|
+
pin(reason) {
|
|
51
|
+
return this.rest.pinMessage(this.channelId, this.id, reason);
|
|
52
|
+
}
|
|
53
|
+
/** Unpins this message from its channel. */
|
|
54
|
+
unpin(reason) {
|
|
55
|
+
return this.rest.unpinMessage(this.channelId, this.id, reason);
|
|
56
|
+
}
|
|
57
|
+
/** Publishes this message in an announcement channel to followers. */
|
|
58
|
+
crosspost() {
|
|
59
|
+
return this.rest.crosspostMessage(this.channelId, this.id);
|
|
60
|
+
}
|
|
61
|
+
toJSON() {
|
|
62
|
+
return this.raw;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { APIAllowedMentions, ChannelType } from "discord-api-types/v10";
|
|
2
|
+
import { type InteractionMessageData, type MessageFlagLike } from "../../utils/interactionMessageHelpers.js";
|
|
3
|
+
export type DiscordMessageFile = {
|
|
4
|
+
name: string;
|
|
5
|
+
data: ArrayBuffer | Blob | Uint8Array;
|
|
6
|
+
contentType?: string;
|
|
7
|
+
};
|
|
8
|
+
/** Reference to an existing message used for replies. */
|
|
9
|
+
export type DiscordMessageReference = {
|
|
10
|
+
messageId: string;
|
|
11
|
+
channelId?: string;
|
|
12
|
+
failIfNotExists?: boolean;
|
|
13
|
+
};
|
|
14
|
+
/** A single answer of a poll. */
|
|
15
|
+
export type DiscordPollAnswer = {
|
|
16
|
+
text: string;
|
|
17
|
+
emoji?: {
|
|
18
|
+
id?: string;
|
|
19
|
+
name?: string;
|
|
20
|
+
animated?: boolean;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
/** Poll attached to a message send payload. */
|
|
24
|
+
export type DiscordPollSpec = {
|
|
25
|
+
question: {
|
|
26
|
+
text: string;
|
|
27
|
+
};
|
|
28
|
+
answers: DiscordPollAnswer[];
|
|
29
|
+
/** How long the poll stays open, in hours (1–768, default 24). */
|
|
30
|
+
durationHours?: number;
|
|
31
|
+
allowMultiselect?: boolean;
|
|
32
|
+
};
|
|
33
|
+
export type BaseDiscordMessageOptions = Omit<InteractionMessageData, "flags"> & {
|
|
34
|
+
flags?: MessageFlagLike | MessageFlagLike[];
|
|
35
|
+
allowedMentions?: APIAllowedMentions;
|
|
36
|
+
attachments?: Array<Record<string, unknown>>;
|
|
37
|
+
stickerIds?: string[];
|
|
38
|
+
files?: DiscordMessageFile[];
|
|
39
|
+
/** Attaches a poll to this message. */
|
|
40
|
+
poll?: DiscordPollSpec;
|
|
41
|
+
/** Sends this message as a reply when provided. */
|
|
42
|
+
messageReference?: DiscordMessageReference;
|
|
43
|
+
};
|
|
44
|
+
export type DiscordSendMessageOptions = BaseDiscordMessageOptions & {
|
|
45
|
+
channelId: string;
|
|
46
|
+
};
|
|
47
|
+
export type DiscordStartThreadOptions = {
|
|
48
|
+
channelId: string;
|
|
49
|
+
messageId: string;
|
|
50
|
+
name: string;
|
|
51
|
+
autoArchiveDuration?: number;
|
|
52
|
+
rateLimitPerUser?: number;
|
|
53
|
+
reason?: string;
|
|
54
|
+
};
|
|
55
|
+
export type DiscordWebhookSendOptions = BaseDiscordMessageOptions & {
|
|
56
|
+
threadId?: string;
|
|
57
|
+
username?: string;
|
|
58
|
+
avatarUrl?: string;
|
|
59
|
+
};
|
|
60
|
+
/** Partial edit payload for channels (thread fields only apply to threads). */
|
|
61
|
+
export type DiscordChannelEditOptions = {
|
|
62
|
+
name?: string;
|
|
63
|
+
topic?: string;
|
|
64
|
+
nsfw?: boolean;
|
|
65
|
+
rateLimitPerUser?: number;
|
|
66
|
+
/** Thread only: whether the thread is archived. */
|
|
67
|
+
archived?: boolean;
|
|
68
|
+
/** Thread only: whether the thread is locked. */
|
|
69
|
+
locked?: boolean;
|
|
70
|
+
/** Thread only: auto-archive duration in minutes. */
|
|
71
|
+
autoArchiveDuration?: number;
|
|
72
|
+
};
|
|
73
|
+
/** Options for creating a thread directly in a channel (no source message). */
|
|
74
|
+
export type DiscordCreateThreadOptions = {
|
|
75
|
+
channelId: string;
|
|
76
|
+
name: string;
|
|
77
|
+
autoArchiveDuration?: number;
|
|
78
|
+
rateLimitPerUser?: number;
|
|
79
|
+
/** Thread type; e.g. `ChannelType.PrivateThread` (12). Defaults per channel context. */
|
|
80
|
+
type?: ChannelType;
|
|
81
|
+
/** Whether the thread is invitable by non-moderators; private threads only. */
|
|
82
|
+
invitable?: boolean;
|
|
83
|
+
reason?: string;
|
|
84
|
+
};
|
|
85
|
+
export type DiscordReaction = string | {
|
|
86
|
+
name: string;
|
|
87
|
+
id?: string;
|
|
88
|
+
animated?: boolean;
|
|
89
|
+
};
|
|
90
|
+
export declare function normaliseDiscordMessagePayload(options: BaseDiscordMessageOptions): Record<string, unknown>;
|
|
91
|
+
export declare function createMessageRequestInit(options: BaseDiscordMessageOptions): {
|
|
92
|
+
body: BodyInit;
|
|
93
|
+
headers?: HeadersInit;
|
|
94
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { MessageFlags } from "discord-api-types/v10";
|
|
2
|
+
import { InteractionFlags } from "../../types/InteractionFlags.js";
|
|
3
|
+
import { normaliseInteractionMessageData, } from "../../utils/interactionMessageHelpers.js";
|
|
4
|
+
export function normaliseDiscordMessagePayload(options) {
|
|
5
|
+
const payload = normaliseInteractionMessageData({
|
|
6
|
+
content: options.content,
|
|
7
|
+
components: options.components,
|
|
8
|
+
embeds: options.embeds,
|
|
9
|
+
flags: options.flags,
|
|
10
|
+
});
|
|
11
|
+
const resolvedPayload = payload ? { ...payload } : {};
|
|
12
|
+
const flags = resolvedPayload.flags;
|
|
13
|
+
if (typeof flags === "number" && (flags & MessageFlags.Ephemeral) === MessageFlags.Ephemeral) {
|
|
14
|
+
throw new Error("[MiniInteraction] Ephemeral flags are not supported for regular channel or webhook messages.");
|
|
15
|
+
}
|
|
16
|
+
if (options.allowedMentions) {
|
|
17
|
+
resolvedPayload.allowed_mentions = options.allowedMentions;
|
|
18
|
+
}
|
|
19
|
+
if (options.messageReference) {
|
|
20
|
+
const { messageId, channelId, failIfNotExists } = options.messageReference;
|
|
21
|
+
resolvedPayload.message_reference = {
|
|
22
|
+
message_id: messageId,
|
|
23
|
+
...(channelId ? { channel_id: channelId } : {}),
|
|
24
|
+
...(failIfNotExists !== undefined ? { fail_if_not_exists: failIfNotExists } : {}),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
if (options.poll) {
|
|
28
|
+
const { question, answers, durationHours, allowMultiselect } = options.poll;
|
|
29
|
+
resolvedPayload.poll = {
|
|
30
|
+
question,
|
|
31
|
+
answers: answers.map((answer) => ({
|
|
32
|
+
poll_media: {
|
|
33
|
+
text: answer.text,
|
|
34
|
+
...(answer.emoji ? { emoji: answer.emoji } : {}),
|
|
35
|
+
},
|
|
36
|
+
})),
|
|
37
|
+
...(durationHours !== undefined ? { duration: durationHours } : {}),
|
|
38
|
+
...(allowMultiselect !== undefined
|
|
39
|
+
? { allow_multiselect: allowMultiselect }
|
|
40
|
+
: {}),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
if (options.stickerIds && options.stickerIds.length > 0) {
|
|
44
|
+
resolvedPayload.sticker_ids = options.stickerIds;
|
|
45
|
+
}
|
|
46
|
+
// Components V2 messages cannot be mixed with legacy content fields.
|
|
47
|
+
const resolvedFlags = Number(resolvedPayload.flags ?? 0);
|
|
48
|
+
if ((resolvedFlags & InteractionFlags.IsComponentsV2) === InteractionFlags.IsComponentsV2) {
|
|
49
|
+
const forbiddenFields = [];
|
|
50
|
+
if (resolvedPayload.content !== undefined && resolvedPayload.content !== null)
|
|
51
|
+
forbiddenFields.push("content");
|
|
52
|
+
if (resolvedPayload.embeds !== undefined && resolvedPayload.embeds !== null)
|
|
53
|
+
forbiddenFields.push("embeds");
|
|
54
|
+
if (resolvedPayload.sticker_ids !== undefined)
|
|
55
|
+
forbiddenFields.push("sticker_ids");
|
|
56
|
+
if (forbiddenFields.length > 0) {
|
|
57
|
+
throw new Error(`[MiniInteraction] ${forbiddenFields.join(", ")} cannot be used together with the IsComponentsV2 flag. Use TextDisplay components instead.`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const files = options.files ?? [];
|
|
61
|
+
if (options.attachments && options.attachments.length > 0) {
|
|
62
|
+
resolvedPayload.attachments = options.attachments;
|
|
63
|
+
}
|
|
64
|
+
else if (files.length > 0) {
|
|
65
|
+
resolvedPayload.attachments = files.map((file, index) => ({
|
|
66
|
+
id: String(index),
|
|
67
|
+
filename: file.name,
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
70
|
+
return resolvedPayload;
|
|
71
|
+
}
|
|
72
|
+
export function createMessageRequestInit(options) {
|
|
73
|
+
const payload = normaliseDiscordMessagePayload(options);
|
|
74
|
+
const files = options.files ?? [];
|
|
75
|
+
if (files.length === 0) {
|
|
76
|
+
return {
|
|
77
|
+
body: JSON.stringify(payload),
|
|
78
|
+
headers: {
|
|
79
|
+
"Content-Type": "application/json",
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
const formData = new FormData();
|
|
84
|
+
formData.set("payload_json", JSON.stringify(payload));
|
|
85
|
+
files.forEach((file, index) => {
|
|
86
|
+
formData.append(`files[${index}]`, toBlob(file), file.name);
|
|
87
|
+
});
|
|
88
|
+
return { body: formData };
|
|
89
|
+
}
|
|
90
|
+
function toBlob(file) {
|
|
91
|
+
if (file.data instanceof Blob) {
|
|
92
|
+
return file.data;
|
|
93
|
+
}
|
|
94
|
+
if (file.data instanceof ArrayBuffer) {
|
|
95
|
+
return new Blob([file.data], file.contentType ? { type: file.contentType } : undefined);
|
|
96
|
+
}
|
|
97
|
+
const bytes = Uint8Array.from(file.data);
|
|
98
|
+
return new Blob([bytes.buffer], file.contentType ? { type: file.contentType } : undefined);
|
|
99
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { DiscordRestClient } from "../http/DiscordRestClient.js";
|
|
2
|
+
import { DiscordSentMessage } from "../messages/DiscordSentMessage.js";
|
|
3
|
+
import { type DiscordWebhookSendOptions } from "../messages/message-payloads.js";
|
|
4
|
+
export declare class DiscordWebhook {
|
|
5
|
+
private readonly rest;
|
|
6
|
+
readonly id: string;
|
|
7
|
+
readonly token: string;
|
|
8
|
+
constructor(rest: DiscordRestClient, id: string, token: string);
|
|
9
|
+
/** Edits a message previously sent by this webhook. */
|
|
10
|
+
edit(messageId: string, options: DiscordWebhookSendOptions): Promise<DiscordSentMessage>;
|
|
11
|
+
/** Deletes a message previously sent by this webhook. */
|
|
12
|
+
delete(messageId: string, threadId?: string): Promise<void>;
|
|
13
|
+
send(options: DiscordWebhookSendOptions): Promise<DiscordSentMessage>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { DiscordSentMessage } from "../messages/DiscordSentMessage.js";
|
|
2
|
+
import { createMessageRequestInit, } from "../messages/message-payloads.js";
|
|
3
|
+
export class DiscordWebhook {
|
|
4
|
+
rest;
|
|
5
|
+
id;
|
|
6
|
+
token;
|
|
7
|
+
constructor(rest, id, token) {
|
|
8
|
+
this.rest = rest;
|
|
9
|
+
this.id = id;
|
|
10
|
+
this.token = token;
|
|
11
|
+
}
|
|
12
|
+
/** Edits a message previously sent by this webhook. */
|
|
13
|
+
async edit(messageId, options) {
|
|
14
|
+
const requestInit = createMessageRequestInit(options);
|
|
15
|
+
const message = await this.rest.request(`/webhooks/${this.id}/${this.token}/messages/${messageId}`, {
|
|
16
|
+
method: "PATCH",
|
|
17
|
+
...requestInit,
|
|
18
|
+
authenticated: false,
|
|
19
|
+
});
|
|
20
|
+
return new DiscordSentMessage(this.rest, message);
|
|
21
|
+
}
|
|
22
|
+
/** Deletes a message previously sent by this webhook. */
|
|
23
|
+
async delete(messageId, threadId) {
|
|
24
|
+
const searchParams = new URLSearchParams();
|
|
25
|
+
if (threadId) {
|
|
26
|
+
searchParams.set("thread_id", threadId);
|
|
27
|
+
}
|
|
28
|
+
const suffix = searchParams.size > 0 ? `?${searchParams.toString()}` : "";
|
|
29
|
+
await this.rest.request(`/webhooks/${this.id}/${this.token}/messages/${messageId}${suffix}`, {
|
|
30
|
+
method: "DELETE",
|
|
31
|
+
authenticated: false,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
async send(options) {
|
|
35
|
+
const { threadId, username, avatarUrl, ...messageOptions } = options;
|
|
36
|
+
const requestInit = createMessageRequestInit(messageOptions);
|
|
37
|
+
const searchParams = new URLSearchParams({ wait: "true" });
|
|
38
|
+
if (threadId) {
|
|
39
|
+
searchParams.set("thread_id", threadId);
|
|
40
|
+
}
|
|
41
|
+
const message = await this.rest.request(`/webhooks/${this.id}/${this.token}?${searchParams.toString()}`, {
|
|
42
|
+
method: "POST",
|
|
43
|
+
body: appendWebhookPayload(requestInit.body, {
|
|
44
|
+
username,
|
|
45
|
+
avatar_url: avatarUrl,
|
|
46
|
+
}),
|
|
47
|
+
headers: requestInit.headers,
|
|
48
|
+
authenticated: false,
|
|
49
|
+
});
|
|
50
|
+
return new DiscordSentMessage(this.rest, message);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function appendWebhookPayload(body, webhookOverrides) {
|
|
54
|
+
if (body instanceof FormData) {
|
|
55
|
+
const payload = JSON.parse(String(body.get("payload_json") ?? "{}"));
|
|
56
|
+
if (webhookOverrides.username)
|
|
57
|
+
payload.username = webhookOverrides.username;
|
|
58
|
+
if (webhookOverrides.avatar_url)
|
|
59
|
+
payload.avatar_url = webhookOverrides.avatar_url;
|
|
60
|
+
body.set("payload_json", JSON.stringify(payload));
|
|
61
|
+
return body;
|
|
62
|
+
}
|
|
63
|
+
const payload = JSON.parse(String(body));
|
|
64
|
+
if (webhookOverrides.username)
|
|
65
|
+
payload.username = webhookOverrides.username;
|
|
66
|
+
if (webhookOverrides.avatar_url)
|
|
67
|
+
payload.avatar_url = webhookOverrides.avatar_url;
|
|
68
|
+
return JSON.stringify(payload);
|
|
69
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a field in the data schema.
|
|
3
|
+
*/
|
|
4
|
+
export interface DataField {
|
|
5
|
+
name: string;
|
|
6
|
+
type: "string" | "number" | "boolean" | "object" | "array";
|
|
7
|
+
required?: boolean;
|
|
8
|
+
default?: unknown;
|
|
9
|
+
description?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Builder for defining data schemas in MiniDatabase.
|
|
13
|
+
* Provides a fluent API for developers to define their data structure.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const userSchema = new MiniDataBuilder()
|
|
18
|
+
* .addField("userId", "string", { required: true })
|
|
19
|
+
* .addField("username", "string", { required: true })
|
|
20
|
+
* .addField("coins", "number", { default: 0 })
|
|
21
|
+
* .addField("metadata", "object", { default: {} });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare class MiniDataBuilder {
|
|
25
|
+
private fields;
|
|
26
|
+
/**
|
|
27
|
+
* Adds a field to the data schema.
|
|
28
|
+
*/
|
|
29
|
+
addField(name: string, type: "string" | "number" | "boolean" | "object" | "array", options?: {
|
|
30
|
+
required?: boolean;
|
|
31
|
+
default?: unknown;
|
|
32
|
+
description?: string;
|
|
33
|
+
}): this;
|
|
34
|
+
/**
|
|
35
|
+
* Removes a field from the schema.
|
|
36
|
+
*/
|
|
37
|
+
removeField(name: string): this;
|
|
38
|
+
/**
|
|
39
|
+
* Gets a specific field definition.
|
|
40
|
+
*/
|
|
41
|
+
getField(name: string): DataField | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Gets all fields in the schema.
|
|
44
|
+
*/
|
|
45
|
+
getFields(): DataField[];
|
|
46
|
+
/**
|
|
47
|
+
* Validates data against the schema.
|
|
48
|
+
*/
|
|
49
|
+
validate(data: Record<string, unknown>): {
|
|
50
|
+
valid: boolean;
|
|
51
|
+
errors: string[];
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Applies default values to data.
|
|
55
|
+
*/
|
|
56
|
+
applyDefaults(data: Record<string, unknown>): Record<string, unknown>;
|
|
57
|
+
/**
|
|
58
|
+
* Serializes the schema to JSON.
|
|
59
|
+
*/
|
|
60
|
+
toJSON(): Record<string, DataField>;
|
|
61
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builder for defining data schemas in MiniDatabase.
|
|
3
|
+
* Provides a fluent API for developers to define their data structure.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* const userSchema = new MiniDataBuilder()
|
|
8
|
+
* .addField("userId", "string", { required: true })
|
|
9
|
+
* .addField("username", "string", { required: true })
|
|
10
|
+
* .addField("coins", "number", { default: 0 })
|
|
11
|
+
* .addField("metadata", "object", { default: {} });
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export class MiniDataBuilder {
|
|
15
|
+
fields = new Map();
|
|
16
|
+
/**
|
|
17
|
+
* Adds a field to the data schema.
|
|
18
|
+
*/
|
|
19
|
+
addField(name, type, options) {
|
|
20
|
+
this.fields.set(name, {
|
|
21
|
+
name,
|
|
22
|
+
type,
|
|
23
|
+
required: options?.required ?? false,
|
|
24
|
+
default: options?.default,
|
|
25
|
+
description: options?.description,
|
|
26
|
+
});
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Removes a field from the schema.
|
|
31
|
+
*/
|
|
32
|
+
removeField(name) {
|
|
33
|
+
this.fields.delete(name);
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Gets a specific field definition.
|
|
38
|
+
*/
|
|
39
|
+
getField(name) {
|
|
40
|
+
return this.fields.get(name);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Gets all fields in the schema.
|
|
44
|
+
*/
|
|
45
|
+
getFields() {
|
|
46
|
+
return Array.from(this.fields.values());
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Validates data against the schema.
|
|
50
|
+
*/
|
|
51
|
+
validate(data) {
|
|
52
|
+
const errors = [];
|
|
53
|
+
for (const field of this.fields.values()) {
|
|
54
|
+
const value = data[field.name];
|
|
55
|
+
// Check required fields
|
|
56
|
+
if (field.required && (value === undefined || value === null)) {
|
|
57
|
+
errors.push(`Field "${field.name}" is required`);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
// Check type if value exists
|
|
61
|
+
if (value !== undefined && value !== null) {
|
|
62
|
+
const actualType = Array.isArray(value) ? "array" : typeof value;
|
|
63
|
+
if (actualType !== field.type) {
|
|
64
|
+
errors.push(`Field "${field.name}" must be of type "${field.type}", got "${actualType}"`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
valid: errors.length === 0,
|
|
70
|
+
errors,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Applies default values to data.
|
|
75
|
+
*/
|
|
76
|
+
applyDefaults(data) {
|
|
77
|
+
const result = { ...data };
|
|
78
|
+
for (const field of this.fields.values()) {
|
|
79
|
+
if (result[field.name] === undefined && field.default !== undefined) {
|
|
80
|
+
result[field.name] =
|
|
81
|
+
typeof field.default === "object"
|
|
82
|
+
? JSON.parse(JSON.stringify(field.default))
|
|
83
|
+
: field.default;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Serializes the schema to JSON.
|
|
90
|
+
*/
|
|
91
|
+
toJSON() {
|
|
92
|
+
const result = {};
|
|
93
|
+
for (const [key, field] of this.fields) {
|
|
94
|
+
result[key] = field;
|
|
95
|
+
}
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
}
|