fix: Modal reply event failed

#1201 #1200 #1064
This commit is contained in:
Elysia
2024-07-04 17:26:04 +07:00
parent f7bb7a4bdd
commit f63b1528d4
5 changed files with 59 additions and 118 deletions

View File

@@ -3,9 +3,10 @@
const { Agent } = require('node:http');
const { parse } = require('node:path');
const process = require('node:process');
const { setTimeout } = require('node:timers');
const { Collection } = require('@discordjs/collection');
const fetch = require('node-fetch');
const { Colors } = require('./Constants');
const { Colors, Events } = require('./Constants');
const { Error: DiscordError, RangeError, TypeError } = require('../errors');
const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
const isObject = d => typeof d === 'object' && d !== null;
@@ -818,6 +819,47 @@ class Util extends null {
static verifyProxyAgent(object) {
return typeof object == 'object' && object.httpAgent instanceof Agent && object.httpsAgent instanceof Agent;
}
static createPromiseInteraction(client, nonce, timeoutMs = 5_000, isHandlerDeferUpdate = false, parent) {
return new Promise((resolve, reject) => {
// Waiting for MsgCreate / ModalCreate
let dataFromInteractionSuccess;
let dataFromNormal;
const handler = data => {
// UnhandledPacket
if (isHandlerDeferUpdate && data.d?.nonce == nonce && data.t == 'INTERACTION_SUCCESS') {
// Interaction#deferUpdate
client.removeListener(Events.MESSAGE_CREATE, handler);
client.removeListener(Events.UNHANDLED_PACKET, handler);
client.removeListener(Events.INTERACTION_MODAL_CREATE, handler);
dataFromInteractionSuccess = parent;
}
if (data.nonce !== nonce) return;
clearTimeout(timeout);
client.removeListener(Events.MESSAGE_CREATE, handler);
client.removeListener(Events.INTERACTION_MODAL_CREATE, handler);
if (isHandlerDeferUpdate) client.removeListener(Events.UNHANDLED_PACKET, handler);
client.decrementMaxListeners();
dataFromNormal = data;
resolve(data);
};
const timeout = setTimeout(() => {
if (dataFromInteractionSuccess || dataFromNormal) {
resolve(dataFromNormal || dataFromInteractionSuccess);
return;
}
client.removeListener(Events.MESSAGE_CREATE, handler);
client.removeListener(Events.INTERACTION_MODAL_CREATE, handler);
if (isHandlerDeferUpdate) client.removeListener(Events.UNHANDLED_PACKET, handler);
client.decrementMaxListeners();
reject(new Error('INTERACTION_FAILED'));
}, timeoutMs).unref();
client.incrementMaxListeners();
client.on(Events.MESSAGE_CREATE, handler);
client.on(Events.INTERACTION_MODAL_CREATE, handler);
if (isHandlerDeferUpdate) client.on(Events.UNHANDLED_PACKET, handler);
});
}
}
module.exports = Util;