JDA:一旦用户被禁止,该机器人就不会直接通知用户

JDA: The bot does not dm the user once they have been ban

当我禁止该成员时,他们在 dms 中没有收到消息,甚至认为用户 dm 是开放的。

代码在这里

public static void kickUser(@NotNull Member member, @NotNull Member author,
            @NotNull String reason, long userId, @NotNull SlashCommandEvent event) {
        String guildName = event.getGuild().getName();
        event.getJDA()
            .openPrivateChannelById(userId)
            .flatMap(channel -> channel.sendMessage(
                    """
                            Hey there, sorry to tell you but unfortunately you have been kicked from the guild %s.
                            If you think this was a mistake, please contact a moderator or admin of the guild.
                            he reason for the kick is: %s
                            """
                        .formatted(guildName, reason)))
            .queue(null,
                    throwable -> logger.info(
                            "I could not dm the user '{}' to inform them that they were kicked. {}",
                            userId, throwable));

        event.getGuild()
            .kick(member, reason)
            .flatMap(v -> event.reply(member.getUser().getAsTag() + " was kicked by "
                    + author.getUser().getAsTag() + " for: " + reason))
            .queue();

        logger.info(" '{} ({})' kicked the user '{} ({})' due to reason being '{}'",
                author.getUser().getAsTag(), author.getIdLong(), member.getUser().getAsTag(),
                userId, reason);
    }

错误消息说即使 dms 已打开,它也无法 dm 用户

如果您与用户共享服务器并且用户没有阻止您,您只能发送直接消息。您需要在 之前 banning/kicking 向用户发送直接消息。

示例:

user.openPrivateChannel()
  .flatMap(channel -> channel.sendMessage("you are banned")) // send a message to the channel
  .mapToResult() // this means the next flatMap runs on success and error signals
  .flatMap(result -> guild.ban(member)) // then ban the member
  .queue(); // queue the chain of actions