mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-19 13:03:31 +01:00
enhancement/feature(bulkDelete): accept array of ids and handle case of 0 or 1 message(s) (#1980)
This commit is contained in:
@@ -276,29 +276,39 @@ class TextBasedChannel {
|
|||||||
/**
|
/**
|
||||||
* Bulk delete given messages that are newer than two weeks.
|
* Bulk delete given messages that are newer than two weeks.
|
||||||
* <warn>This is only available when using a bot account.</warn>
|
* <warn>This is only available when using a bot account.</warn>
|
||||||
* @param {Collection<Snowflake, Message>|Message[]|number} messages Messages or number of messages to delete
|
* @param {Collection<Snowflake, Message>|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
|
* @param {boolean} [filterOld=false] Filter messages to remove those which are older than two weeks automatically
|
||||||
* @returns {Promise<Collection<Snowflake, Message>>} Deleted messages
|
* @returns {Promise<Collection<Snowflake, Message>>} Deleted messages
|
||||||
*/
|
*/
|
||||||
bulkDelete(messages, filterOld = false) {
|
async bulkDelete(messages, filterOld = false) {
|
||||||
if (!isNaN(messages)) {
|
|
||||||
return this.messages.fetch({ limit: messages }).then(msgs => this.bulkDelete(msgs, filterOld));
|
|
||||||
}
|
|
||||||
if (messages instanceof Array || messages instanceof Collection) {
|
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) {
|
if (filterOld) {
|
||||||
messageIDs = messageIDs.filter(id =>
|
messageIDs = messageIDs.filter(id =>
|
||||||
Date.now() - Snowflake.deconstruct(id).date.getTime() < 1209600000
|
Date.now() - Snowflake.deconstruct(id).date.getTime() < 1209600000
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return this.client.api.channels[this.id].messages['bulk-delete']
|
if (messageIDs.length === 0) return new Collection();
|
||||||
.post({ data: { messages: messageIDs } })
|
if (messageIDs.length === 1) {
|
||||||
.then(() =>
|
await this.client.api.channels(this.id).messages(messageIDs[0]).delete();
|
||||||
this.client.actions.MessageDeleteBulk.handle({
|
const message = this.client.actions.MessageDelete.handle({
|
||||||
channel_id: this.id,
|
channel_id: this.id,
|
||||||
ids: messageIDs,
|
id: messageIDs[0],
|
||||||
}).messages
|
}).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');
|
throw new TypeError('MESSAGE_BULK_DELETE_TYPE');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user