gralonium 0.4.1 → 0.5.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/lib/classes/Client.d.ts +7 -1
- package/lib/classes/Client.js +22 -0
- package/lib/classes/Context.js +68 -19
- package/lib/classes/Loader.d.ts +4 -2
- package/lib/classes/Loader.js +40 -0
- package/lib/classes/Utils.js +37 -7
- package/lib/classes/builders/ComponentsV2Builder.d.ts +48 -3
- package/lib/classes/builders/ComponentsV2Builder.js +391 -71
- package/lib/classes/builders/InteractionBuilder.d.ts +1 -0
- package/lib/classes/builders/InteractionBuilder.js +1 -0
- package/lib/classes/builders/components/helpers.d.ts +3 -0
- package/lib/classes/builders/components/helpers.js +56 -3
- package/lib/classes/builders/components/linter.d.ts +61 -0
- package/lib/classes/builders/components/linter.js +252 -0
- package/lib/classes/builders/components/overflow.d.ts +151 -0
- package/lib/classes/builders/components/overflow.js +534 -0
- package/lib/classes/views/V2Adaptive.d.ts +103 -0
- package/lib/classes/views/V2Adaptive.js +502 -0
- package/lib/classes/views/V2PersistentRouter.d.ts +124 -0
- package/lib/classes/views/V2PersistentRouter.js +511 -0
- package/lib/classes/views/V2View.d.ts +1 -0
- package/lib/classes/views/V2View.js +53 -15
- package/lib/experiments.d.ts +2 -0
- package/lib/experiments.js +2 -0
- package/lib/main.d.ts +4 -0
- package/lib/main.js +4 -0
- package/package.json +1 -1
|
@@ -0,0 +1,511 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createPersistentRouter = exports.V2PersistentRouter = void 0;
|
|
4
|
+
|
|
5
|
+
const discord_js_1 = require("discord.js");
|
|
6
|
+
const V2Adaptive_js_1 = require("./V2Adaptive.js");
|
|
7
|
+
|
|
8
|
+
const DEFAULT_MESSAGES = Object.freeze({
|
|
9
|
+
guildOnly: "Esta acción solo está disponible dentro de un servidor.",
|
|
10
|
+
notOwner: "No tienes autorización para usar esta acción.",
|
|
11
|
+
missingUserPerms: "No tienes permisos suficientes para ejecutar esta acción.",
|
|
12
|
+
missingBotPerms: "No tengo permisos suficientes en este servidor para completar esta acción.",
|
|
13
|
+
selfTarget: "No puedes aplicar esta acción sobre ti mismo.",
|
|
14
|
+
guildOwnerTarget: "No puedes aplicar esta acción sobre el dueño del servidor.",
|
|
15
|
+
userHierarchy: "Tu rol más alto no supera al del usuario objetivo.",
|
|
16
|
+
botHierarchy: "Mi rol más alto no supera al del usuario objetivo.",
|
|
17
|
+
memberNotFound: "El usuario objetivo ya no se encuentra en este servidor.",
|
|
18
|
+
invalidArgs: "Los datos de esta acción no son válidos o expiraron.",
|
|
19
|
+
inFlight: "⏳ Esta acción ya se está procesando.",
|
|
20
|
+
error: "Ocurrió un error al procesar esta acción.",
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const SNOWFLAKE_REGEX = /^\d{15,22}$/;
|
|
24
|
+
|
|
25
|
+
function normalizeSchemaField(field, index) {
|
|
26
|
+
if (typeof field === "string") {
|
|
27
|
+
let raw = field.trim();
|
|
28
|
+
const optional = raw.endsWith("?");
|
|
29
|
+
if (optional) raw = raw.slice(0, -1).trim();
|
|
30
|
+
|
|
31
|
+
const colonIdx = raw.indexOf(":");
|
|
32
|
+
const name = colonIdx === -1 ? raw : raw.slice(0, colonIdx).trim();
|
|
33
|
+
const type = colonIdx === -1 ? "string" : raw.slice(colonIdx + 1).trim().toLowerCase();
|
|
34
|
+
return { name: name || `arg${index}`, type: type || "string", optional };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (field && typeof field === "object") {
|
|
38
|
+
return {
|
|
39
|
+
name: String(field.name || `arg${index}`),
|
|
40
|
+
type: String(field.type || "string").toLowerCase(),
|
|
41
|
+
optional: Boolean(field.optional),
|
|
42
|
+
default: field.default,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return { name: `arg${index}`, type: "string", optional: true };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function parseSchema(schema, parts, separator) {
|
|
50
|
+
if (!Array.isArray(schema) || schema.length === 0) {
|
|
51
|
+
return { ok: true, args: {} };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const parsed = {};
|
|
55
|
+
for (let i = 0; i < schema.length; i += 1) {
|
|
56
|
+
const spec = normalizeSchemaField(schema[i], i);
|
|
57
|
+
const isRest = spec.type === "rest";
|
|
58
|
+
const rawValue = isRest ? parts.slice(i).join(separator) : parts[i];
|
|
59
|
+
const missing = rawValue === undefined || rawValue === "";
|
|
60
|
+
|
|
61
|
+
if (missing) {
|
|
62
|
+
if (!spec.optional) {
|
|
63
|
+
return { ok: false, field: spec.name, reason: `Missing required argument "${spec.name}".` };
|
|
64
|
+
}
|
|
65
|
+
parsed[spec.name] = spec.default !== undefined ? spec.default : null;
|
|
66
|
+
if (isRest) break;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
switch (spec.type) {
|
|
71
|
+
case "snowflake":
|
|
72
|
+
case "id": {
|
|
73
|
+
if (!SNOWFLAKE_REGEX.test(rawValue)) {
|
|
74
|
+
return { ok: false, field: spec.name, reason: `Argument "${spec.name}" must be a Discord snowflake.` };
|
|
75
|
+
}
|
|
76
|
+
parsed[spec.name] = rawValue;
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
case "int":
|
|
80
|
+
case "integer": {
|
|
81
|
+
const num = Number(rawValue);
|
|
82
|
+
if (!Number.isInteger(num)) {
|
|
83
|
+
return { ok: false, field: spec.name, reason: `Argument "${spec.name}" must be an integer.` };
|
|
84
|
+
}
|
|
85
|
+
parsed[spec.name] = num;
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
case "number":
|
|
89
|
+
case "float": {
|
|
90
|
+
const num = Number(rawValue);
|
|
91
|
+
if (!Number.isFinite(num)) {
|
|
92
|
+
return { ok: false, field: spec.name, reason: `Argument "${spec.name}" must be a number.` };
|
|
93
|
+
}
|
|
94
|
+
parsed[spec.name] = num;
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
case "bool":
|
|
98
|
+
case "boolean": {
|
|
99
|
+
const lowered = String(rawValue).toLowerCase().trim();
|
|
100
|
+
if (["true", "1", "yes", "si", "sí"].includes(lowered)) {
|
|
101
|
+
parsed[spec.name] = true;
|
|
102
|
+
} else if (["false", "0", "no"].includes(lowered)) {
|
|
103
|
+
parsed[spec.name] = false;
|
|
104
|
+
} else {
|
|
105
|
+
return { ok: false, field: spec.name, reason: `Argument "${spec.name}" must be a boolean.` };
|
|
106
|
+
}
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
case "rest":
|
|
110
|
+
case "string":
|
|
111
|
+
default: {
|
|
112
|
+
parsed[spec.name] = rawValue;
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (isRest) break;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return { ok: true, args: parsed };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function hasRequiredPermissions(sourcePerms, required) {
|
|
124
|
+
if (!required || (Array.isArray(required) && required.length === 0)) return true;
|
|
125
|
+
if (!sourcePerms) return false;
|
|
126
|
+
if (typeof sourcePerms.has === "function") {
|
|
127
|
+
return sourcePerms.has(required);
|
|
128
|
+
}
|
|
129
|
+
try {
|
|
130
|
+
const resolved = new discord_js_1.PermissionsBitField(sourcePerms);
|
|
131
|
+
return resolved.has(required);
|
|
132
|
+
} catch {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function getHighestPosition(member) {
|
|
138
|
+
return member?.roles?.highest?.position ?? 0;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Memory-light stateless router for persistent component customIds (`namespace:action:...`),
|
|
143
|
+
* providing typed argument parsing, anti-double-click in-flight locking, and declarative
|
|
144
|
+
* permission and role-hierarchy checks.
|
|
145
|
+
*/
|
|
146
|
+
class V2PersistentRouter {
|
|
147
|
+
#namespace;
|
|
148
|
+
#separator;
|
|
149
|
+
#routes = new Map();
|
|
150
|
+
#inFlight = new Set();
|
|
151
|
+
#options;
|
|
152
|
+
#messages;
|
|
153
|
+
|
|
154
|
+
constructor(namespace, options = {}) {
|
|
155
|
+
const cleanNamespace = String(namespace || "").trim();
|
|
156
|
+
if (!cleanNamespace) {
|
|
157
|
+
throw new TypeError("[V2PersistentRouter] Non-empty namespace is required.");
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
this.#namespace = cleanNamespace;
|
|
161
|
+
this.#separator = String(options.separator || ":");
|
|
162
|
+
this.#options = {
|
|
163
|
+
inFlight: options.inFlight !== undefined ? options.inFlight : "action",
|
|
164
|
+
inFlightSilent: Boolean(options.inFlightSilent),
|
|
165
|
+
guildOnly: Boolean(options.guildOnly),
|
|
166
|
+
onError: typeof options.onError === "function" ? options.onError : null,
|
|
167
|
+
};
|
|
168
|
+
this.#messages = {
|
|
169
|
+
...DEFAULT_MESSAGES,
|
|
170
|
+
...(options.inFlightMessage ? { inFlight: options.inFlightMessage } : {}),
|
|
171
|
+
...(options.messages || {}),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
get namespace() {
|
|
176
|
+
return this.#namespace;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
get separator() {
|
|
180
|
+
return this.#separator;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
get size() {
|
|
184
|
+
return this.#routes.size;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
get activeLocks() {
|
|
188
|
+
return this.#inFlight.size;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Builds a persistent customId prefixed with this router's namespace.
|
|
193
|
+
*/
|
|
194
|
+
id(action, ...args) {
|
|
195
|
+
const parts = [this.#namespace, String(action)];
|
|
196
|
+
for (const arg of args) {
|
|
197
|
+
if (arg === undefined || arg === null || arg === "") continue;
|
|
198
|
+
parts.push(String(arg));
|
|
199
|
+
}
|
|
200
|
+
const built = parts.join(this.#separator);
|
|
201
|
+
if (built.length > 100) {
|
|
202
|
+
throw new RangeError(`[V2PersistentRouter] Generated customId exceeds Discord's 100-char limit (${built.length}): ${built}`);
|
|
203
|
+
}
|
|
204
|
+
return built;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
scopedId(action, ...args) {
|
|
208
|
+
return this.id(action, ...args);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
matches(customId) {
|
|
212
|
+
if (typeof customId !== "string" || !customId) return false;
|
|
213
|
+
return customId === this.#namespace || customId.startsWith(`${this.#namespace}${this.#separator}`);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
parse(customId) {
|
|
217
|
+
if (!this.matches(customId)) return null;
|
|
218
|
+
if (customId === this.#namespace) {
|
|
219
|
+
return { action: "", parts: [] };
|
|
220
|
+
}
|
|
221
|
+
const remainder = customId.slice(this.#namespace.length + this.#separator.length);
|
|
222
|
+
const segments = remainder.split(this.#separator);
|
|
223
|
+
const action = segments.shift() ?? "";
|
|
224
|
+
return { action, parts: segments };
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
on(action, configOrHandler, maybeHandler) {
|
|
228
|
+
const cleanAction = String(action);
|
|
229
|
+
const isConfig = configOrHandler && typeof configOrHandler === "object";
|
|
230
|
+
const handler = isConfig ? configOrHandler.handler ?? maybeHandler : configOrHandler;
|
|
231
|
+
|
|
232
|
+
if (typeof handler !== "function") {
|
|
233
|
+
throw new TypeError(`[V2PersistentRouter] Route "${cleanAction}" requires a handler function.`);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const cfg = isConfig ? configOrHandler : {};
|
|
237
|
+
this.#routes.set(cleanAction, {
|
|
238
|
+
action: cleanAction,
|
|
239
|
+
schema: cfg.schema || null,
|
|
240
|
+
guildOnly: cfg.guildOnly !== undefined ? Boolean(cfg.guildOnly) : this.#options.guildOnly,
|
|
241
|
+
ownerOnly: Boolean(cfg.ownerOnly),
|
|
242
|
+
userPermissions: cfg.userPermissions ?? cfg.permissions ?? null,
|
|
243
|
+
botPermissions: cfg.botPermissions ?? null,
|
|
244
|
+
hierarchy: cfg.hierarchy ?? false,
|
|
245
|
+
allowSelf: Boolean(cfg.allowSelf),
|
|
246
|
+
requireMemberInGuild: cfg.requireMemberInGuild !== false,
|
|
247
|
+
checkBotHierarchy: cfg.checkBotHierarchy !== false,
|
|
248
|
+
inFlight: cfg.inFlight !== undefined ? cfg.inFlight : this.#options.inFlight,
|
|
249
|
+
inFlightSilent: cfg.inFlightSilent !== undefined ? Boolean(cfg.inFlightSilent) : this.#options.inFlightSilent,
|
|
250
|
+
inFlightMessage: cfg.inFlightMessage || this.#messages.inFlight,
|
|
251
|
+
filter: typeof cfg.filter === "function" ? cfg.filter : null,
|
|
252
|
+
handler,
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
return this;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
route(action, configOrHandler, maybeHandler) {
|
|
259
|
+
return this.on(action, configOrHandler, maybeHandler);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
action(action, configOrHandler, maybeHandler) {
|
|
263
|
+
return this.on(action, configOrHandler, maybeHandler);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
async #deny(interaction, content) {
|
|
267
|
+
try {
|
|
268
|
+
if (!interaction.replied && !interaction.deferred && typeof interaction.reply === "function") {
|
|
269
|
+
await interaction.reply({ content, flags: discord_js_1.MessageFlags.Ephemeral });
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
if (interaction.deferred && typeof interaction.followUp === "function") {
|
|
273
|
+
await interaction.followUp({ content, flags: discord_js_1.MessageFlags.Ephemeral });
|
|
274
|
+
}
|
|
275
|
+
} catch {
|
|
276
|
+
// Ignore closed/expired interaction callback failures.
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
#resolveLockKey(route, ctx) {
|
|
281
|
+
const scope = route.inFlight;
|
|
282
|
+
if (!scope || scope === "none") return null;
|
|
283
|
+
if (typeof scope === "function") return scope(ctx) || null;
|
|
284
|
+
|
|
285
|
+
const msgId = ctx.interaction.message?.id || ctx.interaction.id || "0";
|
|
286
|
+
if (scope === "message") return `${this.#namespace}:msg:${msgId}`;
|
|
287
|
+
if (scope === "user") return `${this.#namespace}:${route.action}:u:${ctx.interaction.user?.id || "0"}`;
|
|
288
|
+
if (scope === "customId") return ctx.customId;
|
|
289
|
+
return `${this.#namespace}:${route.action}:${msgId}`;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
async #checkHierarchy(route, ctx) {
|
|
293
|
+
const guild = ctx.guild;
|
|
294
|
+
if (!guild) {
|
|
295
|
+
return this.#messages.guildOnly;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
let resolvedTarget = null;
|
|
299
|
+
if (typeof route.hierarchy === "function") {
|
|
300
|
+
resolvedTarget = await route.hierarchy(ctx);
|
|
301
|
+
} else if (typeof route.hierarchy === "string") {
|
|
302
|
+
resolvedTarget = ctx.args?.[route.hierarchy] ?? ctx.parts[0];
|
|
303
|
+
} else {
|
|
304
|
+
resolvedTarget = ctx.args?.targetId ?? ctx.args?.userId ?? ctx.args?.memberId ?? ctx.parts[0];
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const targetId =
|
|
308
|
+
typeof resolvedTarget === "object" && resolvedTarget !== null
|
|
309
|
+
? resolvedTarget.id ?? resolvedTarget.user?.id
|
|
310
|
+
: resolvedTarget
|
|
311
|
+
? String(resolvedTarget)
|
|
312
|
+
: null;
|
|
313
|
+
|
|
314
|
+
if (!targetId) {
|
|
315
|
+
return this.#messages.invalidArgs;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (!route.allowSelf && targetId === ctx.user?.id) {
|
|
319
|
+
return this.#messages.selfTarget;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if (guild.ownerId && targetId === guild.ownerId) {
|
|
323
|
+
return this.#messages.guildOwnerTarget;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
let targetMember =
|
|
327
|
+
typeof resolvedTarget === "object" && resolvedTarget !== null && resolvedTarget.roles ? resolvedTarget : null;
|
|
328
|
+
|
|
329
|
+
if (!targetMember && guild.members) {
|
|
330
|
+
targetMember = guild.members.cache?.get?.(targetId) ?? null;
|
|
331
|
+
if (!targetMember && typeof guild.members.fetch === "function") {
|
|
332
|
+
targetMember = await guild.members.fetch(targetId).catch(() => null);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
ctx.targetMember = targetMember;
|
|
337
|
+
|
|
338
|
+
if (!targetMember && route.requireMemberInGuild) {
|
|
339
|
+
return this.#messages.memberNotFound;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if (targetMember) {
|
|
343
|
+
const executorId = ctx.user?.id;
|
|
344
|
+
const isGuildOwner = Boolean(guild.ownerId && executorId && guild.ownerId === executorId);
|
|
345
|
+
if (!isGuildOwner) {
|
|
346
|
+
let executorMember = ctx.member?.roles ? ctx.member : guild.members?.cache?.get?.(executorId) ?? null;
|
|
347
|
+
if (!executorMember && executorId && typeof guild.members?.fetch === "function") {
|
|
348
|
+
executorMember = await guild.members.fetch(executorId).catch(() => null);
|
|
349
|
+
}
|
|
350
|
+
if (executorMember && getHighestPosition(executorMember) <= getHighestPosition(targetMember)) {
|
|
351
|
+
return this.#messages.userHierarchy;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (route.checkBotHierarchy) {
|
|
356
|
+
const botId = ctx.bot?.user?.id ?? ctx.interaction?.client?.user?.id ?? guild.members?.me?.id;
|
|
357
|
+
if (botId && targetId === botId) {
|
|
358
|
+
return this.#messages.botHierarchy;
|
|
359
|
+
}
|
|
360
|
+
const botMember = guild.members?.me ?? (botId ? guild.members?.cache?.get?.(botId) : null);
|
|
361
|
+
if (botMember && getHighestPosition(botMember) <= getHighestPosition(targetMember)) {
|
|
362
|
+
return this.#messages.botHierarchy;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
return null;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
async handle(rawInteraction, bot = null) {
|
|
371
|
+
const interaction = V2Adaptive_js_1.unwrapOverflowFallbackInteraction(rawInteraction);
|
|
372
|
+
if (!interaction || !this.matches(interaction.customId)) {
|
|
373
|
+
return false;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const parsed = this.parse(interaction.customId);
|
|
377
|
+
if (!parsed) return false;
|
|
378
|
+
|
|
379
|
+
const route = this.#routes.get(parsed.action) || this.#routes.get("*");
|
|
380
|
+
if (!route) return false;
|
|
381
|
+
|
|
382
|
+
const clientRef = bot || interaction.client || null;
|
|
383
|
+
const ctx = {
|
|
384
|
+
bot: clientRef,
|
|
385
|
+
client: clientRef,
|
|
386
|
+
interaction,
|
|
387
|
+
router: this,
|
|
388
|
+
namespace: this.#namespace,
|
|
389
|
+
action: parsed.action,
|
|
390
|
+
customId: interaction.customId,
|
|
391
|
+
parts: parsed.parts,
|
|
392
|
+
args: {},
|
|
393
|
+
values: Array.isArray(interaction.values) ? interaction.values : [],
|
|
394
|
+
guild: interaction.guild || null,
|
|
395
|
+
channel: interaction.channel || null,
|
|
396
|
+
member: interaction.member || null,
|
|
397
|
+
user: interaction.user || null,
|
|
398
|
+
targetMember: null,
|
|
399
|
+
id: (actionName, ...rest) => this.id(actionName, ...rest),
|
|
400
|
+
reply: (payload, opts) => V2Adaptive_js_1.V2Adaptive.send(interaction, payload, opts),
|
|
401
|
+
replyEphemeral: (payload, opts) =>
|
|
402
|
+
V2Adaptive_js_1.V2Adaptive.send(interaction, payload, { ...(opts || {}), ephemeral: true }),
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
if (route.guildOnly && !interaction.guild && !interaction.guildId) {
|
|
406
|
+
await this.#deny(interaction, this.#messages.guildOnly);
|
|
407
|
+
return true;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
if (route.ownerOnly) {
|
|
411
|
+
const owners = clientRef?.ops?.owners || [];
|
|
412
|
+
if (!Array.isArray(owners) || !owners.includes(ctx.user?.id)) {
|
|
413
|
+
await this.#deny(interaction, this.#messages.notOwner);
|
|
414
|
+
return true;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
if (route.userPermissions) {
|
|
419
|
+
const sourcePerms = interaction.memberPermissions ?? interaction.member?.permissions ?? null;
|
|
420
|
+
if (!hasRequiredPermissions(sourcePerms, route.userPermissions)) {
|
|
421
|
+
await this.#deny(interaction, this.#messages.missingUserPerms);
|
|
422
|
+
return true;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if (route.botPermissions) {
|
|
427
|
+
const botPerms =
|
|
428
|
+
interaction.appPermissions ?? interaction.guild?.members?.me?.permissions ?? null;
|
|
429
|
+
if (!hasRequiredPermissions(botPerms, route.botPermissions)) {
|
|
430
|
+
await this.#deny(interaction, this.#messages.missingBotPerms);
|
|
431
|
+
return true;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
if (route.schema) {
|
|
436
|
+
const schemaResult = parseSchema(route.schema, parsed.parts, this.#separator);
|
|
437
|
+
if (!schemaResult.ok) {
|
|
438
|
+
await this.#deny(interaction, this.#messages.invalidArgs);
|
|
439
|
+
return true;
|
|
440
|
+
}
|
|
441
|
+
ctx.args = schemaResult.args;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
if (route.hierarchy) {
|
|
445
|
+
const hierarchyError = await this.#checkHierarchy(route, ctx);
|
|
446
|
+
if (hierarchyError) {
|
|
447
|
+
await this.#deny(interaction, hierarchyError);
|
|
448
|
+
return true;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
if (route.filter) {
|
|
453
|
+
const allowed = await route.filter(ctx);
|
|
454
|
+
if (!allowed) return true;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
const lockKey = this.#resolveLockKey(route, ctx);
|
|
458
|
+
if (lockKey && this.#inFlight.has(lockKey)) {
|
|
459
|
+
if (route.inFlightSilent && !interaction.replied && !interaction.deferred && typeof interaction.deferUpdate === "function") {
|
|
460
|
+
await interaction.deferUpdate().catch(() => null);
|
|
461
|
+
} else {
|
|
462
|
+
await this.#deny(interaction, route.inFlightMessage);
|
|
463
|
+
}
|
|
464
|
+
return true;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
if (lockKey) {
|
|
468
|
+
this.#inFlight.add(lockKey);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
try {
|
|
472
|
+
await route.handler(ctx);
|
|
473
|
+
} catch (error) {
|
|
474
|
+
if (this.#options.onError) {
|
|
475
|
+
await this.#options.onError(error, ctx);
|
|
476
|
+
} else {
|
|
477
|
+
await this.#deny(interaction, this.#messages.error);
|
|
478
|
+
}
|
|
479
|
+
} finally {
|
|
480
|
+
if (lockKey) {
|
|
481
|
+
this.#inFlight.delete(lockKey);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
return true;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
attach(client) {
|
|
489
|
+
if (!client || typeof client.on !== "function") {
|
|
490
|
+
throw new TypeError("[V2PersistentRouter] attach() requires a Discord/Gralonium Client.");
|
|
491
|
+
}
|
|
492
|
+
V2Adaptive_js_1.ensureOverflowInteractionBridge(client);
|
|
493
|
+
const listener = (interaction) => {
|
|
494
|
+
void this.handle(interaction, client);
|
|
495
|
+
};
|
|
496
|
+
client.on("interactionCreate", listener);
|
|
497
|
+
return () => {
|
|
498
|
+
if (typeof client.off === "function") {
|
|
499
|
+
client.off("interactionCreate", listener);
|
|
500
|
+
} else if (typeof client.removeListener === "function") {
|
|
501
|
+
client.removeListener("interactionCreate", listener);
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
exports.V2PersistentRouter = V2PersistentRouter;
|
|
507
|
+
|
|
508
|
+
function createPersistentRouter(namespace, options = {}) {
|
|
509
|
+
return new V2PersistentRouter(namespace, options);
|
|
510
|
+
}
|
|
511
|
+
exports.createPersistentRouter = createPersistentRouter;
|
|
@@ -105,6 +105,7 @@ export declare class V2View<S = Record<string, any>> {
|
|
|
105
105
|
}): V2BuiltPayload;
|
|
106
106
|
|
|
107
107
|
stop(reason?: string): this;
|
|
108
|
+
handleInteraction(interaction: CollectedInteraction | any): Promise<void>;
|
|
108
109
|
attach(message: Message | any, collectorOptions?: V2ViewAttachOptions): InteractionCollector<any>;
|
|
109
110
|
send(
|
|
110
111
|
target: Context | TextBasedChannel,
|
|
@@ -6,6 +6,7 @@ const crypto_1 = require("crypto");
|
|
|
6
6
|
const discord_js_1 = require("discord.js");
|
|
7
7
|
const ComponentsV2Builder_js_1 = require("../builders/ComponentsV2Builder.js");
|
|
8
8
|
const helpers_js_1 = require("../builders/components/helpers.js");
|
|
9
|
+
const V2Adaptive_js_1 = require("./V2Adaptive.js");
|
|
9
10
|
|
|
10
11
|
function shortPrefix() {
|
|
11
12
|
if (typeof crypto_1.randomBytes === "function") {
|
|
@@ -250,12 +251,20 @@ class V2View {
|
|
|
250
251
|
}
|
|
251
252
|
|
|
252
253
|
const payload = subView.toPayload({ ephemeral: true });
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
254
|
+
const replyResult = await V2Adaptive_js_1.V2Adaptive.executeWithFallback(
|
|
255
|
+
payload,
|
|
256
|
+
async (sendPayload) => {
|
|
257
|
+
try {
|
|
258
|
+
return await interaction.reply({ ...sendPayload, withResponse: true });
|
|
259
|
+
} catch (err) {
|
|
260
|
+
if (V2Adaptive_js_1.V2Adaptive.isOverflowRejectionError(err)) {
|
|
261
|
+
throw err;
|
|
262
|
+
}
|
|
263
|
+
return interaction.reply(sendPayload);
|
|
264
|
+
}
|
|
265
|
+
},
|
|
266
|
+
{ ephemeral: true }
|
|
267
|
+
);
|
|
259
268
|
|
|
260
269
|
const ephemeralMsg = normalizeMessageTarget(replyResult);
|
|
261
270
|
if (ephemeralMsg && typeof ephemeralMsg.createMessageComponentCollector === "function") {
|
|
@@ -280,8 +289,13 @@ class V2View {
|
|
|
280
289
|
};
|
|
281
290
|
}
|
|
282
291
|
|
|
283
|
-
async
|
|
284
|
-
|
|
292
|
+
async handleInteraction(rawInteraction) {
|
|
293
|
+
return this.#handleInteraction(rawInteraction);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
async #handleInteraction(rawInteraction) {
|
|
297
|
+
const interaction = V2Adaptive_js_1.unwrapOverflowFallbackInteraction(rawInteraction);
|
|
298
|
+
const parsed = this.#parseScopedCustomId(interaction?.customId);
|
|
285
299
|
if (!parsed) return;
|
|
286
300
|
|
|
287
301
|
if (this.#filter) {
|
|
@@ -326,9 +340,17 @@ class V2View {
|
|
|
326
340
|
|
|
327
341
|
const nextPayload = this.toPayload({ ephemeral: this.#isEphemeral });
|
|
328
342
|
if (typeof interaction.update === "function") {
|
|
329
|
-
await
|
|
343
|
+
await V2Adaptive_js_1.V2Adaptive.executeWithFallback(
|
|
344
|
+
nextPayload,
|
|
345
|
+
(p) => interaction.update(p),
|
|
346
|
+
{ ephemeral: this.#isEphemeral }
|
|
347
|
+
);
|
|
330
348
|
} else if (this.#message?.editable) {
|
|
331
|
-
await
|
|
349
|
+
await V2Adaptive_js_1.V2Adaptive.executeWithFallback(
|
|
350
|
+
nextPayload,
|
|
351
|
+
(p) => this.#message.edit(p),
|
|
352
|
+
{ ephemeral: this.#isEphemeral }
|
|
353
|
+
);
|
|
332
354
|
}
|
|
333
355
|
}
|
|
334
356
|
|
|
@@ -353,7 +375,11 @@ class V2View {
|
|
|
353
375
|
if (this.#onEnd === "static") {
|
|
354
376
|
if (this.#renderStatic && this.#message.editable) {
|
|
355
377
|
const staticPayload = this.toPayload({ static: true, isEnded: true, ephemeral: this.#isEphemeral });
|
|
356
|
-
await
|
|
378
|
+
await V2Adaptive_js_1.V2Adaptive.executeWithFallback(
|
|
379
|
+
staticPayload,
|
|
380
|
+
(p) => this.#message.edit(p),
|
|
381
|
+
{ static: true, isEnded: true, ephemeral: this.#isEphemeral }
|
|
382
|
+
).catch(() => null);
|
|
357
383
|
return;
|
|
358
384
|
}
|
|
359
385
|
await helpers_js_1.disableMessageComponents(this.#message);
|
|
@@ -374,6 +400,10 @@ class V2View {
|
|
|
374
400
|
throw new Error("[V2View] attach() requires a Discord Message with createMessageComponentCollector().");
|
|
375
401
|
}
|
|
376
402
|
|
|
403
|
+
if (message.client) {
|
|
404
|
+
V2Adaptive_js_1.ensureOverflowInteractionBridge(message.client);
|
|
405
|
+
}
|
|
406
|
+
|
|
377
407
|
this.#message = message;
|
|
378
408
|
this.#stopped = false;
|
|
379
409
|
this.#endHandled = false;
|
|
@@ -381,7 +411,10 @@ class V2View {
|
|
|
381
411
|
const opts = {
|
|
382
412
|
time: collectorOptions.time ?? this.#timeout,
|
|
383
413
|
...(collectorOptions.idle ?? this.#idle ? { idle: collectorOptions.idle ?? this.#idle } : {}),
|
|
384
|
-
filter: (i) =>
|
|
414
|
+
filter: (i) => {
|
|
415
|
+
const unwrapped = V2Adaptive_js_1.unwrapOverflowFallbackInteraction(i);
|
|
416
|
+
return Boolean(this.#parseScopedCustomId(unwrapped?.customId));
|
|
417
|
+
},
|
|
385
418
|
};
|
|
386
419
|
|
|
387
420
|
const collector = message.createMessageComponentCollector(opts);
|
|
@@ -413,16 +446,21 @@ class V2View {
|
|
|
413
446
|
* Renders and sends the view through a `Context` or `TextBasedChannel`, then attaches the collector automatically.
|
|
414
447
|
*/
|
|
415
448
|
async send(target, sendOptions = {}) {
|
|
416
|
-
const
|
|
449
|
+
const payloadOpts = {
|
|
417
450
|
ephemeral: sendOptions.ephemeral ?? this.#isEphemeral,
|
|
418
451
|
safeMentions: sendOptions.safeMentions,
|
|
419
452
|
allowedMentions: sendOptions.allowedMentions,
|
|
420
453
|
files: sendOptions.files,
|
|
421
|
-
}
|
|
454
|
+
};
|
|
455
|
+
const payload = this.toPayload(payloadOpts);
|
|
422
456
|
|
|
423
457
|
let sent;
|
|
424
458
|
if (target && typeof target.send === "function") {
|
|
425
|
-
sent = await
|
|
459
|
+
sent = await V2Adaptive_js_1.V2Adaptive.executeWithFallback(
|
|
460
|
+
payload,
|
|
461
|
+
(sendPayload) => target.send(sendPayload, sendOptions),
|
|
462
|
+
payloadOpts
|
|
463
|
+
);
|
|
426
464
|
} else {
|
|
427
465
|
throw new TypeError("[V2View] send() requires a Context or TextBasedChannel with a .send() method.");
|
|
428
466
|
}
|
package/lib/experiments.d.ts
CHANGED
|
@@ -2,3 +2,5 @@ export * from "./classes/experiments/Paginator";
|
|
|
2
2
|
export * from "./classes/experiments/Confirmator";
|
|
3
3
|
export * from "./classes/views/V2View";
|
|
4
4
|
export * from "./classes/views/V2Paginator";
|
|
5
|
+
export * from "./classes/views/V2Adaptive";
|
|
6
|
+
export * from "./classes/views/V2PersistentRouter";
|
package/lib/experiments.js
CHANGED
|
@@ -7,3 +7,5 @@ tslib_1.__exportStar(require("./classes/experiments/Paginator"), exports);
|
|
|
7
7
|
tslib_1.__exportStar(require("./classes/experiments/Confirmator"), exports);
|
|
8
8
|
tslib_1.__exportStar(require("./classes/views/V2View"), exports);
|
|
9
9
|
tslib_1.__exportStar(require("./classes/views/V2Paginator"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./classes/views/V2Adaptive"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./classes/views/V2PersistentRouter"), exports);
|
package/lib/main.d.ts
CHANGED
|
@@ -8,9 +8,13 @@ export * from "./classes/builders/InteractionBuilder.js";
|
|
|
8
8
|
export * from "./classes/builders/GroupBuilder.js";
|
|
9
9
|
export * from "./classes/builders/ParamsBuilder.js";
|
|
10
10
|
export * from "./classes/builders/ComponentsV2Builder.js";
|
|
11
|
+
export * from "./classes/builders/components/overflow.js";
|
|
12
|
+
export * from "./classes/builders/components/linter.js";
|
|
11
13
|
export * from "./classes/builders/components/text.js";
|
|
12
14
|
export * from "./classes/views/V2View.js";
|
|
13
15
|
export * from "./classes/views/V2Paginator.js";
|
|
16
|
+
export * from "./classes/views/V2Adaptive.js";
|
|
17
|
+
export * from "./classes/views/V2PersistentRouter.js";
|
|
14
18
|
export * from "./classes/HelpCommand.js";
|
|
15
19
|
export * from "./classes/Cooldowns.js";
|
|
16
20
|
export * as Errors from "./classes/Errors.js";
|
package/lib/main.js
CHANGED
|
@@ -14,9 +14,13 @@ tslib_1.__exportStar(require("./classes/builders/InteractionBuilder.js"), export
|
|
|
14
14
|
tslib_1.__exportStar(require("./classes/builders/GroupBuilder.js"), exports);
|
|
15
15
|
tslib_1.__exportStar(require("./classes/builders/ParamsBuilder.js"), exports);
|
|
16
16
|
tslib_1.__exportStar(require("./classes/builders/ComponentsV2Builder.js"), exports);
|
|
17
|
+
tslib_1.__exportStar(require("./classes/builders/components/overflow.js"), exports);
|
|
18
|
+
tslib_1.__exportStar(require("./classes/builders/components/linter.js"), exports);
|
|
17
19
|
tslib_1.__exportStar(require("./classes/builders/components/text.js"), exports);
|
|
18
20
|
tslib_1.__exportStar(require("./classes/views/V2View.js"), exports);
|
|
19
21
|
tslib_1.__exportStar(require("./classes/views/V2Paginator.js"), exports);
|
|
22
|
+
tslib_1.__exportStar(require("./classes/views/V2Adaptive.js"), exports);
|
|
23
|
+
tslib_1.__exportStar(require("./classes/views/V2PersistentRouter.js"), exports);
|
|
20
24
|
tslib_1.__exportStar(require("./classes/HelpCommand.js"), exports);
|
|
21
25
|
tslib_1.__exportStar(require("./classes/Cooldowns.js"), exports);
|
|
22
26
|
|