Rankail
Rankail

Reputation: 13

Discord bot js - Cant get channel by id

This is my code. The output of 'channel' is always undefined. Does someone know the problem?

var channelId = "myId";

const client = new Discord.Client();

client.login(token);

var channel = client.channels.cache.get(channelId);
console.log(channel);

Upvotes: 1

Views: 60

Answers (1)

Shoejep
Shoejep

Reputation: 4839

client.login(token) returns a promise so it's highly unlikely that you're authenticated when client.channels.cache.get(channelId) runs.

You should try changing your code to the below, so that you get your channel after you've authenticated.

client.login(token).then(x => { 
    var channel = client.channels.cache.get(channelId);
    console.log(channel);
});

Upvotes: 1

Related Questions