Sending message to specific channel based on an argument

Tags: , , ,



I have searched up and found multiple forum posts on both Reddit and StackOverflow where users are asking how to send a message to the specific channel, but I cannot find one where you can send to a specific channel using an Argument. What I mean is that you use

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

I have been testing around with this “function” and have managed to send the message to a specific channel, but it also includes the arg[0] aka the channel id. The command is

announce "CHANNEL ID" "MESSAGE"

It does send the embed with the message to that specific channel I input, but It adds the CHANNEL ID to the embed, so I tried to use arg[0] in the embed’s .setDescription(arg[0]) but it didn’t work. It spat out an error message to me which I have no idea what means. But maybe one if you pro’s out there know what I can do. Here is the entire command Code:

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

And here is the error code. Note that the error only pops up when I put arg[0] in the .setDescription() part of the embed. The channel let works fine with arg[1]

(node:7900) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined
    at Client.bot.on (C:UsersAdminDesktopDiscord BotsDISCORDBOSS.jsindex.js:32:34)
    at Client.emit (events.js:182:13)
    at MessageCreateHandler.handle (C:UsersAdminDesktopDiscord BotsDISCORDBOSS.jsnode_modulesdiscord.jssrcclientwebsocketpacketshandlersMessageCreate.js:9:34)
    at WebSocketPacketManager.handle (C:UsersAdminDesktopDiscord BotsDISCORDBOSS.jsnode_modulesdiscord.jssrcclientwebsocketpacketsWebSocketPacketManager.js:103:65)
    at WebSocketConnection.onPacket (C:UsersAdminDesktopDiscord BotsDISCORDBOSS.jsnode_modulesdiscord.jssrcclientwebsocketWebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (C:UsersAdminDesktopDiscord BotsDISCORDBOSS.jsnode_modulesdiscord.jssrcclientwebsocketWebSocketConnection.js:296:17)
    at WebSocket.onMessage (C:UsersAdminDesktopDiscord BotsDISCORDBOSS.jsnode_moduleswslibevent-target.js:120:16)
    at WebSocket.emit (events.js:182:13)
    at Receiver._receiver.onmessage (C:UsersAdminDesktopDiscord BotsDISCORDBOSS.jsnode_moduleswslibwebsocket.js:137:47)
    at Receiver.dataMessage (C:UsersAdminDesktopDiscord BotsDISCORDBOSS.jsnode_moduleswslibreceiver.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.

Answer

You could use the message.mentions property. So you would do the following:

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

Then to send the message do the following; message.guild.channels.find(t => t.id == announceChannel.id).send(myMessage);



Source: stackoverflow