berensn
berensn

Reputation: 51

Discord: Get channel by ID returns undefined / null

I'm setting up a cron job to run a bot command that unlocks/locks a channel at a certain time every day. Trying to get the channel returns either undefined or null, depending on how I go about it. The bot is added to the discord server and is online

require('dotenv').config();
const Discord = require("discord.js");
const client = new Discord.Client();

client.login(process.env.TOKEN);

const chan = client.channels.cache.get("858211703946084352");
console.log(chan);
const channel = client.channels.fetch("858211703946084352").then(res => {
    console.log(res);
});
console.log(channel);

When I run it in the console I get

undefined
Promise { <pending> }
null

I have looked at many many examples and solutions, but none seem to resolve my issue


Edit:

bot has admin permissions.

I did the 'right click on channel and copy ID' technique, which matched the ID I got when I used dev tools to examine the element containing the channel name

There is a MEE6 bot in the server so I know bots can send messages

Edit2:

For fun and profit I deleted the app and remade it, same issue

I tried using a channel the MEE6 bot sends to, same issue

Upvotes: 1

Views: 3433

Answers (1)

D J
D J

Reputation: 937

Try running this code in the ready event

client.on('ready', () => {
    const chan = client.channels.cache.get("858211703946084352");
    console.log(chan);
});

Upvotes: 4

Related Questions