Refactor la carga del módulo ModalSubmitInteraction para mejorar la compatibilidad y la gestión de errores
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -11,3 +11,6 @@ qodana.yaml
|
|||||||
|
|
||||||
# Generados
|
# Generados
|
||||||
/src/generated/prisma/
|
/src/generated/prisma/
|
||||||
|
|
||||||
|
# Binarios
|
||||||
|
amayo
|
||||||
|
|||||||
@@ -5,13 +5,32 @@
|
|||||||
Source of truth: node_modules/discord.js/src/structures/ModalSubmitInteraction.js
|
Source of truth: node_modules/discord.js/src/structures/ModalSubmitInteraction.js
|
||||||
*/
|
*/
|
||||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||||
const { Collection } = require('@discordjs/collection');
|
const { Collection } = require("@discordjs/collection");
|
||||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||||
const ModalModule = require('../../../node_modules/discord.js/src/structures/ModalSubmitInteraction.js');
|
const ModalModule = (() => {
|
||||||
|
try {
|
||||||
|
const djs = require("discord.js");
|
||||||
|
if (djs.ModalSubmitInteraction)
|
||||||
|
return { ModalSubmitInteraction: djs.ModalSubmitInteraction };
|
||||||
|
if (djs.structures && djs.structures.ModalSubmitInteraction)
|
||||||
|
return { ModalSubmitInteraction: djs.structures.ModalSubmitInteraction };
|
||||||
|
// Try to resolve internal path at runtime without static import
|
||||||
|
return require(require.resolve(
|
||||||
|
"discord.js/src/structures/ModalSubmitInteraction.js"
|
||||||
|
));
|
||||||
|
} catch {
|
||||||
|
try {
|
||||||
|
// Fallback to node_modules relative path if needed
|
||||||
|
return require("../../../node_modules/discord.js/src/structures/ModalSubmitInteraction.js");
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
export function applyModalSubmitInteractionPatch() {
|
export function applyModalSubmitInteractionPatch() {
|
||||||
const ModalSubmitInteraction = ModalModule?.ModalSubmitInteraction;
|
const ModalSubmitInteraction = ModalModule?.ModalSubmitInteraction;
|
||||||
if (!ModalSubmitInteraction || typeof ModalSubmitInteraction !== 'function') {
|
if (!ModalSubmitInteraction || typeof ModalSubmitInteraction !== "function") {
|
||||||
return; // Nothing to patch
|
return; // Nothing to patch
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,16 +38,21 @@ export function applyModalSubmitInteractionPatch() {
|
|||||||
|
|
||||||
// Override with a safer version
|
// Override with a safer version
|
||||||
// eslint-disable-next-line func-names
|
// eslint-disable-next-line func-names
|
||||||
ModalSubmitInteraction.prototype.transformComponent = function (rawComponent: any, resolved: any) {
|
ModalSubmitInteraction.prototype.transformComponent = function (
|
||||||
if ('components' in rawComponent) {
|
rawComponent: any,
|
||||||
|
resolved: any
|
||||||
|
) {
|
||||||
|
if ("components" in rawComponent) {
|
||||||
return {
|
return {
|
||||||
type: rawComponent.type,
|
type: rawComponent.type,
|
||||||
id: rawComponent.id,
|
id: rawComponent.id,
|
||||||
components: rawComponent.components.map((component: any) => this.transformComponent(component, resolved)),
|
components: rawComponent.components.map((component: any) =>
|
||||||
|
this.transformComponent(component, resolved)
|
||||||
|
),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('component' in rawComponent) {
|
if ("component" in rawComponent) {
|
||||||
return {
|
return {
|
||||||
type: rawComponent.type,
|
type: rawComponent.type,
|
||||||
id: rawComponent.id,
|
id: rawComponent.id,
|
||||||
@@ -41,14 +65,17 @@ export function applyModalSubmitInteractionPatch() {
|
|||||||
id: rawComponent.id,
|
id: rawComponent.id,
|
||||||
};
|
};
|
||||||
|
|
||||||
if ('custom_id' in rawComponent) data.customId = rawComponent.custom_id;
|
if ("custom_id" in rawComponent) data.customId = rawComponent.custom_id;
|
||||||
if ('value' in rawComponent) data.value = rawComponent.value;
|
if ("value" in rawComponent) data.value = rawComponent.value;
|
||||||
|
|
||||||
if (rawComponent.values) {
|
if (rawComponent.values) {
|
||||||
data.values = rawComponent.values;
|
data.values = rawComponent.values;
|
||||||
|
|
||||||
if (resolved) {
|
if (resolved) {
|
||||||
const collect = (resolvedData: any, resolver: (val: any, id: string) => any) => {
|
const collect = (
|
||||||
|
resolvedData: any,
|
||||||
|
resolver: (val: any, id: string) => any
|
||||||
|
) => {
|
||||||
const collection = new Collection();
|
const collection = new Collection();
|
||||||
for (const value of data.values as string[]) {
|
for (const value of data.values as string[]) {
|
||||||
if (resolvedData?.[value]) {
|
if (resolvedData?.[value]) {
|
||||||
@@ -59,12 +86,15 @@ export function applyModalSubmitInteractionPatch() {
|
|||||||
return collection.size ? collection : null;
|
return collection.size ? collection : null;
|
||||||
};
|
};
|
||||||
|
|
||||||
const users = collect(resolved.users, (user: any) => this.client.users._add(user));
|
const users = collect(resolved.users, (user: any) =>
|
||||||
|
this.client.users._add(user)
|
||||||
|
);
|
||||||
if (users) data.users = users;
|
if (users) data.users = users;
|
||||||
|
|
||||||
const channels = collect(
|
const channels = collect(
|
||||||
resolved.channels,
|
resolved.channels,
|
||||||
(channel: any) => this.client.channels._add(channel, this.guild) ?? channel,
|
(channel: any) =>
|
||||||
|
this.client.channels._add(channel, this.guild) ?? channel
|
||||||
);
|
);
|
||||||
if (channels) data.channels = channels;
|
if (channels) data.channels = channels;
|
||||||
|
|
||||||
@@ -83,7 +113,10 @@ export function applyModalSubmitInteractionPatch() {
|
|||||||
});
|
});
|
||||||
if (members) data.members = members;
|
if (members) data.members = members;
|
||||||
|
|
||||||
const roles = collect(resolved.roles, (role: any) => this.guild?.roles._add(role) ?? role);
|
const roles = collect(
|
||||||
|
resolved.roles,
|
||||||
|
(role: any) => this.guild?.roles._add(role) ?? role
|
||||||
|
);
|
||||||
if (roles) data.roles = roles;
|
if (roles) data.roles = roles;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -92,6 +125,6 @@ export function applyModalSubmitInteractionPatch() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Keep a reference in case we need to restore
|
// Keep a reference in case we need to restore
|
||||||
(ModalSubmitInteraction.prototype as any).__originalTransformComponent = original;
|
(ModalSubmitInteraction.prototype as any).__originalTransformComponent =
|
||||||
|
original;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user