对discord.js中的“清除”命令感到困惑

时间:2020-08-20 10:51:56

标签: discord.js

我目前正在尝试为我的不和谐机器人发出“清除”命令(!清除 number ),但是显然我做错了;我对编码非常陌生,希望有人能看到我哪里出错了。

在我的main.js文件中,除其他外,我有:

    } else if(command == 'clear'){
        client.commands.get('clear').execute(message, agrs);
    }

在我的clear.js文件中,我有这段代码,这可能是完全错误的。

module.exports= {
    name: 'clear',
    description: 'clears an amount of messages.',
    execute(message, args){
        const args = message.content.split(' ').slice(1);
    const amount = args.join(' ');
    if (!amount) return msg.reply('You haven\'t given an amount of messages which should be deleted!'); 
    
    if (isNaN(amount)) return msg.reply('The amount parameter isn`t a number!'); 
    
    if (amount > 100) return msg.reply('You can`t delete more than 100 messages at once!'); 
    
    if (amount < 1) return msg.reply('You have to delete at least 1 message!'); 
    
    await msg.channel.messages.fetch({ limit: amount }).then(messages => {
        msg.channel.bulkDelete(messages
    )});
    }
}

任何帮助将不胜感激。再次谢谢你!

1 个答案:

答案 0 :(得分:1)

您应该改用它:

// delete `const args = messsage.content...`, args is already defined via command handler
const amount = args[0]

// code...

const fetched = await msg.channel.messages.fetch({
      limit: amount,
    });
    msg.channel
      .bulkDelete(fetched)
      .catch((error) => console.log(`Couldn't clear messages because of ${error}`)