使用 TypeScript 在 Discord Bot 上开始/按计划发送消息
Sending messages on Discord Bot start / on schedule using TypeScript
我一直在用 TypeScript 构建一个 Discord Bot 供我自己和一群朋友使用。我正在尝试在客户端准备就绪时发送一条消息,该消息完全独立于用户与机器人的任何交互(消息、错误、登录等),因此我希望消息在客户端准备就绪后立即发送。
我已经看到一些关于在客户端就绪和调度时发送消息的解决方案 (Send scheduled message) so that is not a problem but the main problem I have with these solutions is that the discord.js type GuildChannels (https://discord.js.org/#/docs/main/stable/class/GuildChannel) does not actually include the send method unless it's of type TextChannel (https://discord.js.org/#/docs/main/stable/class/TextChannel)。但是,由 client.channels.get(channelId) returns 给出的类型是 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?
类
TextChannel
class 扩展了 GuildChannel
class 扩展了 Channel
,这意味着您可以将它们视为子 class es: Channel
==> GuildChannel
==> TextChannel
.
适用于 Channel
的所有内容也适用于 GuildChannel
,适用于 GuildChannel
的所有内容也适用于 TextChannel
,加上为每个内容列出的附加属性和方法.
更新: 要获得正确的类型 (TextChannel
),您可以使用 TypeGuards,如 .
所示
属性 'message' 不存在
message()
不是有效方法。使用 send()
,像这样:
foxbotChannel.send('I am ready for service!');
或者您也可以使用这样的 ID 查找频道。
我们找到服务器和频道后发送消息。
client.guilds.cache.find(guild => guild.id === 'your serveur id here')
.channels.cache.find(channel => channel.id === 'the channel id here')
.send('bot on')
我一直在用 TypeScript 构建一个 Discord Bot 供我自己和一群朋友使用。我正在尝试在客户端准备就绪时发送一条消息,该消息完全独立于用户与机器人的任何交互(消息、错误、登录等),因此我希望消息在客户端准备就绪后立即发送。
我已经看到一些关于在客户端就绪和调度时发送消息的解决方案 (Send scheduled message) so that is not a problem but the main problem I have with these solutions is that the discord.js type GuildChannels (https://discord.js.org/#/docs/main/stable/class/GuildChannel) does not actually include the send method unless it's of type TextChannel (https://discord.js.org/#/docs/main/stable/class/TextChannel)。但是,由 client.channels.get(channelId) returns 给出的类型是 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?
类
TextChannel
class 扩展了 GuildChannel
class 扩展了 Channel
,这意味着您可以将它们视为子 class es: Channel
==> GuildChannel
==> TextChannel
.
适用于 Channel
的所有内容也适用于 GuildChannel
,适用于 GuildChannel
的所有内容也适用于 TextChannel
,加上为每个内容列出的附加属性和方法.
更新: 要获得正确的类型 (TextChannel
),您可以使用 TypeGuards,如
属性 'message' 不存在
message()
不是有效方法。使用 send()
,像这样:
foxbotChannel.send('I am ready for service!');
或者您也可以使用这样的 ID 查找频道。
我们找到服务器和频道后发送消息。
client.guilds.cache.find(guild => guild.id === 'your serveur id here')
.channels.cache.find(channel => channel.id === 'the channel id here')
.send('bot on')