Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send message to specific channel with typescript

I want to send a greeting message to an "welcome" text channel, whenever a new user joins the server (guild).

The problem I'm facing is that, when I find the wanted channel, I will receive the channel with the type GuildChannel.

Since GuildChannel has no send() function, I'm not able to send the message. But I can't find a way to find the TextChannel, so I'm stuck here.

How can I get to the TextChannel so that I'm able to use the send() message? Below the code I'm using by now:

// Get the log channel (change to your liking) 
const logChannel = guild.channels.find(123456); 
if (!logChannel) return;

// A real basic message with the information we need. 
logChannel.send('Hello there!'); // Property 'send' does not exist on type 'GuildChannel'

I'm using version 11.3.0 of discord.js

like image 222
KhorneHoly Avatar asked Oct 27 '25 01:10

KhorneHoly


2 Answers

Thanks to this GitHub issue I've found the solution to my problem.

I need to use a Type Guard to narrow down the correct type.

My code now is this:

// Get the log channel
const logChannel = member.guild.channels.find(channel => channel.id == 123456);

if (!logChannel) return;

// Using a type guard to narrow down the correct type
if (!((logChannel): logChannel is TextChannel => logChannel.type === 'text')(logChannel)) return;

logChannel.send(`Hello there! ${member} joined the server.`);
like image 124
KhorneHoly Avatar answered Oct 28 '25 14:10

KhorneHoly


Maybe for latecomers who are still looking for an answer this worked for me

let channel = client.channels.get("channelid") as Discord.TextChannel;
channel.send("what you want to send to that channel");
like image 44
Cinition Avatar answered Oct 28 '25 16:10

Cinition



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!