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)
}

the config.SEXY_LOG_CHANNELS is an env var with the channels id seperated by commas.
Was this page helpful?