im在Glitch.com中创建了一个不和谐的bot,并且正在创建一个Warn命令,当我完成操作后,我尝试结他,并且它起作用了,但是当我尝试警告某人时,它并没有警告我选择的用户,所以,我转到控制台日志,我为这个错误提供资金:
“(节点:4677)UnhandledPromiseRejectionWarning:未处理的诺言拒绝。此错误是由于在没有catch块的情况下抛出异步函数而引起的,或者是由于拒绝了未经.catch()处理的诺言。(拒绝ID: 4)“
我看着异步,对我来说它很好,但是idk,请你帮帮我吗?
const Discord = require("discord.js");
const fs = require("fs");
const ms = require("ms");
let warns = JSON.parse(fs.readFileSync("./warnings.json", "utf8"));
module.exports.run = async (bot, message, args) => {
if(!message.member.hasPermission("MANAGE_MEMBERS")) return message.reply("I cant warn a staff member");
let wUser = message.guild.member(message.mentions.users.first()) || message.guild.members.get(args[0])
if(!wUser) return message.reply("Sir >:( listen plez mention someone no trollies >:(");
if(wUser.hasPermission("MANAGE_MESSAGES")) return message.reply("hey stahp u dont have permision >:(");
let reason = args.join(" ").slice(22);
if(!warns[wUser.id]) warns[wUser.i
] = {
warns: 0
};
warns[wUser.id].warns++;
fs.writeFile("./warnings.json", JSON.stringify(warns), (err) => {
if(err) console.log(err);
});
let warnEmbed = new Discord.RichEmbed()
.setDescription("Warns")
.setAuthor(message.author.name)
.setColor("#ff0000")
.addField("Warned User", wUser.tag)
.addField("Warned in", message.channel)
.addField("Number of Warnings", warns[wUser.id].warns)
.addField("Reason:", reason);
let warnchannel = message.guild.channels.fin('name', "incidents");
if(!warnchannel) return message.reply("Couldn't find channel, if you dont have one create one");
warnchannel.send(warnEmbed);
if(warns[wUser.id].warns == 2) {
let muterole = message.guild.roles.fin('name', "muted");
if(!muterole) return message.reply("You dont have a muterole!, that breaks my heart :(!");
let mutetime = "10m";
await(wUser.addRole(muterole.id));
message.channel.send('${wUser.tag} has been temporaly muted');
setTimeout(function(){
wUser.removeRole(muterole.id)
message.channel.reply('Carlos is da best')
})
}
if(warns[wUser.id].warns == 3) {
message.guild.member(wUser).ban(reason);
message.channel.send('${wUser.tag} has been banned.')
}
}
答案 0 :(得分:0)
好吧,发生的事情是您的函数内部发生了错误。一个诺言没有解决。要处理承诺失败的情况,如果您使用的是.catch(callback)
样式,请遵循.then(callback).catch(callback)
,或者使用
try{
}
catch(err){
}
如果您使用异步并等待。这应该可以帮助您找出到底是什么地方