简体   繁体   English

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

[英]Discord.js: reaction.js event file not triggering

I am currently creating a reaction role event where people can interact with a message and get a role assigned to themselves after clicking a certain emoji, however for some reason the event will not trigger and no role is assigned to the user.我目前正在创建一个反应角色事件,人们可以在其中与消息交互并在单击某个表情符号后获得分配给自己的角色,但是由于某种原因,该事件不会触发并且没有分配给用户的角色。

Below is my reaction.js event file:下面是我的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;
  }
}

I'm also using a command to let the bot know it has to send the message after typing in the command.我还使用命令让机器人知道它必须在输入命令后发送消息。

Here is my reactionrole.js command file if it helps:如果有帮助,这是我的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);
  }
}

Below is the event handler I am using:下面是我正在使用的事件处理程序:

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));
}

I have been stuck on this for quite a while now and I don't know where the problem lies because it's not throwing any errors.我已经被困在这个问题上很长一段时间了,我不知道问题出在哪里,因为它没有抛出任何错误。

I'm not sure where or when the module from the reaction.js file is executed but if I check your event handler, it seems you want to listen to the reaction event.我不确定reaction.js文件中的模块在何处或何时执行,但如果我检查您的事件处理程序,您似乎想收听reaction事件。 The problem is there is no reaction event.问题是没有reaction事件。 I think what you're looking for is the messageReactionAdd event instead, so you should rename reaction.js to messageReactionAdd.js .我想你要找的是什么messageReactionAdd事件代替,所以你应该重新命名reaction.jsmessageReactionAdd.js

Still, roles like pink , yellow , etc. won't be available in this file if you're declaring them in reactionrole.js .尽管如此,如果您在reactionrole.js声明像pinkyellow等角色在这个文件中将不可用。 You need to define them in this file instead.您需要在此文件中定义它们。

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

I think you need to get ID message when user type reactionrole .我认为您需要在用户键入 reactrole 时获取 ID 消息。 Then in file reaction.js event you write a collector , collection all reaction from message id and give role for you click it.然后在文件reaction.js 事件中编写一个收集器,从消息ID 收集所有反应并为您单击它赋予角色。

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

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