【问题标题】:Sending messages on Discord Bot start / on schedule using TypeScript使用 TypeScript 在 Discord Bot 开始/按时发送消息
【发布时间】: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


    【解决方案1】:

    TextChannel 类扩展了 GuildChannel 类,后者扩展了 Channel,这意味着您可以将它们视为子类:Channel ==> GuildChannel ==> TextChannel

    适用于Channel 的所有内容都适用于GuildChannel,适用于GuildChannel 的所有内容都适用于TextChannel,以及为每个属性和方法列出的其他属性和方法。

    更新:要获得正确的类型 (TextChannel),您可以使用 TypeGuards,如 this solution 所示。


    属性“消息”不存在

    message() 不是有效的方法。使用send(),像这样:

    foxbotChannel.send('I am ready for service!');
    

    【讨论】:

    • 哦,所以我刚刚尝试使用 send 但我仍然收到错误,因为 send 是一个 TextChannel 方法。由于 TextChannel 是 GuildChannel 的子类,这意味着 TextChannel 将具有 GuildChannel 没有的其他方法, send 是其中之一吗?我仍然收到src/index.ts(26,17): error TS2339: Property 'send' does not exist on type 'GuildChannel'.
    • 正确。见here
    • 你使用的是什么版本的 Discord.js?
    • 我目前正在为 Discord.js 使用 11.4.2。所以现在在这种情况下,由于 foxbotChannel 是 GuildChannel,有没有办法可以使用它来创建 TextChannel?
    • 您能否确认您使用的是字符串而不是数字?另外,请确保频道存在。不幸的是,我无法通过我的测试重现这一点。如果频道是文本频道,channels.get()应该返回TextChannel
    【解决方案2】:

    或者你也可以使用这样的 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')
    

    如需更多帮助,请加入我的 discord:https://discord.gg/eJYgEMFW2b

    client.guilds.cache.find(guild => guild.id === 'your serveur id here')
      .channels.cache.find(channel => channel.id === 'the channel id here')
      .send('bot on')
    

    【讨论】:

      猜你喜欢
      • 2020-12-01
      • 2021-03-02
      • 2021-01-12
      • 2021-06-05
      • 2018-05-03
      • 2020-08-31
      • 2020-12-03
      • 2021-09-17
      • 2021-06-17
      相关资源
      最近更新 更多