Skip to content
Advertisement

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

when i banned the member they were no messaged in there dms even thought the users dm were open.

the code is here

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

the error messages says it could not dm the user even though there dms were open

Advertisement

Answer

You can only send direct messages if you share a server with the user and the user does not have you blocked. You need to send the direct message before banning/kicking the user.

Example:

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
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement