I am currently working, on a rewrite of my discord bot from js to ts. To use typing, I extend the discord.js client, I can not fetch channels and send a message there. How can I work around this, besides using type:any?
my extended client:
export class DiscordClient extends Client {
commands
config
}
This does not work anymore with the Client
type set to DiscordClient
.
If I set it to any, it works just fine
client.channels.cache
.find((channel) => channel.id == client.config.ids.channelIDs.dev.botTestLobby)
.send({ embeds: [loginMessage] })
CodePudding user response:
There can be many types of channels (text, voice, etc.), and TypeScript doesn't know which type of channel you're .find
ing, and not every channel type has the .send
function (you can't send messages in voice channels).
To go around this, if you're sure the channel ID you've stored in your client.config
is a text channel ID, you can use type assertion to change that variable's type to TextChannel
.
Here's how it would look:
const channel = client.channels.cache.find((channel) => channel.id == client.config.ids.channelIDs.dev.botTestLobby) as TextChannel
channel.send({ embeds: [loginMessage] })