简体   繁体   English

如何修复反应功能不起作用(机器人重新启动后)

[英]How do I fix Reaction Functions not working (after bot restarted)

So I am working on a discord.js ticket system and I ran through a bug which I can't seem to fix by myself.所以我正在开发一个 discord.js 票务系统,我遇到了一个我自己似乎无法修复的错误。

Basically, I work with reactions to open & close tickets, but if someone makes a ticket and then I restart the bot, the ticket created before the restart doesn't work.基本上,我处理打开和关闭票证的反应,但如果有人制作票证然后我重新启动机器人,则在重新启动之前创建的票证不起作用。 The reaction that should close the ticket, doesn't - but it does work on any tickets made after the reset应该关闭票证的反应不会 - 但它确实适用于重置制作的任何票证

My code:我的代码:

    else if(reaction.emoji.name === '🔒') {

        if(userTickets.has(user.id)) {
            if(reaction.message.channel.id === userTickets.get(user.id)) {
                reaction.remove(user);
                let embed = new RichEmbed();
                embed.setDescription("Ticket will be closed in 5 seconds.")
                reaction.message.channel.send(embed);
                setTimeout(() => {
                    reaction.message.channel.delete('closing ticket')
                    .then(channel => {
                        console.log("Deleted " + channel.name);
                    })
                    .catch(err => console.log(err));
                }, 5000);
            }
        }

        else if(reaction.message.guild.channels.some(channel => channel.name.toLowerCase() === user.username + 's-ticket')) {
            let embed = new RichEmbed();
            embed.setDescription("Ticket will be closed in 5 seconds.");
            reaction.message.channel.send(embed);
            setTimeout(() => {
                reaction.message.guild.channels.forEach(channel => {
                    if(channel.name.toLowerCase() === user.username + 's-ticket') {
                        channel.delete().then(ch => console.log('Deleted Channel ' + ch.id))
                    }
                });
            }, 5000);
        }
    }
});

You must handle events for uncached messages您必须处理未缓存消息的事件

The reason it doesn't work on reload is because the cache is cleared every time you restart the bot.它在重新加载时不起作用的原因是每次重新启动机器人时都会清除缓存。 Only new messages will be cached.只有新消息会被缓存。 Reaction events aren't triggered on unloaded messages.未加载的消息不会触发反应事件。

To solve this problem, you will have to listen for raw events.要解决此问题,您将必须侦听raw事件。 It looks like this:它看起来像这样:

// check for reactions on unloaded messages and trigger the appropriate events
client.on('raw', packet => {
  if (!['MESSAGE_REACTION_ADD', 'MESSAGE_REACTION_REMOVE'].includes(packet.t)) return;
  const channel = client.channels.get(packet.d.channel_id);
  if (channel.messages.has(packet.d.message_id)) return;
  channel.fetchMessage(packet.d.message_id).then(message => {
    const emoji = packet.d.emoji.id ? `${packet.d.emoji.name}:${packet.d.emoji.id}` : packet.d.emoji.name;
    const reaction = message.reactions.get(emoji);
    if (packet.t === 'MESSAGE_REACTION_ADD') { client.emit('messageReactionAdd', reaction, client.users.get(packet.d.user_id)); }
    if (packet.t === 'MESSAGE_REACTION_REMOVE') { client.emit('messageReactionRemove', reaction, client.users.get(packet.d.user_id)); }
  });
});

I can't explain it better than how Évelyne Lachance does in the original guide i took this from , so if you want to understand how this works, read it there.我无法比 Évelyne Lachance 在我从中获取的原始指南中更好地解释它,所以如果你想了解它是如何工作的,请在此处阅读。

This snippet will automatically call the messageReactionAdd and messageReactionRemove events and pass on the necessary arguments, so you won't really need to do much apart from copy-pasting this in your main file.此代码段将自动调用messageReactionAddmessageReactionRemove事件并传递必要的 arguments,因此除了将其复制粘贴到主文件中之外,您实际上不需要做太多事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM