简体   繁体   English

Discord 反应角色问题

[英]Discord reaction roles issue

I am currently coding a discord bot reaction roles command.我目前正在编写一个 discord 机器人反应角色命令。

Everything is running smoothly but I have one issue.一切运行顺利,但我有一个问题。

When I boot the bot and run the command to create the embed, everything works fine.当我启动机器人并运行创建嵌入的命令时,一切正常。 The issue is when I react to the message it doesn't give me the role.问题是当我对消息做出反应时,它并没有赋予我角色。 Here is my code:这是我的代码:

module.exports = {
    name: "reactionrole",
    description: "Sets up a reaction role message!",
    async execute(message, args, Discord, client) {
        const channel = "751217304733351976";
        const yellowTeamRole = message.guild.roles.cache.find((role) => role.name === "ROBLOX");
        const blueTeamRole = message.guild.roles.cache.find((role) => role.name === "Minecraft");
        const redTeamRole = message.guild.roles.cache.find((role) => role.name === "MinecraftBedrock");

        const yellowTeamEmoji = "798904223755927562";
        const blueTeamEmoji = "798904030553440257";
        const redTeamEmoji = "798904121544540181";

        let embed = new Discord.MessageEmbed()
            .setColor("#e42643")
            .setTitle("Choose the games you play!")
            .setDescription("Choosing a game will ping you when others want to play with you!\n\n" + `ROBLOX\n` + `Minecraft\n` + "Minecraft Bedrock");

        let messageEmbed = await message.channel.send(embed);
        messageEmbed.react(yellowTeamEmoji);
        messageEmbed.react(blueTeamEmoji);
        messageEmbed.react(redTeamEmoji);

        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 === yellowTeamEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(yellowTeamRole);
                }
                if (reaction.emoji.name === blueTeamEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(blueTeamRole);
                }
                if (reaction.emoji.name === redTeamEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(redTeamRole);
                } 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 === yellowTeamEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(yellowTeamRole);
                }
                if (reaction.emoji.name === blueTeamEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(blueTeamRole);
                }
                if (reaction.emoji.name === redTeamEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(redTeamRole);
                } else {
                    return;
                }
            }
        });
    },
};

Does it give a 0 exit error?它会给出 0 退出错误吗?

Also check if the Bot's Role is above the Red, blue and yellow role on the role list还要检查机器人的角色是否在角色列表中的红色、蓝色和黄色角色之上

There are two places where you can grab emojis using discord.js: in the client, and in the guilds.您可以在两个地方使用 discord.js 获取表情符号:在客户端和行会中。 client.emojis is a collection of every emoji the client has access to, and guild.emojis is a collection of the emojis of a specific guild. client.emojis是客户端可以访问的每个表情符号的集合,而guild.emojis是特定公会的表情符号的集合。

const yellowTeamEmoji = client.emojis.get("798904223755927562")

You might also know how to use find to get something with another property - so here, I can get yellowTeamEmoji through its name:您可能还知道如何使用find来获取其他属性的内容 - 所以在这里,我可以通过它的名称获取yellowTeamEmoji

const yellowTeamEmoji = client.emojis.find(emoji => emoji.name === "yellowTeamEmoji");

All the best, Florian.一切顺利,弗洛里安。

So what you need to do is:所以你需要做的是:

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

    const yellowTeamEmoji = "reactionEmoji1";
    const blueTeamEmoji = "reactionEmoji2";
    const redTeamEmoji = "reactionEmoji3";

How to get id of emojis?如何获取表情符号的 ID? 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"如果您在浏览器中打开该链接,它将显示如下内容:“https://cdn.discordapp.com/emojis/791979565056000000.png?v=1” 您需要复制最后的数字并粘贴到“emojiID”部分

example:例子:

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

instead of ID use unicode. Just post the emoji in the channel, copy it and paste it in your code.而不是 ID 使用 unicode。只需在频道中发布表情符号,将其复制并粘贴到您的代码中。

const yellowTeamEmoji = "🌕";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM