如何使我的机器人将信息发送到Discord中的某个频道

时间:2019-02-08 23:20:16

标签: node.js discord discord.js

我希望我的机器人将消息发送到不和谐服务器中的某个频道。

所以我想运行一个像-announce [my message here]这样的命令,它将把它发送到我服务器中名为Announcements的通道中。

我已经尝试过类似return message.channels.get("336313710106902529").send(announceembed)这样的操作,但这只是在控制台中给我一个错误。

`const msg = message.content     constannouncechannel = bot.channels.get(“ 336313710106902529”);

if(cmd === `${prefix}announce`){
    let announceembed = new Discord.RichEmbed()
    .setTitle(":flag_jp: **Announcement** :flag_jp:")
    .setDescription(msg)
    .setColor("#ff0000");

    return message.channels.get("336313710106902529").send(announceembed)

}

`

我的机器人的预期结果是将消息发送到公告频道。 除了它给我以下错误:

    at Client.bot.on (C:\Users\craig\OneDrive\Documents\Visual Studio Code\bot\index.js:65:33)
    at Client.emit (events.js:189:13)
    at MessageCreateHandler.handle (C:\Users\craig\OneDrive\Documents\Visual Studio Code\bot\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (C:\Users\craig\OneDrive\Documents\Visual Studio Code\bot\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
    at WebSocketConnection.onPacket (C:\Users\craig\OneDrive\Documents\Visual Studio Code\bot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (C:\Users\craig\OneDrive\Documents\Visual Studio Code\bot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
    at WebSocket.onMessage (C:\Users\craig\OneDrive\Documents\Visual Studio Code\bot\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:189:13)
    at Receiver._receiver.onmessage (C:\Users\craig\OneDrive\Documents\Visual Studio Code\bot\node_modules\ws\lib\websocket.js:137:47)
    at Receiver.dataMessage (C:\Users\craig\OneDrive\Documents\Visual Studio Code\bot\node_modules\ws\lib\receiver.js:409:14)
(node:896) 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(). (rejection id: 1)
(node:896) [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.```


1 个答案:

答案 0 :(得分:0)

<Message>没有channels属性,只有channel这是消息来自的渠道。

您正在寻找的是<Client>.channels,您将这样使用:

const msg = message.content; 
const announceChannel = bot.channels.get("336313710106902529");

if(cmd === `${prefix}announce`){
    let announceEmbed = new Discord.RichEmbed()
    .setTitle(":flag_jp: **Announcement** :flag_jp:")
    .setDescription(msg)
    .setColor("#ff0000");

    return announceChannel.send(announceEmbed);
}