AlexzanderFlores / Worn-Off-Keys-Discord-Js

469 stars 514 forks source link

"TypeError [EMOJI_TYPE]: Emoji must be a string or GuildEmoji/ReactionEmoji" when running the role-claim.js file #25

Closed andyforprez closed 3 years ago

andyforprez commented 3 years ago

Here is how I altered the code for myself. The red_circle and orange_circle emojis etc. are default Discord emojis. When I run the code, it gives the error TypeError [EMOJI_TYPE]: Emoji must be a string or GuildEmoji/ReactionEmoji. The first-message.js file is the same as the one in this repo, and even though I changed a lot of index.js, there are no problems in index.js that have to do with the error I am mentioning.


const firstMessage = require('./first-message')

module.exports = (client) => {
    const channelId = '870818745109585920'

    const getEmoji = (emojiName) => client.emojis.cache.find((emoji) => emoji.name === emojiName)

    const emojis = {
        red_circle: 'CrackShot / Sniper',
        orange_circle: 'Scrambler / Shotgun',
        yellow_circle: 'Whipper / P90',
        green_circle: 'RPEGG / RPG',
        blue_circle: 'Free Ranger / Semi-Auto',
        purple_circle: 'EGG-K / AK-47',
        white_circle: 'TriHard / AUG',
        black_circle: 'Cluck-9mm / Pistol'
    }

    const reactions = []

    let emojiText = '**GUN ROLES**\n\n'
    for (const key in emojis) {
        const emoji = getEmoji(key)
        reactions.push(emoji)

        const role = emojis[key]
        emojiText += `${emoji} = ${role}\n`
    }

    firstMessage(client, channelId, emojiText, reactions)

    const handleReaction = (reaction, user, add) => {
        if (user.id === '869698265698947102') {
            return
        }
        const emoji = reaction._emoji.name

        const { guild } = reaction.message

        const roleName = emojis[emoji]
        if(!roleName) {
            return
        }

        const role = guild.roles.cache.find((role) => role.name === roleName)
        const member = guild.members.cache.find((member) => member.id === user.id)

        if (add) {
            member.roles.add(role)
        } else {
            member.roles.remove(role)
        }
    }

    client.on('messageReactionAdd', (reaction, user) => {
        if (reaction.message.channel.id === channelId) {
            handleReaction(reaction, user, true)
        }
    })

    client.on('messageReactionRemove', (reaction, user) => {
        if (reaction.message.channel.id === channelId) {
            handleReaction(reaction, user, false)
        }
    })
}```
andyforprez commented 3 years ago

I fixed the problem myself by putting the names of the emojis e.g. red_circle etc. as the emoji itself inside a string and then simplified the for loop to

let reactions = [];
    let emojiText = '**GUN ROLES**\n\n';
    Object.entries(emojis).forEach(([emoji, role]) => {
      reactions.push(emoji);
      emojiText += `${emoji} = ${role}\n`;
    });