使用自定义表情符号的反应角色 [英] Reaction Roles with Custom emojis

查看:0
本文介绍了使用自定义表情符号的反应角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在让反应角色使用自定义表情符号方面遇到了问题。如果我使用不和谐的表情符号,它会嵌入消息,创建反应,并在点击反应时应用角色。

但是,如果我使用自定义表情符号,它会嵌入消息并创建正确的反应,但它不会在表情符号被点击时提供角色。

我在切换不一致表情和自定义表情时使用了相同的编码,唯一的更改是表情ID。

我使用的完整代码是:

const { MessageReaction } = require("discord.js");

module.exports = {
    name: "reactionrole",
    description: "Sets up a reaction role message!",
    async execute(message, args, Discord, client) {
        const channel = "764347631081881660";
        const sheRole = message.guild.roles.cache.find((role) => role.name === "She/Her");
        const heRole = message.guild.roles.cache.find((role) => role.name === "He/Him");
        const theyRole = message.guild.roles.cache.find((role) => role.name === "They/Them");

        const sheEmoji = client.emojis.cache.get("805419160497946634");
        const heEmoji = client.emojis.cache.get("795488864721829928");
        const theyEmoji = client.emojis.cache.get("795871326446419968");

        let embed = new Discord.MessageEmbed()
            .setColor("#e42643")
            .setTitle("Self Assignable Roles")
            .setDescription(
                "To Join one of our self-assignable roles, click/tap the reaction with the appropriate emoji for that role. (Note that this is purely for fun and for helpful tags & inclusivity.) By doing this we are hoping to better assist everyone in how they can address others. This also adds helpful knowledge of when someone may be on for trades, events etc.This is also not mandatory but highly recommended. isaheart 

" +
                    `List of corresponding emojis & roles:

` +
                    `Identify yourself:
` +
                    `:judy: She/Her
` +
                    `:raymond: He/Him
` +
                    `:zucker: They/Them

` +
                    `Removing your reaction will also remove the role from yourself. You can have multiple roles so go ahead and choose one from each category ^^If you have any questions please feel free to reach out to any of our Admin`
            );

        let messageEmbed = await message.channel.send(embed);
        messageEmbed.react(sheEmoji);
        messageEmbed.react(heEmoji);
        messageEmbed.react(theyEmoji);

        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 === sheEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(sheRole);
                }
                if (reaction.emoji.name === heEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(heRole);
                }
                if (reaction.emoji.name === theyEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(theyRole);
                }
            } 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 === sheEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(sheRole);
                }
                if (reaction.emoji.name === heEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(heRole);
                }
                if (reaction.emoji.name === theyEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(theyRole);
                }
            } else {
                return;
            }
        });
    },
};

如有任何帮助,我们将不胜感激。

推荐答案

您正在使用Emoji#name自定义表情符号和默认表情符号。这样做的问题是,自定义表情符号没有Emoji#name属性(默认表情符号也是如此,它们没有Emoji#id属性)。

若要解决此问题,您需要专门使用Emoji#id自定义表情符号或使用Emoji#identifier

这意味着您可以

  • reaction.emoji.id === heEmoji.id
  • reaction.emoji === heEmoji(不是100%安全)
  • reaction.emoji.identifier=== heEmoji.identifier

这篇关于使用自定义表情符号的反应角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆