如何使用自定义表情符号制作反应角色?

时间:2021-01-23 15:52:53

标签: javascript discord discord.js

我尝试使用自定义表情符号来制作反应角色,但我不得不犯了一个错误。问题肯定在这里:“if (reaction.emoji.id === rulesEmoji)”。没有错误。 (是的,我正在使用该服务器上的表情符号)

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

1 个答案:

答案 0 :(得分:0)

那么让我们开始修复代码吧!

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

此代码要求发布消息的频道以及您要授予的角色名称!

然后你需要询问表情符号。如果你想要一个自定义表情符号,最简单的方法是在测试频道中发送你想要设置的表情符号,如果你右键单击底部的表情符号,上面写着“打开链接”。如果您在浏览器中打开该链接,它将显示如下内容:“https://cdn.discordapp.com/emojis/791979565056000000.png?v=1”您需要复制最后的数字并粘贴到“emojiID”部分

示例:

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

虽然您拥有自定义表情符号的 id,但您需要将其应用到代码中,因此您的下一步是:

    const reactionEmoji = client.emojis.cache.get("emojiID");
<块引用>

更新: 在上面的代码下你需要设置一条消息让机器人做出反应!你需要这样做:

let embed = new Discord.MessageEmbed()
        .setColor('RANDOM')
        .setTitle('Some Title')
        .setDescription('**some description**\n\n'
        + `>  **${reactionEmoji} for Role**\n`)
        
        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;
        }
        });

    },
};