【问题标题】:client.channels.get(id).send() not workingclient.channels.get(id).send() 不工作
【发布时间】:2021-04-11 14:52:01
【问题描述】:

所以我正在创建的不和谐机器人有一个抽搐通知,它使用 snekfetch 创建一个循环请求函数。在函数返回时,我有 client.channels.get(id).send(embed) 但它不会向我提供的频道 ID 发送消息。

const api = `https://api.twitch.tv/helix/streams?user_login=${streamer}`;

  snekfetch.get(api).set('Client-ID', "XXXXXXXXXXXXXX").then(r => {

    if (r.body.stream === null) {

      setInterval(() => {

        snekfetch.get(api).then(console.log(r.body))

      }, 30000);

    } else {
        const embed = new discord.RichEmbed()
        .setAuthor(
        `${r.body.data.user_name} is live on Twitch`,
      )
        .setThumbnail(`http://static-cdn.jtvnw.net/ttv-boxart/live_user_${streamer}-500x500.jpg`)
        .addField('Views', `${r.body.data.viewer_count}`, true)

      return bot.channels.get(XXXXXXXXXXXX).send("TEST");
    }
  });

我已将我的客户端机器人命名为 bot.channels 而不是 client.channels

理论上它应该将 TEST 消息发送到我给它的任何频道 ID,但是我得到了一个错误。

(node:62565) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined
    at /Users/XXXXXXX/Desktop/HelperBot/main.js:61:50
    at processTicksAndRejections (internal/process/task_queues.js:89:5)
(node:62565) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:62565) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

【问题讨论】:

  • 确保您使用的是字符串作为 ID,而不是数字。 ID 是Snowflakes。
  • XXXXXXXX后面的value格式是什么?你能举个例子,但改变了 ID(将几个数字更改为不同的数字,但不要删除引号或任何其他字符)。如果您没有引号,那么懒惰的建议可能就是这种情况。

标签: javascript node.js discord discord.js


【解决方案1】:

您的错误与并非所有频道都是文本频道有关。在您可以发送消息之前,它必须确保它可以。因此,您可以检查功能 .isText() 是否是这种情况,当您检查它的类型是否应更改为此时为 dm 文本和新闻的文本通道时。那些有 .send 功能

【讨论】:

    【解决方案2】:

    错误说它找不到未定义的东西的.send("Test")函数。

    所以错误来自bot.channels.get(XXXX) bot.channels 返回一个集合。如果您搜索此集合的特定项目。您需要使用.find 而不是.get

    更多关于 .find 的信息在这里:https://discord.js.org/#/docs/main/stable/class/Collection?scrollTo=find

    【讨论】:

    • 直接来自文档中的方法:“Discord.js 中使用的所有集合都使用它们的 id 属性进行映射,如果你想通过 id 查找,你应该使用 get 方法。详情请见MDN。”
    • 就像@slothiful 所说,get 和 find 不会返回集合。它查询“来自集合”方法中的内容。
    猜你喜欢
    • 1970-01-01
    • 2013-08-01
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-15
    • 2019-01-27
    • 2020-03-14
    相关资源
    最近更新 更多