Discord bot 在对消息做出反应时未能添加角色

时间:2021-04-05 10:52:09

标签: javascript node.js discord discord.js

我最近使用此代码在对消息做出反应时给了我一个角色。问题是它不能正常工作。我使用的是 discord.js 11.6.4,但代码来自的 GitHub 存储库使用的是 discord.js 11.5.1。

代码:

function myFunction() {
    var myEle = document.getElementsByClassName("rbutton");
    if(myEle){
        myEle.click();
    }
  
}

myFunction()

当对消息做出反应时,它给了我以下错误:

client.on("message", msg => {
  const args = msg.content
    .slice(prefix.length)
    .trim()
    .split(/ +/g);

  const command = args.shift().toLowerCase();

  // svm = set verification message
  if (
    !msg.author.bot &&
    msg.content.indexOf(prefix) === 0 &&
    command === "svm"
  ) {
    // If sender of message is not the guild owner, cancel action
    if (msg.member.guild.ownerID !== msg.member.id) return;

    const introMessageContent = fs.readFileSync("intro-message.md", {
      encoding: "utf8",
      flag: "r"
    });
    const communityGuidelinesContent = fs.readFileSync(
      "community-guidelines.md",
      { encoding: "utf8", flag: "r" }
    );
    const verificationMessageContent = fs.readFileSync(
      "verification-message.md",
      { encoding: "utf8", flag: "r" }
    );

    const embed = new Discord.RichEmbed();

    const welcomeTitle = `Welcome to ${msg.guild.name}!`;

    embed.addField(welcomeTitle, introMessageContent);
    embed.addField("? Community Guidelines", communityGuidelinesContent);
    embed.addField("? Getting Verified", verificationMessageContent);

    msg.channel.send({ embed }).then(message => {
      message.react("?");
      fs.writeFileSync(
        "data.json",
        JSON.stringify({ messageId: message.id }, null, 4)
      );
    });
    msg.delete();
  }

client.on("messageReactionAdd", ({ message: { channel } }, user) => {
  if (/verification/.test(channel.name)) {
    channel.guild
      .fetchMember(user)
      .then(member => {
        member
          .addRole(verified_role_id)
          .then(() => {
            console.log(
              `The ${roleName} has been removed from member ${user.tag} successfully!`
            );
          })
          .catch(e => console.error("Error adding role"));
      })
      .catch(error => {
        console.error(error);
      });
  }
});

client.on("messageReactionRemove", ({ message: { channel } }, user) => {
  if (/verification/.test(channel.name)) {
    channel.guild
      .fetchMember(user)
      .then(member => {
        member
          .removeRole(verified_role_id)
          .then(() => {
            console.log(
              `The ${roleName} has been removed from member ${user.tag} successfully!`
            );
          })
          .catch(e => console.error("Error removing role"));
      })
      .catch(error => {
        console.error(error);
      });
  }
});

client.on("raw", ({ d: data, t: event }) => {
  if (["MESSAGE_REACTION_ADD", "MESSAGE_REACTION_REMOVE"].includes(event)) {
    const { channel_id, user_id, message_id, emoji } = data;

    const channel = client.channels.get(channel_id);

    if (!channel.messages.has(message_id))
      channel.fetchMessage(message_id).then(message => {
        const reaction = message.reactions.get(
          emoji.id ? `${emoji.name}:${emoji.id}` : emoji.name
        );

        const user = client.users.get(user_id);

        if (reaction) reaction.users.set(user_id, user);

        return client.emit(
          event === "MESSAGE_REACTION_ADD"
            ? "messageReactionAdd"
            : "messageReactionRemove",
          reaction,
          user
        );
      });
  }
});

这是 GitHub 原始存储库:https://github.com/domaincord/verification-bot

如果有人可以帮助我,谢谢。

0 个答案:

没有答案