提问者:小点点

如何使我的机器人在我不和时向某个频道发送消息


我想让我的机器人发送一个消息到我的discord服务器中的某个频道。

所以我想运行一个类似-announce[my message here]的命令,它会将它发送到我的服务器中名为announcements的通道。

我已经尝试了类似于return message.channels.get(“336313710106902529”)。send(announceembed)的操作,但它只是在控制台中给我一个错误。

`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 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个答案

匿名用户

没有channels属性,只有channels,它是消息来自的通道。

您要查找的是.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);
}