Doesn't fetch the channel, why?

const channel = client.channels.fetch("1198299044582281216", { force: true });
console.log(channel.name);
const channel = client.channels.fetch("1198299044582281216", { force: true });
console.log(channel.name);
12 Replies
d.js toolkit
d.js toolkit12mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
zevnda
zevnda12mo ago
You need to await the promise returned when fetching
Tissemyren
TissemyrenOP12mo ago
then it just sets channel to null
zevnda
zevnda12mo ago
Show me how you're doing it
Tissemyren
TissemyrenOP12mo ago
const channel = await client.channels.fetch("1198299044582281216", { force: true });
console.log(channel.name);
const channel = await client.channels.fetch("1198299044582281216", { force: true });
console.log(channel.name);
zevnda
zevnda12mo ago
Show the full code where you're doing this
Tissemyren
TissemyrenOP12mo ago
const { EmbedBuilder, WebhookClient } = require("discord.js");

module.exports = async (client) => {
console.log("Loaded: Message Logger");

try {
const webhook = new WebhookClient({ id: '', token: '' });
const embed = new EmbedBuilder()
const channel = await client.channels.fetch("1198299044582281216", { force: true });
console.log(channel.name);

client.on("messageDelete", function(message) {
if (message.author.bot) return;


})

client.on("messageUpdate", function(oldMessage, message) {
if (message.author.bot) return;


})
} catch(error) {
console.error(error);
}
};
const { EmbedBuilder, WebhookClient } = require("discord.js");

module.exports = async (client) => {
console.log("Loaded: Message Logger");

try {
const webhook = new WebhookClient({ id: '', token: '' });
const embed = new EmbedBuilder()
const channel = await client.channels.fetch("1198299044582281216", { force: true });
console.log(channel.name);

client.on("messageDelete", function(message) {
if (message.author.bot) return;


})

client.on("messageUpdate", function(oldMessage, message) {
if (message.author.bot) return;


})
} catch(error) {
console.error(error);
}
};
zevnda
zevnda12mo ago
Do it in an event once the bot has connected and is in a ready state
d.js docs
d.js docs12mo ago
:event: (event) Client#ready Emitted when the client becomes ready to start working.
zevnda
zevnda12mo ago
And also make sure you reset your token now
Tissemyren
TissemyrenOP12mo ago
👍🏼
ahmood
ahmood12mo ago
You also don't need to force fetch a channel since channels are cached by default (except threads and dm channels)

Did you find this page helpful?