Send message to specific channel

Hi i'm trying to get a list of textchannels to log to. But when i use client.channels.fetch i keep getting 'null'? here's my code.
const channels: TextChannel[] = []
for (const i of config.SEXY_LOG_CHANNELS.split(",")) {
let channel: Channel | null | undefined;
try {
channel = client.channels.fetch(i);
} catch (e) {
if ((e as DiscordAPIError).code === 50001) console.error("SexyUP: Bot doesn't have access to this channel.", i)
continue
}
if (!channel || channel === null) {
console.error("SexyUP: Channel wasn't found.", i, i === null)

continue
}
if (!(channel instanceof TextChannel)) {
console.error("SexyUP: Message isn't a text channel", i)
continue
}
if (!client.user) {
console.error("SexyUP: Something went terribly wrong, the bot user doesn't exist? Very weird.")
continue
}

const permissions = channel.permissionsFor(client.user);
if (!permissions || !permissions.has("SendMessages")) {
console.error("SexyUP: No permissions to send messages in this channel. ", i, permissions)
continue
}

channels.push(channel)
}
const channels: TextChannel[] = []
for (const i of config.SEXY_LOG_CHANNELS.split(",")) {
let channel: Channel | null | undefined;
try {
channel = client.channels.fetch(i);
} catch (e) {
if ((e as DiscordAPIError).code === 50001) console.error("SexyUP: Bot doesn't have access to this channel.", i)
continue
}
if (!channel || channel === null) {
console.error("SexyUP: Channel wasn't found.", i, i === null)

continue
}
if (!(channel instanceof TextChannel)) {
console.error("SexyUP: Message isn't a text channel", i)
continue
}
if (!client.user) {
console.error("SexyUP: Something went terribly wrong, the bot user doesn't exist? Very weird.")
continue
}

const permissions = channel.permissionsFor(client.user);
if (!permissions || !permissions.has("SendMessages")) {
console.error("SexyUP: No permissions to send messages in this channel. ", i, permissions)
continue
}

channels.push(channel)
}
the config.SEXY_LOG_CHANNELS is an env var with the channels id seperated by commas.
21 Replies
d.js toolkit
d.js toolkit3d 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! - Marked as resolved by OP
jsw
jswOP3d ago
I've also tried out client.channels.cache.get(i); and same thing with that one.
treble/luna
treble/luna3d ago
first of all, all channels are cached by the Guilds intent, no need to fetch second, check if your ids are valid eg they arent stored as numbers and log them
jsw
jswOP3d ago
I'll try that thanks The channel ids are correct Also have the guild intent
export const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages,
],
});
export const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages,
],
});
jsw
jswOP3d ago
and gave it permissions to the channel
No description
treble/luna
treble/luna3d ago
the client has access to all channels regardless of permissions if the cache returns nothing, the id is invalid or the channel belongs to a guild the bot isnt in not with the bot scope at least
jsw
jswOP3d ago
I'll try to kick and invite it again to my server
jsw
jswOP3d ago
it has the bot scope but it's till not returning the channel
No description
jsw
jswOP3d ago
when i right click the channel and press copy id i get
1320365173336178750
1320365173336178750
which is the same as the id in the logs
SexyUP: Channel wasn't found. 1320365173336178750 false
SexyUP: Channel wasn't found. 1320365173336178750 false
the weird part is it that it says that it isn't null (that's the 'false' at the end of the log)
if (!channel || channel === null) {
console.error("SexyUP: Channel wasn't found.", i, i === null)
continue
}
if (!channel || channel === null) {
console.error("SexyUP: Channel wasn't found.", i, i === null)
continue
}
jsw
jswOP3d ago
but it's undefined
No description
jsw
jswOP3d ago
oh wait nvm that isn't weird the null part was only for .fetch and not .cache.get
treble/luna
treble/luna3d ago
when does this code run?
jsw
jswOP3d ago
at command initalisation ooooh i'm such a moron
treble/luna
treble/luna3d ago
yeah your client isnt ready yet
jsw
jswOP3d ago
that's before the client logs in thank you sorry for wasting your time lol
treble/luna
treble/luna3d ago
no worries
jsw
jswOP3d ago
i have one final question does registering commands need to happen before logging the client in?
treble/luna
treble/luna3d ago
it doesnt need to happen when logging it at all its supposed to be a separate file that you run when you make changes to your command data
jsw
jswOP3d ago
ohh
treble/luna
treble/luna3d ago
running it every time your bot starts will lead to ratelimits
jsw
jswOP3d ago
okay thanks

Did you find this page helpful?