每当有新用户加入服务器(公会)时,我想向“欢迎”文本 channel 发送问候消息。
我面临的问题是,当我找到想要的 channel 时,我将收到类型为 GuildChannel
的 channel 。
由于 GuildChannel
没有 send()
函数,我无法发送消息。但是我找不到找到 TextChannel
的方法,所以我被困在这里。
我怎样才能到达 TextChannel
以便我能够使用 send()
消息?在我现在使用的代码下方:
// 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'
我使用的是 discord.js 11.3.0 版
最佳答案
多亏了这个 GitHub issue,我找到了解决问题的方法。
我需要使用 Type Guard 来缩小正确类型的范围。
我现在的代码是这样的:
// 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.`);
关于node.js - 使用 typescript 将消息发送到特定 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53563862/