Refactor Modal.reply

This commit is contained in:
March 7th 2022-09-29 19:45:22 +07:00
parent e9961e4134
commit a1acbf2721
6 changed files with 32 additions and 24 deletions

File diff suppressed because one or more lines are too long

View File

@ -393,7 +393,7 @@ class Client extends BaseClient {
/** /**
* Sign in with the QR code on your phone. * Sign in with the QR code on your phone.
* @param {boolean} debug Debug mode * @param {boolean} debug Debug mode
* @returns {DiscordAuthWebsocket} nothing :)) * @returns {DiscordAuthWebsocket}
* @example * @example
* client.QRLogin(); * client.QRLogin();
*/ */

View File

@ -3,7 +3,7 @@ const { Events } = require('../../../util/Constants');
/** /**
* @typedef {Object} InteractionResponseBody * @typedef {Object} InteractionResponseBody
* @property {Snowflake} id maybe id of this event (???) (documentation needed) * @property {Snowflake} id Interaction ID
* @property {Snowflake} nonce nonce in POST /interactions * @property {Snowflake} nonce nonce in POST /interactions
*/ */

View File

@ -128,17 +128,22 @@ class Modal {
} }
/** /**
* @typedef {Object} ModalReplyData * @typedef {Object} TextInputComponentReplyData
* @property {string} [customId] TextInputComponent custom id * @property {string} [customId] TextInputComponent custom id
* @property {string} [value] TextInputComponent value * @property {string} [value] TextInputComponent value
*/ */
/**
* @typedef {Object} ModalReplyData
* @property {GuildResolvable} [guild] Guild to send the modal to
* @property {TextChannelResolvable} [channel] User to send the modal to
* @property {TextInputComponentReplyData[]} [data] Reply data
*/
/** /**
* Reply to this modal with data. (Event only) * Reply to this modal with data. (Event only)
* @param {?Snowflake} guildId GuildID of the guild to send the modal to * @param {ModalReplyData} data Data to send with the modal
* @param {Snowflake} channelId ChannelID of the channel to send the modal to * @returns {Promise<InteractionResponseBody>}
* @param {...ModalReplyData} data Data to send with the modal
* @returns {Promise<Snowflake>} Nonce (Discord Timestamp) when command was sent
* @example * @example
* // With Event * // With Event
* client.on('interactionModalCreate', modal => { * client.on('interactionModalCreate', modal => {
@ -151,22 +156,17 @@ class Modal {
* }) * })
* }) * })
*/ */
async reply(guildId, channelId, ...data) { async reply(data) {
if (typeof data !== 'object') throw new TypeError('ModalReplyData must be an object');
if (!Array.isArray(data.data)) throw new TypeError('ModalReplyData.data must be an array');
if (!this.application) throw new Error('Modal cannot reply (Missing Application)'); if (!this.application) throw new Error('Modal cannot reply (Missing Application)');
let guild, channel; const guild = this.client.guilds.resolveId(data.guild);
if (guildId) { const channel = this.client.channels.resolveId(data.channel);
guild = this.client.guilds.cache.get(guildId);
if (!guild) throw new Error('GUILD_NOT_FOUND', `Guild ${guildId} not found`);
}
channel = guild ? guild.channels.cache.get(channelId) : this.client.channels.cache.get(channelId);
if (!channel) {
throw new Error('CHANNEL_NOT_FOUND', `Channel ${channelId} ${guildId ? `in guild ${guildId}` : ''} not found`);
}
// Add data to components // Add data to components
// this.components = [ MessageActionRow.components = [ TextInputComponent ] ] // this.components = [ MessageActionRow.components = [ TextInputComponent ] ]
// 5 MessageActionRow / Modal, 1 TextInputComponent / 1 MessageActionRow // 5 MessageActionRow / Modal, 1 TextInputComponent / 1 MessageActionRow
for (let i = 0; i < this.components.length; i++) { for (let i = 0; i < this.components.length; i++) {
const value = data.find(d => d.customId == this.components[i].components[0].customId); const value = data.data.find(d => d.customId == this.components[i].components[0].customId);
if (this.components[i].components[0].required == true) { if (this.components[i].components[0].required == true) {
if (!value) { if (!value) {
throw new Error( throw new Error(
@ -207,8 +207,8 @@ class Modal {
const postData = { const postData = {
type: 5, // Modal type: 5, // Modal
application_id: this.application.id, application_id: this.application.id,
guild_id: guildId || null, guild_id: guild || null,
channel_id: channelId, channel_id: channel,
data: dataFinal, data: dataFinal,
nonce, nonce,
session_id: this.client.session_id, session_id: this.client.session_id,
@ -216,7 +216,10 @@ class Modal {
await this.client.api.interactions.post({ await this.client.api.interactions.post({
data: postData, data: postData,
}); });
return nonce; return {
nonce,
id: this.id,
};
} }
} }

View File

@ -382,7 +382,6 @@ class WebEmbed {
} }
} }
// Credit: https://www.npmjs.com/package/node-url-shortener + google :))
const getShorten = async (url, embed) => { const getShorten = async (url, embed) => {
const APIurl = ['https://tinyurl.com/api-create.php?url=', 'https://sagiri-v3dot3.herokuapp.com/short?url=']; const APIurl = ['https://tinyurl.com/api-create.php?url=', 'https://sagiri-v3dot3.herokuapp.com/short?url='];
const shorten = `${ const shorten = `${

8
typings/index.d.ts vendored
View File

@ -2271,10 +2271,16 @@ export class Modal {
): this; ): this;
public setTitle(title: string): this; public setTitle(title: string): this;
public toJSON(): RawModalSubmitInteractionData; public toJSON(): RawModalSubmitInteractionData;
public reply(guildId: Snowflake, channelId: Snowflake, ...args: ModalReplyData[]): Promise<Snowflake>; public reply(data: ModalReplyData): Promise<InteractionResponseBody>;
} }
export interface ModalReplyData { export interface ModalReplyData {
guild: GuildResolvable;
channel: TextChannelResolvable;
data: TextInputComponentReplyData[];
}
export interface TextInputComponentReplyData {
customId: string; customId: string;
value: string; value: string;
} }