我在discord机器人上实现功能时遇到了麻烦,除此部分代码给出的错误外,其他一切都很好,并且错误'TypeError:无法读取未定义的属性'roles'
function jailCheck(guild, member) {
if (!member.roles.find(e => e.name.toLowerCase() === "prisonnier")) return;
if (typeof member.voiceChannel !== "undefined" && member.voiceChannel.name !== getJailChannel(guild).name) {
member.setVoiceChannel(getJailChannel(guild))
.catch(err => {
console.log("Failed to move member: " + err);
});
}
}
我尝试使用其他方式从我在网站上发现的类似问题中查找会员的角色,但仍然没有运气。我该如何解决?
这是完整的代码:
function getJailChannel(guild) {
return guild.channels.find(channel => channel.type === "voice" && channel.name.toLowerCase() === "Jaill");
}
function jailCheck(guild, member) {
if (!member.roles.find(e => e.name.toLowerCase() === "prisonnier")) return;
if (typeof member.voiceChannel !== "undefined" && member.voiceChannel.name !== getJailChannel(guild).name) {
member.setVoiceChannel(getJailChannel(guild))
.catch(err => {
console.log("Failed to move member: " + err);
});
}
}
client.on("message", async function(message) {
if (message.channel.type !== "text") return;
if (message.author.bot) return;
if (message.content.startsWith(prefix + "jail")) {
var member = message.mentions.members.first();
if (typeof member === "undefined") {
message.channel.send('You must mention a valid user!');
return;
}
var jailedRole = message.guild.roles.find(role => role.name.toLowerCase() === "jailedd");
if (jailedRole === null) {
message.channel.send('This guild does not have a \"Prisonnier\" role!');
return;
}
if (!member.manageable) {
message.channel.send('I do not have the permissions necessaryy to punish this member!');
return;
}
member.roles.forEach(role => {
member.removeRole(role.id)
.catch(err => {
console.log("Failed to remove role: " + err);
});
});
member.addRole(jailedRole.id)
.catch(err => {
console.log("Failed to add role: " + err);
});
message.channel.send("Jailed " + member.displayName + "!");
jailCheck(message.guild, member);
};
});