【问题标题】:TypeError: fn is not a function when using client.on(guildCreate)类型错误:使用 client.on(guildCreate) 时 fn 不是函数
【发布时间】:2021-04-26 04:59:17
【问题描述】:

当我的机器人加入服务器时,我试图让它向特定公会的频道发送消息,代码如下:

client.on('guildCreate', guild => {
  guild.systemChannel.send('Thank you for adding Optic to your server. Run ``-help`` to view a full list of commands')
  const server = client.guilds.cache.find(myserverId)
  const channel = server.channels.cache.get(channelId)
  const joinEmbed = new Discord.MessageEmbed()
  .setTitle("Joined")
  .setDescription("Optic was added to a server")
  .addFields(
      { name: 'GuildId', value: guild.id,inline: false },
      { name: 'Name', value: guild.name, inline: false },
      { name: 'Guild OwnerId', value: guild.ownerID, inline: false },
  )
  channel.send(joinEmbed)
});

然后它发送这个错误:

UnhandledPromiseRejectionWarning: TypeError: fn is not a function
at Map.find (/app/node_modules/@discordjs/collection/dist/index.js:161:17)
at Client.<anonymous> (/app/main.js:118:38)
at Client.emit (events.js:314:20)
at Object.module.exports [as GUILD_CREATE] (/app/node_modules/discord.js/src/client/websocket/handlers/GUILD_CREATE.js:33:14)
at WebSocketManager.handlePacket (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/app/node_modules/ws/lib/event-target.js:132:16)
at WebSocket.emit (events.js:314:20)
at Receiver.receiverOnMessage (/app/node_modules/ws/lib/websocket.js:825:20)
(node:4) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:4) [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.

我检查了docs for it,我认为我使用正确。我还删除了 guildId 和 channelId 但它们在实际代码中。 谢谢

【问题讨论】:

    标签: node.js discord discord.js


    【解决方案1】:

    我认为您的问题出现是因为您将 ID 放入 find 中,而这并不是 find 所需要的。 &lt;Collection&gt;.get() 会将 ID 作为参数,因此如果您已经知道 ID,则可以使用它。

    例如:

    const server = client.guilds.cache.get("123");
    

    要使用&lt;Collection&gt;.find(),您可以对名称(或ID)执行类似的操作,但如果您知道ID,则最好使用.get()。

    // For a name
    collection.find(guild => guild.name == "Server");
    
    // For an ID
    collection.find(guild => guild.id == "123");
    

    【讨论】:

      猜你喜欢
      • 2020-11-04
      • 2021-01-26
      • 2021-05-23
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-26
      • 2013-04-01
      相关资源
      最近更新 更多