这是我的代码,直到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;
};
});
这不起作用,因为您从未实际执行过代码的删除部分。
您需要将它定义为一个函数,以便按照您想要的方式运行它。
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] < 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);
});
}