简体   繁体   English

奇怪的反应行为,票务系统discord.js

[英]Weird reaction behavior, ticket system discord.js

I am trying to make an own ticket system for my discord server with my discord.js bot.我正在尝试使用我的 discord.js 机器人为我的 discord 服务器制作自己的票务系统。 If a user reacts to a certain message with the emoji, I want it first to delete the reaction instantly, and then continue.如果用户使用表情符号对某条消息做出反应,我希望它首先立即删除该反应,然后继续。 So basically the reaction count stays at 1 (the only one from my bot).所以基本上反应计数保持在 1(我的机器人中唯一的一个)。 The problem is, if I restart the bot and react to the message, it doesn't delete my reaction, however when I manually delete it, and try again, it works!问题是,如果我重新启动机器人并对消息做出反应,它不会删除我的反应,但是当我手动删除它并重试时,它会起作用!

Here is my code:这是我的代码:

client.on("messageReactionAdd", async (messageReaction, user) => {
    if (messageReaction.partial) {
        try {
            await messageReaction.fetch();
        } catch (error) {
            console.log("couldn't fetch message1", error);
        }
    }
    let msg = messageReaction.message;
    if (msg.id === config.support_msg_id) {
        const userReactions = msg.reactions.cache.filter(reaction => reaction.users.cache.has(user.id));
        try {
            for (const reaction of userReactions.values()) {
                await reaction.users.remove(user.id);
            }
        } catch (error) {
            console.error('Failed to remove reactions.');
        }
    }
});

The problem is that the messageReactionAdd event is only triggered when a reaction is added to a cached message, as stated by the Discord.js docs :问题是messageReactionAdd事件仅在将反应添加到缓存消息时触发,如Discord.js 文档所述:

Emitted whenever a reaction is added to a cached message.每当将反应添加到缓存消息时发出。

You could fix this by adding every message to the cache when the bot starts by using the following code:您可以通过在机器人启动时使用以下代码将每条消息添加到缓存来解决此问题:

client.on('ready', () => {
  client.guilds.cache.each(guild => {
    guild.channels.cache.each(channel => {
      if (channel.type === 'text' || channel.type === 'dm') {
        channel.messages.fetch();
      }
    });
  });
});

If you want to prevent too many messages being added to the cache, you can pass ChannelLogsQueryOptions to the MessageManager.fetch() function.如果要防止将过多消息添加到缓存中,可以将ChannelLogsQueryOptions传递给MessageManager.fetch() function。

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

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