fix: SlashCommand sending TextChannel

This commit is contained in:
March 7th 2022-06-13 18:24:41 +07:00
parent 07d52ee41b
commit 6b89f7c184

View File

@ -3,9 +3,10 @@
const { Message } = require('discord.js'); const { Message } = require('discord.js');
const Base = require('./Base'); const Base = require('./Base');
const ApplicationCommandPermissionsManager = require('../managers/ApplicationCommandPermissionsManager'); const ApplicationCommandPermissionsManager = require('../managers/ApplicationCommandPermissionsManager');
const MessageAttachment = require('../structures/MessageAttachment');
const { ApplicationCommandOptionTypes, ApplicationCommandTypes, ChannelTypes } = require('../util/Constants'); const { ApplicationCommandOptionTypes, ApplicationCommandTypes, ChannelTypes } = require('../util/Constants');
const DataResolver = require('../util/DataResolver');
const SnowflakeUtil = require('../util/SnowflakeUtil'); const SnowflakeUtil = require('../util/SnowflakeUtil');
/** /**
* Represents an application command. * Represents an application command.
* @extends {Base} * @extends {Base}
@ -523,6 +524,21 @@ class ApplicationCommand extends Base {
} }
if (this.type !== 'CHAT_INPUT') return false; if (this.type !== 'CHAT_INPUT') return false;
const optionFormat = []; const optionFormat = [];
const attachments = [];
const attachmentsBuffer = [];
async function addDataFromAttachment(data) {
if (!(data instanceof MessageAttachment)) {
throw new TypeError('The attachment data must be a Discord.MessageAttachment');
}
const id = attachments.length;
attachments.push({
id: id,
filename: data.name,
});
const resource = await DataResolver.resolveFile(data.attachment);
attachmentsBuffer.push({ attachment: data.attachment, name: data.name, file: resource });
return id;
}
let option_ = []; let option_ = [];
let i = 0; let i = 0;
// Check Command type is Sub group ? // Check Command type is Sub group ?
@ -541,11 +557,6 @@ class ApplicationCommand extends Base {
} }
for (i; i < options.length; i++) { for (i; i < options.length; i++) {
const value = options[i]; const value = options[i];
if (typeof value !== 'string') {
throw new TypeError(
`Expected option to be a String, got ${typeof value}. If you type Number, please convert to String.`,
);
}
if (!subCommandCheck && !this?.options[i]) continue; if (!subCommandCheck && !this?.options[i]) continue;
if (subCommandCheck && subCommand?.options && !subCommand?.options[i]) continue; if (subCommandCheck && subCommand?.options && !subCommand?.options[i]) continue;
if (!subCommandCheck) { if (!subCommandCheck) {
@ -556,11 +567,11 @@ class ApplicationCommand extends Base {
this.options[i].choices.find(c => c.name == value) || this.options[i].choices.find(c => c.value == value); this.options[i].choices.find(c => c.name == value) || this.options[i].choices.find(c => c.value == value);
if (!choice) { if (!choice) {
throw new Error( throw new Error(
`Invalid option: ${value} is not a valid choice for this option\nList of choices: ${this.options[ `Invalid option: ${value} is not a valid choice for this option\nList of choices:\n${this.options[
i i
].choices ].choices
.map((c, i) => `#${i + 1} Name: ${c.name} Value: ${c.value}\n`) .map((c, i) => ` #${i + 1} Name: ${c.name} Value: ${c.value}`)
.join('')}`, .join('\n')}`,
); );
} }
} }
@ -569,7 +580,9 @@ class ApplicationCommand extends Base {
name: this.options[i].name, name: this.options[i].name,
value: value:
choice?.value || choice?.value ||
(this.options[i].type == 'INTEGER' (this.options[i].type == 'ATTACHMENT'
? await addDataFromAttachment(value)
: this.options[i].type == 'INTEGER'
? Number(value) ? Number(value)
: this.options[i].type == 'BOOLEAN' : this.options[i].type == 'BOOLEAN'
? Boolean(value) ? Boolean(value)
@ -600,7 +613,9 @@ class ApplicationCommand extends Base {
name: subCommand.options[i].name, name: subCommand.options[i].name,
value: value:
choice?.value || choice?.value ||
(subCommand.options[i].type == 'INTEGER' (subCommand.options[i].type == 'ATTACHMENT'
? await addDataFromAttachment(value)
: subCommand.options[i].type == 'INTEGER'
? Number(value) ? Number(value)
: subCommand.options[i].type == 'BOOLEAN' : subCommand.options[i].type == 'BOOLEAN'
? Boolean(value) ? Boolean(value)
@ -615,25 +630,28 @@ class ApplicationCommand extends Base {
if (subCommandCheck && subCommand?.options && subCommand?.options[i]?.required) { if (subCommandCheck && subCommand?.options && subCommand?.options[i]?.required) {
throw new Error('Value required missing'); throw new Error('Value required missing');
} }
await this.client.api.interactions.post({ const data = {
body: { type: 2, // ???
type: 2, // ??? application_id: this.applicationId,
application_id: this.applicationId, guild_id: message.guildId,
guild_id: message.guildId, channel_id: message.channelId,
channel_id: message.channelId, session_id: this.client.session_id,
session_id: this.client.session_id, data: {
data: { // ApplicationCommandData
// ApplicationCommandData version: this.version,
version: this.version, id: this.id,
id: this.id, name: this.name,
guild_id: message.guildId, type: ApplicationCommandTypes[this.type],
name: this.name, options: option_,
type: ApplicationCommandTypes[this.type], attachments: attachments,
options: option_,
attachments: [], // Todo
},
nonce: SnowflakeUtil.generate(),
}, },
nonce: SnowflakeUtil.generate(),
};
console.log('Send', data);
await this.client.api.interactions.post({
body: data,
data,
files: attachmentsBuffer,
}); });
return true; return true;
} }