繁体   English   中英

如何让我的机器人向我的 Discord 中的某个频道发送消息

[英]How to make my bot send a message to a certain channel in my Discord

我希望我的机器人向我的 Discord 服务器中的某个频道发送消息。

所以,我想就像运行一个命令-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.```


<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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM