【发布时间】:2019-09-22 12:06:07
【问题描述】:
嗨,我一直在用 TypeScript 构建一个 Discord Bot,供我自己和一群朋友使用。我正在尝试在客户端准备好时发送一条消息,该消息完全独立于用户与机器人的任何交互(消息、错误、登录等),因此我希望在客户端准备好后立即发送消息。
我已经看到一些解决方案已经在客户端准备好和调度 (discord.js send scheduled message) 上发送消息,所以这不是问题,但我对这些解决方案的主要问题是 discord.js 类型 GuildChannels (https://discord.js.org/#/docs/main/stable/class/GuildChannel ) 实际上不包括 send 方法,除非它是 TextChannel (https://discord.js.org/#/docs/main/stable/class/TextChannel) 类型。但是,client.channels.get(channelId) 给出的类型返回 GuildChannel(可能是文本类型)。
所以我的代码示例如下所示。
import { Client } from 'discord.js';
import { BOT_SECRET_TOKEN, FOX_GUILD_ID, FOXBOT_CHANNEL } from './secret.json';
const client = new Client();
client.on('ready', () => {
console.log(`Connected as ${client.user.tag}`);
const foxGuild = client.guilds.get(FOX_GUILD_ID);
if (!foxGuild) {
console.log('Guild not found');
return;
}
const foxbotChannel = foxGuild.channels.get(FOXBOT_CHANNEL);
if (!foxbotChannel) {
console.log('Channel not found');
return;
}
foxbotChannel.message('I am ready for service!');
});
线
foxbotChannel.message('I am ready for service!');
会给我这个错误
src/index.ts(26,17): error TS2339: Property 'message' does not exist on type 'GuildChannel'.
我也尝试过导入 TextChannel 并像这样启动 foxbotChannel
foxbotChannel: TextChannel = foxGuild.channels.get(FOXBOT_CHANNEL);
但也得到一个错误,说 GuildChannel 类型缺少一堆属性。
所以我的问题是,如何将 GuildChannel 转换为 TextChannel 以便能够通过它发送消息,或者如何通过客户端找到 TextChannel?
【问题讨论】:
标签: typescript discord.js