Javascript Messages.fetch和bulkDelete don';t删除discord.js中的消息

Javascript Messages.fetch和bulkDelete don';t删除discord.js中的消息,javascript,node.js,discord.js,Javascript,Node.js,Discord.js,这是我的代码,clearChat functon的所有功能都正常工作。它不会抛出错误,但不会删除消息 const bot = new Discord.Client(); bot.on('message', msg => { const args = msg.content.substring(config.prefix.length).split(' '); switch (args[0]) { case '

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

    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;
       };
    });
    
const bot=newdiscord.Client();
bot.on('message',msg=>{
const args=msg.content.substring(config.prefix.length).split(“”);
开关(参数[0]){
案例“明确”:
if(isNaN(args[1])返回msg.reply('定义要清除的消息量!');
else if(args[1]>101)返回msg.reply('不能清除超过100条消息!');
else if(args[1]<1))返回msg.reply('必须清除至少1条消息!');
否则{
clearChat=async(消息,参数)=>{
设deleteAmount=+args[1]+1;
messages=wait msg.channel.messages.fetch({limit:deleteMount})。然后(异步(messages)=>{
等待消息.channel.bulkDelete(消息);
});
};
};
打破
};
});

这不起作用,因为您从未实际执行代码的删除部分

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

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)
}
除此之外,您的
中还有一个额外的
,如果(args[1]<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);
    });
}
if(isNaN(args[1])返回msg.reply('定义要清除的消息量!');
else if(args[1]>101)返回msg.reply('不能清除超过100条消息!');
else if(args[1]<1)返回msg.reply('必须清除至少1条消息!');
否则{
clearChat(msg,args)
}
异步函数clearChat(消息、参数){
设deleteAmount=+args[1]+1;
messages=wait msg.channel.messages.fetch({limit:deleteMount})。然后(异步(messages)=>{
等待消息.channel.bulkDelete(消息);
});
}

另一个问题是,
msg.channel.messages.fetch
未被识别为函数,但
msg.channel.fetchMessages
有效。当我尝试您的解决方案时,我唯一需要更改的是上述内容。您使用的是discord.js的哪个版本?