TypeError: Cannot read properties of undefined (reading ‘guild’)

Total
0
Shares

I want to create a reaction role bot but this line

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

puts me this error : TypeError: Cannot read properties of undefined (reading ‘guild’)

how i can fix this ?
Thanks !

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 correspondnn"
            + `${mathsEmoji} : Mathsn`
            + `${mathsExpertesEmoji} : Maths Expertesn`
            + `${nsiEmoji} : NSIn`
            + `${physiqueEmoji} : Physiquen`
            + `${svtEmoji} : SVTn`
            + `${artsPlastiquesEmoji} : Arts Plastiquesn`);

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

Solution

You are trying to access an undefined object’s property.

If you try to do that, you will get an error.

_x000D_

_x000D_

const someObject = undefined

someObject.someProperty

_x000D_

_x000D_

_x000D_

For those types of situations, you need to be sure that your function’s parameters are correct.

Look at this line

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

You are using . instead of , so you are passing 3 variables to this function: message.args, Discord, client, but you must pass 4 variables:

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

Source: StackOverflow.com

Leave a Reply

Your email address will not be published. Required fields are marked *