[英]discord.js Cannot read property 'guild' of undefined
I've already looked through a few tutorials and pages, but couldn't find a working version to check whether the author has the appropriate role.我已经浏览了一些教程和页面,但找不到工作版本来检查作者是否具有适当的角色。
My last tryed code:我最后尝试的代码:
const fs = require('fs');
const Discord = require('discord.js');
const {
token
} = require('./config.json');
const client = new Discord.Client();
//functions
function loadServer() {
server = getJ('./server.json');
}
function abnahme(message){
let charRole = message.guild.roles.find("name", "Character-Abnehmer");
if (message.content == 'accepted' && message.member.roles.has(charRole)) {
//code....
}else {
return;
}
}
//run
client.once('ready', () => {
console.log('Ready!');
});
//events
loadServer();
client.on('message', message => {
if (message.content.startsWith(server.prefix)) {
const args = message.content.slice(server.prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
message.channel.send(`\`command: ${command} args: ${args}\``);
if(command == 'abnahme'){
abnahme(message);
}
}
});
client.login(token);
Can someone give me an example of how I can just query the role?有人可以举个例子来说明我如何查询角色吗?
The easiest way to see if a member has a role is with GuildMember.roles.cache.has() .查看成员是否具有角色的最简单方法是使用GuildMember.roles.cache.has() 。
Use this code使用此代码
client.on('message', async msg => {
let memberHasRole = msg.member.roles.cache.has('role-id');
//true if member has the role, false if they don’t
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.