Kho*_*oly 3 node.js typescript discord.js
每当新用户加入服务器(公会)时,我都想向“欢迎”文本频道发送问候消息。
我面临的问题是,当我找到想要的频道时,我会收到类型为 的频道GuildChannel。
由于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'
Run Code Online (Sandbox Code Playgroud)
我正在使用 discord.js 的 11.3.0 版
感谢这个GitHub 问题,我找到了解决我的问题的方法。
我需要使用类型保护来缩小正确的类型。
我现在的代码是这样的:
// 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.`);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3654 次 |
| 最近记录: |