'Sending messages on Discord Bot start / on schedule using TypeScript

I've been building a Discord Bot in TypeScript for myself and a group of friends to use. I'm trying to send a message on client ready that's completely independent of any interactions with the bot that the users have (messages, errors, logging in, etc) so I want the message to send as soon as the client is ready.

I've seen some solutions already on sending messages on client ready and scheduling (Send scheduled message) so that is not a problem but the main problem I have with these solutions is that the discord.js type GuildChannels (https://discord.js.org/#/docs/main/stable/class/GuildChannel) does not actually include the send method unless it's of type TextChannel (https://discord.js.org/#/docs/main/stable/class/TextChannel). However, the type that's given by client.channels.get(channelId) returns a GuildChannel (that's possibly of type text).

So an example of my code looks like this.

import { Client } from 'discord.js';

import { BOT_SECRET_TOKEN, FOX_GUILD_ID, FOXBOT_CHANNEL } from './secret.json';

const client = new Client();

client.on('ready', () => {
  console.log(`Connected as ${client.user.tag}`);

  const foxGuild = client.guilds.get(FOX_GUILD_ID);
  if (!foxGuild) {
    console.log('Guild not found');

    return;
  }

  const foxbotChannel = foxGuild.channels.get(FOXBOT_CHANNEL);
  if (!foxbotChannel) {
    console.log('Channel not found');

    return;
  }

  foxbotChannel.message('I am ready for service!');
});

The line

  foxbotChannel.message('I am ready for service!');

Will give me this error

src/index.ts(26,17): error TS2339: Property 'message' does not exist on type 'GuildChannel'.

I have also tried importing TextChannel and initiating foxbotChannel like so

foxbotChannel: TextChannel = foxGuild.channels.get(FOXBOT_CHANNEL);

But also get an error saying that the type GuildChannel is missing a bunch of properties.

So my question is, how do I convert GuildChannel to a TextChannel so that I'm able to send messages through that or, how do I find the TextChannel through client?



Solution 1:[1]

Classes

The TextChannel class extends the GuildChannel class which extends Channel, meaning you could think of them as sub-classes: Channel ==> GuildChannel ==> TextChannel.

Everything that works for Channel will work for GuildChannel, everything that works for GuildChannel will work for TextChannel, plus the additional properties and methods listed for each.

Update: To get the right type (TextChannel), you can use TypeGuards as shown in this solution.


Property 'message' does not exist

message() is not a valid method. Use send(), like so:

foxbotChannel.send('I am ready for service!');

Solution 2:[2]

Or you can just use find a channel with id like this.

We find the server and after the channel and then send a message.

client.guilds.cache.find(guild => guild.id === 'your serveur id here')
  .channels.cache.find(channel => channel.id === 'the channel id here')
  .send('bot on')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Dharman