Repository navigation
Migrate kick, ban and unban command #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
RealYusufIsmail
wants to merge
189
commits into
Together-Java:develop
from
BotCraftHub:feature/kick_and_ban_commands
Closed
Changes from all commits
Commits
Show all changes
189 commits
Select commit
Hold shift + click to select a range
0ce0c77
added ban and kick command
RealYusufIsmail bc393fc
Update Commands.java
RealYusufIsmail aa83502
fixed typos
RealYusufIsmail 2467335
Update KickCommand.java
RealYusufIsmail 551ec76
Update BanCommand.java
RealYusufIsmail f8d59ac
Update BanCommand.java
RealYusufIsmail a7e19c1
Update KickCommand.java
RealYusufIsmail c0b796f
Update KickCommand.java
RealYusufIsmail 73e2517
Update KickCommand.java
RealYusufIsmail 605a680
Update KickCommand.java
RealYusufIsmail 7910cce
Update KickCommand.java
RealYusufIsmail 44ed0bd
Update KickCommand.java
RealYusufIsmail 3226043
Fix the rest tomorrow
RealYusufIsmail b628811
Update KickCommand.java
RealYusufIsmail f1a4aa5
Update KickCommand.java
RealYusufIsmail a46b4c2
Update KickCommand.java
RealYusufIsmail e3dc6de
Update BanCommand.java
RealYusufIsmail 2aba8c2
Update BanCommand.java
RealYusufIsmail 111eec4
Update KickCommand.java
RealYusufIsmail 05694be
Update BanCommand.java
RealYusufIsmail b966304
added UnBan Command
RealYusufIsmail 0c92c3e
Update UnBanCommand.java
RealYusufIsmail 9f7dc06
Apply suggestions from code review
RealYusufIsmail dbe0a35
Update UnBanCommand.java
RealYusufIsmail 01d8069
fixed all commands
RealYusufIsmail b8fd360
Merge branch 'feature/kick_and_ban_commands' of https://github.com/Yu…
RealYusufIsmail a55ecc0
spotlessApply
RealYusufIsmail 0daad10
The bot will dm the user now.
RealYusufIsmail 12fc55a
spotlessApply
RealYusufIsmail fe59425
Update BanCommand.java
RealYusufIsmail 867147f
changed user id from string to long
RealYusufIsmail 365ff7e
it will log the author.
RealYusufIsmail 606793b
Update UnBanCommand.java
RealYusufIsmail 019bec3
Update BanCommand.java
RealYusufIsmail 516ffcf
Update KickCommand.java
RealYusufIsmail 23a6c2e
changed from UnBanCommand to UnbanCommand. Allowed to delete messages…
RealYusufIsmail fd0802d
Update BanCommand.java
RealYusufIsmail 48f9863
Update BanCommand.java
RealYusufIsmail 8b96b3a
I checked the jda guild and its says it has to be between one and 7 days
RealYusufIsmail 7dae25c
Update .gitignore
RealYusufIsmail 9d6c4be
fixed everything
RealYusufIsmail 336d567
Update BanCommand.java
RealYusufIsmail 64d7c66
Fixed everything apart from https://github.com/Together-Java/TJ-Bot/p…
RealYusufIsmail 9ae3477
Update BanCommand.java
RealYusufIsmail d31f7c4
Update UnbanCommand.java
RealYusufIsmail 30eb8ae
Update KickCommand.java
RealYusufIsmail d9bf30b
I think i have solved the problem
RealYusufIsmail b5d9a87
Merge branch 'develop' into feature/kick_and_ban_commands
RealYusufIsmail abfb624
Update BanCommand.java
RealYusufIsmail 84a5b24
Cleaned the code
RealYusufIsmail 15ba489
Merge branch 'develop' into feature/kick_and_ban_commands
RealYusufIsmail a8a1ecf
continue later
RealYusufIsmail b17ef98
Update BanCommand.java
RealYusufIsmail c825f78
Fixed everything + spotlessApply
RealYusufIsmail 59b7dff
Update BanCommand.java
RealYusufIsmail 7226800
Update BanCommand.java
RealYusufIsmail 7687186
Fixed it
RealYusufIsmail 8775730
Update BanCommand.java
RealYusufIsmail 7aee287
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail 5550872
changed some things
RealYusufIsmail 73f30e6
Update BanCommand.java
RealYusufIsmail 81e3f17
a bit cleaner
RealYusufIsmail c6722da
Update BanCommand.java
RealYusufIsmail 1663113
added choices
RealYusufIsmail fe7f1d3
Update BanCommand.java
RealYusufIsmail edc42ed
Update BanCommand.java
RealYusufIsmail 2fb352e
Update BanCommand.java
RealYusufIsmail bd27130
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail 46c2ecb
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail 3a328ff
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail 107e536
Update BanHelperMethods.java
RealYusufIsmail de1b36e
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail 90b88fe
Update BanHelperMethods.java
RealYusufIsmail 198c720
Update BanHelperMethods.java
RealYusufIsmail fa89b64
spotlessApply
RealYusufIsmail 97a4aac
tweaked the ban command. Removed unneeded string in kick command
RealYusufIsmail 7e165f9
spotlessJavaApply
RealYusufIsmail 35b9939
Update BanCommand.java
RealYusufIsmail af1fe46
I have fixed what you told me to fix
RealYusufIsmail 563998a
Update BanCommand.java
RealYusufIsmail c6dd085
Update BanCommand.java
RealYusufIsmail 57e89f9
Update BanCommand.java
RealYusufIsmail 6a4f39a
Update BanCommand.java
RealYusufIsmail 2105989
Update BanCommand.java
RealYusufIsmail 1532d75
Update BanCommand.java
RealYusufIsmail bc90bd4
Update BanCommand.java
RealYusufIsmail f7aac63
Fixed everything
RealYusufIsmail 08be698
Fixed the loggers
RealYusufIsmail cdc8389
Update .gitignore
RealYusufIsmail 12f6a40
fixed kick and unban command
RealYusufIsmail 4d3f4c1
Update KickCommand.java
RealYusufIsmail 19e7b57
Update UnbanCommand.java
RealYusufIsmail 18e0588
Update UnbanCommand.java
RealYusufIsmail 39c8c12
Update BanCommand.java
RealYusufIsmail 7f2389f
Update BanCommand.java
RealYusufIsmail 431d488
Update BanCommand.java
RealYusufIsmail 47f9f28
Update BanCommand.java
RealYusufIsmail e3d2a5e
Update KickCommand.java
RealYusufIsmail 5cef333
Update UnbanCommand.java
RealYusufIsmail 7bfb288
fixed everything that i was told to fix
RealYusufIsmail 547536f
Update UnbanCommand.java
RealYusufIsmail 08c42dc
Update UnbanCommand.java
RealYusufIsmail b44f3af
Tweaking the commands
RealYusufIsmail 6c4f3ed
improving the sentecences
RealYusufIsmail ec26837
added some loggers
RealYusufIsmail 9d199d9
spotlessApply
RealYusufIsmail 9edd2a5
fixed a gramatical mistake
RealYusufIsmail fb6b4f8
Update UnbanCommand.java
RealYusufIsmail e2aee23
Update V1__Database_Listener.sql
RealYusufIsmail d8f4bee
Update V1__Database_Listener.sql
RealYusufIsmail caa53a5
Fixed everything i was told to fix
RealYusufIsmail 7626460
changed logger.error to logger.info
RealYusufIsmail 6a28671
Removed TODO since codefactor said so
RealYusufIsmail 69ea546
undo
RealYusufIsmail f4783bb
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail b7bbd22
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail ddda849
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail 681ee09
fixed most of the code
RealYusufIsmail 0bfeabe
Merge branch 'feature/kick_and_ban_commands' of https://github.com/Yu…
RealYusufIsmail 8ac9bc6
Update KickCommand.java
RealYusufIsmail 7801677
All checks should pass
RealYusufIsmail f04077d
Update UnbanCommand.java
RealYusufIsmail 9bdee00
FIxed my sentences
RealYusufIsmail dbed14b
Update BanCommand.java
RealYusufIsmail 0c9deec
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail 0281c36
Fixed everything
RealYusufIsmail 59e59ea
Merge branch 'feature/kick_and_ban_commands' of https://github.com/Yu…
RealYusufIsmail 8ae5354
spotlessApply
RealYusufIsmail 8bcd882
please fix spotlessApply
RealYusufIsmail a35e1aa
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail 57a190c
spotlessApply
RealYusufIsmail be8d415
Merge branch 'feature/kick_and_ban_commands' of https://github.com/Yu…
RealYusufIsmail 202eb61
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail 06a7923
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail cf415ba
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail fe03fac
fixed the dm bug
RealYusufIsmail 6c02458
fixed some more issues
RealYusufIsmail bf2e651
Update BanCommand.java
RealYusufIsmail dd842ad
Update KickCommand.java
RealYusufIsmail 71cb447
Update KickCommand.java
RealYusufIsmail 08bbc23
Update BanCommand.java
RealYusufIsmail ec722c4
fixed an issue which cause gradle build to fail
RealYusufIsmail 0399339
hopefully fixed everything.
RealYusufIsmail 78de545
Improved Unban And Ban Command
RealYusufIsmail 79cbca1
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail 6076a0f
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail aba48bb
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail 3a4fdff
Fixed everything and I fixed the Objects.requireNonNull
RealYusufIsmail d29e57e
Update UnbanCommand.java
RealYusufIsmail 5d8018f
Update UnbanCommand.java
RealYusufIsmail 02d6d84
Update UnbanCommand.java
RealYusufIsmail d78895b
Revert "Update UnbanCommand.java"
RealYusufIsmail a4231f1
Fixed the sentence
RealYusufIsmail e78cc7f
Update UnbanCommand.java
RealYusufIsmail f868b11
Added reason for kick command
RealYusufIsmail 6b8e1d4
improved the logger
RealYusufIsmail 7bcb663
Update BanCommand.java
RealYusufIsmail c4e534b
Update BanCommand.java
RealYusufIsmail a769472
Update BanCommand.java
RealYusufIsmail 05aaf96
All checks pass + fixed everything I was told to fix.
RealYusufIsmail 8080286
Fixed everything
RealYusufIsmail 149d83f
Update ModerationUtils.java
RealYusufIsmail c584417
Merge branch 'develop' into feature/kick_and_ban_commands
RealYusufIsmail cfb91fc
Fixed everything.
RealYusufIsmail 33261e0
fixed an issue which cause member to null
RealYusufIsmail 6990895
Fixed the issues
RealYusufIsmail f4449f9
added spaces to improve readbility
RealYusufIsmail 2bf9f7b
Improved some sentences
RealYusufIsmail 42c2269
Update ModerationUtils.java
RealYusufIsmail eeb64d2
Update KickCommand.java
RealYusufIsmail 2163662
fixed an error on my side
RealYusufIsmail b002177
Update ModerationUtils.java
RealYusufIsmail 792cb26
Fixed everything
RealYusufIsmail 62ee69e
Update ModerationUtils.java
RealYusufIsmail d795cd2
Update ModerationUtils.java
RealYusufIsmail 77441b0
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail 04b120c
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail 216201b
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail 12c85a8
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail 67de1ed
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail 71a5514
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail de6992b
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail 9170329
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail 4663673
Fixed
RealYusufIsmail 24c6250
Fixed everything Tijs told me to fix
RealYusufIsmail 79fe2a6
spotlessApply
RealYusufIsmail 199cfee
I have fixed everything that Zabuzard told me to fix.
RealYusufIsmail 4d9dce6
Fixed most of the commands.
RealYusufIsmail 02e4803
addressed to the second build point.
RealYusufIsmail File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| package org.togetherjava.tjbot.commands.moderation; | ||
|
|
||
| import net.dv8tion.jda.api.Permission; | ||
| import net.dv8tion.jda.api.entities.Guild; | ||
| import net.dv8tion.jda.api.entities.Member; | ||
| import net.dv8tion.jda.api.entities.User; | ||
| import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; | ||
| import net.dv8tion.jda.api.interactions.commands.OptionMapping; | ||
| import net.dv8tion.jda.api.interactions.commands.OptionType; | ||
| import net.dv8tion.jda.api.interactions.commands.build.OptionData; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.togetherjava.tjbot.commands.SlashCommandAdapter; | ||
| import org.togetherjava.tjbot.commands.SlashCommandVisibility; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * This command can ban users and optionally remove their messages from the past days. Banning can | ||
| * also be paired with a ban reason. The command will also try to DM the user to inform them about | ||
| * the action and the reason. | ||
| * <p> | ||
| * The command fails if the user triggering it is lacking permissions to either ban other users or | ||
| * to ban the specific given user (for example a moderator attempting to ban an admin). | ||
| */ | ||
| public final class BanCommand extends SlashCommandAdapter { | ||
| private static final String USER_OPTION = "user"; | ||
| private static final String DELETE_HISTORY_OPTION = "delete-history"; | ||
| private static final String REASON_OPTION = "reason"; | ||
| private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); | ||
|
|
||
| /** | ||
| * Constructs an instance. | ||
| */ | ||
| public BanCommand() { | ||
| super("ban", "Bans the given user from the server", SlashCommandVisibility.GUILD); | ||
|
|
||
| getData().addOption(OptionType.USER, USER_OPTION, "The user who you want to ban", true) | ||
| .addOption(OptionType.STRING, REASON_OPTION, "Why the user should be banned", true) | ||
| .addOptions(new OptionData(OptionType.INTEGER, DELETE_HISTORY_OPTION, | ||
| "the amount of days of the message history to delete, none means no messages are deleted.", | ||
| true).addChoice("none", 0).addChoice("recent", 1).addChoice("all", 7)); | ||
| } | ||
|
|
||
| @Override | ||
| public void onSlashCommand(@NotNull SlashCommandEvent event) { | ||
| OptionMapping userOption = | ||
| Objects.requireNonNull(event.getOption(USER_OPTION), "The target is null"); | ||
| Member target = userOption.getAsMember(); | ||
| Member author = Objects.requireNonNull(event.getMember(), "The author is null"); | ||
|
|
||
| String reason = Objects.requireNonNull(event.getOption(REASON_OPTION), "The reason is null") | ||
| .getAsString(); | ||
|
|
||
| Guild guild = Objects.requireNonNull(event.getGuild()); | ||
| Member bot = guild.getSelfMember(); | ||
|
|
||
| // Member doesn't exist if attempting to ban a user who is not part of the guild. | ||
| if (target != null && !handleCanInteractWithTarget(target, bot, author, event)) { | ||
| return; | ||
| } | ||
|
|
||
| if (!handleHasPermissions(author, bot, event, guild)) { | ||
| return; | ||
| } | ||
|
|
||
| int deleteHistoryDays = Math | ||
| .toIntExact(Objects.requireNonNull(event.getOption(DELETE_HISTORY_OPTION)).getAsLong()); | ||
|
|
||
| if (!ModerationUtils.handleReason(reason, event)) { | ||
| return; | ||
| } | ||
|
|
||
| banUser(userOption.getAsUser(), author, reason, deleteHistoryDays, guild, event); | ||
| } | ||
|
|
||
| private static void banUser(@NotNull User target, @NotNull Member author, | ||
| @NotNull String reason, int deleteHistoryDays, @NotNull Guild guild, | ||
| @NotNull SlashCommandEvent event) { | ||
| event.getJDA() | ||
| .openPrivateChannelById(target.getIdLong()) | ||
| .flatMap(channel -> channel.sendMessage( | ||
| """ | ||
| Hey there, sorry to tell you but unfortunately you have been banned from the server %s. | ||
| If you think this was a mistake, please contact a moderator or admin of the server. | ||
| The reason for the ban is: %s | ||
| """ | ||
| .formatted(guild.getName(), reason))) | ||
| .mapToResult() | ||
| .flatMap(result -> guild.ban(target, deleteHistoryDays, reason)) | ||
| .flatMap(v -> event.reply(target.getAsTag() + " was banned by " | ||
| + author.getUser().getAsTag() + " for: " + reason)) | ||
| .queue(); | ||
|
|
||
| logger.info( | ||
| " '{} ({})' banned the user '{} ({})' and deleted their message history of the last '{}' days. Reason being'{}'", | ||
| author.getUser().getAsTag(), author.getIdLong(), target.getAsTag(), | ||
| target.getIdLong(), deleteHistoryDays, reason); | ||
| } | ||
|
|
||
| private static boolean handleCanInteractWithTarget(@NotNull Member target, @NotNull Member bot, | ||
| @NotNull Member author, @NotNull SlashCommandEvent event) { | ||
| String targetTag = target.getUser().getAsTag(); | ||
| if (!author.canInteract(target)) { | ||
| event.reply("The user " + targetTag + " is too powerful for you to ban.") | ||
| .setEphemeral(true) | ||
| .queue(); | ||
| return false; | ||
| } | ||
|
|
||
| if (!bot.canInteract(target)) { | ||
| event.reply("The user " + targetTag + " is too powerful for me to ban.") | ||
| .setEphemeral(true) | ||
| .queue(); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private static boolean handleHasPermissions(@NotNull Member author, @NotNull Member bot, | ||
| @NotNull SlashCommandEvent event, @NotNull Guild guild) { | ||
| if (!author.hasPermission(Permission.BAN_MEMBERS)) { | ||
| event.reply( | ||
| "You can not ban users in this guild since you do not have the BAN_MEMBERS permission.") | ||
| .setEphemeral(true) | ||
| .queue(); | ||
| return false; | ||
| } | ||
|
|
||
| if (!bot.hasPermission(Permission.BAN_MEMBERS)) { | ||
| event.reply( | ||
| "I can not ban users in this guild since I do not have the BAN_MEMBERS permission.") | ||
| .setEphemeral(true) | ||
| .queue(); | ||
|
|
||
| logger.error("The bot does not have BAN_MEMBERS permission on the server '{}' ", | ||
| guild.getName()); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| } |
116 changes: 116 additions & 0 deletions
116
application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| package org.togetherjava.tjbot.commands.moderation; | ||
|
|
||
| import net.dv8tion.jda.api.Permission; | ||
| import net.dv8tion.jda.api.entities.Guild; | ||
| import net.dv8tion.jda.api.entities.Member; | ||
| import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; | ||
| import net.dv8tion.jda.api.interactions.commands.OptionMapping; | ||
| import net.dv8tion.jda.api.interactions.commands.OptionType; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.togetherjava.tjbot.commands.SlashCommandAdapter; | ||
| import org.togetherjava.tjbot.commands.SlashCommandVisibility; | ||
| import java.util.Objects; | ||
|
|
||
|
|
||
| /** | ||
| * This command can kicks users. Kicking can also be paired with a kick reason. The command will | ||
| * also try to DM the user to inform them about the action and the reason. | ||
| * <p> | ||
| * The command fails if the user triggering it is lacking permissions to either kick other users or | ||
| * to kick the specific given user (for example a moderator attempting to kick an admin). | ||
| * | ||
| */ | ||
| public final class KickCommand extends SlashCommandAdapter { | ||
| private static final Logger logger = LoggerFactory.getLogger(KickCommand.class); | ||
| private static final String USER_OPTION = "user"; | ||
| private static final String REASON_OPTION = "reason"; | ||
|
|
||
|
Tais993 marked this conversation as resolved.
Resolved
|
||
| /** | ||
| * Constructs an instance. | ||
| */ | ||
| public KickCommand() { | ||
| super("kick", "Kicks the given user from the server", SlashCommandVisibility.GUILD); | ||
|
|
||
| getData().addOption(OptionType.USER, USER_OPTION, "The user who you want to kick", true) | ||
| .addOption(OptionType.STRING, REASON_OPTION, "Why the user should be kicked", true); | ||
|
Zabuzard marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| @Override | ||
| public void onSlashCommand(@NotNull SlashCommandEvent event) { | ||
| OptionMapping userOption = | ||
| Objects.requireNonNull(event.getOption(USER_OPTION), "The target is null"); | ||
| Member target = userOption.getAsMember(); | ||
| Member author = Objects.requireNonNull(event.getMember(), "The author is null"); | ||
|
|
||
| String reason = Objects.requireNonNull(event.getOption(REASON_OPTION), "The reason is null") | ||
| .getAsString(); | ||
|
|
||
|
|
||
| Guild guild = event.getGuild(); | ||
| Member bot = guild.getSelfMember(); | ||
|
|
||
| if (!author.hasPermission(Permission.KICK_MEMBERS)) { | ||
| event.reply( | ||
| "You can not kick users in this guild since you do not have the KICK_MEMBERS permission.") | ||
| .setEphemeral(true) | ||
| .queue(); | ||
| return; | ||
| } | ||
|
|
||
| String userTag = userOption.getAsUser().getAsTag(); | ||
| if (!author.canInteract(target)) { | ||
| event.reply("The user " + userTag + " is too powerful for you to kick.") | ||
| .setEphemeral(true) | ||
| .queue(); | ||
| return; | ||
| } | ||
|
|
||
| if (!bot.hasPermission(Permission.KICK_MEMBERS)) { | ||
| event.reply( | ||
| "I can not kick users in this guild since I do not have the KICK_MEMBERS permission.") | ||
| .setEphemeral(true) | ||
| .queue(); | ||
|
|
||
| logger.error("The bot does not have KICK_MEMBERS permission on the server '{}' ", | ||
| event.getGuild().getName()); | ||
| return; | ||
| } | ||
|
|
||
| if (!bot.canInteract(target)) { | ||
| event.reply("The user " + userTag + " is too powerful for me to kick.") | ||
| .setEphemeral(true) | ||
| .queue(); | ||
| return; | ||
| } | ||
|
|
||
| if (!ModerationUtils.handleReason(reason, event)) { | ||
| return; | ||
| } | ||
|
|
||
| kickUser(target, author, reason, guild, event); | ||
| } | ||
|
|
||
| private static void kickUser(@NotNull Member target, @NotNull Member author, | ||
| @NotNull String reason, @NotNull Guild guild, @NotNull SlashCommandEvent event) { | ||
| event.getJDA() | ||
| .openPrivateChannelById(target.getUser().getId()) | ||
| .flatMap(channel -> channel.sendMessage( | ||
| """ | ||
| Hey there, sorry to tell you but unfortunately you have been kicked from the server %s. | ||
| If you think this was a mistake, please contact a moderator or admin of the server. | ||
| The reason for the kick is: %s | ||
| """ | ||
| .formatted(guild.getName(), reason))) | ||
| .mapToResult() | ||
| .flatMap(result -> guild.kick(target, reason).reason(reason)) | ||
| .flatMap(v -> event.reply(target.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(), target.getUser().getAsTag(), | ||
| target.getUser().getId(), reason); | ||
| } | ||
| } | ||
42 changes: 42 additions & 0 deletions
42
application/src/main/java/org/togetherjava/tjbot/commands/moderation/ModerationUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package org.togetherjava.tjbot.commands.moderation; | ||
|
|
||
| import net.dv8tion.jda.api.entities.Guild; | ||
| import net.dv8tion.jda.api.entities.User; | ||
| import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| /** | ||
| * Utility class offering helpers revolving around user moderation, such as banning or kicking. | ||
| */ | ||
| public enum ModerationUtils { | ||
|
Zabuzard marked this conversation as resolved.
Resolved
|
||
| ; | ||
|
|
||
| /** | ||
| * As stated in {@link Guild#ban(User, int, String)} <br> | ||
| * The reason can be only 512 characters. | ||
| */ | ||
| private static final int REASON_MAX_LENGTH = 512; | ||
|
|
||
| /** | ||
| * This boolean checks if the reason that the user has entered violates the max character length | ||
| * or not. <br> | ||
| * If it does the bot will tell the user they have violated the mex character length and will | ||
| * terminate the command <br> | ||
| * If it does bot the bot will be allowed to continue running the command. | ||
| * | ||
| * @param reason the reason of the action such as banning. | ||
| * @param event This is used to be able to use slash command actions such as event.reply(); | ||
| * @throws IllegalArgumentException if the reason is over 512 characters. | ||
| * | ||
| */ | ||
| public static boolean handleReason(@NotNull String reason, @NotNull SlashCommandEvent event) { | ||
| if (reason.length() <= REASON_MAX_LENGTH) { | ||
| return true; | ||
| } | ||
|
|
||
| event.reply("The reason can not be over " + REASON_MAX_LENGTH + " characters") | ||
| .setEphemeral(true) | ||
| .queue(); | ||
| return false; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.