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
package/dist/bot.js
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { ApplicationCommandType, InteractionResponseType, InteractionType, } from "discord-api-types/v10";
|
|
2
|
+
import { DiscordRestClient } from "./core/http/DiscordRestClient.js";
|
|
3
|
+
import { InteractionContext } from "./core/interactions/InteractionContext.js";
|
|
4
|
+
import { verifyAndParseInteraction } from "./core/interactions/InteractionVerifier.js";
|
|
5
|
+
import { InteractionRouter, } from "./router/InteractionRouter.js";
|
|
6
|
+
function processEnvironment() {
|
|
7
|
+
return typeof process === "undefined" ? undefined : process.env;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* A small, edge-safe Discord interactions bot.
|
|
11
|
+
*
|
|
12
|
+
* The same instance works as a Fetch API handler in Cloudflare Workers,
|
|
13
|
+
* Vercel Edge/Node functions, Next.js route handlers, Bun, Deno, and modern Node.
|
|
14
|
+
*/
|
|
15
|
+
export class MiniBot {
|
|
16
|
+
router;
|
|
17
|
+
options;
|
|
18
|
+
commandPayloads = new Map();
|
|
19
|
+
restClient;
|
|
20
|
+
restClientKey;
|
|
21
|
+
constructor(options = {}) {
|
|
22
|
+
this.options = options;
|
|
23
|
+
this.router = new InteractionRouter({
|
|
24
|
+
rest: () => this.options.rest ?? this.restClient,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
command(nameOrCommand, handler) {
|
|
28
|
+
if (typeof nameOrCommand === "string") {
|
|
29
|
+
this.router.onCommand(nameOrCommand, handler);
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
const payload = nameOrCommand.toJSON();
|
|
33
|
+
this.setCommandPayload(payload);
|
|
34
|
+
this.router.onCommand(payload.name, handler);
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
userCommand(nameOrCommand, handler) {
|
|
38
|
+
if (typeof nameOrCommand === "string") {
|
|
39
|
+
this.router.onUserCommand(nameOrCommand, handler);
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
const payload = nameOrCommand.toJSON();
|
|
43
|
+
this.setCommandPayload(payload);
|
|
44
|
+
this.router.onUserCommand(payload.name, handler);
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
messageCommand(nameOrCommand, handler) {
|
|
48
|
+
if (typeof nameOrCommand === "string") {
|
|
49
|
+
this.router.onMessageCommand(nameOrCommand, handler);
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
const payload = nameOrCommand.toJSON();
|
|
53
|
+
this.setCommandPayload(payload);
|
|
54
|
+
this.router.onMessageCommand(payload.name, handler);
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
entryPointCommand(nameOrCommand, handler) {
|
|
58
|
+
if (typeof nameOrCommand === "string") {
|
|
59
|
+
this.router.onEntryPointCommand(nameOrCommand, handler);
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
const payload = nameOrCommand.toJSON();
|
|
63
|
+
this.setCommandPayload(payload);
|
|
64
|
+
this.router.onEntryPointCommand(payload.name, handler);
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Registers a file-module command and stores its payload for `syncCommands()`.
|
|
69
|
+
* Generated manifests call this through {@link modules}.
|
|
70
|
+
*/
|
|
71
|
+
commandModule(command) {
|
|
72
|
+
const data = command.data;
|
|
73
|
+
const payload = typeof data.toJSON === "function"
|
|
74
|
+
? data.toJSON()
|
|
75
|
+
: data;
|
|
76
|
+
this.setCommandPayload(payload);
|
|
77
|
+
switch (payload.type ?? ApplicationCommandType.ChatInput) {
|
|
78
|
+
case ApplicationCommandType.User:
|
|
79
|
+
this.router.onUserCommand(payload.name, command.handler);
|
|
80
|
+
break;
|
|
81
|
+
case ApplicationCommandType.Message:
|
|
82
|
+
this.router.onMessageCommand(payload.name, command.handler);
|
|
83
|
+
break;
|
|
84
|
+
case ApplicationCommandType.PrimaryEntryPoint:
|
|
85
|
+
this.router.onEntryPointCommand(payload.name, command.handler);
|
|
86
|
+
break;
|
|
87
|
+
default:
|
|
88
|
+
this.router.onCommand(payload.name, command.handler);
|
|
89
|
+
}
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
/** Registers generated/discovered command, component, and modal modules. */
|
|
93
|
+
modules(manifest) {
|
|
94
|
+
for (const command of manifest.commands ?? []) {
|
|
95
|
+
if (this.isCommandModule(command))
|
|
96
|
+
this.commandModule(command);
|
|
97
|
+
}
|
|
98
|
+
for (const component of manifest.components ?? []) {
|
|
99
|
+
if (this.isHandlerModule(component))
|
|
100
|
+
this.component(component.customId, component.handler);
|
|
101
|
+
}
|
|
102
|
+
for (const modal of manifest.modals ?? []) {
|
|
103
|
+
if (this.isHandlerModule(modal))
|
|
104
|
+
this.modal(modal.customId, modal.handler);
|
|
105
|
+
}
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
component(customIdOrPattern, handler) {
|
|
109
|
+
this.router.onComponent(customIdOrPattern, handler);
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
modal(customId, handler) {
|
|
113
|
+
this.router.onModal(customId, handler);
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
autocomplete(commandName, handler) {
|
|
117
|
+
this.router.onAutocomplete(commandName, handler);
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
use(middleware) {
|
|
121
|
+
this.router.use(middleware);
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
onError(handler) {
|
|
125
|
+
this.router.onError(handler);
|
|
126
|
+
return this;
|
|
127
|
+
}
|
|
128
|
+
onFallback(handler) {
|
|
129
|
+
this.router.onFallback(handler);
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Fetch API entrypoint. Use directly as `export const POST = bot.fetch` or
|
|
134
|
+
* pass it to a Cloudflare/Vercel/Node fetch adapter.
|
|
135
|
+
*/
|
|
136
|
+
fetch = async (request, env = {}) => {
|
|
137
|
+
if (request.method !== "POST") {
|
|
138
|
+
return Response.json({ error: "Method not allowed" }, { status: 405 });
|
|
139
|
+
}
|
|
140
|
+
const signature = request.headers.get("x-signature-ed25519");
|
|
141
|
+
const timestamp = request.headers.get("x-signature-timestamp");
|
|
142
|
+
if (!signature || !timestamp) {
|
|
143
|
+
return Response.json({ error: "Missing Discord signature headers" }, { status: 401 });
|
|
144
|
+
}
|
|
145
|
+
const credentials = this.resolveCredentials(env);
|
|
146
|
+
if (!credentials.publicKey) {
|
|
147
|
+
return Response.json({ error: "Missing DISCORD_PUBLIC_KEY" }, { status: 500 });
|
|
148
|
+
}
|
|
149
|
+
let interaction;
|
|
150
|
+
try {
|
|
151
|
+
interaction = await verifyAndParseInteraction({
|
|
152
|
+
body: await request.text(),
|
|
153
|
+
signature,
|
|
154
|
+
timestamp,
|
|
155
|
+
publicKey: credentials.publicKey,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
return Response.json({ error: error instanceof SyntaxError ? "Malformed interaction JSON" : "Invalid interaction signature" }, { status: error instanceof SyntaxError ? 400 : 401 });
|
|
160
|
+
}
|
|
161
|
+
if (interaction.type === InteractionType.Ping) {
|
|
162
|
+
return Response.json({ type: InteractionResponseType.Pong });
|
|
163
|
+
}
|
|
164
|
+
try {
|
|
165
|
+
const context = new InteractionContext({
|
|
166
|
+
interaction,
|
|
167
|
+
rest: this.getRestClient(credentials),
|
|
168
|
+
});
|
|
169
|
+
const response = await this.router.dispatch(interaction, context);
|
|
170
|
+
return Response.json(response ?? this.defaultResponse(interaction, context));
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
console.error("[mini-interactions] interaction handler failed", error);
|
|
174
|
+
return Response.json({ error: "Interaction handler failed" }, { status: 500 });
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
/**
|
|
178
|
+
* Replaces the globally or guild-visible Discord command list with the
|
|
179
|
+
* builder-defined commands registered on this bot.
|
|
180
|
+
*/
|
|
181
|
+
async syncCommands(env = {}) {
|
|
182
|
+
const credentials = this.resolveCredentials(env);
|
|
183
|
+
const applicationId = credentials.applicationId;
|
|
184
|
+
const token = credentials.token;
|
|
185
|
+
if (!applicationId || !token) {
|
|
186
|
+
throw new Error("[mini-interactions] syncCommands requires applicationId and token (or DISCORD_APPLICATION_ID and DISCORD_BOT_TOKEN).");
|
|
187
|
+
}
|
|
188
|
+
const rest = this.getRestClient(credentials);
|
|
189
|
+
if (!rest) {
|
|
190
|
+
throw new Error("[mini-interactions] Unable to create a Discord REST client.");
|
|
191
|
+
}
|
|
192
|
+
const route = credentials.guildId
|
|
193
|
+
? `/applications/${applicationId}/guilds/${credentials.guildId}/commands`
|
|
194
|
+
: `/applications/${applicationId}/commands`;
|
|
195
|
+
return rest.request(route, {
|
|
196
|
+
method: "PUT",
|
|
197
|
+
body: JSON.stringify([...this.commandPayloads.values()]),
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
isCommandModule(value) {
|
|
201
|
+
return typeof value === "object" && value !== null && "data" in value && "handler" in value;
|
|
202
|
+
}
|
|
203
|
+
isHandlerModule(value) {
|
|
204
|
+
return (typeof value === "object" &&
|
|
205
|
+
value !== null &&
|
|
206
|
+
"customId" in value &&
|
|
207
|
+
"handler" in value &&
|
|
208
|
+
typeof value.customId === "string" &&
|
|
209
|
+
typeof value.handler === "function");
|
|
210
|
+
}
|
|
211
|
+
setCommandPayload(payload) {
|
|
212
|
+
const type = payload.type ?? ApplicationCommandType.ChatInput;
|
|
213
|
+
this.commandPayloads.set(`${type}:${payload.name}`, payload);
|
|
214
|
+
}
|
|
215
|
+
defaultResponse(interaction, context) {
|
|
216
|
+
if (interaction.type === InteractionType.ApplicationCommandAutocomplete) {
|
|
217
|
+
return {
|
|
218
|
+
type: InteractionResponseType.ApplicationCommandAutocompleteResult,
|
|
219
|
+
data: { choices: [] },
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
if (interaction.type === InteractionType.MessageComponent) {
|
|
223
|
+
return { type: InteractionResponseType.DeferredMessageUpdate };
|
|
224
|
+
}
|
|
225
|
+
return context.deferReply();
|
|
226
|
+
}
|
|
227
|
+
resolveCredentials(env) {
|
|
228
|
+
const processEnv = processEnvironment();
|
|
229
|
+
return {
|
|
230
|
+
publicKey: this.options.publicKey ??
|
|
231
|
+
env.DISCORD_PUBLIC_KEY ??
|
|
232
|
+
processEnv?.DISCORD_PUBLIC_KEY,
|
|
233
|
+
applicationId: this.options.applicationId ??
|
|
234
|
+
env.DISCORD_APPLICATION_ID ??
|
|
235
|
+
env.DISCORD_APP_ID ??
|
|
236
|
+
processEnv?.DISCORD_APPLICATION_ID ??
|
|
237
|
+
processEnv?.DISCORD_APP_ID,
|
|
238
|
+
token: this.options.token ??
|
|
239
|
+
env.DISCORD_BOT_TOKEN ??
|
|
240
|
+
env.DISCORD_TOKEN ??
|
|
241
|
+
processEnv?.DISCORD_BOT_TOKEN ??
|
|
242
|
+
processEnv?.DISCORD_TOKEN,
|
|
243
|
+
guildId: this.options.guildId ??
|
|
244
|
+
env.DISCORD_GUILD_ID ??
|
|
245
|
+
processEnv?.DISCORD_GUILD_ID,
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
getRestClient(credentials) {
|
|
249
|
+
if (this.options.rest)
|
|
250
|
+
return this.options.rest;
|
|
251
|
+
if (!credentials.applicationId || !credentials.token)
|
|
252
|
+
return undefined;
|
|
253
|
+
const key = `${credentials.applicationId}:${credentials.token}`;
|
|
254
|
+
if (this.restClient && this.restClientKey === key)
|
|
255
|
+
return this.restClient;
|
|
256
|
+
this.restClient = new DiscordRestClient({
|
|
257
|
+
applicationId: credentials.applicationId,
|
|
258
|
+
token: credentials.token,
|
|
259
|
+
apiBaseUrl: this.options.apiBaseUrl,
|
|
260
|
+
maxRetries: this.options.maxRetries,
|
|
261
|
+
fetchImplementation: this.options.fetchImplementation,
|
|
262
|
+
});
|
|
263
|
+
this.restClientKey = key;
|
|
264
|
+
return this.restClient;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
export function createBot(options = {}) {
|
|
268
|
+
return new MiniBot(options);
|
|
269
|
+
}
|
|
270
|
+
/** Identity helper that gives file-based command modules contextual handler types. */
|
|
271
|
+
export function defineCommand(command) {
|
|
272
|
+
return command;
|
|
273
|
+
}
|
|
274
|
+
/** Identity helper that gives file-based user context menu modules contextual handler types. */
|
|
275
|
+
export function defineUserCommand(command) {
|
|
276
|
+
return command;
|
|
277
|
+
}
|
|
278
|
+
/** Identity helper that gives file-based message context menu modules contextual handler types. */
|
|
279
|
+
export function defineMessageCommand(command) {
|
|
280
|
+
return command;
|
|
281
|
+
}
|
|
282
|
+
/** Identity helper that gives file-based entry point command modules contextual handler types. */
|
|
283
|
+
export function defineEntryPointCommand(command) {
|
|
284
|
+
return command;
|
|
285
|
+
}
|
|
286
|
+
/** Identity helper that gives file-based component modules contextual handler types. */
|
|
287
|
+
export function defineComponent(component) {
|
|
288
|
+
return component;
|
|
289
|
+
}
|
|
290
|
+
/** Identity helper that gives file-based modal modules contextual handler types. */
|
|
291
|
+
export function defineModal(modal) {
|
|
292
|
+
return modal;
|
|
293
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { JSONEncodable } from "./shared.js";
|
|
2
|
+
import type { ActionRowComponent, MiniActionRow } from "../types/ComponentTypes.js";
|
|
3
|
+
/** Values accepted when composing component action rows. */
|
|
4
|
+
export type ActionRowComponentLike<T extends ActionRowComponent> = JSONEncodable<T> | T;
|
|
5
|
+
/** Builder for creating Action Row components. */
|
|
6
|
+
export declare class ActionRowBuilder<T extends ActionRowComponent> implements JSONEncodable<MiniActionRow<T>> {
|
|
7
|
+
private components;
|
|
8
|
+
constructor(data?: Partial<MiniActionRow<T>>);
|
|
9
|
+
/**
|
|
10
|
+
* Adds components to this action row.
|
|
11
|
+
*
|
|
12
|
+
* @param components - The components to add (can be builders or raw objects).
|
|
13
|
+
*/
|
|
14
|
+
addComponents(...components: (T | JSONEncodable<T>)[]): this;
|
|
15
|
+
/**
|
|
16
|
+
* Sets the components for this action row, replacing any existing ones.
|
|
17
|
+
*
|
|
18
|
+
* @param components - The new components to set.
|
|
19
|
+
*/
|
|
20
|
+
setComponents(...components: (T | JSONEncodable<T>)[]): this;
|
|
21
|
+
toJSON(): MiniActionRow<T>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { ComponentType } from "discord-api-types/v10";
|
|
2
|
+
import { resolveJSONEncodable } from "./shared.js";
|
|
3
|
+
/** Builder for creating Action Row components. */
|
|
4
|
+
export class ActionRowBuilder {
|
|
5
|
+
components = [];
|
|
6
|
+
constructor(data = {}) {
|
|
7
|
+
this.components = [...(data.components ?? [])];
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Adds components to this action row.
|
|
11
|
+
*
|
|
12
|
+
* @param components - The components to add (can be builders or raw objects).
|
|
13
|
+
*/
|
|
14
|
+
addComponents(...components) {
|
|
15
|
+
this.components.push(...components.map((c) => resolveJSONEncodable(c)));
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Sets the components for this action row, replacing any existing ones.
|
|
20
|
+
*
|
|
21
|
+
* @param components - The new components to set.
|
|
22
|
+
*/
|
|
23
|
+
setComponents(...components) {
|
|
24
|
+
this.components = components.map((c) => resolveJSONEncodable(c));
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
toJSON() {
|
|
28
|
+
return {
|
|
29
|
+
type: ComponentType.ActionRow,
|
|
30
|
+
components: [...this.components],
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type APIAutoModerationAction, type APIAutoModerationRuleTriggerMetadata, AutoModerationRuleEventType, AutoModerationRuleTriggerType } from "discord-api-types/v10";
|
|
2
|
+
import type { RESTPostAPIAutoModerationRuleJSONBody } from "discord-api-types/v10";
|
|
3
|
+
import type { JSONEncodable } from "./shared.js";
|
|
4
|
+
/**
|
|
5
|
+
* Shape describing initial data used to seed the auto moderation rule builder.
|
|
6
|
+
*/
|
|
7
|
+
export type AutomodRuleBuilderData = Partial<RESTPostAPIAutoModerationRuleJSONBody> & {
|
|
8
|
+
name?: string;
|
|
9
|
+
event_type?: AutoModerationRuleEventType;
|
|
10
|
+
trigger_type?: AutoModerationRuleTriggerType;
|
|
11
|
+
actions?: APIAutoModerationAction[];
|
|
12
|
+
};
|
|
13
|
+
/** Builder that produces Discord auto moderation rule payloads. */
|
|
14
|
+
export declare class AutomodRuleBuilder implements JSONEncodable<RESTPostAPIAutoModerationRuleJSONBody> {
|
|
15
|
+
private data;
|
|
16
|
+
constructor(data?: AutomodRuleBuilderData);
|
|
17
|
+
/** Sets the name of the rule. */
|
|
18
|
+
setName(name: string): this;
|
|
19
|
+
/** Sets the event type that will trigger the rule. */
|
|
20
|
+
setEventType(eventType: AutoModerationRuleEventType): this;
|
|
21
|
+
/** Sets the trigger type for the rule. */
|
|
22
|
+
setTriggerType(triggerType: AutoModerationRuleTriggerType): this;
|
|
23
|
+
/**
|
|
24
|
+
* Assigns trigger metadata describing how the rule should match content.
|
|
25
|
+
*/
|
|
26
|
+
setTriggerMetadata(metadata: APIAutoModerationRuleTriggerMetadata | null | undefined): this;
|
|
27
|
+
/**
|
|
28
|
+
* Replaces the actions executed when the rule triggers.
|
|
29
|
+
*/
|
|
30
|
+
setActions(actions: Array<APIAutoModerationAction | JSONEncodable<APIAutoModerationAction>>): this;
|
|
31
|
+
/** Adds an action to execute when the rule triggers. */
|
|
32
|
+
addAction(action: APIAutoModerationAction | JSONEncodable<APIAutoModerationAction>): this;
|
|
33
|
+
/** Enables or disables the rule. */
|
|
34
|
+
setEnabled(enabled: boolean): this;
|
|
35
|
+
/** Replaces the list of exempt role ids. */
|
|
36
|
+
setExemptRoles(roleIds: string[]): this;
|
|
37
|
+
/** Adds an exempt role id if not already present. */
|
|
38
|
+
addExemptRole(roleId: string): this;
|
|
39
|
+
/** Replaces the list of exempt channel ids. */
|
|
40
|
+
setExemptChannels(channelIds: string[]): this;
|
|
41
|
+
/** Adds an exempt channel id if not already present. */
|
|
42
|
+
addExemptChannel(channelId: string): this;
|
|
43
|
+
/** Serialises the builder into a REST auto moderation rule payload. */
|
|
44
|
+
toJSON(): RESTPostAPIAutoModerationRuleJSONBody;
|
|
45
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { resolveJSONEncodable } from "./shared.js";
|
|
2
|
+
/** Builder that produces Discord auto moderation rule payloads. */
|
|
3
|
+
export class AutomodRuleBuilder {
|
|
4
|
+
data;
|
|
5
|
+
constructor(data = {}) {
|
|
6
|
+
this.data = {
|
|
7
|
+
...data,
|
|
8
|
+
actions: data.actions?.map(cloneAutoModerationAction) ?? [],
|
|
9
|
+
trigger_metadata: data.trigger_metadata
|
|
10
|
+
? cloneTriggerMetadata(data.trigger_metadata)
|
|
11
|
+
: undefined,
|
|
12
|
+
exempt_roles: data.exempt_roles ? [...data.exempt_roles] : undefined,
|
|
13
|
+
exempt_channels: data.exempt_channels
|
|
14
|
+
? [...data.exempt_channels]
|
|
15
|
+
: undefined,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/** Sets the name of the rule. */
|
|
19
|
+
setName(name) {
|
|
20
|
+
this.data.name = name;
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
/** Sets the event type that will trigger the rule. */
|
|
24
|
+
setEventType(eventType) {
|
|
25
|
+
this.data.event_type = eventType;
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
/** Sets the trigger type for the rule. */
|
|
29
|
+
setTriggerType(triggerType) {
|
|
30
|
+
this.data.trigger_type = triggerType;
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Assigns trigger metadata describing how the rule should match content.
|
|
35
|
+
*/
|
|
36
|
+
setTriggerMetadata(metadata) {
|
|
37
|
+
this.data.trigger_metadata = metadata
|
|
38
|
+
? cloneTriggerMetadata(metadata)
|
|
39
|
+
: undefined;
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Replaces the actions executed when the rule triggers.
|
|
44
|
+
*/
|
|
45
|
+
setActions(actions) {
|
|
46
|
+
this.data.actions = actions.map((action) => cloneAutoModerationAction(resolveJSONEncodable(action)));
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
/** Adds an action to execute when the rule triggers. */
|
|
50
|
+
addAction(action) {
|
|
51
|
+
if (!this.data.actions) {
|
|
52
|
+
this.data.actions = [];
|
|
53
|
+
}
|
|
54
|
+
this.data.actions.push(cloneAutoModerationAction(resolveJSONEncodable(action)));
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
/** Enables or disables the rule. */
|
|
58
|
+
setEnabled(enabled) {
|
|
59
|
+
this.data.enabled = enabled;
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
/** Replaces the list of exempt role ids. */
|
|
63
|
+
setExemptRoles(roleIds) {
|
|
64
|
+
this.data.exempt_roles = [...roleIds];
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
/** Adds an exempt role id if not already present. */
|
|
68
|
+
addExemptRole(roleId) {
|
|
69
|
+
if (!this.data.exempt_roles) {
|
|
70
|
+
this.data.exempt_roles = [];
|
|
71
|
+
}
|
|
72
|
+
if (!this.data.exempt_roles.includes(roleId)) {
|
|
73
|
+
this.data.exempt_roles.push(roleId);
|
|
74
|
+
}
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
/** Replaces the list of exempt channel ids. */
|
|
78
|
+
setExemptChannels(channelIds) {
|
|
79
|
+
this.data.exempt_channels = [...channelIds];
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
/** Adds an exempt channel id if not already present. */
|
|
83
|
+
addExemptChannel(channelId) {
|
|
84
|
+
if (!this.data.exempt_channels) {
|
|
85
|
+
this.data.exempt_channels = [];
|
|
86
|
+
}
|
|
87
|
+
if (!this.data.exempt_channels.includes(channelId)) {
|
|
88
|
+
this.data.exempt_channels.push(channelId);
|
|
89
|
+
}
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
/** Serialises the builder into a REST auto moderation rule payload. */
|
|
93
|
+
toJSON() {
|
|
94
|
+
const { name, event_type, trigger_type } = this.data;
|
|
95
|
+
const actions = this.data.actions ?? [];
|
|
96
|
+
if (!name) {
|
|
97
|
+
throw new Error("[AutomodRuleBuilder] name is required");
|
|
98
|
+
}
|
|
99
|
+
if (event_type === undefined) {
|
|
100
|
+
throw new Error("[AutomodRuleBuilder] event type is required");
|
|
101
|
+
}
|
|
102
|
+
if (trigger_type === undefined) {
|
|
103
|
+
throw new Error("[AutomodRuleBuilder] trigger type is required");
|
|
104
|
+
}
|
|
105
|
+
if (actions.length === 0) {
|
|
106
|
+
throw new Error("[AutomodRuleBuilder] at least one action is required");
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
name,
|
|
110
|
+
event_type,
|
|
111
|
+
trigger_type,
|
|
112
|
+
trigger_metadata: this.data.trigger_metadata
|
|
113
|
+
? cloneTriggerMetadata(this.data.trigger_metadata)
|
|
114
|
+
: undefined,
|
|
115
|
+
actions: actions.map(cloneAutoModerationAction),
|
|
116
|
+
enabled: this.data.enabled,
|
|
117
|
+
exempt_roles: this.data.exempt_roles
|
|
118
|
+
? [...this.data.exempt_roles]
|
|
119
|
+
: undefined,
|
|
120
|
+
exempt_channels: this.data.exempt_channels
|
|
121
|
+
? [...this.data.exempt_channels]
|
|
122
|
+
: undefined,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function cloneTriggerMetadata(metadata) {
|
|
127
|
+
return {
|
|
128
|
+
keyword_filter: metadata.keyword_filter
|
|
129
|
+
? [...metadata.keyword_filter]
|
|
130
|
+
: undefined,
|
|
131
|
+
presets: metadata.presets ? [...metadata.presets] : undefined,
|
|
132
|
+
allow_list: metadata.allow_list ? [...metadata.allow_list] : undefined,
|
|
133
|
+
regex_patterns: metadata.regex_patterns
|
|
134
|
+
? [...metadata.regex_patterns]
|
|
135
|
+
: undefined,
|
|
136
|
+
mention_total_limit: metadata.mention_total_limit,
|
|
137
|
+
mention_raid_protection_enabled: metadata.mention_raid_protection_enabled,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function cloneAutoModerationAction(action) {
|
|
141
|
+
return {
|
|
142
|
+
type: action.type,
|
|
143
|
+
metadata: action.metadata
|
|
144
|
+
? {
|
|
145
|
+
channel_id: action.metadata.channel_id,
|
|
146
|
+
duration_seconds: action.metadata.duration_seconds,
|
|
147
|
+
custom_message: action.metadata.custom_message,
|
|
148
|
+
}
|
|
149
|
+
: undefined,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type APIButtonComponent, type APIMessageComponentEmoji } from "discord-api-types/v10";
|
|
2
|
+
import type { JSONEncodable } from "./shared.js";
|
|
3
|
+
import { ButtonStyle } from "../types/ButtonStyle.js";
|
|
4
|
+
/** Shape describing initial button data accepted by the builder. */
|
|
5
|
+
export type ButtonBuilderData = {
|
|
6
|
+
style?: ButtonStyle;
|
|
7
|
+
label?: string;
|
|
8
|
+
emoji?: APIMessageComponentEmoji;
|
|
9
|
+
customId?: string;
|
|
10
|
+
url?: string;
|
|
11
|
+
disabled?: boolean;
|
|
12
|
+
skuId?: string;
|
|
13
|
+
};
|
|
14
|
+
/** Builder for Discord button components. */
|
|
15
|
+
export declare class ButtonBuilder implements JSONEncodable<APIButtonComponent> {
|
|
16
|
+
private data;
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new button builder with optional seed data.
|
|
19
|
+
*/
|
|
20
|
+
constructor(data?: ButtonBuilderData);
|
|
21
|
+
/**
|
|
22
|
+
* Sets the visual style of the button.
|
|
23
|
+
*/
|
|
24
|
+
setStyle(style: ButtonStyle): this;
|
|
25
|
+
/**
|
|
26
|
+
* Sets or clears the button label text.
|
|
27
|
+
*/
|
|
28
|
+
setLabel(label: string | null | undefined): this;
|
|
29
|
+
/**
|
|
30
|
+
* Sets or clears the emoji displayed on the button.
|
|
31
|
+
*/
|
|
32
|
+
setEmoji(emoji: APIMessageComponentEmoji | null | undefined): this;
|
|
33
|
+
/**
|
|
34
|
+
* Configures the button as an interaction component with the provided custom identifier.
|
|
35
|
+
*/
|
|
36
|
+
setCustomId(customId: string): this;
|
|
37
|
+
/**
|
|
38
|
+
* Configures the button as a link button.
|
|
39
|
+
*/
|
|
40
|
+
setURL(url: string): this;
|
|
41
|
+
/**
|
|
42
|
+
* Configures the button as a premium purchase button.
|
|
43
|
+
*/
|
|
44
|
+
setSKUId(skuId: string): this;
|
|
45
|
+
/**
|
|
46
|
+
* Toggles whether the button is disabled.
|
|
47
|
+
*/
|
|
48
|
+
setDisabled(disabled: boolean): this;
|
|
49
|
+
/**
|
|
50
|
+
* Serialises the builder into an API compatible button payload.
|
|
51
|
+
*/
|
|
52
|
+
toJSON(): APIButtonComponent;
|
|
53
|
+
}
|