【问题标题】:Discord.js: reaction.js event file not triggeringDiscord.js:reaction.js 事件文件未触发
【发布时间】:2021-10-01 19:06:49
【问题描述】:

我目前正在创建一个反应角色事件,人们可以在其中与消息交互并在单击某个表情符号后获得分配给自己的角色,但是由于某种原因,该事件不会触发并且没有角色分配给用户。

下面是我的reaction.js事件文件:

module.exports = async(reaction, user) => {
  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 === 'pinkEmoji') {
      await reaction.message.guild.members.cache.get(user.id).roles.add(pink);
    }
    if (reaction.emoji.name === 'yellowEmoji') {
      await reaction.message.guild.members.cache.get(user.id).roles.add(yellow);
    }
    if (reaction.emoji.name === 'purpleEmoji') {
      await reaction.message.guild.members.cache.get(user.id).roles.add(purple);
    }
  } else {
    return;
  }

  if (reaction.message.channel.id == channel) {
    if (reaction.emoji.name === 'pinkEmoji') {
      await reaction.message.guild.members.cache.get(user.id).roles.remove(pink);
    }
    if (reaction.emoji.name === 'yellowEmoji') {
      await reaction.message.guild.members.cache.get(user.id).roles.remove(yellow);
    }
    if (reaction.emoji.name === 'purpleEmoji') {
      await reaction.message.guild.members.cache.get(user.id).roles.remove(purple);
    }
  } else {
    return;
  }
}

我还使用命令让机器人知道它必须在输入命令后发送消息。

如果有帮助,这是我的reactionrole.js 命令文件:

module.exports = {
  name: 'reactionrole',
  description: 'sets up a reaction role message!',
  permissions: ["MANAGE_ROLES"],
  cooldown: 10,
  async execute(message, args, cmd, client, Discord) {
    const channel = process.env.WELCOME;

    const pink = message.guild.roles.cache.find(role => role.name === "Pink");
    const yellow = message.guild.roles.cache.find(role => role.name === "Yellow");
    const purple = message.guild.roles.cache.find(role => role.name === "Purple");

    const pinkEmoji = '????';
    const yellowEmoji = '????';
    const purpleEmoji = '????';

    let embed = new Discord.MessageEmbed()
    .setColor('#e42643')
    .setTitle('Choose your own **Color Role**!')
    .setDescription('Choosing a color role will give you a coloured name!\n\n'
        + `${pinkEmoji} for pink color\n`
        + `${yellowEmoji} for yellow color\n`
        + `${purpleEmoji} for purple color`);
    
    let messageEmbed = await message.channel.send(embed);
    messageEmbed.react(pinkEmoji);
    messageEmbed.react(yellowEmoji);
    messageEmbed.react(purpleEmoji);
  }
}

下面是我正在使用的事件处理程序:

const fs = require('fs');

module.exports = (client, Discord) => {
  const load_dir = (dirs) => {
    const event_files = fs.readdirSync(`./events/${dirs}`).filter(file => file.endsWith('.js'));

    for (const file of event_files) {
      const event = require(`../events/${dirs}/${file}`);
      const event_name = file.split('.')[0];
      client.on(event_name, event.bind(null, Discord, client));
    }
  }
  ['client', 'guild'].forEach(e => load_dir(e));
}

我已经坚持了很长一段时间,我不知道问题出在哪里,因为它没有抛出任何错误。

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    我不确定reaction.js 文件中的模块在何处或何时执行,但如果我检查您的事件处理程序,您似乎想收听reaction 事件。问题是没有reaction 事件。我认为您正在寻找的是 messageReactionAdd 事件,因此您应该将 reaction.js 重命名为 messageReactionAdd.js

    不过,如果您在 reactionrole.js 中声明 pinkyellow 等角色,则不会在此文件中使用它们。您需要在此文件中定义它们。

    // messageReactionAdd.js
    module.exports = async (reaction, user) => {
      if (user.bot) return;
      if (reaction.message.partial) await reaction.message.fetch();
      if (reaction.partial) await reaction.fetch();
    
      const channel = process.env.WELCOME;
      const { message } = reaction;
      const { guild } = message;
    
      if (!guild || message.channel.id !== channel) return;
    
      const pinkEmoji = '?';
      const yellowEmoji = '?';
      const purpleEmoji = '?';
    
      const member = guild.members.cache.get(user.id);
      const pink = guild.roles.cache.find((role) => role.name === 'Pink');
      const yellow = guild.roles.cache.find((role) => role.name === 'Yellow');
      const purple = guild.roles.cache.find((role) => role.name === 'Purple');
    
      if (reaction.emoji.name === pinkEmoji) {
        await member.roles.add(pink);
      }
      if (reaction.emoji.name === yellowEmoji) {
        await member.roles.add(yellow);
      }
      if (reaction.emoji.name === purpleEmoji) {
        await member.roles.add(purple);
      }
    };
    
    // reactionrole.js
    module.exports = {
      name: 'reactionrole',
      description: 'sets up a reaction role message!',
      permissions: ['MANAGE_ROLES'],
      cooldown: 10,
      async execute(message, args, cmd, client, Discord) {
        const pinkEmoji = '?';
        const yellowEmoji = '?';
        const purpleEmoji = '?';
    
        let embed = new Discord.MessageEmbed()
          .setColor('#e42643')
          .setTitle('Choose your own **Color Role**!')
          .setDescription(
            'Choosing a color role will give you a coloured name!\n\n' +
              `${pinkEmoji} for pink color\n` +
              `${yellowEmoji} for yellow color\n` +
              `${purpleEmoji} for purple color`,
          );
    
        let messageEmbed = await message.channel.send(embed);
        messageEmbed.react(pinkEmoji);
        messageEmbed.react(yellowEmoji);
        messageEmbed.react(purpleEmoji);
      },
    };
    

    【讨论】:

    • 感谢您的回答!但是我现在仍然遇到一个小问题。执行命令后,机器人似乎在发送嵌入时没有问题,但它不会加载每个表情符号并返回错误说明:TypeError: Cannot read property 'partial' of undefined at the line 3 of the messageReactionAdd.js file。跨度>
    • 看来我打错了reation.message.partial 应该是reaction.message.partial
    • 我修正了错字,但它仍然给我同样的错误。它似乎与 .partial 本身有关
    • 哦,所以它说reaction.message 未定义。如果你交换这两行(第 3 行和第 4 行)以确保首先获取反应。
    • 遗憾地改变这两个并没有做出改变它仍然说“TypeError:无法读取未定义的属性'部分'”
    猜你喜欢
    • 2020-12-22
    • 2022-12-03
    • 2017-07-10
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 2019-01-31
    相关资源
    最近更新 更多