【问题标题】:Send is not a function discord.js发送不是函数 discord.js
【发布时间】:2021-06-18 22:28:11
【问题描述】:

我正在尝试向频道发送消息,但一直收到错误消息:发送不是函数。我在这个问题上卡了一个多小时。

我尝试过的: 使用发送消息 尝试阅读 discord.js 文档,但它似乎根本不起作用。

这是我的代码:

//Grab the Discord Library
const Discord = require("discord.js");

//What will connect to the server.
const bot = new Discord.Client();

//
bot.on('ready', () => {
  console.log("Connected as " + bot.user.tag)

  //Shows and set the activity of the user.
  bot.user.setActivity("El Professor build me", {type: "Watching"})
  //Inform you of the servers this bot is connected to.
  bot.guilds.forEach((guild) => {
    console.log(guild.name)
    guild.channels.forEach((channel) => {
      console.log(` - ${channel.name} ${channel.type} ${channel.id}`)
    })
    //Voice Channel ID = 
  })
  var generalChannel = bot.channels.get("123456789").send("Hello World")
})

错误信息:

C:\Users\F4_ALFA\documents\FirstDiscordBot\index.js:21
  var generalChannel = bot.channels.get("123456789").send("Hello World")
                                                              ^

TypeError: bot.channels.get(...).send is not a function
    at Client.bot.on (C:\Users\F4_ALFA\documents\FirstDiscordBot\index.js:21:63)
    at Client.emit (events.js:194:15)
    at WebSocketConnection.triggerReady (C:\Users\F4_ALFA\documents\FirstDiscordBot\node_modules\discord.js\src\client\websocket\WebSocketConn
ection.js:125:17)
    at WebSocketConnection.checkIfReady (C:\Users\F4_ALFA\documents\FirstDiscordBot\node_modules\discord.js\src\client\websocket\WebSocketConn
ection.js:141:61)
    at GuildCreateHandler.handle (C:\Users\F4_ALFA\documents\FirstDiscordBot\node_modules\discord.js\src\client\websocket\packets\handlers\Gui
ldCreate.js:13:31)
    at WebSocketPacketManager.handle (C:\Users\F4_ALFA\documents\FirstDiscordBot\node_modules\discord.js\src\client\websocket\packets\WebSocke
tPacketManager.js:103:65)
    at WebSocketConnection.onPacket (C:\Users\F4_ALFA\documents\FirstDiscordBot\node_modules\discord.js\src\client\websocket\WebSocketConnecti
on.js:333:35)
    at WebSocketConnection.onMessage (C:\Users\F4_ALFA\documents\FirstDiscordBot\node_modules\discord.js\src\client\websocket\WebSocketConnect
ion.js:296:17)
    at WebSocket.onMessage (C:\Users\F4_ALFA\documents\FirstDiscordBot\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:189:13)

【问题讨论】:

  • 您确定 ID 指向 TextChannel 吗?您可以通过在频道的服务器中发布<#ID> 来检查它是否解决了提及

标签: discord discord.js


【解决方案1】:

Discord.js 使用缓存并且你的代码没有'.cache',你应该试试

bot.channels.cache.get('id').send('Hello world!')

【讨论】:

    【解决方案2】:

    channels 属性返回一个 Collection 和一对 IDChannel object

    Channel 类可以代表任何不和谐的频道。您无法通过它发送任何消息,因为它没有send() 方法。

    【讨论】:

    • 但是如果你做一个 get 你应该得到一个 Channel 对象
    【解决方案3】:

    我之前用过这个client.guilds.find(x => x.name === "ChatLogs").channels.find(y => y.name === "server-testing1").send(botEmbed)试试这个

    【讨论】:

      猜你喜欢
      • 2021-02-10
      • 2020-12-31
      • 2020-12-07
      • 2020-08-23
      • 2020-01-22
      • 2020-11-13
      • 2017-11-28
      • 2021-06-15
      • 2020-12-22
      相关资源
      最近更新 更多