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

时间:2018-11-23 13:40:58

标签: javascript bots discord discord.js

我已经搜索并在Reddit和StackOverflow上找到了多个论坛帖子,用户正在询问如何向特定频道发送消息,但是我找不到可以使用Argument发送至特定频道的帖子。我的意思是您使用

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

我一直在使用此“功能”进行测试,并设法将消息发送到特定的频道,但是它也包含arg [0],也就是频道ID。命令是

announce "CHANNEL ID" "MESSAGE"

它确实将嵌入的消息发送到我输入的特定频道,但是它向嵌入中添加了CHANNEL ID,所以我尝试在嵌入的arg[0]中使用.setDescription(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.

1 个答案:

答案 0 :(得分:3)

您可以使用message.mentions属性。因此,您将执行以下操作:

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

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