【问题标题】:How to make reaction roles with custom emojis?如何使用自定义表情符号制作反应角色?
【发布时间】:2021-04-27 20:49:59
【问题描述】:

我尝试使用自定义表情符号制作反应角色,但我不得不犯错误。问题一定出在:“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;
            }
        });
    }
}

【问题讨论】:

  • 我不认为你可以在其中嵌套监听器,你必须在你的父文件中创建监听器。

标签: javascript discord discord.js


【解决方案1】:

让我们开始修复代码吧!

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

    },
};

【讨论】:

  • 我不知道为什么,但它仍然对我不起作用。问题仍然必须在这里:“如果(reaction.emoji.name === rulesEmoji)”。我更改 rulesEmoji const (const rulesEmoji = client.emojis.cache.get("800181350279020594");) 并在“if (reaction.message.channel.id == rulesChannel)”中添加一个 =。
  • 所以@Miskakao 我建议您从频道中删除现有消息并将其添加到您的命令中。如果您不知道该怎么做,请在下面推荐,我将使用消息修复您的代码。我认为问题是因为缺少消息!
  • 带我看看答案,我会给你一个更新,看看如何解决它!
  • 还是什么都没有:(这里一定有问题:“if (reaction.emoji.name === rulesEmoji)”因为它总是落在这里。
  • if (reaction.message.channel.id === channel) { if (reaction.emoji.name === reactionEmoji) { await react.message.guild.members.cache.get(user .id).roles.add(reactRole) }
猜你喜欢
  • 2021-05-15
  • 2022-07-14
  • 2021-12-02
  • 2021-02-17
  • 2021-08-09
  • 2020-03-20
  • 2020-07-10
  • 2021-09-29
  • 2020-02-28
相关资源
最近更新 更多