.bulkDelete()计算消息被删除

时间:2017-08-24 09:44:35

标签: javascript discord discord.js

我已经想出如何使用Discord.JS的.bulkDelete(),但我该如何计算删除的邮件呢?

很抱歉这个简短的问题,如果您想了解更多信息,请发表评论,我会将其添加。

1 个答案:

答案 0 :(得分:0)

查看bulkDelete的来源,它不会返回值

https://github.com/hydrabolt/discord.js/blob/4520c801d370708bc1d914bfa2d9b61729dbdf1e/src/structures/interfaces/TextBasedChannel.js#L354

 /**
   * Bulk delete given messages that are newer than two weeks.
   * <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 {boolean} [filterOld=false] Filter messages to remove those which are older than two weeks automatically
   * @returns {Promise<Collection<Snowflake, Message>>} Deleted messages
   */
  bulkDelete(messages, filterOld = false) {
    if (!isNaN(messages)) return this.fetchMessages({ limit: messages }).then(msgs => this.bulkDelete(msgs, filterOld));
    if (messages instanceof Array || messages instanceof Collection) {
      let messageIDs = messages instanceof Collection ? messages.keyArray() : messages.map(m => m.id);
      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
        );
    }
    throw new TypeError('MESSAGE_BULK_DELETE_TYPE');
  }

您所能做的就是重新创建批量删除功能,或者使用您自己的函数扩展您正在使用的对象,以返回可以批量删除的文件数。

(未经测试)的内容:

findEglibleForDeletion(messages, filterOld = false) {
if (!isNaN(messages)) return this.fetchMessages({ limit: messages }).length;
if (messages instanceof Array || messages instanceof Collection) {
  let messageIDs = messages instanceof Collection ? messages.keyArray() : messages.map(m => m.id);
  if (filterOld) {
    messageIDs = messageIDs.filter(id =>
      Date.now() - Snowflake.deconstruct(id).date.getTime() < 1209600000
    );
  }
  return messageIDs.length;
}
throw new TypeError('MESSAGE_BULK_DELETE_TYPE');

}