簡體   English   中英

TypeError: fn is not a function when using client.on(guildCreate)

[英]TypeError: fn is not a function when using client.on(guildCreate)

當我的機器人加入服務器時,我試圖讓它向特定公會的頻道發送消息,這是代碼:

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.

我檢查了它的文檔,我認為我正確使用了它。 我還刪除了 guildId 和 channelId 但它們在實際代碼中。 謝謝

我認為您的問題出現是因為您將 ID 放入 find 中,而這並不是 find 所需要的。 <Collection>.get()將 ID 作為參數,因此如果您已經知道 ID,那么您可以使用它。

例如:

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

要使用<Collection>.find()您可以對名稱(或 ID)執行類似的操作,但如果您知道 ID,則最好使用.get()代替。

// For a name
collection.find(guild => guild.name == "Server");

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM