Discord.js:未触发reaction.js事件文件

网编 179 0
提问开始:

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

下面是我的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));
}

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

回答开始:得票数 1

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

但是,如果您在reactionrole.js中声明pink、yellow等角色,则这些角色在此文件中将不可用。您需要在此文件中定义它们。

// 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);
  },
};
总结

以上是真正的电脑专家为你收集整理的Discord.js:未触发reaction.js事件文件的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得真正的电脑专家网站内容还不错,欢迎将真正的电脑专家推荐给好友。

标签: #文件 #事件 #reaction

  • 评论列表

留言评论