Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

javascript - How to make reaction roles with custom emojis?

I tried to make reaction roles with custom emojis, but I had to make a mistake. The problem must be here: "if (reaction.emoji.id === rulesEmoji)". There is no error. (yes, I'm using emoji that are on that server)

module.exports = {
    name: 'reactionrole',
    description: "Sets up a reaction role message!",
    async execute(message, args, Discord, client, chalk) {
        const rulesChannel = '801870345858580531';
        const rulesRole = message.guild.roles.cache.find(role => role.name === "rules");
        const rulesEmoji = "802253842648662026";

        client.on('messageReactionAdd', 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 == rulesChannel) {
                if (reaction.emoji.id === rulesEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(rulesRole);
                }
            } else {
                return;
            }

             
        });
 
        client.on('messageReactionRemove', 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 == rulesChannel) {
                if (reaction.emoji.id === rulesEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(rulesRole);
                }
            } else {
                return;
            }
        });
    }
}
question from:https://stackoverflow.com/questions/65861214/how-to-make-reaction-roles-with-custom-emojis

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So lets begin fixing the code!

module.exports = {
    name: 'reactionrole',
    description: 'A Reaction Message!',
   async execute(message, args, Discord, client){
        const channel = '797456859165491250';
        const reactRole = message.guild.roles.cache.find(role => role.name === "NameOfRole");

This codes asks for a channel that the message will be posted in and the name of the role you want to give !

Then you need to ask for the emoji.If you want a custom emoji the easiest way is to send ,in a test channel, the emoji that you want to set and if you right click on the emoji, at the bottom , it's says "OPEN LINK" . If you open that link in your browser it will show something like this : "https://cdn.discordapp.com/emojis/791979565056000000.png?v=1" You need to copy the numbers in the end and paste is in the section "emojiID"

Example:

const reactionEmoji = client.emojis.cache.get("791979565056000000");

While you have the id of the custom emoji, you need to apply it in the code, so your next step is :

    const reactionEmoji = client.emojis.cache.get("emojiID");

UPDATE: Under the above code you need to set a message for the bot to react!You need to do this :

let embed = new Discord.MessageEmbed()
        .setColor('RANDOM')
        .setTitle('Some Title')
        .setDescription('**some description**

'
        + `>  **${reactionEmoji} for Role**
`)
        
        let messageEmbed = await message.channel.send(embed);
        message.delete();
        messageEmbed.react(reactionEmoji);


     client.on('messageReactionAdd', 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 === reactionEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(reactRole)
                }
       
        } else {
            return;
        }
        });

        client.on('messageReactionRemove', 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 === reactionEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(reactRole)
                }
            }
        } else {
            return;
        }
        });

    },
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...