Reputation: 99
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 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;
}
});
}
}
Upvotes: 6
Views: 120057
Reputation: 1019
You are trying to access an undefined object's property.
If you try to do that, you will get an error.
const someObject = undefined
someObject.someProperty
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);
Upvotes: 10
Reputation: 303
message
appears to be undefined in this context, meaning it contains no data. It seems that in the index file where you call the function in reaction roles, you have used a period instead of a comma to define your parameters, resulting in the undefined value.
if (command === "reactionrole") {
client.commands.get("reactionrole").execute(message, args, Discord, client);
// The error was here ^
// This was a period instead of a comma.
}
If you're new to JS, I advise you use a linter like ESLint, which will point out minor errors like this to you in your editor.
Upvotes: 2