Discord.js使用漫游器将消息发送到特定频道

时间:2020-03-13 06:35:49

标签: node.js bots discord.js

我一直在尝试使用 discord bot 将消息发送到特定频道,并且我尝试了很多在线示例,但都没有用并每次都给出相同的错误。
以下是代码实现:

client.on('ready', () => {
  console.log('Bot is now connected')
  try {
    client.channels.find('test').send("Hello there!");
  } catch (e) {
    console.log('[ERROR:]',e);
  }
});

但是每次出现相同的错误消息:

[ERROR:] TypeError: client.channels.find is not a function
    at Client.client.on (/Users/Kartikeya 1/discordBot/index.js:9:34)
    at Client.emit (events.js:198:13)
    at WebSocketManager.triggerClientReady (/Users/Kartikeya 1/discordBot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:433:17)
    at WebSocketManager.checkShardsReady (/Users/Kartikeya 1/discordBot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:417:10)
    at WebSocketShard.shard.on.unavailableGuilds (/Users/Kartikeya 1/discordBot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:199:14)
    at WebSocketShard.emit (events.js:198:13)
    at WebSocketShard.checkReady (/Users/Kartikeya 1/discordBot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:466:12)
    at WebSocketShard.onPacket (/Users/Kartikeya 1/discordBot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:438:16)
    at WebSocketShard.onMessage (/Users/Kartikeya 1/discordBot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:293:10)
    at WebSocket.onMessage (/Users/Kartikeya 1/discordBot/node_modules/ws/lib/event-target.js:120:16)

任何人都可以让我知道缺少或未执行哪些步骤。
我正在使用以下节点版本: v10.16.3

1 个答案:

答案 0 :(得分:0)

Discord通道具有许多参数,因此您需要指定test参数。

我建议您不要按名称搜索频道,最好使用ID。像

client.channels.get('ID') or client.channels.cache.get('ID')

    client.on('ready', () => {
      console.log('Bot is now connected')
        client.channels.find(channel => channel.name === 'test').send("Hello there!"); // for discord v11
        client.channels.cache.find(channel => channel.name === 'test').send("Hello there!"); // for discord v12
    });

您可以使用npm list discord.js来检查discord.js版本

用于频道列表

let channelsList = ['ID1', 'ID2', 'ID3']
channelsList.forEach(channel => {
    let targetChannel = client.channels.get(channel)
    if (targetChannel) {
        targetChannel.send('TEXT').then(() => {
            console.log(`succes send message to channel ${targetChannel.name}`)
        }).catch(err => console.error)
    }
})