【问题标题】:Messages.fetch and bulkDelete don't delete messages discord.jsMessages.fetch 和 bulkDelete 不会删除消息 discord.js
【发布时间】:2021-01-26 15:05:50
【问题描述】:

这是我的代码,所有 clearChat 功能都可以正常工作。它不会引发错误,但不会删除消息。

    const bot = new Discord.Client();
    bot.on('message', msg => {
      
       const args = msg.content.substring(config.prefix.length).split(' ');

       switch (args[0]) {
           case 'clear':

               if(isNaN(args[1])) return msg.reply('Define the amount of messages you want to clear!');
               else if(args[1] > 101 ) return msg.reply('Cannot clear more than 100 messages!');
               else if(args[1] < 1)) return msg.reply('Must clear at least 1 message!');
               else {

                    clearChat = async(msg, args) => {

                         let deleteAmount = +args[1] + 1;

                         messages = await msg.channel.messages.fetch({ limit: deleteAmount }).then(async(messages) => {
                             await msg.channel.bulkDelete(messages);
                         });
                    };
               };
               break;
       };
    });
    

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    这不起作用,因为您从未真正执行过代码的删除部分。

    您需要将它定义为一个函数,以便在此处按照您想要的方式运行它。

    async function clearChat (msg, args) {
                
        let deleteAmount = +args[1] + 1;
    
        messages = await msg.channel.messages.fetch({ limit: deleteAmount }).then(async (messages) => {
            await msg.channel.bulkDelete(messages);
        });
    }
    

    一旦定义正确,您需要调用该函数

    else {
        clearChat(msg, args)
    }
    

    除此之外,您的else if (args[1] &lt; 1) 中还有一个额外的)

    完整的代码应该看起来像这样:

    if (isNaN(args[1])) return msg.reply('Define the amount of messages you want to clear!');
    else if (args[1] > 101) return msg.reply('Cannot clear more than 100 messages!');
    else if (args[1] < 1) return msg.reply('Must clear at least 1 message!');
    else {
        clearChat(msg, args)
    }
            
    async function clearChat (msg, args) {
                
        let deleteAmount = +args[1] + 1;
    
        messages = await msg.channel.messages.fetch({ limit: deleteAmount }).then(async (messages) => {
            await msg.channel.bulkDelete(messages);
        });
    }
    

    【讨论】:

    • 另一个问题是msg.channel.messages.fetch 未被识别为函数,但msg.channel.fetchMessages 有效。
    • 当我尝试你的解决方案时,我唯一需要改变的就是上面描述的那些。你使用的是什么版本的 discord.js?
    猜你喜欢
    • 2019-02-18
    • 2018-02-02
    • 2018-06-05
    • 2018-08-26
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 2020-05-17
    相关资源
    最近更新 更多