Javascript 如何向特定频道发送消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51120073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:46:12  来源:igfitidea点击:

How to send a message to a specific channel

javascriptbotschanneldiscord.js

提问by aandamitchell

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.jsto update too.

我正在尝试使用我的 Discord 机器人向特定频道发送消息,该机器人位于多个服务器中。我希望机器人从一台服务器接收消息并将消息发送到我的个人服务器,在特定频道中,但我无法让它“找到”频道。API有变化吗?我也尝试npm install discord.js更新。

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)

回答by GMaiolo

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

假设您有client(这将是 的实例Discord.Client())尝试使用Client.find查找所需的频道:

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

If you don't have the clientdirectly but have a messageinstance, you could always access it from within the Message.clientproperty.

如果您没有client直接的但有一个message实例,您总是可以从Message.client属性中访问它。

回答by The Real Underscore

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

好吧,如果你有 " client" 实例Discord.Client(),那么使用这个:

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

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

It's really simple yet precise.

这真的很简单但很精确。