我想创建一个 react Angular 色机器人但是这条线const maths = message.guild.roles.cache.find(role => role.name === "Maths");
给我这个错误:类型错误:无法读取未定义的属性(读取“公会”)
我该如何解决这个问题?
谢谢 !
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);
react Angular 色.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;
}
});
}
}
最佳答案
您正在尝试访问 undefined object 的属性。
如果你尝试这样做,你会得到一个错误。
const someObject = undefined
someObject.someProperty
对于这些类型的情况,您需要确保您的函数的参数是正确的。
看这条线
client.commands.get("reactionrole").execute(message. args, Discord, client);
您正在使用 .
而不是 ,
所以你将 3 个变量传递给这个函数:message.args
, Discord
, client
,但您必须传递 4 个变量:client.commands.get("reactionrole").execute(message, args, Discord, client);
关于javascript - 类型错误 : Cannot read properties of undefined (reading 'guild' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69245416/