问题
i have a problem with the message reaction, i made the bot to delete any message sent on channel names appeal and send it to another channel names the appeals and react to the message with :white_check_mark: and if someone reacted to the message with the :white_check_mark:, the bot will automaticly delete the bot,
thats working but there is a problem, if i restarting the bot and reacting to the message sent before the restarting , the bot don't deleting the message
why?
client.on('message', async message => {
if(message.author.bot) return;
var muted = message.guild.member(message.author).roles.find(j => j.id === "505763004797812766");
if (muted && message.channel.id === "563944611693854721"){
var muted = message.guild.member(message.author).roles.find(j => j.id === "505763004797812766");
const args = message.content.split(" ").slice(0).join(" ");
const appeal = new Discord.RichEmbed()
.setAuthor(message.author.username, message.author.avatarURL)
.setTitle(message.author.username + " appeal")
.setColor("RED")
.addField("Message", args);
message.guild.channels.find(ch => ch.id === "563966341980225536").send(appeal).then(msg => {
msg.react('✅');
client.on('messageReactionAdd', (reaction, user) => {
if(reaction.emoji.name === "✅") {
const whitecheckmark = (reaction, user) => reaction.emoji.name === "✅";
const done = msg.createReactionCollector(whitecheckmark, {time: 60000});
done.on('collect', r => {
msg.delete();
message.guild.channels.find(ch => ch.id === "563966341980225536").send(message.author + " Appeal ended by: " + reaction.users.last())
})
}
});
})
message.delete();
message.channel.overwritePermissions(message.author, {SEND_MESSAGES: false});
}
else if(!muted && message.channel.id === "563944611693854721"){
message.channel.overwritePermissions(message.author, {SEND_MESSAGES: true});
}
});
回答1:
This is events by design. The only way you can get around this is by queuing these events in some kind of durable queue like RabbitMQ or NATS assuming the events make it to your listeners before it restarts.
回答2:
In general, it isn't good practice to "nest" events (that is, add listeners within others). If you place the messageReactionAdd listener outside of the message event on its own, it will listen without needing a message to. Then, if the message is sent and the bot restarts, the reaction event will still be fired. Just make sure to confirm that the message triggering the event is indeed one that should be.
来源:https://stackoverflow.com/questions/55868601/how-to-fix-events-not-working-after-restarting-the-bot