简体   繁体   English

Discord Bot可以在频道上向所有人发送消息吗

[英]Can Discord Bot send a message on a channel to everyone

I'm coding a discord Bot with the discord.js library on a nodeJs server.我正在 nodeJs 服务器上使用 discord.js 库编写 discord Bot。 Here is my question: is that possible when an event occure (like someone sending a message) that the bot answers to members of a role, or to everyone.这是我的问题:当一个事件发生时(比如有人发送消息),机器人是否可以回答某个角色的成员或每个人。 The message.reply("my reply") method only answer to the author of the message as I use it for now... message.reply("my reply") 方法只回复消息的作者,因为我现在使用它......

Your problem is that message.reply() is a very limited method: it always mentions the author of the message, and you can't override that.你的问题是message.reply()是一个非常有限的方法:它总是提到消息的作者,你不能覆盖它。

You need to use a more generic method, channel.send() , and construct the mention yourself.您需要使用更通用的方法channel.send()并自己构建提及。 .reply() is just a shortcut for a frequently used form of it, but you need something custom. .reply()只是常用形式的快捷方式,但您需要一些自定义的东西。

Presumably, you want it to happen in the same channel as the message, so you need message.channel.send("Whatever content you want") .据推测,您希望它与消息发生在同一频道中,因此您需要message.channel.send("Whatever content you want")

Now, to add a role, you need to decide how to pick one.现在,要添加一个角色,您需要决定如何选择一个角色。 Is it fixed?是固定的吗? Then you could hard-code a role mention by role ID : <@&134362454976102401> (of course, it needs to be the role ID you require).然后,您可以通过角色 ID 对角色提及进行硬编码<@&134362454976102401> (当然,它必须是您需要的角色 ID)。

If you want to find a role, for example by name, you need to do that via searching through the guild in question.如果你想找到一个角色,例如通过名字,你需要通过搜索相关的公会来做到这一点。 You can access it though message.guild , but be warned that it will be undefined for DMs.您可以通过message.guild访问它,但请注意它对于 DM 将是未定义的。

Then you can do something like然后你可以做类似的事情

const role = message.guild.roles.find(role => role.name === "NameYouWant");
message.channel.send(`${role} something something`);

because Role objects become mentions when converted to strings.因为 Role 对象在转换为字符串时会被提及。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM