How to send a message to a specific channel

为君一笑 提交于 2020-01-05 05:28:06

问题


I'm trying to send a message to a specific channel with my Discord bot, which is in several servers. I want the bot to pick up on a message from one server and send a message to my personal server, in a specific channel, but I can't get it to 'find' the channel. Has the API changed or something? I tried npm install discord.js to update too.

Code:

if (message.author.id == 'XXXXX' && !mess.includes("Dank") && message.channel.id != 'XXXXX') {
  bot.channels.get('XXXXX').send('memes');
}

I tried a few things but none worked.

TypeError: Cannot read property 'send' of undefined
    at decideIfMention (C:\Users\XXXX\Desktop\Coding Crud\Discord Bot 2\bot.js:80:45)
    at Client.bot.on (C:\Users\XXXX\Desktop\Coding Crud\Discord Bot 2\bot.js:68:3)
    at emitOne (events.js:116:13)
    at Client.emit (events.js:211:7)
    at MessageCreateHandler.handle (C:\Users\XXXX\Desktop\Coding Crud\Discord Bot 2\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (C:\Users\XXXX\Desktop\Coding Crud\Discord Bot 2\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
    at WebSocketConnection.onPacket (C:\Users\XXXX\Desktop\Coding Crud\Discord Bot 2\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (C:\Users\XXXX\Desktop\Coding Crud\Discord Bot 2\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
    at WebSocket.onMessage (C:\Users\XXXX\Desktop\Coding Crud\Discord Bot 2\node_modules\ws\lib\event-target.js:120:16)
    at emitOne (events.js:116:13)

回答1:


Assuming you have the client (which would be an instance of Discord.Client()) try finding the desired channel by using Client.find:

const channel = client.channels.find('name', channelName)
channel.send(message)

If you don't have the client directly but have a message instance, you could always access it from within the Message.client property.




回答2:


Well, if you have the "client" instance of Discord.Client(), then use this:

client.channels.get(`channelID`).send(`Text`)

It's really simple yet precise.



来源:https://stackoverflow.com/questions/51120073/how-to-send-a-message-to-a-specific-channel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!