根据参数向特定频道发送消息
Posted
技术标签:
【中文标题】根据参数向特定频道发送消息【英文标题】:Sending message to specific channel based on an argument 【发布时间】:2019-04-26 03:10:03 【问题描述】:我在 Reddit 和 *** 上搜索并发现了多个论坛帖子,其中用户询问如何将消息发送到特定频道,但我找不到可以使用 Argument 发送到特定频道的帖子。我的意思是你使用
return bot.channels.get(channel).send(embed);
我一直在测试这个“功能”,并设法将消息发送到特定的频道,但它还包括 arg[0] 又名频道 ID。命令是
announce "CHANNEL ID" "MESSAGE"
它确实将带有消息的嵌入发送到我输入的特定频道,但是它将CHANNEL ID
添加到嵌入中,所以我尝试在嵌入的.setDescription(arg[0])
中使用arg[0]
,但它不起作用。它向我吐出一条错误消息,我不知道这是什么意思。但是,如果您的专业人士在那里知道我能做什么,也许一个。这是整个命令代码:
if (cmd === `$prefixannounce`)
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.
【问题讨论】:
我试过了,不让我使用代码格式。它只是被窃听了。谢谢我还需要补充:pastebin.com/SKfe2f9g 谢谢你,乔治 【参考方案1】:您可以使用message.mentions
属性。因此,您将执行以下操作:
let announceChannel = message.mentions.channels.first();
然后发送消息执行以下操作;
message.guild.channels.find(t => t.id == announceChannel.id).send(myMessage);
【讨论】:
这能让我使用#channel_name 获取频道吗?而不是身份证?如果不是,我该怎么做?我回家后也会试试代码。 @Marius 是的,频道提及是#channel_name。因此,您无需复制 ID,只需提及频道即可。 感谢您的代码。该代码现在工作正常。但是,它仍然在我提供的参数前面显示#channel_name。因此,当公告显示在我选择的频道上时,它看起来像这样:来自 Marius H 的公告。#testing-channel-please-mute 这是一个测试 没关系!我得到了一些魔法:D谢谢你。这是工作代码: let taggedChannel = message.mentions.channels.first();让指定频道 = message.guild.channels.find(t => t.id == taggedChannel.id);; console.log("公告发送到" + 指定频道); if(!specifiedchannel) return message.channel.send("那个频道不存在!"); message.delete().catch(O_o=>);指定的channel.send(嵌入); console.log(message.author.username + "在频道中执行了一个公告#" + message.channel.name);以上是关于根据参数向特定频道发送消息的主要内容,如果未能解决你的问题,请参考以下文章