0

I'm unable to retrieve from my reactions.json the emoji name to match the role.name. Not sure what I'm missing and I've been staring at it for hours. '

A bit confused as to what i'm missing here.

reactions.json

{
"channel": "test",
"channelID": "",
"roles": {
  "melee": "🪓 Melee DPS",
  "ranged": "🏹 Ranged DPS",
  "caster": "🧙🏻 Caster DPS",
  "healer": "🚑 Healer",
  "tank": "🔰 Tank"
},
"vote": {
    "melee": "🪓",
    "ranged": "🏹",
    "caster": "🧙🏻",
    "healer": "🚑",
    "beginner": "🔰"
  },
 }

Javascript

client.on('messageReactionAdd', addRole);

async function addRole({message, emojis}, user) {

if (message.partial) {
  try {
    await message.fetch();
    
  } catch (err) {
    console.error('Error fetching message', err);
    return;
  }
}

const { guild } = message;

const member = guild.members.cache.get(user.id);


const reactionsJSON = JSON.parse(fs.readFileSync('reactions.json', 'utf8'));
const { guild } = message;
const role = guild.roles.cache.find((role) => role.name === reactionsJSON[emojis.name]);

if (!role) {
  console.error(`Role not found for '${reactionsJSON[emojis]}'`);
  return;
}

try {
  member.roles.add(role.id);
} catch (err) {
  console.error('Error adding role', err);
  return;
}
}
6
  • What is flagged as undefined? role or emojis? Commented Sep 13, 2021 at 12:56
  • @TheOtterlord emojis Commented Sep 13, 2021 at 12:58
  • oh, wait a minute. in this code, emojis is not defined? Commented Sep 13, 2021 at 13:00
  • @TheOtterlord i'm updating to include the async function. Sorry about that Commented Sep 13, 2021 at 13:01
  • 1
    ok, so whatever is calling addRole passes through an object that includes message, but that doesn't include emojis (hence it's undefined). So I think the problem lies higher up in the stack? Commented Sep 13, 2021 at 13:04

1 Answer 1

1

Ok, I believe that the problem is the deconstruction of the MessageReaction property of the event. The object contains message, but not emojis, however it contains a value emoji which has what you lare looking for.

.emoji (read only)

  • The emoji of this reaction. Either a GuildEmoji object for known custom emojis, or a ReactionEmoji object which has fewer properties. Whatever the prototype of the emoji, it will still have name, id, identifier and toString()
client.on('messageReactionAdd', addRole);

async function addRole({message, emoji}, user) {

if (message.partial) {
  try {
    await message.fetch();
    
  } catch (err) {
    console.error('Error fetching message', err);
    return;
  }
}

const { guild } = message;

const member = guild.members.cache.get(user.id);


const reactionsJSON = JSON.parse(fs.readFileSync('reactions.json', 'utf8'));
const { guild } = message;
const role = guild.roles.cache.find((role) => role.name === reactionsJSON[emoji.name]);

if (!role) {
  console.error(`Role not found for '${reactionsJSON[emoji]}'`);
  return;
}

try {
  member.roles.add(role.id);
} catch (err) {
  console.error('Error adding role', err);
  return;
}
}

Documentation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.