Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
117
rated 0 times [  119] [ 2]  / answers: 1 / hits: 69441  / 6 Years ago, tue, may 22, 2018, 12:00:00

I'm trying to make a discord bot with DiscordJS


I want to fetch a specific channel :


    const Discord = require('discord.js');

var bot = new Discord.Client();
var myToken = 'NDQ2OTQ1...................................................';

var channelFind = bot.channels.find("id","XXXXXXXXXXX"); // null
var channelGet = bot.channels.get("id","XXXXXXXXXXX"); // undefined

Whenever I run my bot, it returns null with find and undefined with get.
I also tried to find my channel by name


More From » node.js

 Answers
4

Make sure you've placed your "find" line within an event listener.
For example, let's say you wanted to find the specific channel when the bot connects successfully:


bot.on('ready', () => {
console.log(`Logged in as ${bot.user.tag}!`);
var offTopic = bot.channels.get("448400100591403024");
console.log(offTopic);
});

Of course, there are a huge range of event listeners available, which will suit the scenario in which you want to find the channel. You'll find event listeners in the Events section of the Client Class in the DiscordJS docs.
Try this out, and let me know if it works.


Update: 2022


In discord.js v12 and above, bot.channels.get(); should now be bot.channels.cache.get();


[#54375] Friday, May 18, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
breanab

Total Points: 731
Total Questions: 95
Total Answers: 79

Location: Liberia
Member since Fri, Oct 22, 2021
3 Years ago
;