重新启动后如何使反应起作用?

时间:2021-06-02 20:11:28

标签: javascript node.js discord discord.js

所以我正在尝试使用 discord.js 制作一个票务机器人。通过点击一个反应,用户将被添加到一个票务频道,在那里他们可以获得帮助。票务系统本身工作得很好,就像我希望它工作的那样。但问题是,在我重新启动机器人后,当您点击反应时,没有任何反应。相关代码如下:

let helpdeskmessageEmbed = await message.channel.send(helpdeskticketEmbed)
    helpdeskmessageEmbed.react("1️⃣")
    helpdeskmessageEmbed.react("2️⃣")
    helpdeskmessageEmbed.react("3️⃣")
   
    client.on('messageReactionAdd', async (reaction, user) => {
     
      const categoryID = "820920950114615318"
      if (reaction.message.partial) await reaction.message.fetch()
      if (reaction.partial) await reaction.fetch()
      if (user.bot) return;
      if (!reaction.message.guild) return;
      if (reaction.message.channel.id == channel ) {
        if(reaction.emoji.name === "1️⃣") {
          reaction.users.remove(user)

         message.guild.channels.create(`report-${user.username}`).then(

其余代码与我的问题无关。谁能告诉我如何解决这个问题。

注意:我正在使用命令处理程序(如果相关)

1 个答案:

答案 0 :(得分:-1)

我前段时间遇到了这个确切的问题。长话短说,您需要使用 partials

请注意:

“Partials 不具备使它们功能齐全的 discord.js 结构所需的所有信息,因此默认情况下启用该功能不是一个好主意。用户在选择加入之前应该知道如何处理它们此功能。”

我看到您已经尝试使用它们,但是您需要在代码中添加处理部分的能力。将 const client = new Discord.Client(); 更改为:

const client = new Discord.Client({
    partials: [`MESSAGE`, `CHANNEL`, `REACTION`], //unsure if message and channel are needed, feel free to test it out
});

这基本上允许处理所有消息,即使是在您的机器人启动之前发布的消息。如果没有上述更改,您的代码将不会检测到部分。

client.on("messageReactionAdd", async (reaction, user) => {
    let msg = reaction.message;
    if (msg.partial) {
        await msg.fetch().catch(() => {});
    }
    //your code goes here
});

回答您的评论:

我已阅读您的 replit 代码,看起来不错,我只能建议更改此代码:

const Discord = require ('discord.js');
const client = new Discord.Client({ partials: [`MESSAGE`, `CHANNEL`, `REACTION` ]});

进入这个:

const { Discord } = require('discord.js');
const client = new Discord({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });