简体   繁体   English

一种向所有服务器成员发送直接消息的方法? Java Discord Api

[英]A way to send Direct Message to all Server Members? Java Discord Api

I wanted to script a command to send an Private Message to all Server Members, is that possible?我想编写一个命令来向所有服务器成员发送私人消息,这可能吗? I know how to send a DM to the Author of the message but not to all Server Members.我知道如何将 DM 发送给消息的作者,但不知道发送给所有服务器成员。

Yes you can, suppose you have your target server's ID, with that you can call on your bot's jda instance是的,你可以,假设你有你的目标服务器的 ID,你可以用它来调用你的机器人的 jda 实例

jda.getGuildById(<ID>);

Or if you just have the name, it's also possible with或者,如果您只有名称,也可以使用

jda.getGuildsByName(<NAME>, <CASE_SENSITIVE>)

The name one will return a list with all the matching names, this is quite impractical, since getting the ID is so easy. name one 将返回一个包含所有匹配名称的列表,这是非常不切实际的,因为获取 ID 非常容易。

You might have gotten your Guild object some other way (Through a listener maybe?), it does not matter.您可能以其他方式获得了Guild object(可能是通过听众?),没关系。

Now you can either call guild.getMembers() or guild.getMemberCache() which one should you choose?现在你可以调用guild.getMembers()guild.getMemberCache()你应该选择哪一个? guild.getMemberCache() is, by default more efficient and in the end it implements the Iterable interface, so for all your intents and purposes, it should be better to use getMemberCache() , the benefit of getMembers() is that you'll get it as a list, making is trivial to do more processing. guild.getMemberCache()默认情况下更高效,最终它实现了Iterable接口,因此对于您的所有意图和目的,最好使用getMemberCache()getMembers() () 的好处是您将把它作为一个列表,做更多的处理是微不足道的。

Now you have your List<Member> or MemberCacheView , let's iterate through it here the simplest option is the forEach() method, or just a for each loop:现在你有了List<Member>MemberCacheView ,让我们在这里遍历它,最简单的选择是forEach()方法,或者只是一个 for each 循环:

for (Member member : guild.getMemberCache())

Or:或者:

guild.getMemberCache().forEach(member -> <DO_SOMETHING>)

(If you choose to use guild.getMembers() it would be exactly the same) (如果您选择使用guild.getMembers()将完全相同)

Now to actually send your messages:现在实际发送您的消息:

Remember we are iterating over Member objects, but JDA does not allow us to open private channels through that, so we need请记住,我们正在迭代Member对象,但 JDA 不允许我们通过它打开私有通道,所以我们需要

PrivateChannel channel = member.getUser().openPrivateChannel().complete();

Note that here you need to either wait for the synchronous complete() and receive the PrivateChannel object normally, or you can pass a consumer to the queue method when opening the PrivateChannel , which will be executed as soon as the channel is available:请注意,这里需要等待同步complete()并正常接收PrivateChannel ,或者您可以在打开PrivateChannel时将消费者传递给 queue 方法,该方法将在通道可用时立即执行:

Consumer<PrivateChannel> messageSender = channel -> channel.sendMessage("Hey~~!").queue();
user.openPrivateChannel().queue(messageSender);

Or:或者:

user.openPrivateChannel().queue(channel -> channel.sendMessage("Hey~~!").queue());

Now we have the channel and sending a message is simple现在我们有了频道,发送消息很简单

channel.sendMessage("Hey~~!").queue();

So putting it all together we get:所以把它们放在一起,我们得到:

Guild guild; //Guild you got from a listener, or from the JDA pool

for(Member member : guild.getMemberCache()) { //Iterating over cached members in the guild
   User user = member.getUser(); //Converting the member object to a User

  user.openPrivateChannel().queue(channel->
             channel.sendMessage("Hey~~!").queue()); //Opening the channel and sending the message

   /*  Now you can optionally close the channel to remove it from the JDA's mapping */
   channel.close().queue();
}

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

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