简体   繁体   中英

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() .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM