0

当我禁止该成员时,他们没有在那里发送消息,甚至认为用户 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 用户

4

1 回答 1

1

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

例子:

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
于 2021-10-26T12:59:08.573 回答