Aircraft Overviewer
Aircraft Overviewer

Reputation: 55

How to send message to specified Channel - Discord.js v13 TypeScript

I have recently started using TypeScript, and as you are all surely aware Discord.js has recently moved to v13. Following this I have been struggling to find a way to send messages to a specified channel using a given Channel ID. Here is the current code I use:

// Define Channel ID
const messageChannelId = 'CHANNEL_ID';

// Define Channel
const messageChannel = client.channels.cache.get(messageChannelId);

// Send Message to Channel
if (messageChannel && messageChannel.type === 'GUILD_TEXT') messageChannel.send('Hello World');

Oddly enough, the following code works fine and it sends the message 'Hello World' to the channel, but I always end up with an intellisense error when I hover over the send method that says Property 'send' does not exist on type 'Channel' in Visual Studio Code. If anyone knows why this happens, or has a solution to this error, please let me know. The documentation for Discord.js does not show the send method on the Channel type, but still allows it to work and I do not know a way around this.

Thanks for any assistance.

Upvotes: 1

Views: 2761

Answers (1)

MrMythical
MrMythical

Reputation: 9041

The send method is not on type Channel. It is on type TextChannel. client.channels.cache.get returns a Channel as it could be a voice channel too! You will have to add as TextChannel to remove that error

const { TextChannel } = require('discord.js')
// Define Channel
const messageChannel = client.channels.cache.get(messageChannelId) as TextChannel;

Upvotes: 3

Related Questions