TypeError:无法读取未定义的属性(读取“公会”)

Posted

技术标签:

【中文标题】TypeError:无法读取未定义的属性(读取“公会”)【英文标题】:TypeError: Cannot read properties of undefined (reading 'guild') 【发布时间】:2021-11-13 15:07:09 【问题描述】:

我想创建一个反应角色机器人但是这一行

const maths = message.guild.roles.cache.find(role => role.name === "Maths");

给我这个错误:TypeError: Cannot read properties of undefined (reading 'guild')

我该如何解决这个问题? 谢谢!

index.js:

const client = new Discord.Client( intents: ["GUILDS", "GUILD_MESSAGES"], partials: ["MESSAGE", "CHANNEL", "REACTION"] )
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("message", 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);

reactionrole.js:

    name: 'reactionrole',
    description: "Sets up a reaction role message",
    async execute(message, args, Discord, client) 
        const channel = "889087325244629012";
        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(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;
            
        );
    

【问题讨论】:

欢迎来到 ***,请用英文写下您的问题,以便更多人帮助您 我怎样才能改变它? 您的帖子下方有一个小按钮,上面写着“编辑” 谢谢我已经改了 【参考方案1】:

您正在尝试访问未定义对象的属性。

如果你尝试这样做,你会得到一个错误。

const someObject = undefined

someObject.someProperty

对于这些类型的情况,你需要确保你的函数的参数是正确的。

看看这一行

client.commands.get("reactionrole").execute(message. args, Discord, client);

您使用的是. 而不是,,因此您将 3 个变量传递给此函数:message.argsDiscordclient,但您必须传递 4 个变量:

client.commands.get("reactionrole").execute(message, args, Discord, client);

【讨论】:

【参考方案2】:

message 在此上下文中似乎未定义,这意味着它不包含数据。似乎在您以反应角色调用函数的索引文件中,您使用句点而不是逗号来定义参数,导致未定义的值。

if (command === "reactionrole") 
    client.commands.get("reactionrole").execute(message, args, Discord, client);
                                 // The error was here ^
                                 // This was a period instead of a comma.

如果你是 JS 新手,我建议你使用像 ESLint 这样的 linter,它会在你的编辑器中向你指出这样的小错误。

【讨论】:

以上是关于TypeError:无法读取未定义的属性(读取“公会”)的主要内容,如果未能解决你的问题,请参考以下文章

TypeError:无法在 gitlab 中读取未定义的属性(读取“读取”)

TypeError:无法读取未定义的属性(读取“问题”)

TypeError:无法读取未定义的属性(读取“匹配”):

TypeError:无法读取未定义的属性“findAll”(expressjs)

ReactJS TypeError:无法读取未定义的属性(读取'main')

TypeError:无法读取未定义的属性(读取“国家”)