Skip to content
Closed
Show file tree
Hide file tree
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 Oct 16, 2021
bc393fc
Update Commands.java
RealYusufIsmail Oct 16, 2021
aa83502
fixed typos
RealYusufIsmail Oct 16, 2021
2467335
Update KickCommand.java
RealYusufIsmail Oct 16, 2021
551ec76
Update BanCommand.java
RealYusufIsmail Oct 16, 2021
f8d59ac
Update BanCommand.java
RealYusufIsmail Oct 16, 2021
a7e19c1
Update KickCommand.java
RealYusufIsmail Oct 16, 2021
c0b796f
Update KickCommand.java
RealYusufIsmail Oct 16, 2021
73e2517
Update KickCommand.java
RealYusufIsmail Oct 16, 2021
605a680
Update KickCommand.java
RealYusufIsmail Oct 16, 2021
7910cce
Update KickCommand.java
RealYusufIsmail Oct 16, 2021
44ed0bd
Update KickCommand.java
RealYusufIsmail Oct 16, 2021
3226043
Fix the rest tomorrow
RealYusufIsmail Oct 16, 2021
b628811
Update KickCommand.java
RealYusufIsmail Oct 16, 2021
f1a4aa5
Update KickCommand.java
RealYusufIsmail Oct 16, 2021
a46b4c2
Update KickCommand.java
RealYusufIsmail Oct 16, 2021
e3dc6de
Update BanCommand.java
RealYusufIsmail Oct 16, 2021
2aba8c2
Update BanCommand.java
RealYusufIsmail Oct 16, 2021
111eec4
Update KickCommand.java
RealYusufIsmail Oct 16, 2021
05694be
Update BanCommand.java
RealYusufIsmail Oct 16, 2021
b966304
added UnBan Command
RealYusufIsmail Oct 16, 2021
0c92c3e
Update UnBanCommand.java
RealYusufIsmail Oct 16, 2021
9f7dc06
Apply suggestions from code review
RealYusufIsmail Oct 17, 2021
dbe0a35
Update UnBanCommand.java
RealYusufIsmail Oct 17, 2021
01d8069
fixed all commands
RealYusufIsmail Oct 17, 2021
b8fd360
Merge branch 'feature/kick_and_ban_commands' of https://github.com/Yu…
RealYusufIsmail Oct 17, 2021
a55ecc0
spotlessApply
RealYusufIsmail Oct 17, 2021
0daad10
The bot will dm the user now.
RealYusufIsmail Oct 17, 2021
12fc55a
spotlessApply
RealYusufIsmail Oct 17, 2021
fe59425
Update BanCommand.java
RealYusufIsmail Oct 17, 2021
867147f
changed user id from string to long
RealYusufIsmail Oct 17, 2021
365ff7e
it will log the author.
RealYusufIsmail Oct 17, 2021
606793b
Update UnBanCommand.java
RealYusufIsmail Oct 17, 2021
019bec3
Update BanCommand.java
RealYusufIsmail Oct 17, 2021
516ffcf
Update KickCommand.java
RealYusufIsmail Oct 17, 2021
23a6c2e
changed from UnBanCommand to UnbanCommand. Allowed to delete messages…
RealYusufIsmail Oct 17, 2021
fd0802d
Update BanCommand.java
RealYusufIsmail Oct 17, 2021
48f9863
Update BanCommand.java
RealYusufIsmail Oct 17, 2021
8b96b3a
I checked the jda guild and its says it has to be between one and 7 days
RealYusufIsmail Oct 17, 2021
7dae25c
Update .gitignore
RealYusufIsmail Oct 18, 2021
9d6c4be
fixed everything
RealYusufIsmail Oct 18, 2021
336d567
Update BanCommand.java
RealYusufIsmail Oct 18, 2021
64d7c66
Fixed everything apart from https://github.com/Together-Java/TJ-Bot/p…
RealYusufIsmail Oct 19, 2021
9ae3477
Update BanCommand.java
RealYusufIsmail Oct 19, 2021
d31f7c4
Update UnbanCommand.java
RealYusufIsmail Oct 19, 2021
30eb8ae
Update KickCommand.java
RealYusufIsmail Oct 19, 2021
d9bf30b
I think i have solved the problem
RealYusufIsmail Oct 19, 2021
b5d9a87
Merge branch 'develop' into feature/kick_and_ban_commands
RealYusufIsmail Oct 19, 2021
abfb624
Update BanCommand.java
RealYusufIsmail Oct 19, 2021
84a5b24
Cleaned the code
RealYusufIsmail Oct 19, 2021
15ba489
Merge branch 'develop' into feature/kick_and_ban_commands
RealYusufIsmail Oct 20, 2021
a8a1ecf
continue later
RealYusufIsmail Oct 20, 2021
b17ef98
Update BanCommand.java
RealYusufIsmail Oct 20, 2021
c825f78
Fixed everything + spotlessApply
RealYusufIsmail Oct 20, 2021
59b7dff
Update BanCommand.java
RealYusufIsmail Oct 20, 2021
7226800
Update BanCommand.java
RealYusufIsmail Oct 20, 2021
7687186
Fixed it
RealYusufIsmail Oct 21, 2021
8775730
Update BanCommand.java
RealYusufIsmail Oct 21, 2021
7aee287
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 21, 2021
5550872
changed some things
RealYusufIsmail Oct 21, 2021
73f30e6
Update BanCommand.java
RealYusufIsmail Oct 21, 2021
81e3f17
a bit cleaner
RealYusufIsmail Oct 21, 2021
c6722da
Update BanCommand.java
RealYusufIsmail Oct 21, 2021
1663113
added choices
RealYusufIsmail Oct 21, 2021
fe7f1d3
Update BanCommand.java
RealYusufIsmail Oct 21, 2021
edc42ed
Update BanCommand.java
RealYusufIsmail Oct 21, 2021
2fb352e
Update BanCommand.java
RealYusufIsmail Oct 21, 2021
bd27130
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 21, 2021
46c2ecb
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 21, 2021
3a328ff
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 21, 2021
107e536
Update BanHelperMethods.java
RealYusufIsmail Oct 21, 2021
de1b36e
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 21, 2021
90b88fe
Update BanHelperMethods.java
RealYusufIsmail Oct 21, 2021
198c720
Update BanHelperMethods.java
RealYusufIsmail Oct 21, 2021
fa89b64
spotlessApply
RealYusufIsmail Oct 21, 2021
97a4aac
tweaked the ban command. Removed unneeded string in kick command
RealYusufIsmail Oct 22, 2021
7e165f9
spotlessJavaApply
RealYusufIsmail Oct 22, 2021
35b9939
Update BanCommand.java
RealYusufIsmail Oct 22, 2021
af1fe46
I have fixed what you told me to fix
RealYusufIsmail Oct 22, 2021
563998a
Update BanCommand.java
RealYusufIsmail Oct 22, 2021
c6dd085
Update BanCommand.java
RealYusufIsmail Oct 22, 2021
57e89f9
Update BanCommand.java
RealYusufIsmail Oct 22, 2021
6a4f39a
Update BanCommand.java
RealYusufIsmail Oct 22, 2021
2105989
Update BanCommand.java
RealYusufIsmail Oct 22, 2021
1532d75
Update BanCommand.java
RealYusufIsmail Oct 22, 2021
bc90bd4
Update BanCommand.java
RealYusufIsmail Oct 22, 2021
f7aac63
Fixed everything
RealYusufIsmail Oct 22, 2021
08be698
Fixed the loggers
RealYusufIsmail Oct 22, 2021
cdc8389
Update .gitignore
RealYusufIsmail Oct 22, 2021
12f6a40
fixed kick and unban command
RealYusufIsmail Oct 22, 2021
4d3f4c1
Update KickCommand.java
RealYusufIsmail Oct 23, 2021
19e7b57
Update UnbanCommand.java
RealYusufIsmail Oct 23, 2021
18e0588
Update UnbanCommand.java
RealYusufIsmail Oct 23, 2021
39c8c12
Update BanCommand.java
RealYusufIsmail Oct 23, 2021
7f2389f
Update BanCommand.java
RealYusufIsmail Oct 23, 2021
431d488
Update BanCommand.java
RealYusufIsmail Oct 23, 2021
47f9f28
Update BanCommand.java
RealYusufIsmail Oct 23, 2021
e3d2a5e
Update KickCommand.java
RealYusufIsmail Oct 23, 2021
5cef333
Update UnbanCommand.java
RealYusufIsmail Oct 23, 2021
7bfb288
fixed everything that i was told to fix
RealYusufIsmail Oct 23, 2021
547536f
Update UnbanCommand.java
RealYusufIsmail Oct 23, 2021
08c42dc
Update UnbanCommand.java
RealYusufIsmail Oct 23, 2021
b44f3af
Tweaking the commands
RealYusufIsmail Oct 23, 2021
6c4f3ed
improving the sentecences
RealYusufIsmail Oct 23, 2021
ec26837
added some loggers
RealYusufIsmail Oct 23, 2021
9d199d9
spotlessApply
RealYusufIsmail Oct 23, 2021
9edd2a5
fixed a gramatical mistake
RealYusufIsmail Oct 23, 2021
fb6b4f8
Update UnbanCommand.java
RealYusufIsmail Oct 23, 2021
e2aee23
Update V1__Database_Listener.sql
RealYusufIsmail Oct 23, 2021
d8f4bee
Update V1__Database_Listener.sql
RealYusufIsmail Oct 23, 2021
caa53a5
Fixed everything i was told to fix
RealYusufIsmail Oct 23, 2021
7626460
changed logger.error to logger.info
RealYusufIsmail Oct 24, 2021
6a28671
Removed TODO since codefactor said so
RealYusufIsmail Oct 24, 2021
69ea546
undo
RealYusufIsmail Oct 24, 2021
f4783bb
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 24, 2021
b7bbd22
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 24, 2021
ddda849
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 24, 2021
681ee09
fixed most of the code
RealYusufIsmail Oct 24, 2021
0bfeabe
Merge branch 'feature/kick_and_ban_commands' of https://github.com/Yu…
RealYusufIsmail Oct 24, 2021
8ac9bc6
Update KickCommand.java
RealYusufIsmail Oct 24, 2021
7801677
All checks should pass
RealYusufIsmail Oct 24, 2021
f04077d
Update UnbanCommand.java
RealYusufIsmail Oct 24, 2021
9bdee00
FIxed my sentences
RealYusufIsmail Oct 25, 2021
dbed14b
Update BanCommand.java
RealYusufIsmail Oct 25, 2021
0c9deec
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 25, 2021
0281c36
Fixed everything
RealYusufIsmail Oct 25, 2021
59e59ea
Merge branch 'feature/kick_and_ban_commands' of https://github.com/Yu…
RealYusufIsmail Oct 25, 2021
8ae5354
spotlessApply
RealYusufIsmail Oct 25, 2021
8bcd882
please fix spotlessApply
RealYusufIsmail Oct 25, 2021
a35e1aa
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 25, 2021
57a190c
spotlessApply
RealYusufIsmail Oct 25, 2021
be8d415
Merge branch 'feature/kick_and_ban_commands' of https://github.com/Yu…
RealYusufIsmail Oct 25, 2021
202eb61
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 25, 2021
06a7923
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 25, 2021
cf415ba
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 25, 2021
fe03fac
fixed the dm bug
RealYusufIsmail Oct 26, 2021
6c02458
fixed some more issues
RealYusufIsmail Oct 26, 2021
bf2e651
Update BanCommand.java
RealYusufIsmail Oct 26, 2021
dd842ad
Update KickCommand.java
RealYusufIsmail Oct 26, 2021
71cb447
Update KickCommand.java
RealYusufIsmail Oct 26, 2021
08bbc23
Update BanCommand.java
RealYusufIsmail Oct 26, 2021
ec722c4
fixed an issue which cause gradle build to fail
RealYusufIsmail Oct 26, 2021
0399339
hopefully fixed everything.
RealYusufIsmail Oct 26, 2021
78de545
Improved Unban And Ban Command
RealYusufIsmail Oct 27, 2021
79cbca1
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 27, 2021
6076a0f
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 27, 2021
aba48bb
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 27, 2021
3a4fdff
Fixed everything and I fixed the Objects.requireNonNull
RealYusufIsmail Oct 27, 2021
d29e57e
Update UnbanCommand.java
RealYusufIsmail Oct 27, 2021
5d8018f
Update UnbanCommand.java
RealYusufIsmail Oct 27, 2021
02d6d84
Update UnbanCommand.java
RealYusufIsmail Oct 27, 2021
d78895b
Revert "Update UnbanCommand.java"
RealYusufIsmail Oct 27, 2021
a4231f1
Fixed the sentence
RealYusufIsmail Oct 27, 2021
e78cc7f
Update UnbanCommand.java
RealYusufIsmail Oct 27, 2021
f868b11
Added reason for kick command
RealYusufIsmail Oct 27, 2021
6b8e1d4
improved the logger
RealYusufIsmail Oct 27, 2021
7bcb663
Update BanCommand.java
RealYusufIsmail Oct 27, 2021
c4e534b
Update BanCommand.java
RealYusufIsmail Oct 27, 2021
a769472
Update BanCommand.java
RealYusufIsmail Oct 27, 2021
05aaf96
All checks pass + fixed everything I was told to fix.
RealYusufIsmail Oct 28, 2021
8080286
Fixed everything
RealYusufIsmail Oct 28, 2021
149d83f
Update ModerationUtils.java
RealYusufIsmail Oct 28, 2021
c584417
Merge branch 'develop' into feature/kick_and_ban_commands
RealYusufIsmail Oct 29, 2021
cfb91fc
Fixed everything.
RealYusufIsmail Oct 29, 2021
33261e0
fixed an issue which cause member to null
RealYusufIsmail Oct 29, 2021
6990895
Fixed the issues
RealYusufIsmail Oct 29, 2021
f4449f9
added spaces to improve readbility
RealYusufIsmail Oct 29, 2021
2bf9f7b
Improved some sentences
RealYusufIsmail Oct 29, 2021
42c2269
Update ModerationUtils.java
RealYusufIsmail Oct 29, 2021
eeb64d2
Update KickCommand.java
RealYusufIsmail Oct 29, 2021
2163662
fixed an error on my side
RealYusufIsmail Oct 29, 2021
b002177
Update ModerationUtils.java
RealYusufIsmail Oct 29, 2021
792cb26
Fixed everything
RealYusufIsmail Oct 29, 2021
62ee69e
Update ModerationUtils.java
RealYusufIsmail Oct 29, 2021
d795cd2
Update ModerationUtils.java
RealYusufIsmail Oct 29, 2021
77441b0
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 30, 2021
04b120c
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 30, 2021
216201b
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 30, 2021
12c85a8
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 30, 2021
67de1ed
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 30, 2021
71a5514
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 30, 2021
de6992b
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 30, 2021
9170329
Update application/src/main/java/org/togetherjava/tjbot/commands/mode…
RealYusufIsmail Oct 30, 2021
4663673
Fixed
RealYusufIsmail Oct 30, 2021
24c6250
Fixed everything Tijs told me to fix
RealYusufIsmail Oct 30, 2021
79fe2a6
spotlessApply
RealYusufIsmail Nov 1, 2021
199cfee
I have fixed everything that Zabuzard told me to fix.
RealYusufIsmail Nov 2, 2021
4d9dce6
Fixed most of the commands.
RealYusufIsmail Nov 2, 2021
02e4803
addressed to the second build point.
RealYusufIsmail Nov 2, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,6 @@ logviewer/types.d.ts
logviewer/pnpm-lock.yaml
logviewer/webpack.config.js
logviewer/webpack.generated.js

### Mac Patch ###
.DS_Store
Comment thread
java-coding-prodigy marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jetbrains.annotations.NotNull;
import org.togetherjava.tjbot.commands.basic.DatabaseCommand;
import org.togetherjava.tjbot.commands.basic.PingCommand;
import org.togetherjava.tjbot.commands.moderation.*;
Comment thread
java-coding-prodigy marked this conversation as resolved.
Resolved
import org.togetherjava.tjbot.commands.basic.VcActivityCommand;
import org.togetherjava.tjbot.commands.mathcommands.TeXCommand;
import org.togetherjava.tjbot.commands.tags.TagCommand;
Expand Down Expand Up @@ -42,6 +43,7 @@ public enum Commands {
// available.
return List.of(new PingCommand(), new DatabaseCommand(database), new TeXCommand(),
new TagCommand(tagSystem), new TagManageCommand(tagSystem),
new TagsCommand(tagSystem), new VcActivityCommand());
new TagsCommand(tagSystem), new BanCommand(), new UnbanCommand(), new KickCommand(),
new VcActivityCommand());
}
}
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;
}
}
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";

Comment thread
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);
Comment thread
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);
}
}
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 {
Comment thread
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;
}
}
Loading