mini-interactions 0.0.0-stage → 0.15.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 +253 -2
- package/dist/bot.d.ts +58 -0
- package/dist/bot.js +217 -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/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 +90 -4
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { InteractionResponseType, } from "discord-api-types/v10";
|
|
2
|
+
import { normaliseInteractionMessageData, normaliseMessageFlags, } from "./interactionMessageHelpers.js";
|
|
3
|
+
/**
|
|
4
|
+
* Wraps a raw modal submit interaction with helper methods mirroring Discord's expected responses.
|
|
5
|
+
*
|
|
6
|
+
* @param interaction - The raw interaction payload from Discord.
|
|
7
|
+
* @param helpers - Optional callback to capture the final interaction response.
|
|
8
|
+
* @returns A helper-augmented interaction object.
|
|
9
|
+
*/
|
|
10
|
+
export function createModalSubmitInteraction(interaction, helpers) {
|
|
11
|
+
let capturedResponse = null;
|
|
12
|
+
let isDeferred = false;
|
|
13
|
+
const captureResponse = (response) => {
|
|
14
|
+
capturedResponse = response;
|
|
15
|
+
return response;
|
|
16
|
+
};
|
|
17
|
+
const reply = async (data) => {
|
|
18
|
+
const normalisedData = normaliseInteractionMessageData(data);
|
|
19
|
+
if (!normalisedData) {
|
|
20
|
+
throw new Error("[MiniInteraction] Modal replies require response data to be provided.");
|
|
21
|
+
}
|
|
22
|
+
const response = captureResponse({
|
|
23
|
+
type: InteractionResponseType.ChannelMessageWithSource,
|
|
24
|
+
data: normalisedData,
|
|
25
|
+
});
|
|
26
|
+
if (isDeferred && helpers?.sendFollowUp) {
|
|
27
|
+
await helpers.sendFollowUp(interaction.token, response, '@original');
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
helpers?.onAck?.(response);
|
|
31
|
+
}
|
|
32
|
+
return response;
|
|
33
|
+
};
|
|
34
|
+
const editReply = async (data) => {
|
|
35
|
+
const normalisedData = normaliseInteractionMessageData(data);
|
|
36
|
+
const response = captureResponse({
|
|
37
|
+
type: InteractionResponseType.ChannelMessageWithSource,
|
|
38
|
+
data: normalisedData ?? { content: "" },
|
|
39
|
+
});
|
|
40
|
+
if (helpers?.sendFollowUp) {
|
|
41
|
+
await helpers.sendFollowUp(interaction.token, response, '@original');
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
helpers?.onAck?.(response);
|
|
45
|
+
}
|
|
46
|
+
return response;
|
|
47
|
+
};
|
|
48
|
+
const deferReply = (options) => {
|
|
49
|
+
const flags = normaliseMessageFlags(options?.flags);
|
|
50
|
+
const response = flags !== undefined
|
|
51
|
+
? {
|
|
52
|
+
type: InteractionResponseType.DeferredChannelMessageWithSource,
|
|
53
|
+
data: { flags },
|
|
54
|
+
}
|
|
55
|
+
: {
|
|
56
|
+
type: InteractionResponseType.DeferredChannelMessageWithSource,
|
|
57
|
+
};
|
|
58
|
+
captureResponse(response);
|
|
59
|
+
isDeferred = true;
|
|
60
|
+
helpers?.onAck?.(response);
|
|
61
|
+
return response;
|
|
62
|
+
};
|
|
63
|
+
const getResponse = () => capturedResponse;
|
|
64
|
+
const getTextFieldValue = (customId) => {
|
|
65
|
+
const findValue = (components) => {
|
|
66
|
+
for (const component of components) {
|
|
67
|
+
if (component?.custom_id === customId &&
|
|
68
|
+
typeof component.value === "string") {
|
|
69
|
+
return component.value;
|
|
70
|
+
}
|
|
71
|
+
// Nested Label wrapper: single child in .component
|
|
72
|
+
if (component?.component) {
|
|
73
|
+
const found = findValue([component.component]);
|
|
74
|
+
if (found)
|
|
75
|
+
return found;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return undefined;
|
|
79
|
+
};
|
|
80
|
+
for (const top of interaction.data.components) {
|
|
81
|
+
if ("components" in top && Array.isArray(top.components)) {
|
|
82
|
+
const result = findValue(top.components);
|
|
83
|
+
if (result)
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
if ("component" in top && top.component) {
|
|
87
|
+
const result = findValue([top.component]);
|
|
88
|
+
if (result)
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return undefined;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Helper method to get the value(s) of a select menu component by its custom ID.
|
|
96
|
+
* Handles the nested structure of modal components (Action Rows -> Components,
|
|
97
|
+
* and Label -> single component). Select menus in modals are typically inside
|
|
98
|
+
* Label components, not Action Rows.
|
|
99
|
+
*/
|
|
100
|
+
const getSelectMenuValues = (customId) => {
|
|
101
|
+
const findValues = (components) => {
|
|
102
|
+
for (const component of components) {
|
|
103
|
+
if (component?.custom_id === customId &&
|
|
104
|
+
Array.isArray(component.values)) {
|
|
105
|
+
return component.values;
|
|
106
|
+
}
|
|
107
|
+
// Nested Label wrapper: single child in .component
|
|
108
|
+
if (component?.component) {
|
|
109
|
+
const found = findValues([component.component]);
|
|
110
|
+
if (found)
|
|
111
|
+
return found;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return undefined;
|
|
115
|
+
};
|
|
116
|
+
for (const top of interaction.data.components) {
|
|
117
|
+
// Action Row: multiple components in .components
|
|
118
|
+
if ("components" in top && Array.isArray(top.components)) {
|
|
119
|
+
const result = findValues(top.components);
|
|
120
|
+
if (result)
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
// Label: select menu (and other modal components) wrapped in .component
|
|
124
|
+
if ("component" in top && top.component) {
|
|
125
|
+
const result = findValues([top.component]);
|
|
126
|
+
if (result)
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return undefined;
|
|
131
|
+
};
|
|
132
|
+
const getComponentValue = (customId) => {
|
|
133
|
+
const textValue = getTextFieldValue(customId);
|
|
134
|
+
if (textValue !== undefined) {
|
|
135
|
+
return textValue;
|
|
136
|
+
}
|
|
137
|
+
return getSelectMenuValues(customId);
|
|
138
|
+
};
|
|
139
|
+
/** Recursively locates a submitted component by its custom id, walking
|
|
140
|
+
* Action Row children and Label wrappers alike. */
|
|
141
|
+
const findSubmittedComponent = (components, id) => {
|
|
142
|
+
for (const component of components) {
|
|
143
|
+
if (component?.custom_id === id)
|
|
144
|
+
return component;
|
|
145
|
+
if (Array.isArray(component?.components)) {
|
|
146
|
+
const found = findSubmittedComponent(component.components, id);
|
|
147
|
+
if (found)
|
|
148
|
+
return found;
|
|
149
|
+
}
|
|
150
|
+
if (component?.component) {
|
|
151
|
+
const found = findSubmittedComponent([component.component], id);
|
|
152
|
+
if (found)
|
|
153
|
+
return found;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return undefined;
|
|
157
|
+
};
|
|
158
|
+
const findTopLevelById = (id) => {
|
|
159
|
+
for (const top of interaction.data.components) {
|
|
160
|
+
if (top.custom_id === id)
|
|
161
|
+
return top;
|
|
162
|
+
if ("components" in top && Array.isArray(top.components)) {
|
|
163
|
+
const found = findSubmittedComponent(top.components, id);
|
|
164
|
+
if (found)
|
|
165
|
+
return found;
|
|
166
|
+
}
|
|
167
|
+
if ("component" in top && top.component) {
|
|
168
|
+
const found = findSubmittedComponent([top.component], id);
|
|
169
|
+
if (found)
|
|
170
|
+
return found;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return undefined;
|
|
174
|
+
};
|
|
175
|
+
const getRadioGroupValue = (customId) => {
|
|
176
|
+
const component = findTopLevelById(customId);
|
|
177
|
+
return typeof component?.value === "string" ? component.value : undefined;
|
|
178
|
+
};
|
|
179
|
+
const getCheckboxGroupValues = (customId) => {
|
|
180
|
+
const component = findTopLevelById(customId);
|
|
181
|
+
return Array.isArray(component?.values) ? component.values : [];
|
|
182
|
+
};
|
|
183
|
+
const getCheckboxValue = (customId) => {
|
|
184
|
+
const component = findTopLevelById(customId);
|
|
185
|
+
return typeof component?.value === "boolean" ? component.value : undefined;
|
|
186
|
+
};
|
|
187
|
+
const getFileUploadValues = (customId) => {
|
|
188
|
+
const component = findTopLevelById(customId);
|
|
189
|
+
return Array.isArray(component?.values) ? component.values : [];
|
|
190
|
+
};
|
|
191
|
+
const getRoles = (customId) => {
|
|
192
|
+
const values = getSelectMenuValues(customId);
|
|
193
|
+
const resolved = interaction.data.resolved;
|
|
194
|
+
if (!values || !resolved?.roles)
|
|
195
|
+
return [];
|
|
196
|
+
const roles = [];
|
|
197
|
+
for (const id of values) {
|
|
198
|
+
const role = resolved.roles[id];
|
|
199
|
+
if (role)
|
|
200
|
+
roles.push(role);
|
|
201
|
+
}
|
|
202
|
+
return roles;
|
|
203
|
+
};
|
|
204
|
+
const getRole = (customId) => getRoles(customId)[0];
|
|
205
|
+
const getUsers = (customId) => {
|
|
206
|
+
const values = getSelectMenuValues(customId);
|
|
207
|
+
const resolved = interaction.data.resolved;
|
|
208
|
+
if (!values || !resolved?.users)
|
|
209
|
+
return [];
|
|
210
|
+
const users = [];
|
|
211
|
+
for (const id of values) {
|
|
212
|
+
const user = resolved.users[id];
|
|
213
|
+
if (user) {
|
|
214
|
+
const member = resolved.members?.[id];
|
|
215
|
+
users.push({ user, member });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return users;
|
|
219
|
+
};
|
|
220
|
+
const getUser = (customId) => getUsers(customId)[0];
|
|
221
|
+
const getChannels = (customId) => {
|
|
222
|
+
const values = getSelectMenuValues(customId);
|
|
223
|
+
const resolved = interaction.data.resolved;
|
|
224
|
+
if (!values || !resolved?.channels)
|
|
225
|
+
return [];
|
|
226
|
+
const channels = [];
|
|
227
|
+
for (const id of values) {
|
|
228
|
+
const channel = resolved.channels[id];
|
|
229
|
+
if (channel)
|
|
230
|
+
channels.push(channel);
|
|
231
|
+
}
|
|
232
|
+
return channels;
|
|
233
|
+
};
|
|
234
|
+
const getChannel = (customId) => getChannels(customId)[0];
|
|
235
|
+
const getAttachment = (customId) => {
|
|
236
|
+
const value = getComponentValue(customId);
|
|
237
|
+
if (!value)
|
|
238
|
+
return undefined;
|
|
239
|
+
const attachmentId = Array.isArray(value) ? value[0] : value;
|
|
240
|
+
if (!attachmentId)
|
|
241
|
+
return undefined;
|
|
242
|
+
return interaction.data.resolved?.attachments?.[attachmentId];
|
|
243
|
+
};
|
|
244
|
+
return Object.assign(interaction, {
|
|
245
|
+
reply,
|
|
246
|
+
deferReply,
|
|
247
|
+
editReply,
|
|
248
|
+
getResponse,
|
|
249
|
+
getTextFieldValue,
|
|
250
|
+
getSelectMenuValues,
|
|
251
|
+
getComponentValue,
|
|
252
|
+
getRadioGroupValue,
|
|
253
|
+
getCheckboxGroupValues,
|
|
254
|
+
getCheckboxValue,
|
|
255
|
+
getFileUploadValues,
|
|
256
|
+
getRoles,
|
|
257
|
+
getRole,
|
|
258
|
+
getUsers,
|
|
259
|
+
getUser,
|
|
260
|
+
getChannels,
|
|
261
|
+
getChannel,
|
|
262
|
+
getAttachment,
|
|
263
|
+
sendFollowUp: helpers?.sendFollowUp,
|
|
264
|
+
canRespond: helpers?.canRespond,
|
|
265
|
+
trackResponse: helpers?.trackResponse,
|
|
266
|
+
});
|
|
267
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Discord CDN URL builders (discord.js `CDN` class equivalents) as pure functions.
|
|
3
|
+
*
|
|
4
|
+
* All helpers accept an optional `{ format?, size? }` options object; animated
|
|
5
|
+
* hashes default to the GIF format.
|
|
6
|
+
*/
|
|
7
|
+
export type ImageSize = 16 | 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096;
|
|
8
|
+
export type ImageFormat = "png" | "jpg" | "jpeg" | "webp" | "gif";
|
|
9
|
+
export type ImageOptions = {
|
|
10
|
+
format?: ImageFormat;
|
|
11
|
+
size?: ImageSize;
|
|
12
|
+
};
|
|
13
|
+
/** A user's avatar; pass the `avatar` hash from the user object. */
|
|
14
|
+
export declare function avatarURL(userId: string, avatarHash: string, options?: ImageOptions): string;
|
|
15
|
+
/** The default avatar shown when a user has no custom avatar. */
|
|
16
|
+
export declare function defaultAvatarURL(index: number | string): string;
|
|
17
|
+
/** A user's banner; requires the profile fetch to obtain the hash. */
|
|
18
|
+
export declare function userBannerURL(userId: string, bannerHash: string, options?: ImageOptions): string;
|
|
19
|
+
/** A guild's icon. */
|
|
20
|
+
export declare function guildIconURL(guildId: string, iconHash: string, options?: ImageOptions): string;
|
|
21
|
+
/** A guild's banner. */
|
|
22
|
+
export declare function guildBannerURL(guildId: string, bannerHash: string, options?: ImageOptions): string;
|
|
23
|
+
/** A guild's splash image. */
|
|
24
|
+
export declare function guildSplashURL(guildId: string, splashHash: string, options?: ImageOptions): string;
|
|
25
|
+
/** A custom emoji image. */
|
|
26
|
+
export declare function emojiURL(emojiId: string, animated?: boolean, size?: ImageSize): string;
|
|
27
|
+
/** A sticker image (stickers always render as PNG or Lottie). */
|
|
28
|
+
export declare function stickerURL(stickerId: string): string;
|
|
29
|
+
/** Rebuilds a CDN media URL from a message attachment's fields. */
|
|
30
|
+
export declare function attachmentURL(channelId: string, fileId: string, filename: string): string;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Discord CDN URL builders (discord.js `CDN` class equivalents) as pure functions.
|
|
3
|
+
*
|
|
4
|
+
* All helpers accept an optional `{ format?, size? }` options object; animated
|
|
5
|
+
* hashes default to the GIF format.
|
|
6
|
+
*/
|
|
7
|
+
const CDN_BASE = "https://cdn.discordapp.com";
|
|
8
|
+
const MEDIA_BASE = "https://media.discordapp.net";
|
|
9
|
+
function buildUrl(base, path, hash, options) {
|
|
10
|
+
const isAnimated = hash.startsWith("a_");
|
|
11
|
+
const format = options?.format ?? (isAnimated ? "gif" : "png");
|
|
12
|
+
const params = new URLSearchParams();
|
|
13
|
+
if (options?.size !== undefined)
|
|
14
|
+
params.set("size", String(options.size));
|
|
15
|
+
const query = params.size > 0 ? `?${params.toString()}` : "";
|
|
16
|
+
return `${base}${path}/${hash}.${format}${query}`;
|
|
17
|
+
}
|
|
18
|
+
/** A user's avatar; pass the `avatar` hash from the user object. */
|
|
19
|
+
export function avatarURL(userId, avatarHash, options) {
|
|
20
|
+
return buildUrl(CDN_BASE, `/avatars/${userId}`, avatarHash, options);
|
|
21
|
+
}
|
|
22
|
+
/** The default avatar shown when a user has no custom avatar. */
|
|
23
|
+
export function defaultAvatarURL(index) {
|
|
24
|
+
const resolvedIndex = typeof index === "number"
|
|
25
|
+
? Math.abs(index) % 6
|
|
26
|
+
: Number((BigInt(index) >> 22n) % 6n);
|
|
27
|
+
return `${CDN_BASE}/embed/avatars/${resolvedIndex}.png`;
|
|
28
|
+
}
|
|
29
|
+
/** A user's banner; requires the profile fetch to obtain the hash. */
|
|
30
|
+
export function userBannerURL(userId, bannerHash, options) {
|
|
31
|
+
return buildUrl(CDN_BASE, `/banners/${userId}`, bannerHash, options);
|
|
32
|
+
}
|
|
33
|
+
/** A guild's icon. */
|
|
34
|
+
export function guildIconURL(guildId, iconHash, options) {
|
|
35
|
+
return buildUrl(CDN_BASE, `/icons/${guildId}`, iconHash, options);
|
|
36
|
+
}
|
|
37
|
+
/** A guild's banner. */
|
|
38
|
+
export function guildBannerURL(guildId, bannerHash, options) {
|
|
39
|
+
return buildUrl(CDN_BASE, `/banners/${guildId}`, bannerHash, options);
|
|
40
|
+
}
|
|
41
|
+
/** A guild's splash image. */
|
|
42
|
+
export function guildSplashURL(guildId, splashHash, options) {
|
|
43
|
+
return buildUrl(CDN_BASE, `/splashes/${guildId}`, splashHash, options);
|
|
44
|
+
}
|
|
45
|
+
/** A custom emoji image. */
|
|
46
|
+
export function emojiURL(emojiId, animated = false, size) {
|
|
47
|
+
const params = new URLSearchParams();
|
|
48
|
+
if (size !== undefined)
|
|
49
|
+
params.set("size", String(size));
|
|
50
|
+
const query = params.size > 0 ? `?${params.toString()}` : "";
|
|
51
|
+
return `${CDN_BASE}/emojis/${emojiId}.${animated ? "gif" : "png"}${query}`;
|
|
52
|
+
}
|
|
53
|
+
/** A sticker image (stickers always render as PNG or Lottie). */
|
|
54
|
+
export function stickerURL(stickerId) {
|
|
55
|
+
return `${CDN_BASE}/stickers/${stickerId}.png`;
|
|
56
|
+
}
|
|
57
|
+
/** Rebuilds a CDN media URL from a message attachment's fields. */
|
|
58
|
+
export function attachmentURL(channelId, fileId, filename) {
|
|
59
|
+
return `${MEDIA_BASE}/attachments/${channelId}/${fileId}/${encodeURIComponent(filename)}`;
|
|
60
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Discord markdown / text formatting helpers.
|
|
3
|
+
*
|
|
4
|
+
* Every helper is a pure string-to-string function so they compose naturally:
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { bold, italic, heading, userMention } from "mini-interactions";
|
|
8
|
+
*
|
|
9
|
+
* const text = heading(`Welcome ${userMention(userId)}!`, 2) + "\n" + italic(bold("Enjoy your stay."));
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
/** Renders text in **bold**. */
|
|
13
|
+
export declare function bold(text: string): string;
|
|
14
|
+
/** Renders text in *italic*. */
|
|
15
|
+
export declare function italic(text: string): string;
|
|
16
|
+
/** Renders text as __underline__. */
|
|
17
|
+
export declare function underline(text: string): string;
|
|
18
|
+
/** Renders text as ~~strikethrough~~. */
|
|
19
|
+
export declare function strikethrough(text: string): string;
|
|
20
|
+
/** Renders inline code. */
|
|
21
|
+
export declare function inlineCode(text: string): string;
|
|
22
|
+
/**
|
|
23
|
+
* Renders a fenced code block, optionally with a syntax-highlighting language.
|
|
24
|
+
*
|
|
25
|
+
* @param code - The code content (should not contain backtick fences).
|
|
26
|
+
* @param language - Optional language identifier, e.g. `"ts"`.
|
|
27
|
+
*/
|
|
28
|
+
export declare function codeBlock(code: string, language?: string): string;
|
|
29
|
+
/** Renders a single-line (> ) block quote. Multi-line input is quoted per line. */
|
|
30
|
+
export declare function blockQuote(text: string): string;
|
|
31
|
+
/** Renders a multi-line (>>> ) block quote. */
|
|
32
|
+
export declare function multilineBlockQuote(text: string): string;
|
|
33
|
+
/** Renders text as a ||spoiler||. */
|
|
34
|
+
export declare function spoiler(text: string): string;
|
|
35
|
+
/** Renders small, muted subtext (-# text). */
|
|
36
|
+
export declare function subtext(text: string): string;
|
|
37
|
+
/**
|
|
38
|
+
* Renders a markdown heading (# / ## / ###).
|
|
39
|
+
*
|
|
40
|
+
* @param text - The heading content (newlines are not allowed in headings).
|
|
41
|
+
* @param level - Heading level 1-3; defaults to 1.
|
|
42
|
+
*/
|
|
43
|
+
export declare function heading(text: string, level?: 1 | 2 | 3): string;
|
|
44
|
+
/** Renders items as a bulleted list. */
|
|
45
|
+
export declare function bulletList(items: readonly string[]): string;
|
|
46
|
+
/** Renders items as a numbered list. */
|
|
47
|
+
export declare function numberedList(items: readonly string[]): string;
|
|
48
|
+
/** Renders masked links: [label](url). Only works inside embeds and normal messages. */
|
|
49
|
+
export declare function maskLink(label: string, url: string): string;
|
|
50
|
+
/** Timestamp styles supported by Discord's dynamic timestamps. */
|
|
51
|
+
export type DiscordTimestampStyle = "t" | "T" | "d" | "D" | "f" | "F" | "R";
|
|
52
|
+
/**
|
|
53
|
+
* Renders a dynamic timestamp (<t:unix:style>).
|
|
54
|
+
*
|
|
55
|
+
* @param time - A Date, an ISO/parseable date string, or a **unix-seconds** number
|
|
56
|
+
* (matching Discord's own format — not milliseconds).
|
|
57
|
+
* @param style - Optional display style; `"R"` shows relative time ("in 3 minutes").
|
|
58
|
+
*/
|
|
59
|
+
export declare function timestamp(time: Date | number | string, style?: DiscordTimestampStyle): string;
|
|
60
|
+
/** Mentions a user by id (<@id>). */
|
|
61
|
+
export declare function userMention(userId: string): string;
|
|
62
|
+
/** Mentions a role by id (<@&id>). */
|
|
63
|
+
export declare function roleMention(roleId: string): string;
|
|
64
|
+
/** Mentions a channel by id (<#id>). */
|
|
65
|
+
export declare function channelMention(channelId: string): string;
|
|
66
|
+
/** Links a slash command by its full name (e.g. `"ping"` or `"config set key"`) and id. */
|
|
67
|
+
export declare function slashCommandMention(commandName: string, commandId: string): string;
|
|
68
|
+
/**
|
|
69
|
+
* Escapes Discord markdown characters so the text renders literally.
|
|
70
|
+
*
|
|
71
|
+
* @param text - The raw text to escape.
|
|
72
|
+
* @param options.characters - Restrict escaping to these specific characters.
|
|
73
|
+
*/
|
|
74
|
+
export declare function escapeMarkdown(text: string, options?: {
|
|
75
|
+
characters?: readonly string[];
|
|
76
|
+
}): string;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Discord markdown / text formatting helpers.
|
|
3
|
+
*
|
|
4
|
+
* Every helper is a pure string-to-string function so they compose naturally:
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { bold, italic, heading, userMention } from "mini-interactions";
|
|
8
|
+
*
|
|
9
|
+
* const text = heading(`Welcome ${userMention(userId)}!`, 2) + "\n" + italic(bold("Enjoy your stay."));
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
/** Renders text in **bold**. */
|
|
13
|
+
export function bold(text) {
|
|
14
|
+
return `**${text}**`;
|
|
15
|
+
}
|
|
16
|
+
/** Renders text in *italic*. */
|
|
17
|
+
export function italic(text) {
|
|
18
|
+
return `*${text}*`;
|
|
19
|
+
}
|
|
20
|
+
/** Renders text as __underline__. */
|
|
21
|
+
export function underline(text) {
|
|
22
|
+
return `__${text}__`;
|
|
23
|
+
}
|
|
24
|
+
/** Renders text as ~~strikethrough~~. */
|
|
25
|
+
export function strikethrough(text) {
|
|
26
|
+
return `~~${text}~~`;
|
|
27
|
+
}
|
|
28
|
+
/** Renders inline code. */
|
|
29
|
+
export function inlineCode(text) {
|
|
30
|
+
return `\`${text}\``;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Renders a fenced code block, optionally with a syntax-highlighting language.
|
|
34
|
+
*
|
|
35
|
+
* @param code - The code content (should not contain backtick fences).
|
|
36
|
+
* @param language - Optional language identifier, e.g. `"ts"`.
|
|
37
|
+
*/
|
|
38
|
+
export function codeBlock(code, language) {
|
|
39
|
+
return `\`\`\`${language ?? ""}\n${code}\n\`\`\``;
|
|
40
|
+
}
|
|
41
|
+
/** Renders a single-line (> ) block quote. Multi-line input is quoted per line. */
|
|
42
|
+
export function blockQuote(text) {
|
|
43
|
+
return text
|
|
44
|
+
.split("\n")
|
|
45
|
+
.map((line) => `> ${line}`)
|
|
46
|
+
.join("\n");
|
|
47
|
+
}
|
|
48
|
+
/** Renders a multi-line (>>> ) block quote. */
|
|
49
|
+
export function multilineBlockQuote(text) {
|
|
50
|
+
return `>>> ${text}`;
|
|
51
|
+
}
|
|
52
|
+
/** Renders text as a ||spoiler||. */
|
|
53
|
+
export function spoiler(text) {
|
|
54
|
+
return `||${text}||`;
|
|
55
|
+
}
|
|
56
|
+
/** Renders small, muted subtext (-# text). */
|
|
57
|
+
export function subtext(text) {
|
|
58
|
+
return `-# ${text}`;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Renders a markdown heading (# / ## / ###).
|
|
62
|
+
*
|
|
63
|
+
* @param text - The heading content (newlines are not allowed in headings).
|
|
64
|
+
* @param level - Heading level 1-3; defaults to 1.
|
|
65
|
+
*/
|
|
66
|
+
export function heading(text, level = 1) {
|
|
67
|
+
const hashes = "#".repeat(Math.min(Math.max(level, 1), 3));
|
|
68
|
+
return `${hashes} ${text.replace(/\n/g, " ")}`;
|
|
69
|
+
}
|
|
70
|
+
/** Renders items as a bulleted list. */
|
|
71
|
+
export function bulletList(items) {
|
|
72
|
+
return items.map((item) => `- ${item}`).join("\n");
|
|
73
|
+
}
|
|
74
|
+
/** Renders items as a numbered list. */
|
|
75
|
+
export function numberedList(items) {
|
|
76
|
+
return items.map((item, index) => `${index + 1}. ${item}`).join("\n");
|
|
77
|
+
}
|
|
78
|
+
/** Renders masked links: [label](url). Only works inside embeds and normal messages. */
|
|
79
|
+
export function maskLink(label, url) {
|
|
80
|
+
return `[${label}](${url})`;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Renders a dynamic timestamp (<t:unix:style>).
|
|
84
|
+
*
|
|
85
|
+
* @param time - A Date, an ISO/parseable date string, or a **unix-seconds** number
|
|
86
|
+
* (matching Discord's own format — not milliseconds).
|
|
87
|
+
* @param style - Optional display style; `"R"` shows relative time ("in 3 minutes").
|
|
88
|
+
*/
|
|
89
|
+
export function timestamp(time, style) {
|
|
90
|
+
let seconds;
|
|
91
|
+
if (time instanceof Date) {
|
|
92
|
+
seconds = Math.floor(time.getTime() / 1000);
|
|
93
|
+
}
|
|
94
|
+
else if (typeof time === "number") {
|
|
95
|
+
seconds = Math.floor(time);
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
seconds = Math.floor(new Date(time).getTime() / 1000);
|
|
99
|
+
}
|
|
100
|
+
return style ? `<t:${seconds}:${style}>` : `<t:${seconds}>`;
|
|
101
|
+
}
|
|
102
|
+
/** Mentions a user by id (<@id>). */
|
|
103
|
+
export function userMention(userId) {
|
|
104
|
+
return `<@${userId}>`;
|
|
105
|
+
}
|
|
106
|
+
/** Mentions a role by id (<@&id>). */
|
|
107
|
+
export function roleMention(roleId) {
|
|
108
|
+
return `<@&${roleId}>`;
|
|
109
|
+
}
|
|
110
|
+
/** Mentions a channel by id (<#id>). */
|
|
111
|
+
export function channelMention(channelId) {
|
|
112
|
+
return `<#${channelId}>`;
|
|
113
|
+
}
|
|
114
|
+
/** Links a slash command by its full name (e.g. `"ping"` or `"config set key"`) and id. */
|
|
115
|
+
export function slashCommandMention(commandName, commandId) {
|
|
116
|
+
return `</${commandName}:${commandId}>`;
|
|
117
|
+
}
|
|
118
|
+
const ESCAPABLE = ["\\", "*", "_", "~", "`", "|", "#", ">", "-"];
|
|
119
|
+
/**
|
|
120
|
+
* Escapes Discord markdown characters so the text renders literally.
|
|
121
|
+
*
|
|
122
|
+
* @param text - The raw text to escape.
|
|
123
|
+
* @param options.characters - Restrict escaping to these specific characters.
|
|
124
|
+
*/
|
|
125
|
+
export function escapeMarkdown(text, options) {
|
|
126
|
+
const chars = options?.characters ?? ESCAPABLE;
|
|
127
|
+
const unique = [...new Set(chars)];
|
|
128
|
+
return text.replace(new RegExp(`[${unique.map(escapeRegExp).join("")}]`, "g"), (match) => `\\${match}`);
|
|
129
|
+
}
|
|
130
|
+
function escapeRegExp(character) {
|
|
131
|
+
return character.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
132
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { MessageFlags, type APIInteractionResponseCallbackData } from "discord-api-types/v10";
|
|
2
|
+
import type { APIContainerComponent } from "discord-api-types/v10";
|
|
3
|
+
import type { JSONEncodable } from "../builders/shared.js";
|
|
4
|
+
import { InteractionFlags } from "../types/InteractionFlags.js";
|
|
5
|
+
import type { MessageMiniActionRow } from "../types/ComponentTypes.js";
|
|
6
|
+
/** Union of helper flag enums and raw Discord message flags. */
|
|
7
|
+
export type MessageFlagLike = MessageFlags | InteractionFlags;
|
|
8
|
+
/** Top-level components allowed in messages (ActionRows or Containers) */
|
|
9
|
+
export type TopLevelComponent = MessageMiniActionRow | APIContainerComponent;
|
|
10
|
+
/** Message payload accepted by helper reply/edit functions. */
|
|
11
|
+
export type InteractionMessageData = {
|
|
12
|
+
content?: string;
|
|
13
|
+
components?: TopLevelComponent[] | JSONEncodable<TopLevelComponent>[] | JSONEncodable<TopLevelComponent[]>;
|
|
14
|
+
embeds?: unknown[];
|
|
15
|
+
flags?: MessageFlagLike | MessageFlagLike[];
|
|
16
|
+
/** Convenience flag for an ephemeral interaction response. */
|
|
17
|
+
ephemeral?: boolean;
|
|
18
|
+
};
|
|
19
|
+
/** Deferred response payload recognised by helper methods. */
|
|
20
|
+
export type DeferredResponseData = {
|
|
21
|
+
flags: MessageFlagLike | MessageFlagLike[];
|
|
22
|
+
};
|
|
23
|
+
/** Options accepted when deferring a reply. */
|
|
24
|
+
export type DeferReplyOptions = {
|
|
25
|
+
flags?: MessageFlagLike | MessageFlagLike[];
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Normalises helper flag enums into the raw Discord `MessageFlags` bitfield.
|
|
29
|
+
*
|
|
30
|
+
* @param flags - A flag or array of flags from helper enums or raw Discord flags.
|
|
31
|
+
* @returns The value coerced to a `MessageFlags` compatible bitfield.
|
|
32
|
+
*/
|
|
33
|
+
export declare function normaliseMessageFlags(flags: MessageFlagLike | MessageFlagLike[] | undefined): MessageFlags | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* Ensures helper message payloads include correctly normalised message flags.
|
|
36
|
+
*
|
|
37
|
+
* @param data - The helper-supplied response payload.
|
|
38
|
+
* @returns A payload safe to send to Discord's API.
|
|
39
|
+
*/
|
|
40
|
+
export declare function normaliseInteractionMessageData(data?: InteractionMessageData): APIInteractionResponseCallbackData | undefined;
|