【问题标题】:can someone explain the change to bulkDelete (discord.js)有人可以解释对 bulkDelete 的更改(discord.js)
【发布时间】:2020-07-31 21:05:26
【问题描述】:

这是我现在的代码

module.exports ={
    name:'purge',
    description:'Clears the number of messages you set in a seccond ARG.',
    aliases:['clear','nuke'],
    execute(msg, args)
    {
        if(!msg.member.hasPermission("MANAGE_MESSAGES")) {
            return msg.reply("You do not have permission to run this command").then(msg => msg.delete(5000));
        }

        if (!isNaN(args[0]) || parseInt(args[0]) <= 0) {
            return msg.reply("That is not a number").then(msg => msg.delete(5000));
        }

        if(!msg.guild.me.hasPermission("MANAGE_MESSAGES")) {
            return msg.reply("Sorry I can't messages please make sure i have the correct permissions").then(msg => msg.delete());
        }

        let deleteAmount;

        if(parseInt(args[0]) > 100) {
            deleteAmount = 100;
        } else {
            deleteAmount = parseInt(args[0]);
        }

        msg.channel.bulkDelete(deleteAmount, true);
    }
}

错误:

(node:25048) UnhandledPromiseRejectionWarning: TypeError [MESSAGE_BULK_DELETE_TYPE]: The messages must be an Array, Collection, or number.
    at TextChannel.bulkDelete (C:\StrangeOccBot\node_modules\discord.js\src\structures\interfaces\TextBasedChannel.js:364:11)
    at Object.execute (C:\StrangeOccBot\commands\purge.js:27:21)
    at Client.<anonymous> (C:\StrangeOccBot\index.js:46:39)
    at Client.emit (events.js:315:20)
    at WebSocketShard.onPacket (C:\StrangeOccBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (C:\StrangeOccBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
    at WebSocket.onMessage (C:\StrangeOccBot\node_modules\ws\lib\event-target.js:120:16)

【问题讨论】:

    标签: discord discord.js


    【解决方案1】:

    所以我不确定int 是否仍然是使用bulkDelete() 的方法
    但据我所知,您可以在频道中获取消息并将其传递给方法!

    msg.channel.messages.fetch({ limit: deleteAmount }).then(messages => {
      msg.channel.bulkDelete(messages).then(deleted => {
        msg.channel.send(`${deleted.size} messages pruged!`).then(msg => msg.delete(5000));
      })
    })
    

    这就是我最近几次创建函数的方式,例如您尝试归档的函数。
    您还可以将.catch() 方法添加到.then() 方法中,以便检查是否出现问题!

    编辑:我对您的代码进行了一些更改,特别是在最后,所以您知道我会如何完成它来存档您想要存档的内容

    module.exports ={
      name:'purge',
      description:'Clears the number of messages you set in a seccond ARG.',
      aliases:['clear','nuke'],
      execute(msg, args) {
    
        if (!msg.member.hasPermission("MANAGE_MESSAGES")) {
            return msg.reply("You do not have permission to run this command").then(msg => msg.delete(5000));
        }
    
        if (!msg.guild.me.hasPermission("MANAGE_MESSAGES")) {
          return msg.reply("Sorry I can't messages please make sure i have the correct permissions").then(msg => msg.delete());
        }
    
        if (!isNaN(args[0]) || parseInt(args[0]) <= 0) {
          return msg.reply("That is not a number").then(msg => msg.delete(5000));
        }
    
        // I changed this slightly to how I would do it!
        let deleteAmount = 100;
        if (parseInt(args[0]) < 100) {
          deleteAmount = parseInt(args[0])
        }
    
        // If I'm not mistaken, the bulkDelete() method also works with fetched messages in an variable.
        // To archive this, you can use `msg.channel.messages.fetch()` as I will show below!
        // Ofcourse I did not use the .catch() method, so if something isn't correctly done it can cause issues!
        msg.channel.messages.fetch({ limit: deleteAmount }).then(messages => {
          msg.channel.bulkDelete(messages).then(deleted => {
            msg.reply(`${deleted.size} messages pruged!`).then(msg => msg.delete(5000));
          })
        })
    
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      • 2012-05-29
      • 2012-05-23
      • 2010-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多