javascript - Sending message to specific channel based on an argument
问题描述
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:\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.
解决方案
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);
推荐阅读
- c# - 如果你杀死启动“异步”任务的线程,你会杀死这个任务吗?
- android - 如何将主题信息从原生 Android 应用程序发送到颤振模块“aar”模块?
- c++ - 问题切换玩家 - Tic Tac Toe
- java - 如何使用 JWNL 获取名词类别列表
- javascript - 有人可以解释一下,为什么我在 Javascript 中出现此错误
- python - 拆分数据集,但在训练数据集中不起作用
- python-3.x - 需要将 Python 中的位掩码转换为 VB.NET
- python - 如何在 DAG 中找到一组覆盖从源到汇的所有边的路径?
- html - 我怎样才能 ul 相对于标题的大小?
- php - MySql 查询以删除重复行(在 localhost MariaDB 10.4 上工作正常,但在实时服务器 Mysql 5.6 上出现错误)