diff --git a/src/structures/interfaces/TextBasedChannel.js b/src/structures/interfaces/TextBasedChannel.js index 960faa1bb..90580d570 100644 --- a/src/structures/interfaces/TextBasedChannel.js +++ b/src/structures/interfaces/TextBasedChannel.js @@ -276,29 +276,39 @@ class TextBasedChannel { /** * Bulk delete given messages that are newer than two weeks. * This is only available when using a bot account. - * @param {Collection|Message[]|number} messages Messages or number of messages to delete + * @param {Collection|Message[]|Snowflake[]|number} messages + * Messages or number of messages to delete * @param {boolean} [filterOld=false] Filter messages to remove those which are older than two weeks automatically * @returns {Promise>} Deleted messages */ - bulkDelete(messages, filterOld = false) { - if (!isNaN(messages)) { - return this.messages.fetch({ limit: messages }).then(msgs => this.bulkDelete(msgs, filterOld)); - } + async bulkDelete(messages, filterOld = false) { if (messages instanceof Array || messages instanceof Collection) { - let messageIDs = messages instanceof Collection ? messages.keyArray() : messages.map(m => m.id); + let messageIDs = messages instanceof Collection ? messages.keyArray() : messages.map(m => m.id || m); if (filterOld) { messageIDs = messageIDs.filter(id => Date.now() - Snowflake.deconstruct(id).date.getTime() < 1209600000 ); } - return this.client.api.channels[this.id].messages['bulk-delete'] - .post({ data: { messages: messageIDs } }) - .then(() => - this.client.actions.MessageDeleteBulk.handle({ - channel_id: this.id, - ids: messageIDs, - }).messages - ); + if (messageIDs.length === 0) return new Collection(); + if (messageIDs.length === 1) { + await this.client.api.channels(this.id).messages(messageIDs[0]).delete(); + const message = this.client.actions.MessageDelete.handle({ + channel_id: this.id, + id: messageIDs[0], + }).message; + if (message) return new Collection([[message.id, message]]); + return new Collection(); + } + await this.client.api.channels[this.id].messages['bulk-delete'] + .post({ data: { messages: messageIDs } }); + return this.client.actions.MessageDeleteBulk.handle({ + channel_id: this.id, + ids: messageIDs, + }).messages; + } + if (!isNaN(messages)) { + const msgs = await this.messages.fetch({ limit: messages }); + return this.bulkDelete(msgs, filterOld); } throw new TypeError('MESSAGE_BULK_DELETE_TYPE'); }