refactor: Modal#reply

This commit is contained in:
March 7th 2022-12-08 15:14:17 +07:00
parent c20b04275b
commit a3ce786fe6
2 changed files with 30 additions and 39 deletions

View File

@ -11,6 +11,10 @@
await Button.click(Message);
//
await message.clickButton(buttonID);
//
await message.clickButton(); // first button
//
await message.clickButton({ row: 0, col: 0})
```
</details>
<details open>
@ -26,18 +30,8 @@ await message.selectMenu(options) // If message has 1 menu
<details open>
<summary>Slash Command</summary>
<strong>[Demo](https://github.com/aiko-chan-ai/discord.js-selfbot-v13/blob/main/Document/SlashCommand.md)</strong>
### [Click here](https://github.com/aiko-chan-ai/discord.js-selfbot-v13/blob/main/Document/SlashCommand.md)
```js
// v2
await Channel.sendSlash(botID, commandName, 'option1', 123, true, new MessageAttachment(buffer, 'test.png'));
// Eg /addrole roleID: 12345678987654321 userID: 98765432123456789
// => await Channel.sendSlash(botID, 'addrole', ['12345678987654321', '98765432123456789']);
// Command group
await Channel.sendSlash(botID, commandName, 'sub command', 'option1', 'option2');
// Eg: /role add roleID: 12345678987654321 userID: 98765432123456789
// => await Channel.sendSlash(botID, 'role', ['add', '12345678987654321', '98765432123456789']);
```
</details>
<details open>
<summary>Message Context Command</summary>

View File

@ -171,7 +171,9 @@ class Modal {
* customId: 'message',
* value: 'hello'
* }
* ]
* ],
* channel: 'id', // optional
* guild: 'id', // optional
* })
* })
*/
@ -182,46 +184,41 @@ class Modal {
const data_cache = this.sendFromInteraction;
const guild = this.client.guilds.resolveId(data.guild) || data_cache.guildId || null;
const channel = this.client.channels.resolveId(data.channel) || data_cache.channelId;
if (!channel) throw new Error('Modal cannot reply (Missing data)');
// Add data to components
// this.components = [ MessageActionRow.components = [ TextInputComponent ] ]
// 5 MessageActionRow / Modal, 1 TextInputComponent / 1 MessageActionRow
for (let i = 0; i < this.components.length; i++) {
const value = data.data.find(d => d.customId == this.components[i].components[0].customId);
if (this.components[i].components[0].required == true) {
if (!value) {
throw new Error(
'MODAL_REQUIRED_FIELD_MISSING\n' +
`Required fieldId ${this.components[i].components[0].customId} missing value`,
);
}
if (!(typeof value?.value == 'string')) {
throw new Error(
'MODAL_REPLY_DATA_INVALID\n' +
`Data (Required) must be strings, got ${typeof value.value} [Custom ID: ${value.customId}]`,
);
}
if (this.components[i].components[0].required == true && !value) {
throw new Error(
'MODAL_REQUIRED_FIELD_MISSING\n' +
`Required fieldId ${this.components[i].components[0].customId} missing value`,
);
}
if (value) {
if (!(typeof value?.value == 'string')) {
console.warn(
'Warning: MODAL_REPLY_DATA_INVALID',
`Data (Not required) must be strings, got ${typeof value.value} [Custom ID: ${value.customId}]`,
if (value?.value?.includes('\n') && this.components[i].components[0].style == 'SHORT') {
throw new Error(
'MODAL_REPLY_DATA_INVALID\n' +
`value must be a single line, got multiple lines [Custom ID: ${value.customId}]`,
);
continue;
}
this.components[i].components[0].value = value.value;
this.components[i].components[0].setValue(value.value);
}
delete this.components[i].components[0].maxLength;
delete this.components[i].components[0].minLength;
delete this.components[i].components[0].required;
delete this.components[i].components[0].placeholder;
delete this.components[i].components[0].label;
delete this.components[i].components[0].style;
}
// Filter
this.components = this.components.filter(c => c.components[0].value && c.components[0].value !== '');
// Get Object
const dataFinal = this.toJSON();
dataFinal.components = dataFinal.components
.map(c => {
delete c.components[0].max_length;
delete c.components[0].min_length;
delete c.components[0].required;
delete c.components[0].placeholder;
delete c.components[0].label;
delete c.components[0].style;
return c;
})
.filter(c => c.components[0].value && c.components[0].value !== '');
delete dataFinal.title;
const nonce = SnowflakeUtil.generate();
const postData = {