首页 > 解决方案 > Discord.js 反应角色机器人不分配角色,但嵌入工作

问题描述

我的反应角色机器人发送带有反应的嵌入消息,但是当您选择反应时,角色不是属性

我有这个代码 index.js :

const Discord = require("discord.js");
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"]}, {partials: ["MESSAGE", "CHANNEL", "REACTION", "MANAGE_ROLES"] })
require("dotenv").config();

const prefix = "!";

const fs = require("fs");

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync("./commands/").filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
    const command = require(`./commands/${file}`);

    client.commands.set(command.name, command);
}

client.on("ready", () => {
    console.log("bot online");
});

client.on("messageCreate", message => {

    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();
    if (command === "reactionrole") {
        client.commands.get("reactionrole").execute(message, args, Discord, client);
    }
});

client.login(process.env.BOT_TOKEN);

反应角色.js:

module.exports = {
    name: 'reactionrole',
    description: "Sets up a reaction role message",
    async execute(message, args, Discord, client) {
        const channel = "889233386781151232";
        const maths = message.guild.roles.cache.find(role => role.name === "Maths");
        const mathsExpertes = message.guild.roles.cache.find(role => role.name === "Maths expertes");
        const nsi = message.guild.roles.cache.find(role => role.name === "NSI");
        const physique = message.guild.roles.cache.find(role => role.name === "Physique");
        const svt = message.guild.roles.cache.find(role => role.name === "SVT");
        const artsPlastique = message.guild.roles.cache.find(role => role.name === "Arts Plastiques");

        const mathsEmoji = '';
        const mathsExpertesEmoji = '';
        const nsiEmoji = '';
        const physiqueEmoji = '';
        const svtEmoji = '';
        const artsPlastiquesEmoji = '';

        let embed = new Discord.MessageEmbed()
            .setColor("#e42643")
            .setTitle("Selection des matière")
            .setDescription("Choisis tes matières en cliquant sur la reaction qui correspond\n\n"
                + `${mathsEmoji} : Maths\n`
                + `${mathsExpertesEmoji} : Maths Expertes\n`
                + `${nsiEmoji} : NSI\n`
                + `${physiqueEmoji} : Physique\n`
                + `${svtEmoji} : SVT\n`
                + `${artsPlastiquesEmoji} : Arts Plastiques\n`);

        let messageEmbed = await message.channel.send({embeds: [embed]});
        messageEmbed.react(mathsEmoji);
        messageEmbed.react(mathsExpertesEmoji);
        messageEmbed.react(nsiEmoji);
        messageEmbed.react(physiqueEmoji);
        messageEmbed.react(svtEmoji);
        messageEmbed.react(artsPlastiquesEmoji);

        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 === mathsEmoji){
                    await reaction.message.guild.members.cache.get(user.id).roles.add(maths);
                }
                if (reaction.emoji.name === mathsExpertesEmoji){
                    await reaction.message.guild.members.cache.get(user.id).roles.add(mathsExpertes);
                }
                if (reaction.emoji.name === nsiEmoji){
                    await reaction.message.guild.members.cache.get(user.id).roles.add(nsi);
                }
                if (reaction.emoji.name === physiqueEmoji){
                    await reaction.message.guild.members.cache.get(user.id).roles.add(physique);
                }
                if (reaction.emoji.name === svtEmoji){
                    await reaction.message.guild.members.cache.get(user.id).roles.add(svt);
                }
                if (reaction.emoji.name === artsPlastiquesEmoji){
                    await reaction.message.guild.members.cache.get(user.id).roles.add(artsPlastique);
                }
            } 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 === mathsEmoji){
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(maths);
                }
                if (reaction.emoji.name === mathsExpertesEmoji){
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(mathsExpertes);
                }
                if (reaction.emoji.name === nsiEmoji){
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(nsi);
                }
                if (reaction.emoji.name === physiqueEmoji){
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(physique);
                }
                if (reaction.emoji.name === svtEmoji){
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(svt);
                }
                if (reaction.emoji.name === artsPlastiquesEmoji){
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(artsPlastique);
                }
            } else {
                return;
            }
        });
    }
}

并且在启动命令时我没有错误

你能帮我吗

我知道这个问题已经被问过了,但我不明白答案或不清楚

标签: javascriptdiscord.js

解决方案


推荐阅读