Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向所有服务器成员发送直接消息的方法?Java Discord Api_Java_Discord Jda - Fatal编程技术网

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

向所有服务器成员发送直接消息的方法?Java Discord Api,java,discord-jda,Java,Discord Jda,我想编写一个命令脚本,向所有服务器成员发送一条私人消息,这可能吗? 我知道如何将DM发送给消息的作者,但不是发送给所有服务器成员。是的,假设您有目标服务器的ID,您可以调用bot的jda实例 jda.getGuildById(<ID>); 或: 请注意,这里您需要等待同步完成并正常接收PrivateChannel对象,或者在打开PrivateChannel时将使用者传递给queue方法,该方法将在通道可用时立即执行: Consumer<PrivateChannel> m

我想编写一个命令脚本,向所有服务器成员发送一条私人消息,这可能吗?
我知道如何将DM发送给消息的作者,但不是发送给所有服务器成员。

是的,假设您有目标服务器的ID,您可以调用bot的jda实例

jda.getGuildById(<ID>);
或:

请注意,这里您需要等待同步完成并正常接收PrivateChannel对象,或者在打开PrivateChannel时将使用者传递给queue方法,该方法将在通道可用时立即执行:

Consumer<PrivateChannel> messageSender = channel -> channel.sendMessage("Hey~~!").queue();
user.openPrivateChannel().queue(messageSender);
现在我们有了频道,发送消息很简单

channel.sendMessage("Hey~~!").queue();
因此,把所有这些放在一起,我们得到:

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

是的,你可以找到每一个公会成员并通过循环发送消息给他们。顺便说一句,您使用的是哪个库JDA或Discord4J?@AnimeshSahu JDA=Java Discord Api@KlemensMorbe-yespif如果您实现了任何侦听器,请检查事件。该事件有一个JDA实例,其中包含所有公会/服务器成员的列表。看,哈哈,我没有问xDD这个问题
PrivateChannel channel = member.getUser().openPrivateChannel().complete();
Consumer<PrivateChannel> messageSender = channel -> channel.sendMessage("Hey~~!").queue();
user.openPrivateChannel().queue(messageSender);
user.openPrivateChannel().queue(channel -> channel.sendMessage("Hey~~!").queue());
channel.sendMessage("Hey~~!").queue();
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();
}