根据参数将消息发送到特定频道

Sending message to specific channel based on an argument

我在 Reddit 和 Whosebug 上搜索并找到了多个论坛帖子,用户在这些帖子中询问如何将消息发送到特定频道,但我找不到可以使用参数发送到特定频道的帖子。我的意思是你使用

return bot.channels.get(channel).send(embed);

我一直在测试这个 "function" 并设法将消息发送到特定频道,但它还包括 arg[0] 也就是频道 ID。命令是

announce "CHANNEL ID" "MESSAGE"

它确实将带有消息的嵌入发送到我输入的那个特定频道,但是它将 CHANNEL ID 添加到嵌入中,所以我尝试在嵌入的 [=16= 中使用 arg[0] ] 但是没有用。它向我吐出一条错误消息,我不知道它是什么意思。但如果你是专业人士,也许你知道我能做什么。这是整个命令代码:

if (cmd === `${prefix}announce`) {
  console.log(message.author.username + " executed an Announcement in the channel #" + message.channel.name);
  const embed = new Discord.RichEmbed()
    .setColor("#e56b00")
    .setAuthor("Announcement from " + message.author.username, message.author.avatarURL)
    .setDescription(arg)
    .setFooter(message.author.username)
    .setTimestamp();

  return bot.channels.get(channel).send(embed);
}

这是错误代码。请注意,仅当我将 arg[0] 放入嵌入的 .setDescription() 部分时才会弹出错误。频道让 arg[1]

正常工作
(node:7900) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined
    at Client.bot.on (C:\Users\Admin\Desktop\Discord Bots\DISCORDBOSS.js\index.js:32:34)
    at Client.emit (events.js:182:13)
    at MessageCreateHandler.handle (C:\Users\Admin\Desktop\Discord Bots\DISCORDBOSS.js\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (C:\Users\Admin\Desktop\Discord Bots\DISCORDBOSS.js\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
    at WebSocketConnection.onPacket (C:\Users\Admin\Desktop\Discord Bots\DISCORDBOSS.js\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (C:\Users\Admin\Desktop\Discord Bots\DISCORDBOSS.js\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
    at WebSocket.onMessage (C:\Users\Admin\Desktop\Discord Bots\DISCORDBOSS.js\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:182:13)
    at Receiver._receiver.onmessage (C:\Users\Admin\Desktop\Discord Bots\DISCORDBOSS.js\node_modules\ws\lib\websocket.js:137:47)
    at Receiver.dataMessage (C:\Users\Admin\Desktop\Discord Bots\DISCORDBOSS.js\node_modules\ws\lib\receiver.js:409:14)
(node:7900) 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:7900) [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.mentions 属性。所以你会做以下事情:

let announceChannel = message.mentions.channels.first();

然后发送消息执行以下操作; message.guild.channels.find(t => t.id == announceChannel.id).send(myMessage);