Numero3bis
Numero3bis

Reputation: 16

discord js message.guild is always null message.member

i'm new with discord.js and i'm having a problem, i want to move to another voice channel the user that message my bot. I tryed many thing already, and every time when i want to get the member from msg it's null, msg.guild is also null and in don't get why

client.on('message', msg => {
    var params = msg.content.split(" ");
    if (params[0] === "!join" && params[1] != null) {
      channel = client.channels.cache.find(c => c.name == params[1] && c.type == "voice");
      member = msg.guild.members.get(msg.authorID);  
      member.voice.setChannel(channel)
    }
});

i tried this also and still : TypeError: Cannot read property 'members' of null

client.on("message", async message => {
  var params = message.content.split(" ");
  if (params[0] === "!join" && params[1] != null) {
    channel = client.channels.cache.find(c => c.name == params[1] && c.type == "voice");
    message.guild.members.fetch(message.authorID)
  }
})

Upvotes: 0

Views: 2975

Answers (4)

Numero3bis
Numero3bis

Reputation: 16

i'm posting this because i found a quick solution (maybe not the best) but if it can help someone

client.on("message", async message => {
  var params = message.content.split(" ");
  if (params[0] === "!join" && params[1] != null) {
    channel = client.channels.cache.find(c => c.name == params[1] && c.type == "voice"); // get the channel to join 
    member = server.members.cache.find(c => c.id === message.author.id)  // get the member
    member.voice.setChannel(channel);          //set the voice channel for the user  
  }
})

And the "server" var come from a global variable that i have,

server = client.guilds.cache.get('XXXXXXXXXXXXX');

Upvotes: 0

Erkaloth
Erkaloth

Reputation: 33

First of all, maybe try msg.guild.channels.cache.find instead of client, since you're searching for a channel in a guild, not the client.
Second of all, try this for member let member = msg.guild.members.cache.find(m => m.id === msg.author.id).
Each message has a author object that has an ID field, the message doesn't have an authorID, it has author.id

Upvotes: 0

yi fan song
yi fan song

Reputation: 427

A message sent in private message to the both will not have a guild property, there is also the possibility of partials but it is only on opt-in.

Upvotes: 0

chills
chills

Reputation: 131

In the newest version (v12) you should be doing

message.guild.members.fetch(id)

Refer to this guide and a similar Stack Overflow post

Also, make the message event asynchronous and use "message" as the parameter

client.on("message", async message => {}

Refer to docs here

Upvotes: 1

Related Questions