From 0ce0c778b64b890fbb783342ad95714fb92bc075 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 16:15:06 +0100 Subject: [PATCH 001/140] added ban and kick command --- .../togetherjava/tjbot/commands/Commands.java | 4 +- .../tjbot/commands/moderation/BanCommand.java | 70 +++++++++++++++++++ .../commands/moderation/KickCommand.java | 63 +++++++++++++++++ .../commands/moderation/package-info.java | 4 ++ 4 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/moderation/package-info.java diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java index 88d6f4dd6e..ee5e4e7fae 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java @@ -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.*; import org.togetherjava.tjbot.db.Database; import java.util.Collection; @@ -33,6 +34,7 @@ public enum Commands { // NOTE The command system can add special system relevant commands also by itself, // hence this list may not necessarily represent the full list of all commands actually // available. - return List.of(new PingCommand(), new DatabaseCommand(database)); + return List.of(new PingCommand(), new DatabaseCommand(database), new KickCommand(), new BanCommand()); + } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java new file mode 100644 index 0000000000..1b5c39c513 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -0,0 +1,70 @@ +package org.togetherjava.tjbot.commands.moderation; + +import net.dv8tion.jda.api.Permission; +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.InteractionHook; +import net.dv8tion.jda.api.interactions.commands.OptionMapping; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import org.jetbrains.annotations.NotNull; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; +import org.togetherjava.tjbot.commands.SlashCommandVisibility; + +/** + * @author RealYusufIsmail + */ +public class BanCommand extends SlashCommandAdapter { + public BanCommand() { + super("ban", "Use this command to ban a user", SlashCommandVisibility.GUILD); + + getData().addOption(OptionType.USER, "ban", "The user which you want to ban", true) + .addOption(OptionType.INTEGER, "del_days", "The delete message histroy", false); + + } + @Override + public void onSlashCommand(@NotNull SlashCommandEvent event) { + // Used to get the member + final Member member = event.getOption("user").getAsMember(); + + event.deferReply(true).queue(); // Let the user know we received the command before doing + // anything else + InteractionHook hook = event.getHook(); // This is a special webhook that allows you to send + // messages without having permissions in the + // channel and also allows ephemeral messages + hook.setEphemeral(true); // All messages here will now be ephemeral implicitly + + // Checks if the author has perms + if (!event.getMember().hasPermission(Permission.BAN_MEMBERS)) + { + hook.sendMessage("You do not have the required permissions to ban users from this server.").queue(); + return; + } + + // Checks if the bot has perms + Member selfMember = event.getGuild().getSelfMember(); + if (!selfMember.hasPermission(Permission.BAN_MEMBERS)) + { + hook.sendMessage("I don't have the required permissions to ban users from this server.").queue(); + return; + } + + + // Check if the user can be banned + if (member != null && !selfMember.canInteract(member)) + { + hook.sendMessage("This user is too powerful for me to ban.").queue(); + return; + } + + //Used to delete message history + int delDays = 0; + OptionMapping option = event.getOption("del_days"); + if (option != null) // null = not provided + delDays = (int) Math.max(0, Math.min(7, option.getAsLong())); + // Ban the user and send a success response + event.getGuild().ban(member, delDays) + .flatMap(v -> hook.sendMessage("Banned user " + member.getUser())) + .queue(); + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java new file mode 100644 index 0000000000..0c648f5496 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -0,0 +1,63 @@ +package org.togetherjava.tjbot.commands.moderation; + +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.InteractionHook; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import org.jetbrains.annotations.NotNull; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; +import org.togetherjava.tjbot.commands.SlashCommandVisibility; + +/** + * @author RealYusufIsmail + */ +public class KickCommand extends SlashCommandAdapter { + public KickCommand() { + super("Kick", "Use this command to kick a user", SlashCommandVisibility.GUILD); + + getData().addOption(OptionType.USER, "user", "The user which you want to kick", true); + } + + @Override + public void onSlashCommand(@NotNull SlashCommandEvent event) { + // Used to get the member + final Member member = event.getOption("user").getAsMember(); + + event.deferReply(true).queue(); // Let the user know we received the command before doing + // anything else + InteractionHook hook = event.getHook(); // This is a special webhook that allows you to send + // messages without having permissions in the + // channel and also allows ephemeral messages + hook.setEphemeral(true); // All messages here will now be ephemeral implicitly + + // Checks if the author has perms + if (!event.getMember().hasPermission(Permission.KICK_MEMBERS)) { + hook.sendMessage( + "You do not have the required permissions to kick users from this server.") + .queue(); + return; + } + + // Checks if the bot has perms + Member selfMember = event.getGuild().getSelfMember(); + if (!selfMember.hasPermission(Permission.KICK_MEMBERS)) { + hook.sendMessage( + "I don't have the required permissions to kick users from this server.") + .queue(); + return; + } + + // Check if the user can be kicked + if (member != null && !selfMember.canInteract(member)) { + hook.sendMessage("This user is too powerful for me to kick.").queue(); + return; + } + + // Kicks the user and send a success response + event.getGuild() + .kick(member) + .flatMap(v -> hook.sendMessage("kicked user " + member.getUser())) + .queue(); + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/package-info.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/package-info.java new file mode 100644 index 0000000000..7971cce2ff --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/package-info.java @@ -0,0 +1,4 @@ +/** + * This package offers all the moderation commands in the application. + */ +package org.togetherjava.tjbot.commands.moderation; From bc393fc2e4b35b839d1b4b3117ddf306dd7125be Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 16:17:49 +0100 Subject: [PATCH 002/140] Update Commands.java --- .../main/java/org/togetherjava/tjbot/commands/Commands.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java index ee5e4e7fae..f6d6d8d70c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java @@ -34,7 +34,8 @@ public enum Commands { // NOTE The command system can add special system relevant commands also by itself, // hence this list may not necessarily represent the full list of all commands actually // available. - return List.of(new PingCommand(), new DatabaseCommand(database), new KickCommand(), new BanCommand()); + return List.of(new PingCommand(), new DatabaseCommand(database), new KickCommand(), + new BanCommand()); } } From aa83502dafc218acc5d44adbbc4c827a5e876243 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 16:50:09 +0100 Subject: [PATCH 003/140] fixed typos --- .../tjbot/commands/moderation/BanCommand.java | 28 ++++++++++--------- .../commands/moderation/KickCommand.java | 4 +-- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 1b5c39c513..b68ef701f0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -19,9 +19,10 @@ public BanCommand() { super("ban", "Use this command to ban a user", SlashCommandVisibility.GUILD); getData().addOption(OptionType.USER, "ban", "The user which you want to ban", true) - .addOption(OptionType.INTEGER, "del_days", "The delete message histroy", false); + .addOption(OptionType.INTEGER, "del_days", "The delete message history", false); } + @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { // Used to get the member @@ -35,36 +36,37 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { hook.setEphemeral(true); // All messages here will now be ephemeral implicitly // Checks if the author has perms - if (!event.getMember().hasPermission(Permission.BAN_MEMBERS)) - { - hook.sendMessage("You do not have the required permissions to ban users from this server.").queue(); + if (!event.getMember().hasPermission(Permission.BAN_MEMBERS)) { + hook.sendMessage( + "You do not have the required permissions to ban users from this server.") + .queue(); return; } // Checks if the bot has perms Member selfMember = event.getGuild().getSelfMember(); - if (!selfMember.hasPermission(Permission.BAN_MEMBERS)) - { - hook.sendMessage("I don't have the required permissions to ban users from this server.").queue(); + if (!selfMember.hasPermission(Permission.BAN_MEMBERS)) { + hook.sendMessage("I don't have the required permissions to ban users from this server.") + .queue(); return; } // Check if the user can be banned - if (member != null && !selfMember.canInteract(member)) - { + if (member != null && !selfMember.canInteract(member)) { hook.sendMessage("This user is too powerful for me to ban.").queue(); return; } - //Used to delete message history + // Used to delete message history int delDays = 0; OptionMapping option = event.getOption("del_days"); if (option != null) // null = not provided delDays = (int) Math.max(0, Math.min(7, option.getAsLong())); // Ban the user and send a success response - event.getGuild().ban(member, delDays) - .flatMap(v -> hook.sendMessage("Banned user " + member.getUser())) - .queue(); + event.getGuild() + .ban(member, delDays) + .flatMap(v -> hook.sendMessage("Banned user " + member.getUser())) + .queue(); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 0c648f5496..92ca1517d8 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -25,7 +25,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { final Member member = event.getOption("user").getAsMember(); event.deferReply(true).queue(); // Let the user know we received the command before doing - // anything else + // anything else InteractionHook hook = event.getHook(); // This is a special webhook that allows you to send // messages without having permissions in the // channel and also allows ephemeral messages @@ -57,7 +57,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { // Kicks the user and send a success response event.getGuild() .kick(member) - .flatMap(v -> hook.sendMessage("kicked user " + member.getUser())) + .flatMap(v -> hook.sendMessage("Kicked the user" + member.getUser())) .queue(); } } From 246733552c8c6c4d9cf6549eaf846a96e577ce01 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 17:15:15 +0100 Subject: [PATCH 004/140] Update KickCommand.java --- .../commands/moderation/KickCommand.java | 43 +++++++++++++------ 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 92ca1517d8..edad380d10 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -9,27 +9,44 @@ import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.SlashCommandVisibility; -/** - * @author RealYusufIsmail - */ + public class KickCommand extends SlashCommandAdapter { + /** + * Creates an instance of the kick command. + */ public KickCommand() { super("Kick", "Use this command to kick a user", SlashCommandVisibility.GUILD); - getData().addOption(OptionType.USER, "user", "The user which you want to kick", true); + getData().addOption(OptionType.USER, "user", "The user which you want to kick", true) + .addOption(OptionType.STRING, "reason", "The reason of the kick", true); } + /** + * When triggered with {@code /kick @user reason}, the bot will respond will check if the user + * has perms. Then it will check if itself has perms to kick. If it does it will check if the user is the user + * is too powerful or not. If the user is not then bot will kick the user. + * + * @param event the corresponding event + */ @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { // Used to get the member - final Member member = event.getOption("user").getAsMember(); + Member member = event.getOption("user").getAsMember(); + + //Used for the reason + String reason = event.getOption("reason").getAsString(); + + // Let the user know we received the command before doing anything else + event.deferReply(true).queue(); + + // This is a special webhook that allows you to send + // messages without having permissions in the + // channel and also allows ephemeral messages + InteractionHook hook = event.getHook(); + + // All messages here will now be ephemeral implicitly + hook.setEphemeral(true); - event.deferReply(true).queue(); // Let the user know we received the command before doing - // anything else - InteractionHook hook = event.getHook(); // This is a special webhook that allows you to send - // messages without having permissions in the - // channel and also allows ephemeral messages - hook.setEphemeral(true); // All messages here will now be ephemeral implicitly // Checks if the author has perms if (!event.getMember().hasPermission(Permission.KICK_MEMBERS)) { @@ -56,8 +73,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { // Kicks the user and send a success response event.getGuild() - .kick(member) - .flatMap(v -> hook.sendMessage("Kicked the user" + member.getUser())) + .kick(member, reason) + .flatMap(v -> event.reply("Kicked the user" + member.getUser().getAsTag())) .queue(); } } From 551ec7659db6d0a35a50dc569bde8f78357f0ac0 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 17:18:29 +0100 Subject: [PATCH 005/140] Update BanCommand.java --- .../tjbot/commands/moderation/BanCommand.java | 49 ++++++++++++++----- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index b68ef701f0..dc48ec6392 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -11,29 +11,46 @@ import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.SlashCommandVisibility; -/** - * @author RealYusufIsmail - */ + public class BanCommand extends SlashCommandAdapter { + /** + * Creates an instance of the ban command. + */ public BanCommand() { super("ban", "Use this command to ban a user", SlashCommandVisibility.GUILD); - getData().addOption(OptionType.USER, "ban", "The user which you want to ban", true) - .addOption(OptionType.INTEGER, "del_days", "The delete message history", false); + getData().addOption(OptionType.USER, "user", "The user which you want to ban", true) + .addOption(OptionType.INTEGER, "del_days", "The delete message history", true) + .addOption(OptionType.STRING, "reason", "The reason of the ban", true); + } + /** + * When triggered with {@code /ban del_days @user reason}, the bot will respond will check if the user + * has perms. Then it will check if itself has perms to ban. If it does it will check if the user is the user + * is too powerful or not. If the user is not then bot will ban the user. + * + * @param event the corresponding event + */ @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { // Used to get the member - final Member member = event.getOption("user").getAsMember(); + Member member = event.getOption("user").getAsMember(); + + //Used for the reason of the ban + String reason = event.getOption("reason").getAsString(); - event.deferReply(true).queue(); // Let the user know we received the command before doing - // anything else - InteractionHook hook = event.getHook(); // This is a special webhook that allows you to send + // Let the user know we received the command before doing anything else + event.deferReply(true).queue(); + + // This is a special webhook that allows you to send // messages without having permissions in the // channel and also allows ephemeral messages - hook.setEphemeral(true); // All messages here will now be ephemeral implicitly + InteractionHook hook = event.getHook(); + + // All messages here will now be ephemeral implicitly + hook.setEphemeral(true); // Checks if the author has perms if (!event.getMember().hasPermission(Permission.BAN_MEMBERS)) { @@ -60,13 +77,19 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { // Used to delete message history int delDays = 0; + OptionMapping option = event.getOption("del_days"); - if (option != null) // null = not provided + + // null = not provided + if (option != null) { + hook.sendMessage("You have not provided a number"); delDays = (int) Math.max(0, Math.min(7, option.getAsLong())); + } + // Ban the user and send a success response event.getGuild() - .ban(member, delDays) - .flatMap(v -> hook.sendMessage("Banned user " + member.getUser())) + .ban(member, delDays, reason) + .flatMap(v -> event.reply("Banned the user " + member.getUser().getAsTag())) .queue(); } } From f8d59ac4a55fce2fffad75bb6394b0774bc1659c Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 17:19:15 +0100 Subject: [PATCH 006/140] Update BanCommand.java --- .../org/togetherjava/tjbot/commands/moderation/BanCommand.java | 1 - 1 file changed, 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index dc48ec6392..38b6f221ee 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -2,7 +2,6 @@ import net.dv8tion.jda.api.Permission; 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.InteractionHook; import net.dv8tion.jda.api.interactions.commands.OptionMapping; From a7e19c1b32c45abce36353d5cbef621c33473e92 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 19:15:46 +0100 Subject: [PATCH 007/140] Update KickCommand.java --- .../commands/moderation/KickCommand.java | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index edad380d10..c82ef3ac69 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -31,35 +31,23 @@ public KickCommand() { @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { // Used to get the member - Member member = event.getOption("user").getAsMember(); + final Member member = event.getOption("user").getAsMember(); //Used for the reason - String reason = event.getOption("reason").getAsString(); - - // Let the user know we received the command before doing anything else - event.deferReply(true).queue(); - - // This is a special webhook that allows you to send - // messages without having permissions in the - // channel and also allows ephemeral messages - InteractionHook hook = event.getHook(); - - // All messages here will now be ephemeral implicitly - hook.setEphemeral(true); - + final String reason = event.getOption("reason").getAsString(); // Checks if the author has perms if (!event.getMember().hasPermission(Permission.KICK_MEMBERS)) { - hook.sendMessage( + event.reply( "You do not have the required permissions to kick users from this server.") .queue(); return; } // Checks if the bot has perms - Member selfMember = event.getGuild().getSelfMember(); + final Member selfMember = event.getGuild().getSelfMember(); if (!selfMember.hasPermission(Permission.KICK_MEMBERS)) { - hook.sendMessage( + event.reply( "I don't have the required permissions to kick users from this server.") .queue(); return; @@ -67,7 +55,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { // Check if the user can be kicked if (member != null && !selfMember.canInteract(member)) { - hook.sendMessage("This user is too powerful for me to kick.").queue(); + event.reply("This user is too powerful for me to kick.").queue(); return; } From c0b796f93c4b90b8e7a4eb6aba2539b0f0e6f35d Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 19:17:42 +0100 Subject: [PATCH 008/140] Update KickCommand.java --- .../org/togetherjava/tjbot/commands/moderation/KickCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index c82ef3ac69..9d2e546a04 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -24,7 +24,7 @@ public KickCommand() { /** * When triggered with {@code /kick @user reason}, the bot will respond will check if the user * has perms. Then it will check if itself has perms to kick. If it does it will check if the user is the user - * is too powerful or not. If the user is not then bot will kick the user. + * is too powerful or not. If the user is not then bot will kick the user and reply with {@code Kicked User!}. * * @param event the corresponding event */ From 73e251712d780413985c6eee38db1d51614c9894 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 19:19:26 +0100 Subject: [PATCH 009/140] Update KickCommand.java --- .../togetherjava/tjbot/commands/moderation/KickCommand.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 9d2e546a04..3e96d47e39 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -6,11 +6,15 @@ import net.dv8tion.jda.api.interactions.InteractionHook; 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; public class KickCommand extends SlashCommandAdapter { + //the logger + private static final Logger logger = LoggerFactory.getLogger(KickCommand.class); /** * Creates an instance of the kick command. */ From 605a6809f1a9ef24b88b41bcec4e7627e6c758ad Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 19:22:23 +0100 Subject: [PATCH 010/140] Update KickCommand.java --- .../togetherjava/tjbot/commands/moderation/KickCommand.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 3e96d47e39..b138a75358 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -3,7 +3,6 @@ import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.interactions.InteractionHook; import net.dv8tion.jda.api.interactions.commands.OptionType; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; @@ -12,6 +11,10 @@ import org.togetherjava.tjbot.commands.SlashCommandVisibility; +/** + *

+ * The implemented command is {@code /kick @user reason}, upon which the bot will kick the user. + */ public class KickCommand extends SlashCommandAdapter { //the logger private static final Logger logger = LoggerFactory.getLogger(KickCommand.class); From 7910cce334cdb55bb4935c138deefafb06106271 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 19:24:14 +0100 Subject: [PATCH 011/140] Update KickCommand.java --- .../togetherjava/tjbot/commands/moderation/KickCommand.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index b138a75358..f7d3cde42e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -37,13 +37,10 @@ public KickCommand() { */ @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { - // Used to get the member final Member member = event.getOption("user").getAsMember(); - //Used for the reason final String reason = event.getOption("reason").getAsString(); - // Checks if the author has perms if (!event.getMember().hasPermission(Permission.KICK_MEMBERS)) { event.reply( "You do not have the required permissions to kick users from this server.") @@ -51,7 +48,6 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - // Checks if the bot has perms final Member selfMember = event.getGuild().getSelfMember(); if (!selfMember.hasPermission(Permission.KICK_MEMBERS)) { event.reply( @@ -60,7 +56,6 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - // Check if the user can be kicked if (member != null && !selfMember.canInteract(member)) { event.reply("This user is too powerful for me to kick.").queue(); return; From 44ed0bd3a4c57f19bc671bc442125fe60702ad8b Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 19:24:52 +0100 Subject: [PATCH 012/140] Update KickCommand.java --- .../tjbot/commands/moderation/KickCommand.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index f7d3cde42e..17250df00c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -10,6 +10,8 @@ import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import java.util.Objects; + /** *

@@ -37,18 +39,18 @@ public KickCommand() { */ @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { - final Member member = event.getOption("user").getAsMember(); + final Member member = Objects.requireNonNull(event.getOption("user")).getAsMember(); - final String reason = event.getOption("reason").getAsString(); + final String reason = Objects.requireNonNull(event.getOption("reason")).getAsString(); - if (!event.getMember().hasPermission(Permission.KICK_MEMBERS)) { + if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.KICK_MEMBERS)) { event.reply( "You do not have the required permissions to kick users from this server.") .queue(); return; } - final Member selfMember = event.getGuild().getSelfMember(); + final Member selfMember = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!selfMember.hasPermission(Permission.KICK_MEMBERS)) { event.reply( "I don't have the required permissions to kick users from this server.") From 32260430d4d61b1bef40248a23c10ae526cd7d40 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 19:26:50 +0100 Subject: [PATCH 013/140] Fix the rest tomorrow --- .../tjbot/commands/moderation/BanCommand.java | 47 ++++++++----------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 38b6f221ee..f310dff090 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -7,11 +7,21 @@ 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; + +/** + *

+ * The implemented command is {@code @user /ban delete-message-history-days reason}, upon which the bot will ban the user. + */ public class BanCommand extends SlashCommandAdapter { + //the logger + private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); /** * Creates an instance of the ban command. */ @@ -19,7 +29,7 @@ public BanCommand() { super("ban", "Use this command to ban a user", SlashCommandVisibility.GUILD); getData().addOption(OptionType.USER, "user", "The user which you want to ban", true) - .addOption(OptionType.INTEGER, "del_days", "The delete message history", true) + .addOption(OptionType.INTEGER, "delete-message-history-days", "The delete message history", true) .addOption(OptionType.STRING, "reason", "The reason of the ban", true); @@ -35,55 +45,38 @@ public BanCommand() { @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { // Used to get the member - Member member = event.getOption("user").getAsMember(); + final Member member = Objects.requireNonNull(event.getOption("user")).getAsMember(); //Used for the reason of the ban - String reason = event.getOption("reason").getAsString(); - - // Let the user know we received the command before doing anything else - event.deferReply(true).queue(); - - // This is a special webhook that allows you to send - // messages without having permissions in the - // channel and also allows ephemeral messages - InteractionHook hook = event.getHook(); + final String reason = Objects.requireNonNull(event.getOption("reason")).getAsString(); - // All messages here will now be ephemeral implicitly - hook.setEphemeral(true); - - // Checks if the author has perms - if (!event.getMember().hasPermission(Permission.BAN_MEMBERS)) { - hook.sendMessage( + if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.BAN_MEMBERS)) { + event.reply( "You do not have the required permissions to ban users from this server.") .queue(); return; } - // Checks if the bot has perms - Member selfMember = event.getGuild().getSelfMember(); + Member selfMember = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!selfMember.hasPermission(Permission.BAN_MEMBERS)) { - hook.sendMessage("I don't have the required permissions to ban users from this server.") + event.reply("I don't have the required permissions to ban users from this server.") .queue(); return; } - - // Check if the user can be banned if (member != null && !selfMember.canInteract(member)) { - hook.sendMessage("This user is too powerful for me to ban.").queue(); + event.reply("This user is too powerful for me to ban.").queue(); return; } // Used to delete message history int delDays = 0; - OptionMapping option = event.getOption("del_days"); + OptionMapping option = event.getOption("delete-message-history-days"); // null = not provided - if (option != null) { - hook.sendMessage("You have not provided a number"); + if (option != null) delDays = (int) Math.max(0, Math.min(7, option.getAsLong())); - } // Ban the user and send a success response event.getGuild() From b6288111ac5f5c411727c628339e6e6f31fa7c44 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 19:38:35 +0100 Subject: [PATCH 014/140] Update KickCommand.java --- .../tjbot/commands/moderation/KickCommand.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 17250df00c..5a779eb9eb 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -39,7 +39,7 @@ public KickCommand() { */ @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { - final Member member = Objects.requireNonNull(event.getOption("user")).getAsMember(); + final Member user = Objects.requireNonNull(event.getOption("user")).getAsMember(); final String reason = Objects.requireNonNull(event.getOption("reason")).getAsString(); @@ -58,15 +58,19 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - if (member != null && !selfMember.canInteract(member)) { + if (user != null && !selfMember.canInteract(user)) { event.reply("This user is too powerful for me to kick.").queue(); return; } + + assert user != null; + logger.error("The user is not provided"); + // Kicks the user and send a success response event.getGuild() - .kick(member, reason) - .flatMap(v -> event.reply("Kicked the user" + member.getUser().getAsTag())) + .kick(user, reason) + .flatMap(v -> event.reply("Kicked the user" + user.getUser().getAsTag())) .queue(); } } From f1a4aa5a855d89a449a7985ee5af094d7c449b41 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 19:40:11 +0100 Subject: [PATCH 015/140] Update KickCommand.java --- .../org/togetherjava/tjbot/commands/moderation/KickCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 5a779eb9eb..e9e7e4f43e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -24,7 +24,7 @@ public class KickCommand extends SlashCommandAdapter { * Creates an instance of the kick command. */ public KickCommand() { - super("Kick", "Use this command to kick a user", SlashCommandVisibility.GUILD); + super("kick", "Use this command to kick a user", SlashCommandVisibility.GUILD); getData().addOption(OptionType.USER, "user", "The user which you want to kick", true) .addOption(OptionType.STRING, "reason", "The reason of the kick", true); From a46b4c2f7656f86fbebba37bd3f7c48add3298cd Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 19:46:28 +0100 Subject: [PATCH 016/140] Update KickCommand.java --- .../togetherjava/tjbot/commands/moderation/KickCommand.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index e9e7e4f43e..bdf795aa1c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -67,6 +67,10 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { assert user != null; logger.error("The user is not provided"); + //Add this to audit log + logger.info("User '{}' Kicked user '{}' Reason was '{}'", + selfMember, user, reason); + // Kicks the user and send a success response event.getGuild() .kick(user, reason) From e3dc6dedf06f8a67f4e0411481062200f3b07520 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 20:10:53 +0100 Subject: [PATCH 017/140] Update BanCommand.java --- .../tjbot/commands/moderation/BanCommand.java | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index f310dff090..fb9460e244 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -3,8 +3,6 @@ import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.interactions.InteractionHook; -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; @@ -45,7 +43,7 @@ public BanCommand() { @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { // Used to get the member - final Member member = Objects.requireNonNull(event.getOption("user")).getAsMember(); + final Member user = Objects.requireNonNull(event.getOption("user")).getAsMember(); //Used for the reason of the ban final String reason = Objects.requireNonNull(event.getOption("reason")).getAsString(); @@ -64,24 +62,37 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - if (member != null && !selfMember.canInteract(member)) { + if (user != null && !selfMember.canInteract(user)) { event.reply("This user is too powerful for me to ban.").queue(); return; } // Used to delete message history - int delDays = 0; + long option = Objects.requireNonNull(event.getOption("delete-message-history-days")).getAsLong(); + int delDays = (int) option; - OptionMapping option = event.getOption("delete-message-history-days"); + if(delDays < 1) { + event.reply("The deletion days of the messages must be between 1 and 7 days"); + } else if(delDays > 7) { + event.reply("The deletion days of the messages must be between 1 and 7 days"); - // null = not provided - if (option != null) - delDays = (int) Math.max(0, Math.min(7, option.getAsLong())); + } else { + delDays = Math.toIntExact(option); + } + + + + assert user != null; + logger.error("The user is not provided"); + + //Add this to audit log + logger.info("User '{}' banned user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", + selfMember, user, delDays, reason); // Ban the user and send a success response event.getGuild() - .ban(member, delDays, reason) - .flatMap(v -> event.reply("Banned the user " + member.getUser().getAsTag())) + .ban(user, delDays, reason) + .flatMap(v -> event.reply("Banned the user " + user.getUser().getAsTag())) .queue(); } } From 2aba8c23c071d9e7acaf63f51a2a5479bdfb9051 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 20:17:32 +0100 Subject: [PATCH 018/140] Update BanCommand.java --- .../tjbot/commands/moderation/BanCommand.java | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index fb9460e244..646a12040d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -15,11 +15,13 @@ /** *

- * The implemented command is {@code @user /ban delete-message-history-days reason}, upon which the bot will ban the user. + * The implemented command is {@code @user /ban delete-message-history-days reason}, upon which the + * bot will ban the user. */ public class BanCommand extends SlashCommandAdapter { - //the logger + // the logger private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); + /** * Creates an instance of the ban command. */ @@ -27,16 +29,18 @@ public BanCommand() { super("ban", "Use this command to ban a user", SlashCommandVisibility.GUILD); getData().addOption(OptionType.USER, "user", "The user which you want to ban", true) - .addOption(OptionType.INTEGER, "delete-message-history-days", "The delete message history", true) - .addOption(OptionType.STRING, "reason", "The reason of the ban", true); + .addOption(OptionType.INTEGER, "delete-message-history-days", + "The delete message history", true) + .addOption(OptionType.STRING, "reason", "The reason of the ban", true); } /** - * When triggered with {@code /ban del_days @user reason}, the bot will respond will check if the user - * has perms. Then it will check if itself has perms to ban. If it does it will check if the user is the user - * is too powerful or not. If the user is not then bot will ban the user. + * When triggered with {@code /ban del_days @user reason}, the bot will respond will check if + * the user has perms. Then it will check if itself has perms to ban. If it does it will check + * if the user is the user is too powerful or not. If the user is not then bot will ban the + * user. * * @param event the corresponding event */ @@ -45,12 +49,11 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { // Used to get the member final Member user = Objects.requireNonNull(event.getOption("user")).getAsMember(); - //Used for the reason of the ban + // Used for the reason of the ban final String reason = Objects.requireNonNull(event.getOption("reason")).getAsString(); if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.BAN_MEMBERS)) { - event.reply( - "You do not have the required permissions to ban users from this server.") + event.reply("You do not have the required permissions to ban users from this server.") .queue(); return; } @@ -68,12 +71,13 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { } // Used to delete message history - long option = Objects.requireNonNull(event.getOption("delete-message-history-days")).getAsLong(); + long option = + Objects.requireNonNull(event.getOption("delete-message-history-days")).getAsLong(); int delDays = (int) option; - if(delDays < 1) { + if (delDays < 1) { event.reply("The deletion days of the messages must be between 1 and 7 days"); - } else if(delDays > 7) { + } else if (delDays > 7) { event.reply("The deletion days of the messages must be between 1 and 7 days"); } else { @@ -85,8 +89,9 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { assert user != null; logger.error("The user is not provided"); - //Add this to audit log - logger.info("User '{}' banned user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", + // Add this to audit log + logger.info( + "User '{}' banned user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", selfMember, user, delDays, reason); // Ban the user and send a success response From 111eec4a454902ee57dabd42b4fd153d70007bd8 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 20:17:41 +0100 Subject: [PATCH 019/140] Update KickCommand.java --- .../commands/moderation/KickCommand.java | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index bdf795aa1c..dfe3e04987 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -18,8 +18,9 @@ * The implemented command is {@code /kick @user reason}, upon which the bot will kick the user. */ public class KickCommand extends SlashCommandAdapter { - //the logger + // the logger private static final Logger logger = LoggerFactory.getLogger(KickCommand.class); + /** * Creates an instance of the kick command. */ @@ -27,13 +28,14 @@ public KickCommand() { super("kick", "Use this command to kick a user", SlashCommandVisibility.GUILD); getData().addOption(OptionType.USER, "user", "The user which you want to kick", true) - .addOption(OptionType.STRING, "reason", "The reason of the kick", true); + .addOption(OptionType.STRING, "reason", "The reason of the kick", true); } /** * When triggered with {@code /kick @user reason}, the bot will respond will check if the user - * has perms. Then it will check if itself has perms to kick. If it does it will check if the user is the user - * is too powerful or not. If the user is not then bot will kick the user and reply with {@code Kicked User!}. + * has perms. Then it will check if itself has perms to kick. If it does it will check if the + * user is the user is too powerful or not. If the user is not then bot will kick the user and + * reply with {@code Kicked User!}. * * @param event the corresponding event */ @@ -44,16 +46,14 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { final String reason = Objects.requireNonNull(event.getOption("reason")).getAsString(); if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.KICK_MEMBERS)) { - event.reply( - "You do not have the required permissions to kick users from this server.") + event.reply("You do not have the required permissions to kick users from this server.") .queue(); return; } final Member selfMember = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!selfMember.hasPermission(Permission.KICK_MEMBERS)) { - event.reply( - "I don't have the required permissions to kick users from this server.") + event.reply("I don't have the required permissions to kick users from this server.") .queue(); return; } @@ -67,9 +67,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { assert user != null; logger.error("The user is not provided"); - //Add this to audit log - logger.info("User '{}' Kicked user '{}' Reason was '{}'", - selfMember, user, reason); + // Add this to audit log + logger.info("User '{}' Kicked user '{}' Reason was '{}'", selfMember, user, reason); // Kicks the user and send a success response event.getGuild() From 05694be5d2c0918bc1402179425753680222180b Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 21:48:01 +0100 Subject: [PATCH 020/140] Update BanCommand.java --- .../org/togetherjava/tjbot/commands/moderation/BanCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 646a12040d..25a89f6820 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -15,7 +15,7 @@ /** *

- * The implemented command is {@code @user /ban delete-message-history-days reason}, upon which the + * The implemented command is {@code /ban @user delete-message-history-days reason}, upon which the * bot will ban the user. */ public class BanCommand extends SlashCommandAdapter { From b9663042980cc30a93991c91765e156499e92d09 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 21:52:20 +0100 Subject: [PATCH 021/140] added UnBan Command --- .../togetherjava/tjbot/commands/Commands.java | 2 +- .../tjbot/commands/moderation/BanCommand.java | 8 +-- .../commands/moderation/KickCommand.java | 8 +-- .../commands/moderation/UnBanCommand.java | 68 +++++++++++++++++++ 4 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java index f6d6d8d70c..1c0e9e55e0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java @@ -35,7 +35,7 @@ public enum Commands { // hence this list may not necessarily represent the full list of all commands actually // available. return List.of(new PingCommand(), new DatabaseCommand(database), new KickCommand(), - new BanCommand()); + new BanCommand(), new UnBanCommand()); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 25a89f6820..8fcbc03533 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -58,14 +58,14 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - Member selfMember = Objects.requireNonNull(event.getGuild()).getSelfMember(); - if (!selfMember.hasPermission(Permission.BAN_MEMBERS)) { + Member author = Objects.requireNonNull(event.getGuild()).getSelfMember(); + if (!author.hasPermission(Permission.BAN_MEMBERS)) { event.reply("I don't have the required permissions to ban users from this server.") .queue(); return; } - if (user != null && !selfMember.canInteract(user)) { + if (user != null && !author.canInteract(user)) { event.reply("This user is too powerful for me to ban.").queue(); return; } @@ -92,7 +92,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { // Add this to audit log logger.info( "User '{}' banned user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", - selfMember, user, delDays, reason); + author, user, delDays, reason); // Ban the user and send a success response event.getGuild() diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index dfe3e04987..8bce55a1af 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -51,14 +51,14 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - final Member selfMember = Objects.requireNonNull(event.getGuild()).getSelfMember(); - if (!selfMember.hasPermission(Permission.KICK_MEMBERS)) { + final Member author = Objects.requireNonNull(event.getGuild()).getSelfMember(); + if (!author.hasPermission(Permission.KICK_MEMBERS)) { event.reply("I don't have the required permissions to kick users from this server.") .queue(); return; } - if (user != null && !selfMember.canInteract(user)) { + if (user != null && !author.canInteract(user)) { event.reply("This user is too powerful for me to kick.").queue(); return; } @@ -68,7 +68,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { logger.error("The user is not provided"); // Add this to audit log - logger.info("User '{}' Kicked user '{}' Reason was '{}'", selfMember, user, reason); + logger.info("User '{}' Kicked user '{}' Reason was '{}'", author, user, reason); // Kicks the user and send a success response event.getGuild() diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java new file mode 100644 index 0000000000..5ab8b4e4f1 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java @@ -0,0 +1,68 @@ +package org.togetherjava.tjbot.commands.moderation; + +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +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; + +/** + *

+ * The implemented command is {@code /unban user_id}, upon which the + * bot will unban the user. + */ +public class UnBanCommand extends SlashCommandAdapter { + // the logger + private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); + + /** + * Creates an instance of the ban command. + */ + public UnBanCommand() { + super("unban", "Use this command to unban a user", SlashCommandVisibility.GUILD); + + getData().addOption(OptionType.STRING, "user_id", "The user if of the user which you want to unban", + true); + + } + + /** + * When triggered with {@code /unban user_id}}, the bot will respond will check if + * the user has perms. Then it will check if itself has perms to unban. If it does it will check + * if the user is the user is too powerful or not. If the user is not then unban will ban the + * user. + * + * @param event the corresponding event + */ + @Override + public void onSlashCommand(@NotNull SlashCommandEvent event) { + final String userId = Objects.requireNonNull(event.getOption("user_id")).getAsString(); + + if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.BAN_MEMBERS)) + { + event.reply("You do not have the required permissions to ban users from this server.").queue(); + return; + } + + Member author = Objects.requireNonNull(event.getGuild()).getSelfMember(); + if (!author.hasPermission(Permission.BAN_MEMBERS)) + { + event.reply("I don't have the required permissions to unban the user from this server.").queue(); + return; + } + + // Add this to audit log + logger.info("User '{}' unbanned user id '{}'", author, userId); + + //Unbans the user + event.getGuild().unban(userId) + .flatMap(v -> event.reply("Unbanned the user")) + .queue(); + } +} From 0c92c3e962eb023d9b65aa5a9f6b3538fc941339 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 16 Oct 2021 21:55:56 +0100 Subject: [PATCH 022/140] Update UnBanCommand.java --- .../commands/moderation/UnBanCommand.java | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java index 5ab8b4e4f1..4ca6e2cf82 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java @@ -14,8 +14,7 @@ /** *

- * The implemented command is {@code /unban user_id}, upon which the - * bot will unban the user. + * The implemented command is {@code /unban user_id}, upon which the bot will unban the user. */ public class UnBanCommand extends SlashCommandAdapter { // the logger @@ -27,16 +26,15 @@ public class UnBanCommand extends SlashCommandAdapter { public UnBanCommand() { super("unban", "Use this command to unban a user", SlashCommandVisibility.GUILD); - getData().addOption(OptionType.STRING, "user_id", "The user if of the user which you want to unban", - true); + getData().addOption(OptionType.STRING, "user_id", + "The user if of the user which you want to unban", true); } /** - * When triggered with {@code /unban user_id}}, the bot will respond will check if - * the user has perms. Then it will check if itself has perms to unban. If it does it will check - * if the user is the user is too powerful or not. If the user is not then unban will ban the - * user. + * When triggered with {@code /unban user_id}}, the bot will respond will check if the user has + * perms. Then it will check if itself has perms to unban. If it does it will check if the user + * is the user is too powerful or not. If the user is not then unban will ban the user. * * @param event the corresponding event */ @@ -44,25 +42,23 @@ public UnBanCommand() { public void onSlashCommand(@NotNull SlashCommandEvent event) { final String userId = Objects.requireNonNull(event.getOption("user_id")).getAsString(); - if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.BAN_MEMBERS)) - { - event.reply("You do not have the required permissions to ban users from this server.").queue(); + if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.BAN_MEMBERS)) { + event.reply("You do not have the required permissions to ban users from this server.") + .queue(); return; } Member author = Objects.requireNonNull(event.getGuild()).getSelfMember(); - if (!author.hasPermission(Permission.BAN_MEMBERS)) - { - event.reply("I don't have the required permissions to unban the user from this server.").queue(); + if (!author.hasPermission(Permission.BAN_MEMBERS)) { + event.reply("I don't have the required permissions to unban the user from this server.") + .queue(); return; } // Add this to audit log logger.info("User '{}' unbanned user id '{}'", author, userId); - //Unbans the user - event.getGuild().unban(userId) - .flatMap(v -> event.reply("Unbanned the user")) - .queue(); + // Unbans the user + event.getGuild().unban(userId).flatMap(v -> event.reply("Unbanned the user")).queue(); } } From 9f7dc06f4d4766835f6f0b8c19d5e432b4913f6a Mon Sep 17 00:00:00 2001 From: Yusuf Arfan Ismail Date: Sun, 17 Oct 2021 09:11:59 +0100 Subject: [PATCH 023/140] Apply suggestions from code review Co-authored-by: java-coding-prodigy <85149944+java-coding-prodigy@users.noreply.github.com> --- .../togetherjava/tjbot/commands/moderation/UnBanCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java index 4ca6e2cf82..b741c530cc 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java @@ -27,7 +27,7 @@ public UnBanCommand() { super("unban", "Use this command to unban a user", SlashCommandVisibility.GUILD); getData().addOption(OptionType.STRING, "user_id", - "The user if of the user which you want to unban", true); + "The id of the user which you want to unban", true); } From dbe0a35ce25f0f769ad2f8ccf3e7a85d906e1c0d Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 09:14:01 +0100 Subject: [PATCH 024/140] Update UnBanCommand.java --- .../togetherjava/tjbot/commands/moderation/UnBanCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java index 4ca6e2cf82..66d55e4cd4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java @@ -27,7 +27,7 @@ public UnBanCommand() { super("unban", "Use this command to unban a user", SlashCommandVisibility.GUILD); getData().addOption(OptionType.STRING, "user_id", - "The user if of the user which you want to unban", true); + "The user id of the user which you want to unban", true); } From 01d8069a12fe31925b48b5852aeec7e744a839c3 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 09:47:50 +0100 Subject: [PATCH 025/140] fixed all commands --- .../tjbot/commands/moderation/BanCommand.java | 80 ++++++++----------- .../commands/moderation/KickCommand.java | 37 ++++----- .../commands/moderation/UnBanCommand.java | 17 ++-- 3 files changed, 62 insertions(+), 72 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 8fcbc03533..2eccc20d22 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -14,13 +14,18 @@ /** - *

- * The implemented command is {@code /ban @user delete-message-history-days reason}, upon which the - * bot will ban the user. + * When triggered with {@code /ban del_days @user rea son}, the bot will respond then will check if + * the user has perms. Then it will check if itself has perms to ban. If it does it will check + * if the user is too powerful or not. If the user is not then bot will ban the + * user abd reply with {@code Banned User!}. + * */ -public class BanCommand extends SlashCommandAdapter { +public final class BanCommand extends SlashCommandAdapter { // the logger private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); + private static final String USER_OPTION = "user"; + private static final String DELETE_MESSAGE_HISTORY_DAYS_OPTION = "delete-message-history-days"; + private static final String REASON_OPTION = "reason"; /** * Creates an instance of the ban command. @@ -28,76 +33,59 @@ public class BanCommand extends SlashCommandAdapter { public BanCommand() { super("ban", "Use this command to ban a user", SlashCommandVisibility.GUILD); - getData().addOption(OptionType.USER, "user", "The user which you want to ban", true) - .addOption(OptionType.INTEGER, "delete-message-history-days", - "The delete message history", true) - .addOption(OptionType.STRING, "reason", "The reason of the ban", true); + getData().addOption(OptionType.USER, USER_OPTION, "The user which you want to ban", true) + .addOption(OptionType.INTEGER, DELETE_MESSAGE_HISTORY_DAYS_OPTION, + "The delete message history. Between 1 to 7 days", true) + .addOption(OptionType.STRING, REASON_OPTION, "The reason of the ban", true); } - /** - * When triggered with {@code /ban del_days @user reason}, the bot will respond will check if - * the user has perms. Then it will check if itself has perms to ban. If it does it will check - * if the user is the user is too powerful or not. If the user is not then bot will ban the - * user. - * - * @param event the corresponding event - */ + @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { - // Used to get the member - final Member user = Objects.requireNonNull(event.getOption("user")).getAsMember(); + Member user = Objects.requireNonNull(event.getOption(USER_OPTION)).getAsMember(); - // Used for the reason of the ban - final String reason = Objects.requireNonNull(event.getOption("reason")).getAsString(); + String reason = Objects.requireNonNull(event.getOption(REASON_OPTION)).getAsString(); if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.BAN_MEMBERS)) { event.reply("You do not have the required permissions to ban users from this server.") - .queue(); + .setEphemeral(true).queue(); return; } Member author = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!author.hasPermission(Permission.BAN_MEMBERS)) { event.reply("I don't have the required permissions to ban users from this server.") - .queue(); + .setEphemeral(true).queue(); return; } - - if (user != null && !author.canInteract(user)) { - event.reply("This user is too powerful for me to ban.").queue(); + + if (!author.canInteract(Objects.requireNonNull(user))) { + event.reply("This user is too powerful for me to ban.") + .setEphemeral(true).queue(); return; } - // Used to delete message history - long option = - Objects.requireNonNull(event.getOption("delete-message-history-days")).getAsLong(); - int delDays = (int) option; - if (delDays < 1) { - event.reply("The deletion days of the messages must be between 1 and 7 days"); - } else if (delDays > 7) { - event.reply("The deletion days of the messages must be between 1 and 7 days"); + int deleteMessageHistoryDays = (int)Objects.requireNonNull( + event.getOption("delete-message-history-days")).getAsLong(); - } else { - delDays = Math.toIntExact(option); + if (deleteMessageHistoryDays < 1 || deleteMessageHistoryDays > 7) { + event.reply("The deletion days of the messages must be between 1 and 7 days") + .setEphemeral(true).queue(); + return; } - - - assert user != null; - logger.error("The user is not provided"); - - // Add this to audit log - logger.info( - "User '{}' banned user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", - author, user, delDays, reason); - // Ban the user and send a success response event.getGuild() - .ban(user, delDays, reason) + .ban(user, deleteMessageHistoryDays, reason) .flatMap(v -> event.reply("Banned the user " + user.getUser().getAsTag())) .queue(); + + // Add this to audit log + logger.info( + "User '{}' banned user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", + author, user, deleteMessageHistoryDays, reason); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 8bce55a1af..b0a9e2f455 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -17,9 +17,12 @@ *

* The implemented command is {@code /kick @user reason}, upon which the bot will kick the user. */ -public class KickCommand extends SlashCommandAdapter { +public final class KickCommand extends SlashCommandAdapter { // the logger private static final Logger logger = LoggerFactory.getLogger(KickCommand.class); + private static final String USER_OPTION = "user"; + private static final String REASON_OPTION = "reason"; + /** * Creates an instance of the kick command. @@ -27,53 +30,51 @@ public class KickCommand extends SlashCommandAdapter { public KickCommand() { super("kick", "Use this command to kick a user", SlashCommandVisibility.GUILD); - getData().addOption(OptionType.USER, "user", "The user which you want to kick", true) - .addOption(OptionType.STRING, "reason", "The reason of the kick", true); + getData().addOption(OptionType.USER, USER_OPTION, "The user which you want to kick", true) + .addOption(OptionType.STRING, REASON_OPTION, "The reason of the kick", true); } /** - * When triggered with {@code /kick @user reason}, the bot will respond will check if the user + * When triggered with {@code /kick @user reason}, the bot will respond then it will check if the user * has perms. Then it will check if itself has perms to kick. If it does it will check if the - * user is the user is too powerful or not. If the user is not then bot will kick the user and + * user is too powerful or not. If the user is not then bot will kick the user and * reply with {@code Kicked User!}. * * @param event the corresponding event */ @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { - final Member user = Objects.requireNonNull(event.getOption("user")).getAsMember(); + Member user = Objects.requireNonNull(event.getOption(USER_OPTION)).getAsMember(); - final String reason = Objects.requireNonNull(event.getOption("reason")).getAsString(); + String reason = Objects.requireNonNull(event.getOption(REASON_OPTION)).getAsString(); if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.KICK_MEMBERS)) { event.reply("You do not have the required permissions to kick users from this server.") - .queue(); + .setEphemeral(true).queue(); return; } - final Member author = Objects.requireNonNull(event.getGuild()).getSelfMember(); + Member author = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!author.hasPermission(Permission.KICK_MEMBERS)) { event.reply("I don't have the required permissions to kick users from this server.") - .queue(); + .setEphemeral(true).queue(); return; } - if (user != null && !author.canInteract(user)) { - event.reply("This user is too powerful for me to kick.").queue(); + if (!author.canInteract(Objects.requireNonNull(user))) { + event.reply("This user is too powerful for me to kick.") + .setEphemeral(true).queue(); return; } - assert user != null; - logger.error("The user is not provided"); - - // Add this to audit log - logger.info("User '{}' Kicked user '{}' Reason was '{}'", author, user, reason); - // Kicks the user and send a success response event.getGuild() .kick(user, reason) .flatMap(v -> event.reply("Kicked the user" + user.getUser().getAsTag())) .queue(); + + // Add this to audit log + logger.info("User '{}' Kicked user '{}' Reason was '{}'", author, user, reason); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java index 66d55e4cd4..4cedaaf85c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java @@ -16,9 +16,10 @@ *

* The implemented command is {@code /unban user_id}, upon which the bot will unban the user. */ -public class UnBanCommand extends SlashCommandAdapter { +public final class UnBanCommand extends SlashCommandAdapter { // the logger private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); + private static final String USER_ID = "user_id"; /** * Creates an instance of the ban command. @@ -26,7 +27,7 @@ public class UnBanCommand extends SlashCommandAdapter { public UnBanCommand() { super("unban", "Use this command to unban a user", SlashCommandVisibility.GUILD); - getData().addOption(OptionType.STRING, "user_id", + getData().addOption(OptionType.STRING, USER_ID, "The user id of the user which you want to unban", true); } @@ -40,25 +41,25 @@ public UnBanCommand() { */ @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { - final String userId = Objects.requireNonNull(event.getOption("user_id")).getAsString(); + final String userId = Objects.requireNonNull(event.getOption(USER_ID)).getAsString(); if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.BAN_MEMBERS)) { event.reply("You do not have the required permissions to ban users from this server.") - .queue(); + .setEphemeral(true).queue(); return; } Member author = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!author.hasPermission(Permission.BAN_MEMBERS)) { event.reply("I don't have the required permissions to unban the user from this server.") - .queue(); + .setEphemeral(true).queue(); return; } - // Add this to audit log - logger.info("User '{}' unbanned user id '{}'", author, userId); - // Unbans the user event.getGuild().unban(userId).flatMap(v -> event.reply("Unbanned the user")).queue(); + + // Add this to audit log + logger.info("User '{}' unbanned user id '{}'", author, userId); } } From a55ecc01b9ec0edf7d76f21f4d940db9b2b17eaa Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 09:52:53 +0100 Subject: [PATCH 026/140] spotlessApply --- .../tjbot/commands/moderation/BanCommand.java | 27 ++++++++++--------- .../commands/moderation/KickCommand.java | 17 ++++++------ .../commands/moderation/UnBanCommand.java | 6 +++-- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 2eccc20d22..c2f83afa53 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -14,10 +14,10 @@ /** - * When triggered with {@code /ban del_days @user rea son}, the bot will respond then will check if - * the user has perms. Then it will check if itself has perms to ban. If it does it will check - * if the user is too powerful or not. If the user is not then bot will ban the - * user abd reply with {@code Banned User!}. + * When triggered with {@code /ban del_days @user rea son}, the bot will respond then will check + * if the user has perms. Then it will check if itself has perms to ban. If it does it will check if + * the user is too powerful or not. If the user is not then bot will ban the user abd reply with + * {@code Banned User!}. * */ public final class BanCommand extends SlashCommandAdapter { @@ -50,30 +50,33 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.BAN_MEMBERS)) { event.reply("You do not have the required permissions to ban users from this server.") - .setEphemeral(true).queue(); + .setEphemeral(true) + .queue(); return; } Member author = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!author.hasPermission(Permission.BAN_MEMBERS)) { event.reply("I don't have the required permissions to ban users from this server.") - .setEphemeral(true).queue(); + .setEphemeral(true) + .queue(); return; } - + if (!author.canInteract(Objects.requireNonNull(user))) { - event.reply("This user is too powerful for me to ban.") - .setEphemeral(true).queue(); + event.reply("This user is too powerful for me to ban.").setEphemeral(true).queue(); return; } - int deleteMessageHistoryDays = (int)Objects.requireNonNull( - event.getOption("delete-message-history-days")).getAsLong(); + int deleteMessageHistoryDays = + (int) Objects.requireNonNull(event.getOption("delete-message-history-days")) + .getAsLong(); if (deleteMessageHistoryDays < 1 || deleteMessageHistoryDays > 7) { event.reply("The deletion days of the messages must be between 1 and 7 days") - .setEphemeral(true).queue(); + .setEphemeral(true) + .queue(); return; } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index b0a9e2f455..7633954a88 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -35,10 +35,10 @@ public KickCommand() { } /** - * When triggered with {@code /kick @user reason}, the bot will respond then it will check if the user - * has perms. Then it will check if itself has perms to kick. If it does it will check if the - * user is too powerful or not. If the user is not then bot will kick the user and - * reply with {@code Kicked User!}. + * When triggered with {@code /kick @user reason}, the bot will respond then it will check if + * the user has perms. Then it will check if itself has perms to kick. If it does it will check + * if the user is too powerful or not. If the user is not then bot will kick the user and reply + * with {@code Kicked User!}. * * @param event the corresponding event */ @@ -50,20 +50,21 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.KICK_MEMBERS)) { event.reply("You do not have the required permissions to kick users from this server.") - .setEphemeral(true).queue(); + .setEphemeral(true) + .queue(); return; } Member author = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!author.hasPermission(Permission.KICK_MEMBERS)) { event.reply("I don't have the required permissions to kick users from this server.") - .setEphemeral(true).queue(); + .setEphemeral(true) + .queue(); return; } if (!author.canInteract(Objects.requireNonNull(user))) { - event.reply("This user is too powerful for me to kick.") - .setEphemeral(true).queue(); + event.reply("This user is too powerful for me to kick.").setEphemeral(true).queue(); return; } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java index 4cedaaf85c..06b379485a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java @@ -45,14 +45,16 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.BAN_MEMBERS)) { event.reply("You do not have the required permissions to ban users from this server.") - .setEphemeral(true).queue(); + .setEphemeral(true) + .queue(); return; } Member author = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!author.hasPermission(Permission.BAN_MEMBERS)) { event.reply("I don't have the required permissions to unban the user from this server.") - .setEphemeral(true).queue(); + .setEphemeral(true) + .queue(); return; } From 0daad10413a241afdb641521540538b83540303f Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 10:04:16 +0100 Subject: [PATCH 027/140] The bot will dm the user now. The bot will send a message to the user saying you have been banned for this reason : --- .../tjbot/commands/moderation/BanCommand.java | 10 ++++++++++ .../tjbot/commands/moderation/KickCommand.java | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index c2f83afa53..737778b0ec 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -1,5 +1,6 @@ package org.togetherjava.tjbot.commands.moderation; +import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; @@ -44,10 +45,14 @@ public BanCommand() { @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { + JDA jda = event.getJDA(); + Member user = Objects.requireNonNull(event.getOption(USER_OPTION)).getAsMember(); String reason = Objects.requireNonNull(event.getOption(REASON_OPTION)).getAsString(); + String userId = Objects.requireNonNull(user).getUser().getId(); + if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.BAN_MEMBERS)) { event.reply("You do not have the required permissions to ban users from this server.") .setEphemeral(true) @@ -80,6 +85,11 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } + //tells ths user he has been banned + jda.openPrivateChannelById(userId) + .flatMap(channel -> channel.sendMessage("You have been banned for this reason " + reason)) + .queue(); + // Ban the user and send a success response event.getGuild() .ban(user, deleteMessageHistoryDays, reason) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 7633954a88..48c3cea9fc 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -1,7 +1,9 @@ package org.togetherjava.tjbot.commands.moderation; +import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.Permission; 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.OptionType; import org.jetbrains.annotations.NotNull; @@ -44,10 +46,14 @@ public KickCommand() { */ @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { + JDA jda = event.getJDA(); + Member user = Objects.requireNonNull(event.getOption(USER_OPTION)).getAsMember(); String reason = Objects.requireNonNull(event.getOption(REASON_OPTION)).getAsString(); + String userId = Objects.requireNonNull(user).getUser().getId(); + if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.KICK_MEMBERS)) { event.reply("You do not have the required permissions to kick users from this server.") .setEphemeral(true) @@ -68,6 +74,10 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } + //tells ths user he has been kicked + jda.openPrivateChannelById(userId) + .flatMap(channel -> channel.sendMessage("You have been kicked for this reason " + reason)) + .queue(); // Kicks the user and send a success response event.getGuild() From 12fc55afbfe425b3db9d9ffdce478833bba8fc39 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 10:05:13 +0100 Subject: [PATCH 028/140] spotlessApply --- .../togetherjava/tjbot/commands/moderation/BanCommand.java | 7 ++++--- .../tjbot/commands/moderation/KickCommand.java | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 737778b0ec..dfd185ece6 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -85,10 +85,11 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - //tells ths user he has been banned + // tells ths user he has been banned jda.openPrivateChannelById(userId) - .flatMap(channel -> channel.sendMessage("You have been banned for this reason " + reason)) - .queue(); + .flatMap(channel -> channel + .sendMessage("You have been banned for this reason " + reason)) + .queue(); // Ban the user and send a success response event.getGuild() diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 48c3cea9fc..76f7a0da2d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -74,10 +74,11 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - //tells ths user he has been kicked + // tells ths user he has been kicked jda.openPrivateChannelById(userId) - .flatMap(channel -> channel.sendMessage("You have been kicked for this reason " + reason)) - .queue(); + .flatMap(channel -> channel + .sendMessage("You have been kicked for this reason " + reason)) + .queue(); // Kicks the user and send a success response event.getGuild() From fe594254ef64cec13cc6cc813e6c68f3ff6a2368 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 10:12:32 +0100 Subject: [PATCH 029/140] Update BanCommand.java --- .../org/togetherjava/tjbot/commands/moderation/BanCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index dfd185ece6..aec23cbc78 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -36,7 +36,7 @@ public BanCommand() { getData().addOption(OptionType.USER, USER_OPTION, "The user which you want to ban", true) .addOption(OptionType.INTEGER, DELETE_MESSAGE_HISTORY_DAYS_OPTION, - "The delete message history. Between 1 to 7 days", true) + "The delete message history. Between 1 to as many days as you want", true) .addOption(OptionType.STRING, REASON_OPTION, "The reason of the ban", true); From 867147f9c273039c2ce928e81c8ceb7988e05625 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 10:20:12 +0100 Subject: [PATCH 030/140] changed user id from string to long --- .../org/togetherjava/tjbot/commands/moderation/BanCommand.java | 2 +- .../org/togetherjava/tjbot/commands/moderation/KickCommand.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index aec23cbc78..53f3ea5ea8 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -51,7 +51,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { String reason = Objects.requireNonNull(event.getOption(REASON_OPTION)).getAsString(); - String userId = Objects.requireNonNull(user).getUser().getId(); + long userId = Objects.requireNonNull(user).getUser().getIdLong(); if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.BAN_MEMBERS)) { event.reply("You do not have the required permissions to ban users from this server.") diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 76f7a0da2d..7aea19fe6e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -52,7 +52,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { String reason = Objects.requireNonNull(event.getOption(REASON_OPTION)).getAsString(); - String userId = Objects.requireNonNull(user).getUser().getId(); + long userId = Objects.requireNonNull(user).getUser().getIdLong(); if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.KICK_MEMBERS)) { event.reply("You do not have the required permissions to kick users from this server.") From 365ff7ea1627b7d9479650cdeb43d5c0c7c245f4 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 10:28:42 +0100 Subject: [PATCH 031/140] it will log the author. Who use the kick/ban command --- .../tjbot/commands/moderation/BanCommand.java | 14 ++++++++------ .../tjbot/commands/moderation/KickCommand.java | 12 +++++++----- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 53f3ea5ea8..17ca25fb7f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -49,26 +49,28 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { Member user = Objects.requireNonNull(event.getOption(USER_OPTION)).getAsMember(); + Member author = Objects.requireNonNull(event.getMember()); + String reason = Objects.requireNonNull(event.getOption(REASON_OPTION)).getAsString(); long userId = Objects.requireNonNull(user).getUser().getIdLong(); - if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.BAN_MEMBERS)) { + if (!author.hasPermission(Permission.BAN_MEMBERS)) { event.reply("You do not have the required permissions to ban users from this server.") .setEphemeral(true) .queue(); return; } - Member author = Objects.requireNonNull(event.getGuild()).getSelfMember(); - if (!author.hasPermission(Permission.BAN_MEMBERS)) { + Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); + if (!bot.hasPermission(Permission.BAN_MEMBERS)) { event.reply("I don't have the required permissions to ban users from this server.") .setEphemeral(true) .queue(); return; } - if (!author.canInteract(Objects.requireNonNull(user))) { + if (!bot.canInteract(Objects.requireNonNull(user))) { event.reply("This user is too powerful for me to ban.").setEphemeral(true).queue(); return; } @@ -99,7 +101,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { // Add this to audit log logger.info( - "User '{}' banned user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", - author, user, deleteMessageHistoryDays, reason); + "Bot '{}' was made to banned the user '{}' by '{}' and deleted the message history of the last '{}' days. Reason was '{}'", + bot, user, author, deleteMessageHistoryDays, reason); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 7aea19fe6e..2720f380bf 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -50,26 +50,28 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { Member user = Objects.requireNonNull(event.getOption(USER_OPTION)).getAsMember(); + Member author = Objects.requireNonNull(event.getMember()); + String reason = Objects.requireNonNull(event.getOption(REASON_OPTION)).getAsString(); long userId = Objects.requireNonNull(user).getUser().getIdLong(); - if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.KICK_MEMBERS)) { + if (!author.hasPermission(Permission.KICK_MEMBERS)) { event.reply("You do not have the required permissions to kick users from this server.") .setEphemeral(true) .queue(); return; } - Member author = Objects.requireNonNull(event.getGuild()).getSelfMember(); - if (!author.hasPermission(Permission.KICK_MEMBERS)) { + Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); + if (!bot.hasPermission(Permission.KICK_MEMBERS)) { event.reply("I don't have the required permissions to kick users from this server.") .setEphemeral(true) .queue(); return; } - if (!author.canInteract(Objects.requireNonNull(user))) { + if (!bot.canInteract(Objects.requireNonNull(user))) { event.reply("This user is too powerful for me to kick.").setEphemeral(true).queue(); return; } @@ -87,6 +89,6 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { .queue(); // Add this to audit log - logger.info("User '{}' Kicked user '{}' Reason was '{}'", author, user, reason); + logger.info("User '{}' was made to kick the user '{}' by '{}' due to reason being '{}'", bot, user, author, reason); } } From 606793bda732779a8f878f5d20a9330a98c60766 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 10:29:47 +0100 Subject: [PATCH 032/140] Update UnBanCommand.java --- .../tjbot/commands/moderation/UnBanCommand.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java index 06b379485a..3a15d85a80 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java @@ -43,6 +43,8 @@ public UnBanCommand() { public void onSlashCommand(@NotNull SlashCommandEvent event) { final String userId = Objects.requireNonNull(event.getOption(USER_ID)).getAsString(); + Member author = Objects.requireNonNull(event.getMember()); + if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.BAN_MEMBERS)) { event.reply("You do not have the required permissions to ban users from this server.") .setEphemeral(true) @@ -50,8 +52,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - Member author = Objects.requireNonNull(event.getGuild()).getSelfMember(); - if (!author.hasPermission(Permission.BAN_MEMBERS)) { + Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); + if (!bot.hasPermission(Permission.BAN_MEMBERS)) { event.reply("I don't have the required permissions to unban the user from this server.") .setEphemeral(true) .queue(); @@ -62,6 +64,6 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { event.getGuild().unban(userId).flatMap(v -> event.reply("Unbanned the user")).queue(); // Add this to audit log - logger.info("User '{}' unbanned user id '{}'", author, userId); + logger.info("Bot was forced to '{}' unban user id '{}' by '{}'", bot, userId, author); } } From c9b2f25f22ebac4737ce5ce672b16874c6c776c9 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 11:49:26 +0100 Subject: [PATCH 033/140] added delete messages command --- .../tjbot/commands/moderation/BanCommand.java | 4 +- .../moderation/DeleteMessageCommand.java | 67 +++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 17ca25fb7f..f654bc89e2 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -15,9 +15,9 @@ /** - * When triggered with {@code /ban del_days @user rea son}, the bot will respond then will check + * When triggered with {@code /ban del_days @user reason}, the bot will check * if the user has perms. Then it will check if itself has perms to ban. If it does it will check if - * the user is too powerful or not. If the user is not then bot will ban the user abd reply with + * the user is too powerful or not. If the user is not then bot will ban the user and reply with * {@code Banned User!}. * */ diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java new file mode 100644 index 0000000000..7a0c2f8d6d --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java @@ -0,0 +1,67 @@ +package org.togetherjava.tjbot.commands.moderation; + +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.TextChannel; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import org.jetbrains.annotations.NotNull; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; +import org.togetherjava.tjbot.commands.SlashCommandVisibility; + +import java.util.Objects; + + +/** + * When triggered with {@code /prune "number_of_messages"}, the bot will check + * if the user has perms. Then it will check if itself has perms to delete messages. + * If it does the bot will delete that amount of messages. + * {@code Banned User!}. + * + */ +public class DeleteMessageCommand extends SlashCommandAdapter { + private static final String NUMBER_OF_MESSAGES = "number_of_messages"; + + + protected DeleteMessageCommand(@NotNull String name, @NotNull String description, SlashCommandVisibility visibility) { + super("prune", "Use this command to delete a batch of messages", SlashCommandVisibility.GUILD); + + getData().addOption(OptionType.INTEGER, NUMBER_OF_MESSAGES, "The number of messages you want to delete. 1 to 100", + true); + } + + @Override + public void onSlashCommand(@NotNull SlashCommandEvent event) { + final Member author = event.getMember(); + + final TextChannel channel = event.getTextChannel(); + + if (!Objects.requireNonNull(author).hasPermission(Permission.MESSAGE_MANAGE)) { + event.reply("You are missing permission to manage the message") + .setEphemeral(true) + .queue(); + return; + } + + final Member bot = event.getGuild().getSelfMember(); + + if (!bot.hasPermission(Permission.MESSAGE_MANAGE)) { + event.reply("I am missing permissions to manage these messages") + .setEphemeral(true) + .queue(); + return; + } + + int amount = (int) Objects.requireNonNull(event.getOption(NUMBER_OF_MESSAGES)).getAsLong(); + + var messageHistory = channel.getHistory().retrievePast(amount).complete(); + + if(amount < 100) { + event.reply("You can only delete 1 to 100 messages") + .setEphemeral(true) + .queue(); + } else { + channel.purgeMessages(messageHistory); + } + } +} From 8cac49518e7af473ca4ed43d803553e95638f176 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 11:50:14 +0100 Subject: [PATCH 034/140] Update DeleteMessageCommand.java --- .../tjbot/commands/moderation/DeleteMessageCommand.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java index 7a0c2f8d6d..476842fb0e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java @@ -19,11 +19,11 @@ * {@code Banned User!}. * */ -public class DeleteMessageCommand extends SlashCommandAdapter { +public final class DeleteMessageCommand extends SlashCommandAdapter { private static final String NUMBER_OF_MESSAGES = "number_of_messages"; - protected DeleteMessageCommand(@NotNull String name, @NotNull String description, SlashCommandVisibility visibility) { + public DeleteMessageCommand() { super("prune", "Use this command to delete a batch of messages", SlashCommandVisibility.GUILD); getData().addOption(OptionType.INTEGER, NUMBER_OF_MESSAGES, "The number of messages you want to delete. 1 to 100", @@ -43,7 +43,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - final Member bot = event.getGuild().getSelfMember(); + final Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!bot.hasPermission(Permission.MESSAGE_MANAGE)) { event.reply("I am missing permissions to manage these messages") From df8bb9085316e283c9c1d2b3d14e03c83403b0b4 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 11:50:17 +0100 Subject: [PATCH 035/140] Update Commands.java --- .../src/main/java/org/togetherjava/tjbot/commands/Commands.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java index 1c0e9e55e0..4437d68d21 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java @@ -35,7 +35,7 @@ public enum Commands { // hence this list may not necessarily represent the full list of all commands actually // available. return List.of(new PingCommand(), new DatabaseCommand(database), new KickCommand(), - new BanCommand(), new UnBanCommand()); + new BanCommand(), new UnBanCommand(), new DeleteMessageCommand()); } } From 019bec37ad79d3c6603e61b28b91479d392cb0dd Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 11:50:30 +0100 Subject: [PATCH 036/140] Update BanCommand.java --- .../togetherjava/tjbot/commands/moderation/BanCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 17ca25fb7f..f654bc89e2 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -15,9 +15,9 @@ /** - * When triggered with {@code /ban del_days @user rea son}, the bot will respond then will check + * When triggered with {@code /ban del_days @user reason}, the bot will check * if the user has perms. Then it will check if itself has perms to ban. If it does it will check if - * the user is too powerful or not. If the user is not then bot will ban the user abd reply with + * the user is too powerful or not. If the user is not then bot will ban the user and reply with * {@code Banned User!}. * */ From 516ffcf1e12aaefc686446bdc9f38be13d209fa4 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 11:51:00 +0100 Subject: [PATCH 037/140] Update KickCommand.java --- .../org/togetherjava/tjbot/commands/moderation/KickCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 2720f380bf..882268adfa 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -37,7 +37,7 @@ public KickCommand() { } /** - * When triggered with {@code /kick @user reason}, the bot will respond then it will check if + * When triggered with {@code /kick @user reason}, the bot will check if * the user has perms. Then it will check if itself has perms to kick. If it does it will check * if the user is too powerful or not. If the user is not then bot will kick the user and reply * with {@code Kicked User!}. From c3f3c9a19664de6609430813b7c9c8f6ad681e37 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 11:53:21 +0100 Subject: [PATCH 038/140] Create WarnCommand.java --- .../commands/moderation/WarnCommand.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java new file mode 100644 index 0000000000..cf5b712ba1 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java @@ -0,0 +1,24 @@ +package org.togetherjava.tjbot.commands.moderation; + +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import org.jetbrains.annotations.NotNull; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; +import org.togetherjava.tjbot.commands.SlashCommandVisibility; + +public class WarnCommand extends SlashCommandAdapter { + private static final String REASON_OPTION = "reason"; + private static final String USER_OPTION = "user"; + + protected WarnCommand() { + super("warn", "Use this command to warn", SlashCommandVisibility.GUILD); + + getData().addOption(OptionType.USER, USER_OPTION, "The user which you want to warn", true) + .addOption(OptionType.STRING, REASON_OPTION, "The reason of the warb", true); + } + + @Override + public void onSlashCommand(@NotNull SlashCommandEvent event) { + + } +} From 1f369ab3bb12abc4384eabd2645832f7bd7749d2 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 11:55:38 +0100 Subject: [PATCH 039/140] Update DeleteMessageCommand.java --- .../commands/moderation/DeleteMessageCommand.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java index 476842fb0e..bf854443fe 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java @@ -6,6 +6,8 @@ import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; 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; @@ -20,9 +22,9 @@ * */ public final class DeleteMessageCommand extends SlashCommandAdapter { + private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); private static final String NUMBER_OF_MESSAGES = "number_of_messages"; - - + public DeleteMessageCommand() { super("prune", "Use this command to delete a batch of messages", SlashCommandVisibility.GUILD); @@ -63,5 +65,9 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { } else { channel.purgeMessages(messageHistory); } + // Add this to audit log + logger.info( + "Bot '{}' was made to delete this amount of messages '{}' by '{}'", + bot, amount, author); } } From a7c2aa0c46c085261c49470abf4db11647332f16 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 12:21:37 +0100 Subject: [PATCH 040/140] update --- .../tjbot/commands/moderation/DeleteMessageCommand.java | 2 +- .../togetherjava/tjbot/commands/moderation/WarnCommand.java | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java index bf854443fe..d13fbbcb57 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java @@ -24,7 +24,7 @@ public final class DeleteMessageCommand extends SlashCommandAdapter { private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); private static final String NUMBER_OF_MESSAGES = "number_of_messages"; - + public DeleteMessageCommand() { super("prune", "Use this command to delete a batch of messages", SlashCommandVisibility.GUILD); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java index cf5b712ba1..f61269da35 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java @@ -1,11 +1,14 @@ package org.togetherjava.tjbot.commands.moderation; +import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import net.dv8tion.jda.api.interactions.commands.OptionType; import org.jetbrains.annotations.NotNull; import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import java.util.Objects; + public class WarnCommand extends SlashCommandAdapter { private static final String REASON_OPTION = "reason"; private static final String USER_OPTION = "user"; @@ -19,6 +22,8 @@ protected WarnCommand() { @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { + Member user = Objects.requireNonNull(event.getOption(USER_OPTION)).getAsMember(); + } } From 23a6c2e5e8c9852ccc13b09397139cbe44e8b420 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 12:22:56 +0100 Subject: [PATCH 041/140] changed from UnBanCommand to UnbanCommand. Allowed to delete messages for more than 7 days --- .../main/java/org/togetherjava/tjbot/commands/Commands.java | 2 +- .../togetherjava/tjbot/commands/moderation/BanCommand.java | 2 +- .../moderation/{UnBanCommand.java => UnbanCommand.java} | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) rename application/src/main/java/org/togetherjava/tjbot/commands/moderation/{UnBanCommand.java => UnbanCommand.java} (96%) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java index 1c0e9e55e0..b91cfc23dd 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java @@ -35,7 +35,7 @@ public enum Commands { // hence this list may not necessarily represent the full list of all commands actually // available. return List.of(new PingCommand(), new DatabaseCommand(database), new KickCommand(), - new BanCommand(), new UnBanCommand()); + new BanCommand(), new UnbanCommand()); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index f654bc89e2..6d933d6130 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -80,7 +80,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { (int) Objects.requireNonNull(event.getOption("delete-message-history-days")) .getAsLong(); - if (deleteMessageHistoryDays < 1 || deleteMessageHistoryDays > 7) { + if (deleteMessageHistoryDays < 1) { event.reply("The deletion days of the messages must be between 1 and 7 days") .setEphemeral(true) .queue(); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java similarity index 96% rename from application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java rename to application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java index 3a15d85a80..718e4d8d51 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnBanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java @@ -16,7 +16,7 @@ *

* The implemented command is {@code /unban user_id}, upon which the bot will unban the user. */ -public final class UnBanCommand extends SlashCommandAdapter { +public final class UnbanCommand extends SlashCommandAdapter { // the logger private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); private static final String USER_ID = "user_id"; @@ -24,7 +24,7 @@ public final class UnBanCommand extends SlashCommandAdapter { /** * Creates an instance of the ban command. */ - public UnBanCommand() { + public UnbanCommand() { super("unban", "Use this command to unban a user", SlashCommandVisibility.GUILD); getData().addOption(OptionType.STRING, USER_ID, From fd0802d48e089e752f43e69e0734b6254d406e41 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 12:23:05 +0100 Subject: [PATCH 042/140] Update BanCommand.java --- .../org/togetherjava/tjbot/commands/moderation/BanCommand.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 6d933d6130..4626662700 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -38,8 +38,6 @@ public BanCommand() { .addOption(OptionType.INTEGER, DELETE_MESSAGE_HISTORY_DAYS_OPTION, "The delete message history. Between 1 to as many days as you want", true) .addOption(OptionType.STRING, REASON_OPTION, "The reason of the ban", true); - - } From 48f98634af703c890ec447ee0e2d7a935caf6c5b Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 12:37:13 +0100 Subject: [PATCH 043/140] Update BanCommand.java --- .../org/togetherjava/tjbot/commands/moderation/BanCommand.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 4626662700..4aab6346b4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -36,7 +36,8 @@ public BanCommand() { getData().addOption(OptionType.USER, USER_OPTION, "The user which you want to ban", true) .addOption(OptionType.INTEGER, DELETE_MESSAGE_HISTORY_DAYS_OPTION, - "The delete message history. Between 1 to as many days as you want", true) + "The messages in these day will be deleted. 1 to as many days as you want", + true) .addOption(OptionType.STRING, REASON_OPTION, "The reason of the ban", true); } From 8b96b3ab4fffd9b518f797f9c75a388bfd08b2dd Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 12:49:55 +0100 Subject: [PATCH 044/140] I checked the jda guild and its says it has to be between one and 7 days --- .../tjbot/commands/moderation/BanCommand.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 4aab6346b4..ca9339bbc9 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -36,7 +36,7 @@ public BanCommand() { getData().addOption(OptionType.USER, USER_OPTION, "The user which you want to ban", true) .addOption(OptionType.INTEGER, DELETE_MESSAGE_HISTORY_DAYS_OPTION, - "The messages in these day will be deleted. 1 to as many days as you want", + "The messages in these days will be deleted. 0 to 7 days. 0 means no messages deleted", true) .addOption(OptionType.STRING, REASON_OPTION, "The reason of the ban", true); } @@ -77,10 +77,10 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { int deleteMessageHistoryDays = (int) Objects.requireNonNull(event.getOption("delete-message-history-days")) - .getAsLong(); + .getAsLong(); - if (deleteMessageHistoryDays < 1) { - event.reply("The deletion days of the messages must be between 1 and 7 days") + if (deleteMessageHistoryDays < 0 || deleteMessageHistoryDays > 7) { + event.reply("The deletion days of the messages must be between 0 and 7 days.") .setEphemeral(true) .queue(); return; From d054f3acd9da0fd9fcf311158e476dbf0d7116c1 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 15:45:33 +0100 Subject: [PATCH 045/140] Update V1__Database_Listener.sql --- application/src/main/resources/db/V1__Database_Listener.sql | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/application/src/main/resources/db/V1__Database_Listener.sql b/application/src/main/resources/db/V1__Database_Listener.sql index f1956f216e..da2178b267 100644 --- a/application/src/main/resources/db/V1__Database_Listener.sql +++ b/application/src/main/resources/db/V1__Database_Listener.sql @@ -3,3 +3,9 @@ CREATE TABLE storage key TEXT NOT NULL PRIMARY KEY, value TEXT NOT NULL ) +CREATE TABLE warn +( + user_id INTEGER NOT NULL, + guild_id INTEGER NOT NULL, + warns INTEGER NOT NULL +) From 2e503f3aface4bf154ab931d25807afe9de5abe4 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 15:47:02 +0100 Subject: [PATCH 046/140] Update V1__Database_Listener.sql --- .../src/main/resources/db/V1__Database_Listener.sql | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/application/src/main/resources/db/V1__Database_Listener.sql b/application/src/main/resources/db/V1__Database_Listener.sql index da2178b267..8f16bf2dc6 100644 --- a/application/src/main/resources/db/V1__Database_Listener.sql +++ b/application/src/main/resources/db/V1__Database_Listener.sql @@ -2,10 +2,4 @@ CREATE TABLE storage ( key TEXT NOT NULL PRIMARY KEY, value TEXT NOT NULL -) -CREATE TABLE warn -( - user_id INTEGER NOT NULL, - guild_id INTEGER NOT NULL, - warns INTEGER NOT NULL -) +) \ No newline at end of file From 76a12f2eddffdcda41a053b39759fd30bf9199c9 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 15:47:16 +0100 Subject: [PATCH 047/140] Create V2_ADD_WARN_TABLE.sql --- application/src/main/resources/db/V2_ADD_WARN_TABLE.sql | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 application/src/main/resources/db/V2_ADD_WARN_TABLE.sql diff --git a/application/src/main/resources/db/V2_ADD_WARN_TABLE.sql b/application/src/main/resources/db/V2_ADD_WARN_TABLE.sql new file mode 100644 index 0000000000..c80a66e8ff --- /dev/null +++ b/application/src/main/resources/db/V2_ADD_WARN_TABLE.sql @@ -0,0 +1,6 @@ +CREATE TABLE warn +( + user_id INTEGER NOT NULL, + guild_id INTEGER NOT NULL, + warns INTEGER NOT NULL +) From fc92173c2b55ffb4991df2c0b089b243a3c44f5b Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 15:49:00 +0100 Subject: [PATCH 048/140] Update Commands.java --- .../src/main/java/org/togetherjava/tjbot/commands/Commands.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java index 4437d68d21..0edb1c3716 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java @@ -35,7 +35,7 @@ public enum Commands { // hence this list may not necessarily represent the full list of all commands actually // available. return List.of(new PingCommand(), new DatabaseCommand(database), new KickCommand(), - new BanCommand(), new UnBanCommand(), new DeleteMessageCommand()); + new BanCommand(), new UnBanCommand(), new DeleteMessageCommand(), new WarnCommand(database)); } } From a29dc688387f2381883601ef92ab5a2696189a5b Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 16:09:47 +0100 Subject: [PATCH 049/140] Update Commands.java --- .../src/main/java/org/togetherjava/tjbot/commands/Commands.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java index b91cfc23dd..f2daf6b328 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java @@ -35,7 +35,7 @@ public enum Commands { // hence this list may not necessarily represent the full list of all commands actually // available. return List.of(new PingCommand(), new DatabaseCommand(database), new KickCommand(), - new BanCommand(), new UnbanCommand()); + new BanCommand(), new UnbanCommand(), new DeleteMessageCommand(), new WarnCommand(database)); } } From 380bd048e96e33fc1420963c8954b45fa883926c Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 16:41:10 +0100 Subject: [PATCH 050/140] Update V2_Add_Warn_Table.sql --- .../resources/db/{V2_ADD_WARN_TABLE.sql => V2_Add_Warn_Table.sql} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename application/src/main/resources/db/{V2_ADD_WARN_TABLE.sql => V2_Add_Warn_Table.sql} (100%) diff --git a/application/src/main/resources/db/V2_ADD_WARN_TABLE.sql b/application/src/main/resources/db/V2_Add_Warn_Table.sql similarity index 100% rename from application/src/main/resources/db/V2_ADD_WARN_TABLE.sql rename to application/src/main/resources/db/V2_Add_Warn_Table.sql From 92eb572352ae308db10512b9ebf0f46ce076b702 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sun, 17 Oct 2021 16:45:09 +0100 Subject: [PATCH 051/140] Update V1_Add_Warn_Table.sql --- .../resources/db/{V2_Add_Warn_Table.sql => V1_Add_Warn_Table.sql} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename application/src/main/resources/db/{V2_Add_Warn_Table.sql => V1_Add_Warn_Table.sql} (100%) diff --git a/application/src/main/resources/db/V2_Add_Warn_Table.sql b/application/src/main/resources/db/V1_Add_Warn_Table.sql similarity index 100% rename from application/src/main/resources/db/V2_Add_Warn_Table.sql rename to application/src/main/resources/db/V1_Add_Warn_Table.sql From 5f1a55d10ee90067dc1ce174d5ba2a21b7c102d4 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Mon, 18 Oct 2021 16:33:10 +0100 Subject: [PATCH 052/140] update --- .gitignore | 1 + .../togetherjava/tjbot/commands/Commands.java | 3 +- .../tjbot/commands/moderation/BanCommand.java | 8 ++--- .../moderation/DeleteMessageCommand.java | 33 +++++++++---------- .../commands/moderation/KickCommand.java | 11 ++++--- 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index 0ce9cdb2ce..f24a747936 100644 --- a/.gitignore +++ b/.gitignore @@ -158,3 +158,4 @@ logviewer/types.d.ts logviewer/pnpm-lock.yaml logviewer/webpack.config.js logviewer/webpack.generated.js +.DS_Store diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java index f2daf6b328..47610a3903 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java @@ -35,7 +35,8 @@ public enum Commands { // hence this list may not necessarily represent the full list of all commands actually // available. return List.of(new PingCommand(), new DatabaseCommand(database), new KickCommand(), - new BanCommand(), new UnbanCommand(), new DeleteMessageCommand(), new WarnCommand(database)); + new BanCommand(), new UnbanCommand(), new DeleteMessageCommand(), + new WarnCommand(database)); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index ca9339bbc9..db2f35e313 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -15,9 +15,9 @@ /** - * When triggered with {@code /ban del_days @user reason}, the bot will check - * if the user has perms. Then it will check if itself has perms to ban. If it does it will check if - * the user is too powerful or not. If the user is not then bot will ban the user and reply with + * When triggered with {@code /ban del_days @user reason}, the bot will check if the user has perms. + * Then it will check if itself has perms to ban. If it does it will check if the user is too + * powerful or not. If the user is not then bot will ban the user and reply with * {@code Banned User!}. * */ @@ -77,7 +77,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { int deleteMessageHistoryDays = (int) Objects.requireNonNull(event.getOption("delete-message-history-days")) - .getAsLong(); + .getAsLong(); if (deleteMessageHistoryDays < 0 || deleteMessageHistoryDays > 7) { event.reply("The deletion days of the messages must be between 0 and 7 days.") diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java index d13fbbcb57..1081042de7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java @@ -15,10 +15,9 @@ /** - * When triggered with {@code /prune "number_of_messages"}, the bot will check - * if the user has perms. Then it will check if itself has perms to delete messages. - * If it does the bot will delete that amount of messages. - * {@code Banned User!}. + * When triggered with {@code /prune "number_of_messages"}, the bot will check if the user has + * perms. Then it will check if itself has perms to delete messages. If it does the bot will delete + * that amount of messages. {@code Banned User!}. * */ public final class DeleteMessageCommand extends SlashCommandAdapter { @@ -26,10 +25,11 @@ public final class DeleteMessageCommand extends SlashCommandAdapter { private static final String NUMBER_OF_MESSAGES = "number_of_messages"; public DeleteMessageCommand() { - super("prune", "Use this command to delete a batch of messages", SlashCommandVisibility.GUILD); + super("prune", "Use this command to delete a batch of messages", + SlashCommandVisibility.GUILD); - getData().addOption(OptionType.INTEGER, NUMBER_OF_MESSAGES, "The number of messages you want to delete. 1 to 100", - true); + getData().addOption(OptionType.INTEGER, NUMBER_OF_MESSAGES, + "The number of messages you want to delete. 1 to 100", true); } @Override @@ -40,8 +40,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { if (!Objects.requireNonNull(author).hasPermission(Permission.MESSAGE_MANAGE)) { event.reply("You are missing permission to manage the message") - .setEphemeral(true) - .queue(); + .setEphemeral(true) + .queue(); return; } @@ -49,8 +49,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { if (!bot.hasPermission(Permission.MESSAGE_MANAGE)) { event.reply("I am missing permissions to manage these messages") - .setEphemeral(true) - .queue(); + .setEphemeral(true) + .queue(); return; } @@ -58,16 +58,13 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { var messageHistory = channel.getHistory().retrievePast(amount).complete(); - if(amount < 100) { - event.reply("You can only delete 1 to 100 messages") - .setEphemeral(true) - .queue(); + if (amount < 100) { + event.reply("You can only delete 1 to 100 messages").setEphemeral(true).queue(); } else { channel.purgeMessages(messageHistory); } // Add this to audit log - logger.info( - "Bot '{}' was made to delete this amount of messages '{}' by '{}'", - bot, amount, author); + logger.info("Bot '{}' was made to delete this amount of messages '{}' by '{}'", bot, amount, + author); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 882268adfa..79580bbfb8 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -37,10 +37,10 @@ public KickCommand() { } /** - * When triggered with {@code /kick @user reason}, the bot will check if - * the user has perms. Then it will check if itself has perms to kick. If it does it will check - * if the user is too powerful or not. If the user is not then bot will kick the user and reply - * with {@code Kicked User!}. + * When triggered with {@code /kick @user reason}, the bot will check if the user has perms. + * Then it will check if itself has perms to kick. If it does it will check if the user is too + * powerful or not. If the user is not then bot will kick the user and reply with + * {@code Kicked User!}. * * @param event the corresponding event */ @@ -89,6 +89,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { .queue(); // Add this to audit log - logger.info("User '{}' was made to kick the user '{}' by '{}' due to reason being '{}'", bot, user, author, reason); + logger.info("User '{}' was made to kick the user '{}' by '{}' due to reason being '{}'", + bot, user, author, reason); } } From 917658fa7b5a46e89aaf4eeef066c1c8a30916f8 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Mon, 18 Oct 2021 16:33:15 +0100 Subject: [PATCH 053/140] Update WarnCommand.java --- .../commands/moderation/WarnCommand.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java index f61269da35..66ad183434 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java @@ -6,23 +6,37 @@ import org.jetbrains.annotations.NotNull; import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.db.Database; import java.util.Objects; -public class WarnCommand extends SlashCommandAdapter { +public final class WarnCommand extends SlashCommandAdapter { private static final String REASON_OPTION = "reason"; private static final String USER_OPTION = "user"; + private final Database database; - protected WarnCommand() { + public WarnCommand(Database database) { super("warn", "Use this command to warn", SlashCommandVisibility.GUILD); - getData().addOption(OptionType.USER, USER_OPTION, "The user which you want to warn", true) - .addOption(OptionType.STRING, REASON_OPTION, "The reason of the warb", true); + .addOption(OptionType.STRING, REASON_OPTION, "The reason of the warb", true); + + this.database = database; } @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { Member user = Objects.requireNonNull(event.getOption(USER_OPTION)).getAsMember(); + String reason = Objects.requireNonNull(event.getOption(REASON_OPTION)).getAsString(); + + /* + * try { database.write(context -> { StorageRecord storageRecord = + * context.newRecord(Storage.STORAGE).setKey(user).setValue(reason); if + * (storageRecord.update() == 0) { storageRecord.insert(); } }); + * + * event.reply("Saved under '" + key + "'.").queue(); } catch (DatabaseException e) { + * logger.error("Failed to put message", e); + * event.reply("Sorry, something went wrong.").setEphemeral(true).queue(); } + */ } From 7dae25c77a204166d6689bad398f6ab830b9d8e1 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Mon, 18 Oct 2021 16:33:34 +0100 Subject: [PATCH 054/140] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 0ce9cdb2ce..f24a747936 100644 --- a/.gitignore +++ b/.gitignore @@ -158,3 +158,4 @@ logviewer/types.d.ts logviewer/pnpm-lock.yaml logviewer/webpack.config.js logviewer/webpack.generated.js +.DS_Store From 9d6c4be26bcf193a98138c57be9a8ad7f50d3849 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Mon, 18 Oct 2021 16:50:12 +0100 Subject: [PATCH 055/140] fixed everything --- .../tjbot/commands/moderation/BanCommand.java | 40 ++++++++++--------- .../commands/moderation/KickCommand.java | 28 ++++++------- .../commands/moderation/UnbanCommand.java | 6 +-- 3 files changed, 37 insertions(+), 37 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index ca9339bbc9..efc9c9a93b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -1,6 +1,5 @@ package org.togetherjava.tjbot.commands.moderation; -import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; @@ -15,14 +14,15 @@ /** - * When triggered with {@code /ban del_days @user reason}, the bot will check - * if the user has perms. Then it will check if itself has perms to ban. If it does it will check if - * the user is too powerful or not. If the user is not then bot will ban the user and reply with - * {@code Banned User!}. + * 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 him about + * the action and the reason. + *

+ * 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 { - // the logger private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); private static final String USER_OPTION = "user"; private static final String DELETE_MESSAGE_HISTORY_DAYS_OPTION = "delete-message-history-days"; @@ -37,15 +37,13 @@ public BanCommand() { getData().addOption(OptionType.USER, USER_OPTION, "The user which you want to ban", true) .addOption(OptionType.INTEGER, DELETE_MESSAGE_HISTORY_DAYS_OPTION, "The messages in these days will be deleted. 0 to 7 days. 0 means no messages deleted", - true) + false) .addOption(OptionType.STRING, REASON_OPTION, "The reason of the ban", true); } @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { - JDA jda = event.getJDA(); - Member user = Objects.requireNonNull(event.getOption(USER_OPTION)).getAsMember(); Member author = Objects.requireNonNull(event.getMember()); @@ -61,6 +59,11 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } + if (!author.canInteract(Objects.requireNonNull(user))) { + event.reply("This user is too powerful for you to ban.").setEphemeral(true).queue(); + return; + } + Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!bot.hasPermission(Permission.BAN_MEMBERS)) { event.reply("I don't have the required permissions to ban users from this server.") @@ -74,33 +77,32 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - int deleteMessageHistoryDays = (int) Objects.requireNonNull(event.getOption("delete-message-history-days")) - .getAsLong(); + .getAsLong(); - if (deleteMessageHistoryDays < 0 || deleteMessageHistoryDays > 7) { - event.reply("The deletion days of the messages must be between 0 and 7 days.") + if (deleteMessageHistoryDays < 1 || deleteMessageHistoryDays > 7) { + event.reply("The deletion days of the messages must be between 1 and 7 days.") .setEphemeral(true) .queue(); return; } - // tells ths user he has been banned - jda.openPrivateChannelById(userId) + event.getJDA() + .openPrivateChannelById(userId) .flatMap(channel -> channel .sendMessage("You have been banned for this reason " + reason)) .queue(); - // Ban the user and send a success response event.getGuild() .ban(user, deleteMessageHistoryDays, reason) .flatMap(v -> event.reply("Banned the user " + user.getUser().getAsTag())) .queue(); - // Add this to audit log + String userName = user.getId(); + String authorName = author.getId(); logger.info( - "Bot '{}' was made to banned the user '{}' by '{}' and deleted the message history of the last '{}' days. Reason was '{}'", - bot, user, author, deleteMessageHistoryDays, reason); + " '{}' banned the user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", + authorName, userName, deleteMessageHistoryDays, reason); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 882268adfa..413944c70e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -1,9 +1,7 @@ package org.togetherjava.tjbot.commands.moderation; -import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.Permission; 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.OptionType; import org.jetbrains.annotations.NotNull; @@ -20,7 +18,6 @@ * The implemented command is {@code /kick @user reason}, upon which the bot will kick the user. */ public final class KickCommand extends SlashCommandAdapter { - // the logger private static final Logger logger = LoggerFactory.getLogger(KickCommand.class); private static final String USER_OPTION = "user"; private static final String REASON_OPTION = "reason"; @@ -37,17 +34,15 @@ public KickCommand() { } /** - * When triggered with {@code /kick @user reason}, the bot will check if - * the user has perms. Then it will check if itself has perms to kick. If it does it will check - * if the user is too powerful or not. If the user is not then bot will kick the user and reply - * with {@code Kicked User!}. + * When triggered with {@code /kick @user reason}, the bot will check if the user has perms. + * Then it will check if itself has perms to kick. If it does it will check if the user is too + * powerful or not. If the user is not then bot will kick the user and reply with + * {@code Kicked User!}. * * @param event the corresponding event */ @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { - JDA jda = event.getJDA(); - Member user = Objects.requireNonNull(event.getOption(USER_OPTION)).getAsMember(); Member author = Objects.requireNonNull(event.getMember()); @@ -63,6 +58,11 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } + if (!author.canInteract(Objects.requireNonNull(user))) { + event.reply("This user is too powerful for you to ban.").setEphemeral(true).queue(); + return; + } + Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!bot.hasPermission(Permission.KICK_MEMBERS)) { event.reply("I don't have the required permissions to kick users from this server.") @@ -76,19 +76,19 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - // tells ths user he has been kicked - jda.openPrivateChannelById(userId) + event.getJDA() + .openPrivateChannelById(userId) .flatMap(channel -> channel .sendMessage("You have been kicked for this reason " + reason)) .queue(); - // Kicks the user and send a success response event.getGuild() .kick(user, reason) .flatMap(v -> event.reply("Kicked the user" + user.getUser().getAsTag())) .queue(); - // Add this to audit log - logger.info("User '{}' was made to kick the user '{}' by '{}' due to reason being '{}'", bot, user, author, reason); + String userName = user.getId(); + String authorName = author.getId(); + logger.info(" '{}' kicked the user '{}' due to reason being '{}'", authorName, userName, reason); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java index 718e4d8d51..abdf57df36 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java @@ -17,7 +17,6 @@ * The implemented command is {@code /unban user_id}, upon which the bot will unban the user. */ public final class UnbanCommand extends SlashCommandAdapter { - // the logger private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); private static final String USER_ID = "user_id"; @@ -60,10 +59,9 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - // Unbans the user event.getGuild().unban(userId).flatMap(v -> event.reply("Unbanned the user")).queue(); - // Add this to audit log - logger.info("Bot was forced to '{}' unban user id '{}' by '{}'", bot, userId, author); + String authorName = author.getId(); + logger.info(" '{}' unbanned user id '{}' ", authorName, userId); } } From 336d567cf0ede2be911309b38ac07ebac295da76 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Mon, 18 Oct 2021 18:00:19 +0100 Subject: [PATCH 056/140] Update BanCommand.java --- .../org/togetherjava/tjbot/commands/moderation/BanCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index efc9c9a93b..347ecd415b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -36,7 +36,7 @@ public BanCommand() { getData().addOption(OptionType.USER, USER_OPTION, "The user which you want to ban", true) .addOption(OptionType.INTEGER, DELETE_MESSAGE_HISTORY_DAYS_OPTION, - "The messages in these days will be deleted. 0 to 7 days. 0 means no messages deleted", + "The messages in these days will be deleted. 1 to 7 days.", false) .addOption(OptionType.STRING, REASON_OPTION, "The reason of the ban", true); } From 64d7c661668b2af9f1dde746c86f6fa04ac44265 Mon Sep 17 00:00:00 2001 From: Yusuf Arfan Ismail Date: Tue, 19 Oct 2021 13:14:12 +0100 Subject: [PATCH 057/140] Fixed everything apart from https://github.com/Together-Java/TJ-Bot/pull/196/files#r731556495 --- .../tjbot/commands/moderation/BanCommand.java | 37 ++++++++++++------- .../commands/moderation/KickCommand.java | 17 ++++++--- .../commands/moderation/UnbanCommand.java | 4 +- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 347ecd415b..d3a103c9c5 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -32,16 +32,15 @@ public final class BanCommand extends SlashCommandAdapter { * Creates an instance of the ban command. */ public BanCommand() { - super("ban", "Use this command to ban a user", SlashCommandVisibility.GUILD); + super("ban", "Use this command to ban a given user", SlashCommandVisibility.GUILD); - getData().addOption(OptionType.USER, USER_OPTION, "The user which you want to ban", true) + getData().addOption(OptionType.USER, USER_OPTION, "The user who you want to ban", true) .addOption(OptionType.INTEGER, DELETE_MESSAGE_HISTORY_DAYS_OPTION, - "The messages in these days will be deleted. 1 to 7 days.", + "the amount of days of the message history to delete, otherwise no messages are deleted. 1 to 7 days.", false) - .addOption(OptionType.STRING, REASON_OPTION, "The reason of the ban", true); + .addOption(OptionType.STRING, REASON_OPTION, "why the user should be banned", true); } - @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { Member user = Objects.requireNonNull(event.getOption(USER_OPTION)).getAsMember(); @@ -50,30 +49,36 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { String reason = Objects.requireNonNull(event.getOption(REASON_OPTION)).getAsString(); - long userId = Objects.requireNonNull(user).getUser().getIdLong(); + long userId = user.getUser().getIdLong(); if (!author.hasPermission(Permission.BAN_MEMBERS)) { - event.reply("You do not have the required permissions to ban users from this server.") + event.reply("You do not have the BAN_MEMBERS permission to ban users from this server.") .setEphemeral(true) .queue(); return; } if (!author.canInteract(Objects.requireNonNull(user))) { - event.reply("This user is too powerful for you to ban.").setEphemeral(true).queue(); + event.reply( + "This user is too powerful for you to ban because he has more permissions than uou.") + .setEphemeral(true) + .queue(); return; } Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!bot.hasPermission(Permission.BAN_MEMBERS)) { - event.reply("I don't have the required permissions to ban users from this server.") + event.reply("I don't have the BAN_MEMBERS permission to ban users from this server.") .setEphemeral(true) .queue(); return; } if (!bot.canInteract(Objects.requireNonNull(user))) { - event.reply("This user is too powerful for me to ban.").setEphemeral(true).queue(); + event.reply( + "This user is too powerful for me to ban because he has more permissions than me.") + .setEphemeral(true) + .queue(); return; } @@ -82,7 +87,9 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { .getAsLong(); if (deleteMessageHistoryDays < 1 || deleteMessageHistoryDays > 7) { - event.reply("The deletion days of the messages must be between 1 and 7 days.") + event.reply( + "The amount of days of the message history to delete must be between 1 and 7, but was " + + deleteMessageHistoryDays + ".") .setEphemeral(true) .queue(); return; @@ -90,13 +97,15 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { event.getJDA() .openPrivateChannelById(userId) - .flatMap(channel -> channel - .sendMessage("You have been banned for this reason " + reason)) + .flatMap(channel -> channel.sendMessage( + "Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Togherer Java'. If you think this was a mistake, please contact a moderator or adming of the guild. The ban reason is: " + + reason)) .queue(); event.getGuild() .ban(user, deleteMessageHistoryDays, reason) - .flatMap(v -> event.reply("Banned the user " + user.getUser().getAsTag())) + .flatMap(v -> event.reply(user.getUser().getAsTag() + "was banned by " + + author.getEffectiveName() + "for: " + reason)) .queue(); String userName = user.getId(); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 413944c70e..743d9bd9db 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -27,10 +27,10 @@ public final class KickCommand extends SlashCommandAdapter { * Creates an instance of the kick command. */ public KickCommand() { - super("kick", "Use this command to kick a user", SlashCommandVisibility.GUILD); + super("kick", "Use this command to kick a given user", SlashCommandVisibility.GUILD); getData().addOption(OptionType.USER, USER_OPTION, "The user which you want to kick", true) - .addOption(OptionType.STRING, REASON_OPTION, "The reason of the kick", true); + .addOption(OptionType.STRING, REASON_OPTION, "why the user should be kicked", true); } /** @@ -59,7 +59,10 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { } if (!author.canInteract(Objects.requireNonNull(user))) { - event.reply("This user is too powerful for you to ban.").setEphemeral(true).queue(); + event.reply( + "This user is too powerful for you to kick because he has more permissions than uou.") + .setEphemeral(true) + .queue(); return; } @@ -72,7 +75,10 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { } if (!bot.canInteract(Objects.requireNonNull(user))) { - event.reply("This user is too powerful for me to kick.").setEphemeral(true).queue(); + event.reply( + "This user is too powerful for me to kick because he has more permissions than me.") + .setEphemeral(true) + .queue(); return; } @@ -89,6 +95,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { String userName = user.getId(); String authorName = author.getId(); - logger.info(" '{}' kicked the user '{}' due to reason being '{}'", authorName, userName, reason); + logger.info(" '{}' kicked the user '{}' due to reason being '{}'", authorName, userName, + reason); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java index abdf57df36..d726f1c6d2 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java @@ -45,7 +45,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { Member author = Objects.requireNonNull(event.getMember()); if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.BAN_MEMBERS)) { - event.reply("You do not have the required permissions to ban users from this server.") + event.reply("You do not have the BAN_MEMBERS permission to urban users from this server.") .setEphemeral(true) .queue(); return; @@ -53,7 +53,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!bot.hasPermission(Permission.BAN_MEMBERS)) { - event.reply("I don't have the required permissions to unban the user from this server.") + event.reply("I don't have the BAN_MEMBERS permission to unban the user from this server.") .setEphemeral(true) .queue(); return; From 9ae34779cc1567041d655e40ee8d323a1f6ce6d7 Mon Sep 17 00:00:00 2001 From: Yusuf Arfan Ismail Date: Tue, 19 Oct 2021 13:16:25 +0100 Subject: [PATCH 058/140] Update BanCommand.java --- .../org/togetherjava/tjbot/commands/moderation/BanCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index d3a103c9c5..3507084cc1 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -98,7 +98,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { event.getJDA() .openPrivateChannelById(userId) .flatMap(channel -> channel.sendMessage( - "Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Togherer Java'. If you think this was a mistake, please contact a moderator or adming of the guild. The ban reason is: " + "Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'. If you think this was a mistake, please contact a moderator or admin of the guild. The ban reason is: " + reason)) .queue(); From d31f7c456ca8c72921a50f57484f8533d087171e Mon Sep 17 00:00:00 2001 From: Yusuf Arfan Ismail Date: Tue, 19 Oct 2021 13:17:45 +0100 Subject: [PATCH 059/140] Update UnbanCommand.java --- .../tjbot/commands/moderation/UnbanCommand.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java index d726f1c6d2..bd9beb414b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java @@ -45,7 +45,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { Member author = Objects.requireNonNull(event.getMember()); if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.BAN_MEMBERS)) { - event.reply("You do not have the BAN_MEMBERS permission to urban users from this server.") + event.reply( + "You do not have the BAN_MEMBERS permission to urban users from this server.") .setEphemeral(true) .queue(); return; @@ -53,7 +54,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!bot.hasPermission(Permission.BAN_MEMBERS)) { - event.reply("I don't have the BAN_MEMBERS permission to unban the user from this server.") + event.reply( + "I don't have the BAN_MEMBERS permission to unban the user from this server.") .setEphemeral(true) .queue(); return; From 30eb8aed7f8a53d84dcc1a07f958a55dcb3fb7f1 Mon Sep 17 00:00:00 2001 From: Yusuf Arfan Ismail Date: Tue, 19 Oct 2021 13:17:48 +0100 Subject: [PATCH 060/140] Update KickCommand.java --- .../tjbot/commands/moderation/KickCommand.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 743d9bd9db..fcba1d63ee 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -52,7 +52,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { long userId = Objects.requireNonNull(user).getUser().getIdLong(); if (!author.hasPermission(Permission.KICK_MEMBERS)) { - event.reply("You do not have the required permissions to kick users from this server.") + event.reply( + "You do not have the KICK_MEMBERS permission to kick users from this server.") .setEphemeral(true) .queue(); return; @@ -68,7 +69,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!bot.hasPermission(Permission.KICK_MEMBERS)) { - event.reply("I don't have the required permissions to kick users from this server.") + event.reply("I don't have the KICK_MEMBERS permission to kick users from this server.") .setEphemeral(true) .queue(); return; @@ -84,8 +85,9 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { event.getJDA() .openPrivateChannelById(userId) - .flatMap(channel -> channel - .sendMessage("You have been kicked for this reason " + reason)) + .flatMap(channel -> channel.sendMessage( + "Hey there, sorry to tell you but unfortunately you have been kicked from the guild 'Together Java'. If you think this was a mistake, please contact a moderator or admin of the guild. The kick reason is: " + + reason)) .queue(); event.getGuild() From d9bf30b073c87fea1e7b81976068d6570ebdb964 Mon Sep 17 00:00:00 2001 From: Yusuf Arfan Ismail Date: Tue, 19 Oct 2021 13:53:10 +0100 Subject: [PATCH 061/140] I think i have solved the problem --- .../togetherjava/tjbot/commands/moderation/BanCommand.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 3507084cc1..a1d2bad66e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -83,9 +83,9 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { } int deleteMessageHistoryDays = - (int) Objects.requireNonNull(event.getOption("delete-message-history-days")) - .getAsLong(); - + Math.toIntExact(Objects.requireNonNull(event.getOption("delete-message-history-days")) + .getAsLong()); + if (deleteMessageHistoryDays < 1 || deleteMessageHistoryDays > 7) { event.reply( "The amount of days of the message history to delete must be between 1 and 7, but was " From b69853415a85957967d5707b17e0320c13927bd4 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Tue, 19 Oct 2021 22:05:05 +0100 Subject: [PATCH 062/140] update --- .../togetherjava/tjbot/commands/Commands.java | 3 +- ...eMessageCommand.java => PurgeCommand.java} | 4 +- .../commands/moderation/WarnCommand.java | 43 ------------------- .../main/resources/db/V1_Add_Warn_Table.sql | 6 --- 4 files changed, 3 insertions(+), 53 deletions(-) rename application/src/main/java/org/togetherjava/tjbot/commands/moderation/{DeleteMessageCommand.java => PurgeCommand.java} (96%) delete mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java delete mode 100644 application/src/main/resources/db/V1_Add_Warn_Table.sql diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java index db84fb8c80..38d3c6672e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java @@ -37,7 +37,6 @@ public enum Commands { // hence this list may not necessarily represent the full list of all commands actually // available. return List.of(new PingCommand(), new DatabaseCommand(database), new KickCommand(), - new BanCommand(), new UnbanCommand(), new DeleteMessageCommand(), - new WarnCommand(database), new TeXCommand()); + new BanCommand(), new UnbanCommand(), new PurgeCommand(), new TeXCommand()); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java similarity index 96% rename from application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java rename to application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java index 1081042de7..f315ccd84f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/DeleteMessageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java @@ -20,11 +20,11 @@ * that amount of messages. {@code Banned User!}. * */ -public final class DeleteMessageCommand extends SlashCommandAdapter { +public final class PurgeCommand extends SlashCommandAdapter { private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); private static final String NUMBER_OF_MESSAGES = "number_of_messages"; - public DeleteMessageCommand() { + public PurgeCommand() { super("prune", "Use this command to delete a batch of messages", SlashCommandVisibility.GUILD); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java deleted file mode 100644 index 66ad183434..0000000000 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.togetherjava.tjbot.commands.moderation; - -import net.dv8tion.jda.api.entities.Member; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.interactions.commands.OptionType; -import org.jetbrains.annotations.NotNull; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; -import org.togetherjava.tjbot.db.Database; - -import java.util.Objects; - -public final class WarnCommand extends SlashCommandAdapter { - private static final String REASON_OPTION = "reason"; - private static final String USER_OPTION = "user"; - private final Database database; - - public WarnCommand(Database database) { - super("warn", "Use this command to warn", SlashCommandVisibility.GUILD); - getData().addOption(OptionType.USER, USER_OPTION, "The user which you want to warn", true) - .addOption(OptionType.STRING, REASON_OPTION, "The reason of the warb", true); - - this.database = database; - } - - @Override - public void onSlashCommand(@NotNull SlashCommandEvent event) { - Member user = Objects.requireNonNull(event.getOption(USER_OPTION)).getAsMember(); - String reason = Objects.requireNonNull(event.getOption(REASON_OPTION)).getAsString(); - - /* - * try { database.write(context -> { StorageRecord storageRecord = - * context.newRecord(Storage.STORAGE).setKey(user).setValue(reason); if - * (storageRecord.update() == 0) { storageRecord.insert(); } }); - * - * event.reply("Saved under '" + key + "'.").queue(); } catch (DatabaseException e) { - * logger.error("Failed to put message", e); - * event.reply("Sorry, something went wrong.").setEphemeral(true).queue(); } - */ - - - } -} diff --git a/application/src/main/resources/db/V1_Add_Warn_Table.sql b/application/src/main/resources/db/V1_Add_Warn_Table.sql deleted file mode 100644 index c80a66e8ff..0000000000 --- a/application/src/main/resources/db/V1_Add_Warn_Table.sql +++ /dev/null @@ -1,6 +0,0 @@ -CREATE TABLE warn -( - user_id INTEGER NOT NULL, - guild_id INTEGER NOT NULL, - warns INTEGER NOT NULL -) From c531c091611fe81460def574e7f317f959c46a27 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Tue, 19 Oct 2021 22:27:18 +0100 Subject: [PATCH 063/140] Update PurgeCommand.java --- .../tjbot/commands/moderation/PurgeCommand.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java index f315ccd84f..5560641cdb 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java @@ -39,7 +39,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { final TextChannel channel = event.getTextChannel(); if (!Objects.requireNonNull(author).hasPermission(Permission.MESSAGE_MANAGE)) { - event.reply("You are missing permission to manage the message") + event.reply("You are missing MESSAGE_MANAGE permission to delete these the message") .setEphemeral(true) .queue(); return; @@ -48,14 +48,14 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { final Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!bot.hasPermission(Permission.MESSAGE_MANAGE)) { - event.reply("I am missing permissions to manage these messages") + event.reply("I am missing MESSAGE_MANAGE permission to delete these messages") .setEphemeral(true) .queue(); return; } - int amount = (int) Objects.requireNonNull(event.getOption(NUMBER_OF_MESSAGES)).getAsLong(); - + int amount = Math.toIntExact(Objects.requireNonNull(event.getOption(NUMBER_OF_MESSAGES)) + .getAsLong()); var messageHistory = channel.getHistory().retrievePast(amount).complete(); if (amount < 100) { From abfb6248277c6f80f75ea9167dc49a7862b1a71b Mon Sep 17 00:00:00 2001 From: Yusuf Date: Tue, 19 Oct 2021 22:28:17 +0100 Subject: [PATCH 064/140] Update BanCommand.java --- .../org/togetherjava/tjbot/commands/moderation/BanCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index a1d2bad66e..d2ab9b23a0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -83,7 +83,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { } int deleteMessageHistoryDays = - Math.toIntExact(Objects.requireNonNull(event.getOption("delete-message-history-days")) + Math.toIntExact(Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) .getAsLong()); if (deleteMessageHistoryDays < 1 || deleteMessageHistoryDays > 7) { From 6e1102473dc304b774d1b527ac180aa69c473da7 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Tue, 19 Oct 2021 22:31:26 +0100 Subject: [PATCH 065/140] Update PurgeCommand.java --- .../tjbot/commands/moderation/PurgeCommand.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java index 5560641cdb..fa3b3648d5 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java @@ -56,10 +56,13 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { int amount = Math.toIntExact(Objects.requireNonNull(event.getOption(NUMBER_OF_MESSAGES)) .getAsLong()); + var messageHistory = channel.getHistory().retrievePast(amount).complete(); - if (amount < 100) { - event.reply("You can only delete 1 to 100 messages").setEphemeral(true).queue(); + if (amount < 100 || amount > 1) { + event.reply("You can only delete 1 to 100 messages") + .setEphemeral(true) + .queue(); } else { channel.purgeMessages(messageHistory); } From 02cf3bedc859186c84c2b2930022676cc0115b29 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Tue, 19 Oct 2021 22:32:29 +0100 Subject: [PATCH 066/140] Cleaned the code --- .../tjbot/commands/moderation/BanCommand.java | 8 ++++---- .../tjbot/commands/moderation/PurgeCommand.java | 10 ++++------ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index d2ab9b23a0..b1e4add31c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -82,10 +82,10 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - int deleteMessageHistoryDays = - Math.toIntExact(Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) - .getAsLong()); - + int deleteMessageHistoryDays = Math + .toIntExact(Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) + .getAsLong()); + if (deleteMessageHistoryDays < 1 || deleteMessageHistoryDays > 7) { event.reply( "The amount of days of the message history to delete must be between 1 and 7, but was " diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java index fa3b3648d5..f023eac22b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java @@ -54,15 +54,13 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - int amount = Math.toIntExact(Objects.requireNonNull(event.getOption(NUMBER_OF_MESSAGES)) - .getAsLong()); + int amount = Math + .toIntExact(Objects.requireNonNull(event.getOption(NUMBER_OF_MESSAGES)).getAsLong()); var messageHistory = channel.getHistory().retrievePast(amount).complete(); - if (amount < 100 || amount > 1) { - event.reply("You can only delete 1 to 100 messages") - .setEphemeral(true) - .queue(); + if (amount < 100 || amount > 1) { + event.reply("You can only delete 1 to 100 messages").setEphemeral(true).queue(); } else { channel.purgeMessages(messageHistory); } From 84a5b24c1e00d39fe322248d04b2ceea6b8f652c Mon Sep 17 00:00:00 2001 From: Yusuf Date: Tue, 19 Oct 2021 22:33:19 +0100 Subject: [PATCH 067/140] Cleaned the code --- .../java/org/togetherjava/tjbot/commands/Commands.java | 2 +- .../tjbot/commands/moderation/BanCommand.java | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java index 8a47e7bf2f..b8dc8374ae 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java @@ -37,6 +37,6 @@ public enum Commands { // hence this list may not necessarily represent the full list of all commands actually // available. return List.of(new PingCommand(), new DatabaseCommand(database), new KickCommand(), - new BanCommand(), new UnbanCommand(), new TeXCommand()); + new BanCommand(), new UnbanCommand(), new TeXCommand()); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index d2ab9b23a0..b1e4add31c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -82,10 +82,10 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - int deleteMessageHistoryDays = - Math.toIntExact(Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) - .getAsLong()); - + int deleteMessageHistoryDays = Math + .toIntExact(Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) + .getAsLong()); + if (deleteMessageHistoryDays < 1 || deleteMessageHistoryDays > 7) { event.reply( "The amount of days of the message history to delete must be between 1 and 7, but was " From a8a1ecff810a14a7357e78ad4607471d2c6fcdfc Mon Sep 17 00:00:00 2001 From: Yusuf Date: Wed, 20 Oct 2021 20:13:58 +0100 Subject: [PATCH 068/140] continue later --- .../tjbot/commands/moderation/BanCommand.java | 44 ++++++++++--------- .../commands/moderation/KickCommand.java | 7 +-- .../commands/moderation/UnbanCommand.java | 2 +- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index b1e4add31c..7ce516b92b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -3,6 +3,7 @@ import net.dv8tion.jda.api.Permission; 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; @@ -27,26 +28,26 @@ public final class BanCommand extends SlashCommandAdapter { private static final String USER_OPTION = "user"; private static final String DELETE_MESSAGE_HISTORY_DAYS_OPTION = "delete-message-history-days"; private static final String REASON_OPTION = "reason"; + //private static final Integer DELETE_HISTORY_MIN_DAYS = 1; + //private static final Integer DELETE_HISTORY_MAX_DAYS = 7; /** * Creates an instance of the ban command. */ public BanCommand() { - super("ban", "Use this command to ban a given user", SlashCommandVisibility.GUILD); + super("ban", "Bans a given user", 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) .addOption(OptionType.INTEGER, DELETE_MESSAGE_HISTORY_DAYS_OPTION, - "the amount of days of the message history to delete, otherwise no messages are deleted. 1 to 7 days.", - false) - .addOption(OptionType.STRING, REASON_OPTION, "why the user should be banned", true); + "the amount(1-7) of days of the message history to delete, otherwise no messages are deleted.", + false); } @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { Member user = Objects.requireNonNull(event.getOption(USER_OPTION)).getAsMember(); - Member author = Objects.requireNonNull(event.getMember()); - String reason = Objects.requireNonNull(event.getOption(REASON_OPTION)).getAsString(); long userId = user.getUser().getIdLong(); @@ -60,7 +61,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { if (!author.canInteract(Objects.requireNonNull(user))) { event.reply( - "This user is too powerful for you to ban because he has more permissions than uou.") + "This user is too powerful for you to ban because he has more permissions than you.") .setEphemeral(true) .queue(); return; @@ -82,30 +83,33 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - int deleteMessageHistoryDays = Math - .toIntExact(Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) - .getAsLong()); + OptionMapping deleteMessageHistoryDaysOption = + event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION); - if (deleteMessageHistoryDays < 1 || deleteMessageHistoryDays > 7) { - event.reply( - "The amount of days of the message history to delete must be between 1 and 7, but was " - + deleteMessageHistoryDays + ".") - .setEphemeral(true) - .queue(); + int deleteMessageHistoryDays = (int) event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION).getAsLong(); + + if (deleteMessageHistoryDaysOption != null) { + deleteMessageHistoryDays = 0; + } else if(deleteMessageHistoryDays < 1 || deleteMessageHistoryDays > 7) { + event.reply("The deletion days of the messages must be between 1 and 7 days.") + .setEphemeral(true) + .queue(); return; } + event.getJDA() .openPrivateChannelById(userId) .flatMap(channel -> channel.sendMessage( - "Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'. If you think this was a mistake, please contact a moderator or admin of the guild. The ban reason is: " - + reason)) + "Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'. " + + "If you think this was a mistake, please contact a moderator or admin of the guild. " + + "The ban reason is: " + reason)) .queue(); event.getGuild() .ban(user, deleteMessageHistoryDays, reason) - .flatMap(v -> event.reply(user.getUser().getAsTag() + "was banned by " - + author.getEffectiveName() + "for: " + reason)) + .flatMap(v -> event.reply(user.getUser().getAsTag() + " was banned by " + + author.getUser().getAsTag() + " for: " + reason)) .queue(); String userName = user.getId(); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index fcba1d63ee..8ca7375d01 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -27,7 +27,7 @@ public final class KickCommand extends SlashCommandAdapter { * Creates an instance of the kick command. */ public KickCommand() { - super("kick", "Use this command to kick a given user", SlashCommandVisibility.GUILD); + super("kick", "Kicks a given user", SlashCommandVisibility.GUILD); getData().addOption(OptionType.USER, USER_OPTION, "The user which you want to kick", true) .addOption(OptionType.STRING, REASON_OPTION, "why the user should be kicked", true); @@ -61,7 +61,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { if (!author.canInteract(Objects.requireNonNull(user))) { event.reply( - "This user is too powerful for you to kick because he has more permissions than uou.") + "This user is too powerful for you to kick because he has more permissions than you.") .setEphemeral(true) .queue(); return; @@ -92,7 +92,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { event.getGuild() .kick(user, reason) - .flatMap(v -> event.reply("Kicked the user" + user.getUser().getAsTag())) + .flatMap(v -> event.reply(user.getUser().getAsTag() + " was kicked by " + + author.getUser().getAsTag() + " for: " + reason)) .queue(); String userName = user.getId(); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java index bd9beb414b..a9cc389d16 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java @@ -24,7 +24,7 @@ public final class UnbanCommand extends SlashCommandAdapter { * Creates an instance of the ban command. */ public UnbanCommand() { - super("unban", "Use this command to unban a user", SlashCommandVisibility.GUILD); + super("unban", "Unbans a given user", SlashCommandVisibility.GUILD); getData().addOption(OptionType.STRING, USER_ID, "The user id of the user which you want to unban", true); From b17ef9821ea2e873623e86dfc22762480bc6d207 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Wed, 20 Oct 2021 20:17:27 +0100 Subject: [PATCH 069/140] Update BanCommand.java --- .../tjbot/commands/moderation/BanCommand.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 7ce516b92b..2ac0d6ab61 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -28,8 +28,8 @@ public final class BanCommand extends SlashCommandAdapter { private static final String USER_OPTION = "user"; private static final String DELETE_MESSAGE_HISTORY_DAYS_OPTION = "delete-message-history-days"; private static final String REASON_OPTION = "reason"; - //private static final Integer DELETE_HISTORY_MIN_DAYS = 1; - //private static final Integer DELETE_HISTORY_MAX_DAYS = 7; + // private static final Integer DELETE_HISTORY_MIN_DAYS = 1; + // private static final Integer DELETE_HISTORY_MAX_DAYS = 7; /** * Creates an instance of the ban command. @@ -86,14 +86,15 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { OptionMapping deleteMessageHistoryDaysOption = event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION); - int deleteMessageHistoryDays = (int) event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION).getAsLong(); + int deleteMessageHistoryDays = + (int) event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION).getAsLong(); if (deleteMessageHistoryDaysOption != null) { deleteMessageHistoryDays = 0; - } else if(deleteMessageHistoryDays < 1 || deleteMessageHistoryDays > 7) { + } else if (deleteMessageHistoryDays < 1 || deleteMessageHistoryDays > 7) { event.reply("The deletion days of the messages must be between 1 and 7 days.") - .setEphemeral(true) - .queue(); + .setEphemeral(true) + .queue(); return; } From c825f78cc01d094d650ff3100b0242d7dd8edcbc Mon Sep 17 00:00:00 2001 From: Yusuf Date: Wed, 20 Oct 2021 20:27:42 +0100 Subject: [PATCH 070/140] Fixed everything + spotlessApply --- application/build.gradle | 2 +- .../org/togetherjava/tjbot/commands/moderation/BanCommand.java | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/application/build.gradle b/application/build.gradle index ab2815d6fb..29b5e2fa36 100644 --- a/application/build.gradle +++ b/application/build.gradle @@ -45,7 +45,7 @@ shadowJar { dependencies { implementation project(':database') - implementation 'net.dv8tion:JDA:4.3.0_334' + implementation 'net.dv8tion:JDA:4.3.0_335' implementation 'org.apache.logging.log4j:log4j-api:2.14.1' implementation 'org.apache.logging.log4j:log4j-core:2.14.1' diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 2ac0d6ab61..24ea41435e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -28,8 +28,6 @@ public final class BanCommand extends SlashCommandAdapter { private static final String USER_OPTION = "user"; private static final String DELETE_MESSAGE_HISTORY_DAYS_OPTION = "delete-message-history-days"; private static final String REASON_OPTION = "reason"; - // private static final Integer DELETE_HISTORY_MIN_DAYS = 1; - // private static final Integer DELETE_HISTORY_MAX_DAYS = 7; /** * Creates an instance of the ban command. @@ -98,7 +96,6 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - event.getJDA() .openPrivateChannelById(userId) .flatMap(channel -> channel.sendMessage( From 59b7dff361cc676002a8330df76c0fda971c8e0e Mon Sep 17 00:00:00 2001 From: Yusuf Date: Wed, 20 Oct 2021 20:47:56 +0100 Subject: [PATCH 071/140] Update BanCommand.java --- .../tjbot/commands/moderation/BanCommand.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 24ea41435e..55733c7d04 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -11,6 +11,7 @@ import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import java.awt.*; import java.util.Objects; @@ -81,6 +82,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } + + /* OptionMapping deleteMessageHistoryDaysOption = event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION); @@ -95,6 +98,19 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { .queue(); return; } + + */ + + Choice days = new Choice(); + days.add("0"); + days.add("1"); + days.add("2"); + days.add("3"); + days.add("5"); + days.add("6"); + days.add("7"); + + event.getJDA() .openPrivateChannelById(userId) From 72268008cd8b572d71da341296b28d4527149494 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Wed, 20 Oct 2021 20:56:37 +0100 Subject: [PATCH 072/140] Update BanCommand.java --- .../tjbot/commands/moderation/BanCommand.java | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 55733c7d04..ab712b250f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -84,25 +84,21 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { /* - OptionMapping deleteMessageHistoryDaysOption = - event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION); + int deleteMessageHistoryDays = Math + .toIntExact(Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) + .getAsLong()); - int deleteMessageHistoryDays = - (int) event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION).getAsLong(); - - if (deleteMessageHistoryDaysOption != null) { - deleteMessageHistoryDays = 0; - } else if (deleteMessageHistoryDays < 1 || deleteMessageHistoryDays > 7) { - event.reply("The deletion days of the messages must be between 1 and 7 days.") + if (deleteMessageHistoryDays < 1 || deleteMessageHistoryDays > 7) { + event.reply( + "The amount of days of the message history to delete must be between 1 and 7, but was " + + deleteMessageHistoryDays + ".") .setEphemeral(true) .queue(); return; } - */ Choice days = new Choice(); - days.add("0"); days.add("1"); days.add("2"); days.add("3"); @@ -112,6 +108,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { + + event.getJDA() .openPrivateChannelById(userId) .flatMap(channel -> channel.sendMessage( From 7687186044240cd26ffa9be55271062aef7949fa Mon Sep 17 00:00:00 2001 From: Yusuf Date: Thu, 21 Oct 2021 06:39:23 +0100 Subject: [PATCH 073/140] Fixed it I have put it in a if statement. If it is not null then use deldays if there is no deldays then set to 0 --- .../tjbot/commands/moderation/BanCommand.java | 93 +++++++++++-------- 1 file changed, 52 insertions(+), 41 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index ab712b250f..3bc9feda67 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -11,7 +11,6 @@ import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.SlashCommandVisibility; -import java.awt.*; import java.util.Objects; @@ -83,51 +82,63 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { } - /* - int deleteMessageHistoryDays = Math - .toIntExact(Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) - .getAsLong()); - - if (deleteMessageHistoryDays < 1 || deleteMessageHistoryDays > 7) { - event.reply( - "The amount of days of the message history to delete must be between 1 and 7, but was " - + deleteMessageHistoryDays + ".") - .setEphemeral(true) + OptionMapping option = event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION); + + if (option != null) { + int deleteMessageHistoryDays = Math.toIntExact( + Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) + .getAsLong()); + + if (deleteMessageHistoryDays < 1 || deleteMessageHistoryDays > 7) { + event.reply( + "The amount of days of the message history to delete must be between 1 and 7, but was " + + deleteMessageHistoryDays + ".") + .setEphemeral(true) + .queue(); + return; + } + + event.getJDA() + .openPrivateChannelById(userId) + .flatMap(channel -> channel.sendMessage( + "Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'. " + + "If you think this was a mistake, please contact a moderator or admin of the guild. " + + "The ban reason is: " + reason)) .queue(); - return; - } - */ - - Choice days = new Choice(); - days.add("1"); - days.add("2"); - days.add("3"); - days.add("5"); - days.add("6"); - days.add("7"); - - + event.getGuild() + .ban(user, deleteMessageHistoryDays, reason) + .flatMap(v -> event.reply(user.getUser().getAsTag() + " was banned by " + + author.getUser().getAsTag() + " for: " + reason)) + .queue(); + String userName = user.getId(); + String authorName = author.getId(); + logger.info( + " '{}' banned the user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", + authorName, userName, deleteMessageHistoryDays, reason); + + } else { + event.getJDA() + .openPrivateChannelById(userId) + .flatMap(channel -> channel.sendMessage( + "Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'. " + + "If you think this was a mistake, please contact a moderator or admin of the guild. " + + "The ban reason is: " + reason)) + .queue(); - event.getJDA() - .openPrivateChannelById(userId) - .flatMap(channel -> channel.sendMessage( - "Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'. " - + "If you think this was a mistake, please contact a moderator or admin of the guild. " - + "The ban reason is: " + reason)) - .queue(); + event.getGuild() + .ban(user, 0, reason) + .flatMap(v -> event.reply(user.getUser().getAsTag() + " was banned by " + + author.getUser().getAsTag() + " for: " + reason)) + .queue(); - event.getGuild() - .ban(user, deleteMessageHistoryDays, reason) - .flatMap(v -> event.reply(user.getUser().getAsTag() + " was banned by " - + author.getUser().getAsTag() + " for: " + reason)) - .queue(); + String userName = user.getId(); + String authorName = author.getId(); + logger.info( + " '{}' banned the user '{}' and deleted the message history of the last 0 days. Reason was '{}'", + authorName, userName, reason); - String userName = user.getId(); - String authorName = author.getId(); - logger.info( - " '{}' banned the user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", - authorName, userName, deleteMessageHistoryDays, reason); + } } } From 8775730c353a97a3af8c24af731b896ec33ae7d2 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Thu, 21 Oct 2021 06:55:26 +0100 Subject: [PATCH 074/140] Update BanCommand.java --- .../org/togetherjava/tjbot/commands/moderation/BanCommand.java | 1 - 1 file changed, 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 3bc9feda67..1f0d78c40a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -81,7 +81,6 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - OptionMapping option = event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION); if (option != null) { From 7aee2878ee089592903e13780889eded8ed91d99 Mon Sep 17 00:00:00 2001 From: Yusuf Arfan Ismail Date: Thu, 21 Oct 2021 15:06:39 +0100 Subject: [PATCH 075/140] Update application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java Co-authored-by: java-coding-prodigy <85149944+java-coding-prodigy@users.noreply.github.com> --- .../togetherjava/tjbot/commands/moderation/UnbanCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java index a9cc389d16..2720ee8a9d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java @@ -17,7 +17,7 @@ * The implemented command is {@code /unban user_id}, upon which the bot will unban the user. */ public final class UnbanCommand extends SlashCommandAdapter { - private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); + private static final Logger logger = LoggerFactory.getLogger(UnbanCommand.class); private static final String USER_ID = "user_id"; /** From 5550872822309b7dd200691496d96e2ab71bb92c Mon Sep 17 00:00:00 2001 From: Yusuf Date: Thu, 21 Oct 2021 19:57:45 +0100 Subject: [PATCH 076/140] changed some things --- application/build.gradle | 2 +- .../tjbot/commands/moderation/BanCommand.java | 85 ++++++++++--------- .../commands/moderation/KickCommand.java | 2 +- 3 files changed, 45 insertions(+), 44 deletions(-) diff --git a/application/build.gradle b/application/build.gradle index 29b5e2fa36..ab2815d6fb 100644 --- a/application/build.gradle +++ b/application/build.gradle @@ -45,7 +45,7 @@ shadowJar { dependencies { implementation project(':database') - implementation 'net.dv8tion:JDA:4.3.0_335' + implementation 'net.dv8tion:JDA:4.3.0_334' implementation 'org.apache.logging.log4j:log4j-api:2.14.1' implementation 'org.apache.logging.log4j:log4j-core:2.14.1' diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 1f0d78c40a..0530a56020 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -11,6 +11,7 @@ import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import java.awt.*; import java.util.Objects; @@ -28,6 +29,8 @@ public final class BanCommand extends SlashCommandAdapter { private static final String USER_OPTION = "user"; private static final String DELETE_MESSAGE_HISTORY_DAYS_OPTION = "delete-message-history-days"; private static final String REASON_OPTION = "reason"; + private static final Integer DELETE_HISTORY_MIN_DAYS = 1; + private static final Integer DELETE_HISTORY_MAX_DAYS = 7; /** * Creates an instance of the ban command. @@ -35,11 +38,16 @@ public final class BanCommand extends SlashCommandAdapter { public BanCommand() { super("ban", "Bans a given user", SlashCommandVisibility.GUILD); + //TODO add choices + Choice days = new Choice(); + days.add("1"); + 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) .addOption(OptionType.INTEGER, DELETE_MESSAGE_HISTORY_DAYS_OPTION, "the amount(1-7) of days of the message history to delete, otherwise no messages are deleted.", false); + } @Override @@ -59,7 +67,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { if (!author.canInteract(Objects.requireNonNull(user))) { event.reply( - "This user is too powerful for you to ban because he has more permissions than you.") + "This user is too powerful for you to ban.") .setEphemeral(true) .queue(); return; @@ -84,60 +92,53 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { OptionMapping option = event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION); if (option != null) { - int deleteMessageHistoryDays = Math.toIntExact( + int days = Math.toIntExact( Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) - .getAsLong()); - - if (deleteMessageHistoryDays < 1 || deleteMessageHistoryDays > 7) { - event.reply( - "The amount of days of the message history to delete must be between 1 and 7, but was " - + deleteMessageHistoryDays + ".") + .getAsLong()); + deleteMessageHistory(days, event); + banGuild(user, reason, days, event); + String userName = user.getId(); + String authorName = author.getId(); + logger.info( + " '{}' banned the user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", + authorName, userName, days, reason); + } else { + openPrivateChannel(userId, reason, event); + banGuild(user, reason, 0, event); + String userName = user.getId(); + String authorName = author.getId(); + logger.info( + " '{}' banned the user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", + authorName, userName, 0, reason); + } + } + public static void deleteMessageHistory(int days , @NotNull SlashCommandEvent event) { + if (days < DELETE_HISTORY_MIN_DAYS || days > DELETE_HISTORY_MAX_DAYS) { + event.reply( + "The amount of days of the message history to delete must be between " + + DELETE_HISTORY_MIN_DAYS + " and " + DELETE_HISTORY_MAX_DAYS + " , but was " + + days + ".") .setEphemeral(true) .queue(); - return; - } - - event.getJDA() - .openPrivateChannelById(userId) - .flatMap(channel -> channel.sendMessage( - "Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'. " - + "If you think this was a mistake, please contact a moderator or admin of the guild. " - + "The ban reason is: " + reason)) - .queue(); + } + } - event.getGuild() - .ban(user, deleteMessageHistoryDays, reason) + public static void banGuild(Member user, String reason, int days, @NotNull SlashCommandEvent event) { + Member author = Objects.requireNonNull(event.getMember()); + event.getGuild() + .ban(user, days, reason) .flatMap(v -> event.reply(user.getUser().getAsTag() + " was banned by " + author.getUser().getAsTag() + " for: " + reason)) .queue(); + } - String userName = user.getId(); - String authorName = author.getId(); - logger.info( - " '{}' banned the user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", - authorName, userName, deleteMessageHistoryDays, reason); - - } else { - event.getJDA() + public static void openPrivateChannel(long userId, String reason, @NotNull SlashCommandEvent event) { + event.getJDA() .openPrivateChannelById(userId) .flatMap(channel -> channel.sendMessage( "Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'. " + "If you think this was a mistake, please contact a moderator or admin of the guild. " + "The ban reason is: " + reason)) .queue(); - - event.getGuild() - .ban(user, 0, reason) - .flatMap(v -> event.reply(user.getUser().getAsTag() + " was banned by " - + author.getUser().getAsTag() + " for: " + reason)) - .queue(); - - String userName = user.getId(); - String authorName = author.getId(); - logger.info( - " '{}' banned the user '{}' and deleted the message history of the last 0 days. Reason was '{}'", - authorName, userName, reason); - - } } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 8ca7375d01..307796b62b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -61,7 +61,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { if (!author.canInteract(Objects.requireNonNull(user))) { event.reply( - "This user is too powerful for you to kick because he has more permissions than you.") + "\"This user is too powerful for you to kick.") .setEphemeral(true) .queue(); return; From 73f30e686936e9db95a73fe52f17cac0fd2ad71a Mon Sep 17 00:00:00 2001 From: Yusuf Date: Thu, 21 Oct 2021 20:01:55 +0100 Subject: [PATCH 077/140] Update BanCommand.java --- .../tjbot/commands/moderation/BanCommand.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 0530a56020..e8842d8c2a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -41,7 +41,7 @@ public BanCommand() { //TODO add choices Choice days = new Choice(); days.add("1"); - + 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) .addOption(OptionType.INTEGER, DELETE_MESSAGE_HISTORY_DAYS_OPTION, @@ -91,25 +91,22 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { OptionMapping option = event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION); + String userName = user.getId(); + String authorName = author.getId(); if (option != null) { int days = Math.toIntExact( Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) .getAsLong()); deleteMessageHistory(days, event); banGuild(user, reason, days, event); - String userName = user.getId(); - String authorName = author.getId(); + logger.info( " '{}' banned the user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", authorName, userName, days, reason); } else { openPrivateChannel(userId, reason, event); banGuild(user, reason, 0, event); - String userName = user.getId(); - String authorName = author.getId(); - logger.info( - " '{}' banned the user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", - authorName, userName, 0, reason); + logger(authorName, userName, 0, reason); } } public static void deleteMessageHistory(int days , @NotNull SlashCommandEvent event) { @@ -141,4 +138,9 @@ public static void openPrivateChannel(long userId, String reason, @NotNull Slash + "The ban reason is: " + reason)) .queue(); } + public static void logger(String authorNameId, String userNameId, int days, String reason) { + logger.info( + " '{}' banned the user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", + authorNameId, userNameId, days, reason); + } } From 81e3f17dc0c2ee4c5fb88bf032c614eb3a75f99c Mon Sep 17 00:00:00 2001 From: Yusuf Date: Thu, 21 Oct 2021 20:05:12 +0100 Subject: [PATCH 078/140] a bit cleaner --- .../tjbot/commands/moderation/BanCommand.java | 46 ++--------------- .../commands/moderation/BanHelperMethods.java | 50 +++++++++++++++++++ 2 files changed, 55 insertions(+), 41 deletions(-) create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index e8842d8c2a..2eebb9f269 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -29,8 +29,6 @@ public final class BanCommand extends SlashCommandAdapter { private static final String USER_OPTION = "user"; private static final String DELETE_MESSAGE_HISTORY_DAYS_OPTION = "delete-message-history-days"; private static final String REASON_OPTION = "reason"; - private static final Integer DELETE_HISTORY_MIN_DAYS = 1; - private static final Integer DELETE_HISTORY_MAX_DAYS = 7; /** * Creates an instance of the ban command. @@ -97,50 +95,16 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { int days = Math.toIntExact( Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) .getAsLong()); - deleteMessageHistory(days, event); - banGuild(user, reason, days, event); + BanHelperMethods.deleteMessageHistory(days, event); + BanHelperMethods.banGuild(user, reason, days, event); logger.info( " '{}' banned the user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", authorName, userName, days, reason); } else { - openPrivateChannel(userId, reason, event); - banGuild(user, reason, 0, event); - logger(authorName, userName, 0, reason); + BanHelperMethods.openPrivateChannel(userId, reason, event); + BanHelperMethods.banGuild(user, reason, 0, event); + BanHelperMethods.logger(authorName, userName, 0, reason); } } - public static void deleteMessageHistory(int days , @NotNull SlashCommandEvent event) { - if (days < DELETE_HISTORY_MIN_DAYS || days > DELETE_HISTORY_MAX_DAYS) { - event.reply( - "The amount of days of the message history to delete must be between " + - DELETE_HISTORY_MIN_DAYS + " and " + DELETE_HISTORY_MAX_DAYS + " , but was " - + days + ".") - .setEphemeral(true) - .queue(); - } - } - - public static void banGuild(Member user, String reason, int days, @NotNull SlashCommandEvent event) { - Member author = Objects.requireNonNull(event.getMember()); - event.getGuild() - .ban(user, days, reason) - .flatMap(v -> event.reply(user.getUser().getAsTag() + " was banned by " - + author.getUser().getAsTag() + " for: " + reason)) - .queue(); - } - - public static void openPrivateChannel(long userId, String reason, @NotNull SlashCommandEvent event) { - event.getJDA() - .openPrivateChannelById(userId) - .flatMap(channel -> channel.sendMessage( - "Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'. " - + "If you think this was a mistake, please contact a moderator or admin of the guild. " - + "The ban reason is: " + reason)) - .queue(); - } - public static void logger(String authorNameId, String userNameId, int days, String reason) { - logger.info( - " '{}' banned the user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", - authorNameId, userNameId, days, reason); - } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java new file mode 100644 index 0000000000..0b34e57810 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java @@ -0,0 +1,50 @@ +package org.togetherjava.tjbot.commands.moderation; + +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Objects; + +public class BanHelperMethods { + private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); + private static final Integer DELETE_HISTORY_MIN_DAYS = 1; + private static final Integer DELETE_HISTORY_MAX_DAYS = 7; + + public static void deleteMessageHistory(int days , @NotNull SlashCommandEvent event) { + if (days < DELETE_HISTORY_MIN_DAYS || days > DELETE_HISTORY_MAX_DAYS) { + event.reply( + "The amount of days of the message history to delete must be between " + + DELETE_HISTORY_MIN_DAYS + " and " + DELETE_HISTORY_MAX_DAYS + " , but was " + + days + ".") + .setEphemeral(true) + .queue(); + } + } + + public static void banGuild(Member user, String reason, int days, @NotNull SlashCommandEvent event) { + Member author = Objects.requireNonNull(event.getMember()); + event.getGuild() + .ban(user, days, reason) + .flatMap(v -> event.reply(user.getUser().getAsTag() + " was banned by " + + author.getUser().getAsTag() + " for: " + reason)) + .queue(); + } + + public static void openPrivateChannel(long userId, String reason, @NotNull SlashCommandEvent event) { + event.getJDA() + .openPrivateChannelById(userId) + .flatMap(channel -> channel.sendMessage( + "Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'. " + + "If you think this was a mistake, please contact a moderator or admin of the guild. " + + "The ban reason is: " + reason)) + .queue(); + } + public static void logger(String authorNameId, String userNameId, int days, String reason) { + logger.info( + " '{}' banned the user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", + authorNameId, userNameId, days, reason); + } +} From c6722dacc931c4c4af54aba97caba8a08a6eead6 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Thu, 21 Oct 2021 20:39:32 +0100 Subject: [PATCH 079/140] Update BanCommand.java --- .../tjbot/commands/moderation/BanCommand.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 2eebb9f269..8e3f1c60dc 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -5,6 +5,7 @@ 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; @@ -36,16 +37,19 @@ public final class BanCommand extends SlashCommandAdapter { public BanCommand() { super("ban", "Bans a given user", SlashCommandVisibility.GUILD); - //TODO add choices - Choice days = new Choice(); - days.add("1"); - 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) - .addOption(OptionType.INTEGER, DELETE_MESSAGE_HISTORY_DAYS_OPTION, - "the amount(1-7) of days of the message history to delete, otherwise no messages are deleted.", - false); - + .addOptions(new OptionData(OptionType.INTEGER, DELETE_MESSAGE_HISTORY_DAYS_OPTION, + "the amount of days of the message history to delete, otherwise no messages are deleted.", + true) + .addChoice("1",1) + .addChoice("2",2) + .addChoice("3",3) + .addChoice("4",4) + .addChoice("5",5) + .addChoice("6",6) + .addChoice("7",7) + ); } @Override From 166311381bd037ecce4f95129a91274d9b9fde81 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Thu, 21 Oct 2021 20:40:26 +0100 Subject: [PATCH 080/140] added choices --- .../tjbot/commands/moderation/BanCommand.java | 24 ++++------- .../commands/moderation/BanHelperMethods.java | 41 ++++++++++--------- .../commands/moderation/KickCommand.java | 5 +-- 3 files changed, 32 insertions(+), 38 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 8e3f1c60dc..2ef210d7cb 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -12,7 +12,6 @@ import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.SlashCommandVisibility; -import java.awt.*; import java.util.Objects; @@ -41,15 +40,13 @@ public BanCommand() { .addOption(OptionType.STRING, REASON_OPTION, "why the user should be banned", true) .addOptions(new OptionData(OptionType.INTEGER, DELETE_MESSAGE_HISTORY_DAYS_OPTION, "the amount of days of the message history to delete, otherwise no messages are deleted.", - true) - .addChoice("1",1) - .addChoice("2",2) - .addChoice("3",3) - .addChoice("4",4) - .addChoice("5",5) - .addChoice("6",6) - .addChoice("7",7) - ); + true).addChoice("1", 1) + .addChoice("2", 2) + .addChoice("3", 3) + .addChoice("4", 4) + .addChoice("5", 5) + .addChoice("6", 6) + .addChoice("7", 7)); } @Override @@ -68,10 +65,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { } if (!author.canInteract(Objects.requireNonNull(user))) { - event.reply( - "This user is too powerful for you to ban.") - .setEphemeral(true) - .queue(); + event.reply("This user is too powerful for you to ban.").setEphemeral(true).queue(); return; } @@ -98,7 +92,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { if (option != null) { int days = Math.toIntExact( Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) - .getAsLong()); + .getAsLong()); BanHelperMethods.deleteMessageHistory(days, event); BanHelperMethods.banGuild(user, reason, days, event); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java index 0b34e57810..7812ad3cbc 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java @@ -13,35 +13,38 @@ public class BanHelperMethods { private static final Integer DELETE_HISTORY_MIN_DAYS = 1; private static final Integer DELETE_HISTORY_MAX_DAYS = 7; - public static void deleteMessageHistory(int days , @NotNull SlashCommandEvent event) { + public static void deleteMessageHistory(int days, @NotNull SlashCommandEvent event) { if (days < DELETE_HISTORY_MIN_DAYS || days > DELETE_HISTORY_MAX_DAYS) { - event.reply( - "The amount of days of the message history to delete must be between " + - DELETE_HISTORY_MIN_DAYS + " and " + DELETE_HISTORY_MAX_DAYS + " , but was " - + days + ".") - .setEphemeral(true) - .queue(); + event + .reply("The amount of days of the message history to delete must be between " + + DELETE_HISTORY_MIN_DAYS + " and " + DELETE_HISTORY_MAX_DAYS + + " , but was " + days + ".") + .setEphemeral(true) + .queue(); } } - public static void banGuild(Member user, String reason, int days, @NotNull SlashCommandEvent event) { + public static void banGuild(Member user, String reason, int days, + @NotNull SlashCommandEvent event) { Member author = Objects.requireNonNull(event.getMember()); event.getGuild() - .ban(user, days, reason) - .flatMap(v -> event.reply(user.getUser().getAsTag() + " was banned by " - + author.getUser().getAsTag() + " for: " + reason)) - .queue(); + .ban(user, days, reason) + .flatMap(v -> event.reply(user.getUser().getAsTag() + " was banned by " + + author.getUser().getAsTag() + " for: " + reason)) + .queue(); } - public static void openPrivateChannel(long userId, String reason, @NotNull SlashCommandEvent event) { + public static void openPrivateChannel(long userId, String reason, + @NotNull SlashCommandEvent event) { event.getJDA() - .openPrivateChannelById(userId) - .flatMap(channel -> channel.sendMessage( - "Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'. " - + "If you think this was a mistake, please contact a moderator or admin of the guild. " - + "The ban reason is: " + reason)) - .queue(); + .openPrivateChannelById(userId) + .flatMap(channel -> channel.sendMessage( + "Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'. " + + "If you think this was a mistake, please contact a moderator or admin of the guild. " + + "The ban reason is: " + reason)) + .queue(); } + public static void logger(String authorNameId, String userNameId, int days, String reason) { logger.info( " '{}' banned the user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 307796b62b..b5b790ee2c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -60,10 +60,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { } if (!author.canInteract(Objects.requireNonNull(user))) { - event.reply( - "\"This user is too powerful for you to kick.") - .setEphemeral(true) - .queue(); + event.reply("\"This user is too powerful for you to kick.").setEphemeral(true).queue(); return; } From fe7f1d391051f63e9063ba2c7dd962bdc0468577 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Thu, 21 Oct 2021 20:43:02 +0100 Subject: [PATCH 081/140] Update BanCommand.java --- .../org/togetherjava/tjbot/commands/moderation/BanCommand.java | 1 + 1 file changed, 1 insertion(+) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 2ef210d7cb..b2c497bf64 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -90,6 +90,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { String userName = user.getId(); String authorName = author.getId(); if (option != null) { + //TODO Implement the same delete message structure for the purge message. int days = Math.toIntExact( Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) .getAsLong()); From edc42ed3f12c96739dc67e37687d645e17d8380d Mon Sep 17 00:00:00 2001 From: Yusuf Date: Thu, 21 Oct 2021 20:45:07 +0100 Subject: [PATCH 082/140] Update BanCommand.java --- .../org/togetherjava/tjbot/commands/moderation/BanCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index b2c497bf64..856efb60e4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -40,7 +40,7 @@ public BanCommand() { .addOption(OptionType.STRING, REASON_OPTION, "why the user should be banned", true) .addOptions(new OptionData(OptionType.INTEGER, DELETE_MESSAGE_HISTORY_DAYS_OPTION, "the amount of days of the message history to delete, otherwise no messages are deleted.", - true).addChoice("1", 1) + false).addChoice("1", 1) .addChoice("2", 2) .addChoice("3", 3) .addChoice("4", 4) From 2fb352ef62df997a0019d67313fb6210202d3f8d Mon Sep 17 00:00:00 2001 From: Yusuf Date: Thu, 21 Oct 2021 20:45:48 +0100 Subject: [PATCH 083/140] Update BanCommand.java --- .../togetherjava/tjbot/commands/moderation/BanCommand.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 856efb60e4..cd2019935b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -40,13 +40,16 @@ public BanCommand() { .addOption(OptionType.STRING, REASON_OPTION, "why the user should be banned", true) .addOptions(new OptionData(OptionType.INTEGER, DELETE_MESSAGE_HISTORY_DAYS_OPTION, "the amount of days of the message history to delete, otherwise no messages are deleted.", - false).addChoice("1", 1) + false) + .addChoice("0", 0) + .addChoice("1", 1) .addChoice("2", 2) .addChoice("3", 3) .addChoice("4", 4) .addChoice("5", 5) .addChoice("6", 6) - .addChoice("7", 7)); + .addChoice("7", 7) + ); } @Override From bd27130160fb2e96b6254023da1ff0ced64ff584 Mon Sep 17 00:00:00 2001 From: Yusuf Arfan Ismail Date: Thu, 21 Oct 2021 21:07:14 +0100 Subject: [PATCH 084/140] Update application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java Co-authored-by: Tais993 <49957334+Tais993@users.noreply.github.com> --- .../togetherjava/tjbot/commands/moderation/KickCommand.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index b5b790ee2c..69b0a232f9 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -93,9 +93,9 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { + author.getUser().getAsTag() + " for: " + reason)) .queue(); - String userName = user.getId(); - String authorName = author.getId(); - logger.info(" '{}' kicked the user '{}' due to reason being '{}'", authorName, userName, + String userId = user.getId(); + String authorId= author.getId(); + logger.info(" '{}' kicked the user '{}' due to reason being '{}'", authorId, userId , reason); } } From 46c2ecbdcf32c73fa6add2f972e4cc83d843e1b9 Mon Sep 17 00:00:00 2001 From: Yusuf Arfan Ismail Date: Thu, 21 Oct 2021 21:07:27 +0100 Subject: [PATCH 085/140] Update application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java Co-authored-by: Tais993 <49957334+Tais993@users.noreply.github.com> --- .../togetherjava/tjbot/commands/moderation/UnbanCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java index 2720ee8a9d..a39f1478de 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java @@ -63,7 +63,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { event.getGuild().unban(userId).flatMap(v -> event.reply("Unbanned the user")).queue(); - String authorName = author.getId(); - logger.info(" '{}' unbanned user id '{}' ", authorName, userId); + String authorId = author.getId(); + logger.info(" '{}' unbanned user id '{}' ", authorId, userId); } } From 3a328ffa2c596f9c25e0eb82bdb419333d79598f Mon Sep 17 00:00:00 2001 From: Yusuf Arfan Ismail Date: Thu, 21 Oct 2021 21:08:24 +0100 Subject: [PATCH 086/140] Update application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java Co-authored-by: Tais993 <49957334+Tais993@users.noreply.github.com> --- .../togetherjava/tjbot/commands/moderation/BanCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index cd2019935b..e6e22293d1 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -90,8 +90,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { OptionMapping option = event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION); - String userName = user.getId(); - String authorName = author.getId(); + long userName = user.getIdLong(); + long authorName = author.getIdLong(); if (option != null) { //TODO Implement the same delete message structure for the purge message. int days = Math.toIntExact( From 107e536fb9a646e5612fc0dad3a288d2b7f5a3cf Mon Sep 17 00:00:00 2001 From: Yusuf Date: Thu, 21 Oct 2021 21:09:38 +0100 Subject: [PATCH 087/140] Update BanHelperMethods.java --- .../tjbot/commands/moderation/BanHelperMethods.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java index 7812ad3cbc..5272680bec 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java @@ -45,7 +45,7 @@ public static void openPrivateChannel(long userId, String reason, .queue(); } - public static void logger(String authorNameId, String userNameId, int days, String reason) { + public static void logger(long authorNameId, long userNameId, int days, String reason) { logger.info( " '{}' banned the user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", authorNameId, userNameId, days, reason); From de1b36e01a791fb915e7b47ff473332e56c11dfe Mon Sep 17 00:00:00 2001 From: Yusuf Arfan Ismail Date: Thu, 21 Oct 2021 21:10:00 +0100 Subject: [PATCH 088/140] Update application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java Co-authored-by: Tais993 <49957334+Tais993@users.noreply.github.com> --- .../tjbot/commands/moderation/BanHelperMethods.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java index 5272680bec..5ebad3ec1d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java @@ -39,9 +39,11 @@ public static void openPrivateChannel(long userId, String reason, event.getJDA() .openPrivateChannelById(userId) .flatMap(channel -> channel.sendMessage( - "Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'. " - + "If you think this was a mistake, please contact a moderator or admin of the guild. " - + "The ban reason is: " + reason)) + """ + Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'. + If you think this was a mistake, please contact a moderator or admin of the guild. + The ban reason is: %s + """.formatted(reason);)) .queue(); } From 90b88fe6f3d1ec476620db77634e1640741f9cac Mon Sep 17 00:00:00 2001 From: Yusuf Date: Thu, 21 Oct 2021 21:12:58 +0100 Subject: [PATCH 089/140] Update BanHelperMethods.java --- .../tjbot/commands/moderation/BanHelperMethods.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java index 5ebad3ec1d..e987915615 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java @@ -43,7 +43,7 @@ public static void openPrivateChannel(long userId, String reason, Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'. If you think this was a mistake, please contact a moderator or admin of the guild. The ban reason is: %s - """.formatted(reason);)) + """.formatted(reason))) .queue(); } From 198c720b08b19a73660532dc5837d01f21fc52ac Mon Sep 17 00:00:00 2001 From: Yusuf Date: Thu, 21 Oct 2021 21:13:18 +0100 Subject: [PATCH 090/140] Update BanHelperMethods.java --- .../tjbot/commands/moderation/BanHelperMethods.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java index e987915615..ce583dd62a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java @@ -40,7 +40,7 @@ public static void openPrivateChannel(long userId, String reason, .openPrivateChannelById(userId) .flatMap(channel -> channel.sendMessage( """ - Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'. + Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'.\040 If you think this was a mistake, please contact a moderator or admin of the guild. The ban reason is: %s """.formatted(reason))) From fa89b6426f0e86fe7755bf9db13605698ff480b3 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Thu, 21 Oct 2021 21:17:05 +0100 Subject: [PATCH 091/140] spotlessApply --- .../tjbot/commands/moderation/BanCommand.java | 8 +++----- .../tjbot/commands/moderation/BanHelperMethods.java | 9 +++++---- .../tjbot/commands/moderation/KickCommand.java | 4 ++-- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index e6e22293d1..f35a9da4a9 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -40,16 +40,14 @@ public BanCommand() { .addOption(OptionType.STRING, REASON_OPTION, "why the user should be banned", true) .addOptions(new OptionData(OptionType.INTEGER, DELETE_MESSAGE_HISTORY_DAYS_OPTION, "the amount of days of the message history to delete, otherwise no messages are deleted.", - false) - .addChoice("0", 0) + false).addChoice("0", 0) .addChoice("1", 1) .addChoice("2", 2) .addChoice("3", 3) .addChoice("4", 4) .addChoice("5", 5) .addChoice("6", 6) - .addChoice("7", 7) - ); + .addChoice("7", 7)); } @Override @@ -93,7 +91,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { long userName = user.getIdLong(); long authorName = author.getIdLong(); if (option != null) { - //TODO Implement the same delete message structure for the purge message. + // TODO Implement the same delete message structure for the purge message. int days = Math.toIntExact( Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) .getAsLong()); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java index ce583dd62a..1f84d2f27d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java @@ -40,10 +40,11 @@ public static void openPrivateChannel(long userId, String reason, .openPrivateChannelById(userId) .flatMap(channel -> channel.sendMessage( """ - Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'.\040 - If you think this was a mistake, please contact a moderator or admin of the guild. - The ban reason is: %s - """.formatted(reason))) + Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'.\040 + If you think this was a mistake, please contact a moderator or admin of the guild. + The ban reason is: %s + """ + .formatted(reason))) .queue(); } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 69b0a232f9..71eb24116f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -94,8 +94,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { .queue(); String userId = user.getId(); - String authorId= author.getId(); - logger.info(" '{}' kicked the user '{}' due to reason being '{}'", authorId, userId , + String authorId = author.getId(); + logger.info(" '{}' kicked the user '{}' due to reason being '{}'", authorId, userId, reason); } } From 97a4aacc3f8a9ea0e23111778f3d3c9205a2c8c3 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Fri, 22 Oct 2021 07:16:33 +0100 Subject: [PATCH 092/140] tweaked the ban command. Removed unneeded string in kick command --- .../tjbot/commands/moderation/BanCommand.java | 21 ++++++++----------- .../commands/moderation/BanHelperMethods.java | 14 ++++++------- .../commands/moderation/KickCommand.java | 1 - 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index f35a9da4a9..9dd3d97578 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -25,10 +25,11 @@ * */ public final class BanCommand extends SlashCommandAdapter { - private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); private static final String USER_OPTION = "user"; private static final String DELETE_MESSAGE_HISTORY_DAYS_OPTION = "delete-message-history-days"; private static final String REASON_OPTION = "reason"; + private static final Integer DELETE_HISTORY_MIN_DAYS = 0; + private static final Integer DELETE_HISTORY_MAX_DAYS = 7; /** * Creates an instance of the ban command. @@ -88,23 +89,19 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { OptionMapping option = event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION); - long userName = user.getIdLong(); - long authorName = author.getIdLong(); + long authorId = author.getIdLong(); if (option != null) { // TODO Implement the same delete message structure for the purge message. int days = Math.toIntExact( Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) .getAsLong()); - BanHelperMethods.deleteMessageHistory(days, event); - BanHelperMethods.banGuild(user, reason, days, event); - - logger.info( - " '{}' banned the user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", - authorName, userName, days, reason); + BanHelperMethods.getDeleteMessageHistory(days, DELETE_HISTORY_MIN_DAYS, DELETE_HISTORY_MAX_DAYS, event); + BanHelperMethods.getBanGuild(user, reason, days, event); + BanHelperMethods.getLogger(authorId, userId, days, reason); } else { - BanHelperMethods.openPrivateChannel(userId, reason, event); - BanHelperMethods.banGuild(user, reason, 0, event); - BanHelperMethods.logger(authorName, userName, 0, reason); + BanHelperMethods.getOpenPrivateChannel(userId, reason, event); + BanHelperMethods.getBanGuild(user, reason, 0, event); + BanHelperMethods.getLogger(authorId, userId, 0, reason); } } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java index 1f84d2f27d..274cfac5b7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java @@ -10,21 +10,19 @@ public class BanHelperMethods { private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); - private static final Integer DELETE_HISTORY_MIN_DAYS = 1; - private static final Integer DELETE_HISTORY_MAX_DAYS = 7; - public static void deleteMessageHistory(int days, @NotNull SlashCommandEvent event) { - if (days < DELETE_HISTORY_MIN_DAYS || days > DELETE_HISTORY_MAX_DAYS) { + public static void getDeleteMessageHistory(int days, int minDays, int maxDays, @NotNull SlashCommandEvent event) { + if (days < minDays || days > maxDays) { event .reply("The amount of days of the message history to delete must be between " - + DELETE_HISTORY_MIN_DAYS + " and " + DELETE_HISTORY_MAX_DAYS + + minDays + " and " + maxDays + " , but was " + days + ".") .setEphemeral(true) .queue(); } } - public static void banGuild(Member user, String reason, int days, + public static void getBanGuild(Member user, String reason, int days, @NotNull SlashCommandEvent event) { Member author = Objects.requireNonNull(event.getMember()); event.getGuild() @@ -34,7 +32,7 @@ public static void banGuild(Member user, String reason, int days, .queue(); } - public static void openPrivateChannel(long userId, String reason, + public static void getOpenPrivateChannel(long userId, String reason, @NotNull SlashCommandEvent event) { event.getJDA() .openPrivateChannelById(userId) @@ -48,7 +46,7 @@ public static void openPrivateChannel(long userId, String reason, .queue(); } - public static void logger(long authorNameId, long userNameId, int days, String reason) { + public static void getLogger(long authorNameId, long userNameId, int days, String reason) { logger.info( " '{}' banned the user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", authorNameId, userNameId, days, reason); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 71eb24116f..4cf01f8ddb 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -93,7 +93,6 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { + author.getUser().getAsTag() + " for: " + reason)) .queue(); - String userId = user.getId(); String authorId = author.getId(); logger.info(" '{}' kicked the user '{}' due to reason being '{}'", authorId, userId, reason); From 7e165f9d7aee951f307d2fda6350fcf363591282 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Fri, 22 Oct 2021 07:17:16 +0100 Subject: [PATCH 093/140] spotlessJavaApply --- .../togetherjava/tjbot/commands/moderation/BanCommand.java | 3 ++- .../tjbot/commands/moderation/BanHelperMethods.java | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 9dd3d97578..1a287a2b1c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -95,7 +95,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { int days = Math.toIntExact( Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) .getAsLong()); - BanHelperMethods.getDeleteMessageHistory(days, DELETE_HISTORY_MIN_DAYS, DELETE_HISTORY_MAX_DAYS, event); + BanHelperMethods.getDeleteMessageHistory(days, DELETE_HISTORY_MIN_DAYS, + DELETE_HISTORY_MAX_DAYS, event); BanHelperMethods.getBanGuild(user, reason, days, event); BanHelperMethods.getLogger(authorId, userId, days, reason); } else { diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java index 274cfac5b7..8e8805f03b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java @@ -11,12 +11,12 @@ public class BanHelperMethods { private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); - public static void getDeleteMessageHistory(int days, int minDays, int maxDays, @NotNull SlashCommandEvent event) { + public static void getDeleteMessageHistory(int days, int minDays, int maxDays, + @NotNull SlashCommandEvent event) { if (days < minDays || days > maxDays) { event .reply("The amount of days of the message history to delete must be between " - + minDays + " and " + maxDays - + " , but was " + days + ".") + + minDays + " and " + maxDays + " , but was " + days + ".") .setEphemeral(true) .queue(); } From 35b9939c7b85c8bc46ded172ad345354801a85ad Mon Sep 17 00:00:00 2001 From: Yusuf Date: Fri, 22 Oct 2021 07:21:52 +0100 Subject: [PATCH 094/140] Update BanCommand.java --- .../org/togetherjava/tjbot/commands/moderation/BanCommand.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 1a287a2b1c..c1c955ecf8 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -7,8 +7,6 @@ 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; From 5d42005232cf372d616d44593931a40df266fdfb Mon Sep 17 00:00:00 2001 From: Yusuf Date: Fri, 22 Oct 2021 12:43:59 +0100 Subject: [PATCH 095/140] Fixed the command --- .../togetherjava/tjbot/commands/Commands.java | 4 ++-- .../commands/moderation/PurgeCommand.java | 22 +++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java index bce6d24cf4..483789c4f7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java @@ -42,7 +42,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 BanCommand(), new UnbanCommand(), - new KickCommand(), new PurgeCommand()); + new TagsCommand(tagSystem), new BanCommand(), new UnbanCommand(), new KickCommand(), + new PurgeCommand()); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java index f023eac22b..9a2897ef71 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java @@ -22,20 +22,23 @@ */ public final class PurgeCommand extends SlashCommandAdapter { private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); - private static final String NUMBER_OF_MESSAGES = "number_of_messages"; + private static final String NUMBER_OF_MESSAGES_TO_DELETE = "number_of_messages"; + private static final String USER_MESSAGES = "user"; public PurgeCommand() { super("prune", "Use this command to delete a batch of messages", SlashCommandVisibility.GUILD); - getData().addOption(OptionType.INTEGER, NUMBER_OF_MESSAGES, - "The number of messages you want to delete. 1 to 100", true); + getData() + .addOption(OptionType.INTEGER, NUMBER_OF_MESSAGES_TO_DELETE, + "The number of messages you want to delete. 1 to 100", true) + .addOption(OptionType.USER, USER_MESSAGES, + "If you want to delete messages for a specific user.", false); } @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { final Member author = event.getMember(); - final TextChannel channel = event.getTextChannel(); if (!Objects.requireNonNull(author).hasPermission(Permission.MESSAGE_MANAGE)) { @@ -54,18 +57,15 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - int amount = Math - .toIntExact(Objects.requireNonNull(event.getOption(NUMBER_OF_MESSAGES)).getAsLong()); - + int amount = Math.toIntExact( + Objects.requireNonNull(event.getOption(NUMBER_OF_MESSAGES_TO_DELETE)).getAsLong()); var messageHistory = channel.getHistory().retrievePast(amount).complete(); - if (amount < 100 || amount > 1) { + if (amount > 100 || amount < 1) { event.reply("You can only delete 1 to 100 messages").setEphemeral(true).queue(); } else { channel.purgeMessages(messageHistory); + logger.info(" '{}' deleted this amount of messages '{}'", author, amount); } - // Add this to audit log - logger.info("Bot '{}' was made to delete this amount of messages '{}' by '{}'", bot, amount, - author); } } From af1fe466843c18f706386b822beef32085045d7d Mon Sep 17 00:00:00 2001 From: Yusuf Date: Fri, 22 Oct 2021 13:16:58 +0100 Subject: [PATCH 096/140] I have fixed what you told me to fix --- .../tjbot/commands/moderation/BanCommand.java | 61 +++++++++++-------- .../commands/moderation/BanHelperMethods.java | 54 ---------------- 2 files changed, 37 insertions(+), 78 deletions(-) delete mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index c1c955ecf8..585aa5f109 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -3,10 +3,11 @@ import net.dv8tion.jda.api.Permission; 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 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; @@ -26,8 +27,7 @@ public final class BanCommand extends SlashCommandAdapter { private static final String USER_OPTION = "user"; private static final String DELETE_MESSAGE_HISTORY_DAYS_OPTION = "delete-message-history-days"; private static final String REASON_OPTION = "reason"; - private static final Integer DELETE_HISTORY_MIN_DAYS = 0; - private static final Integer DELETE_HISTORY_MAX_DAYS = 7; + private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); /** * Creates an instance of the ban command. @@ -39,7 +39,7 @@ public BanCommand() { .addOption(OptionType.STRING, REASON_OPTION, "why the user should be banned", true) .addOptions(new OptionData(OptionType.INTEGER, DELETE_MESSAGE_HISTORY_DAYS_OPTION, "the amount of days of the message history to delete, otherwise no messages are deleted.", - false).addChoice("0", 0) + true).addChoice("none", 0) .addChoice("1", 1) .addChoice("2", 2) .addChoice("3", 3) @@ -78,29 +78,42 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { } if (!bot.canInteract(Objects.requireNonNull(user))) { - event.reply( - "This user is too powerful for me to ban because he has more permissions than me.") - .setEphemeral(true) - .queue(); + event.reply("This user is too powerful for me to ban.").setEphemeral(true).queue(); return; } - OptionMapping option = event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION); - long authorId = author.getIdLong(); - if (option != null) { - // TODO Implement the same delete message structure for the purge message. - int days = Math.toIntExact( - Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) - .getAsLong()); - BanHelperMethods.getDeleteMessageHistory(days, DELETE_HISTORY_MIN_DAYS, - DELETE_HISTORY_MAX_DAYS, event); - BanHelperMethods.getBanGuild(user, reason, days, event); - BanHelperMethods.getLogger(authorId, userId, days, reason); - } else { - BanHelperMethods.getOpenPrivateChannel(userId, reason, event); - BanHelperMethods.getBanGuild(user, reason, 0, event); - BanHelperMethods.getLogger(authorId, userId, 0, reason); - } + // TODO Implement the same delete message structure for the purge message. + int days = Math + .toIntExact(Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) + .getAsLong()); + + banUser(userId, authorId, user, reason, days, event); + } + + private static void banUser(long userId, long authorNameId, Member user, String reason, + int days, @NotNull SlashCommandEvent event) { + event.getJDA() + .openPrivateChannelById(userId) + .flatMap(channel -> channel.sendMessage( + """ + Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'.\040 + If you think this was a mistake, please contact a moderator or admin of the guild. + The ban reason is: %s + """ + .formatted(reason))) + .queue(); + + Member author = Objects.requireNonNull(event.getMember()); + + event.getGuild() + .ban(user, days, reason) + .flatMap(v -> event.reply(user.getUser().getAsTag() + " was banned by " + + author.getUser().getAsTag() + " for: " + reason)) + .queue(); + + logger.info( + " '{}' banned the user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", + authorNameId, userId, days, reason); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java deleted file mode 100644 index 8e8805f03b..0000000000 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanHelperMethods.java +++ /dev/null @@ -1,54 +0,0 @@ -package org.togetherjava.tjbot.commands.moderation; - -import net.dv8tion.jda.api.entities.Member; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import org.jetbrains.annotations.NotNull; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Objects; - -public class BanHelperMethods { - private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); - - public static void getDeleteMessageHistory(int days, int minDays, int maxDays, - @NotNull SlashCommandEvent event) { - if (days < minDays || days > maxDays) { - event - .reply("The amount of days of the message history to delete must be between " - + minDays + " and " + maxDays + " , but was " + days + ".") - .setEphemeral(true) - .queue(); - } - } - - public static void getBanGuild(Member user, String reason, int days, - @NotNull SlashCommandEvent event) { - Member author = Objects.requireNonNull(event.getMember()); - event.getGuild() - .ban(user, days, reason) - .flatMap(v -> event.reply(user.getUser().getAsTag() + " was banned by " - + author.getUser().getAsTag() + " for: " + reason)) - .queue(); - } - - public static void getOpenPrivateChannel(long userId, String reason, - @NotNull SlashCommandEvent event) { - event.getJDA() - .openPrivateChannelById(userId) - .flatMap(channel -> channel.sendMessage( - """ - Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'.\040 - If you think this was a mistake, please contact a moderator or admin of the guild. - The ban reason is: %s - """ - .formatted(reason))) - .queue(); - } - - public static void getLogger(long authorNameId, long userNameId, int days, String reason) { - logger.info( - " '{}' banned the user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", - authorNameId, userNameId, days, reason); - } -} From 563998a16c273f73d3734f60862ec51f3c33ae0a Mon Sep 17 00:00:00 2001 From: Yusuf Date: Fri, 22 Oct 2021 13:21:22 +0100 Subject: [PATCH 097/140] Update BanCommand.java --- .../org/togetherjava/tjbot/commands/moderation/BanCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 585aa5f109..bbc0be1ddf 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -38,7 +38,7 @@ public BanCommand() { 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_MESSAGE_HISTORY_DAYS_OPTION, - "the amount of days of the message history to delete, otherwise no messages are deleted.", + "the amount of days of the message history to delete, none means no messages are deleted.", true).addChoice("none", 0) .addChoice("1", 1) .addChoice("2", 2) From c6dd085fc5b3e4ae1010409744e3e62f511576d9 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Fri, 22 Oct 2021 16:14:38 +0100 Subject: [PATCH 098/140] Update BanCommand.java --- .../tjbot/commands/moderation/BanCommand.java | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index bbc0be1ddf..833724ea93 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -13,7 +13,6 @@ 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 him about @@ -28,7 +27,6 @@ public final class BanCommand extends SlashCommandAdapter { private static final String DELETE_MESSAGE_HISTORY_DAYS_OPTION = "delete-message-history-days"; private static final String REASON_OPTION = "reason"; private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); - /** * Creates an instance of the ban command. */ @@ -37,16 +35,13 @@ public BanCommand() { 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_MESSAGE_HISTORY_DAYS_OPTION, - "the amount of days of the message history to delete, none means no messages are deleted.", - true).addChoice("none", 0) - .addChoice("1", 1) - .addChoice("2", 2) - .addChoice("3", 3) - .addChoice("4", 4) - .addChoice("5", 5) - .addChoice("6", 6) - .addChoice("7", 7)); + .addOptions(new OptionData(OptionType.INTEGER, DELETE_MESSAGE_HISTORY_DAYS_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 @@ -82,12 +77,13 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - long authorId = author.getIdLong(); + + // TODO Implement the same delete message structure for the purge message. int days = Math - .toIntExact(Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) - .getAsLong()); - + .toIntExact(Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) + .getAsLong()); + long authorId = author.getIdLong(); banUser(userId, authorId, user, reason, days, event); } From 57e89f946ea3ac72785feb1d488a928470e52266 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Fri, 22 Oct 2021 16:19:11 +0100 Subject: [PATCH 099/140] Update BanCommand.java --- .../togetherjava/tjbot/commands/moderation/BanCommand.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 833724ea93..27f3b00d20 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -84,11 +84,11 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { .toIntExact(Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) .getAsLong()); long authorId = author.getIdLong(); - banUser(userId, authorId, user, reason, days, event); + banUser(user, reason, days, userId, authorId, event); } - private static void banUser(long userId, long authorNameId, Member user, String reason, - int days, @NotNull SlashCommandEvent event) { + private static void banUser(Member user, String reason, + int days, long userId, long authorNameId, @NotNull SlashCommandEvent event) { event.getJDA() .openPrivateChannelById(userId) .flatMap(channel -> channel.sendMessage( From 6a4f39a2f805cd986aa519718d7359d5d564f094 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Fri, 22 Oct 2021 16:25:53 +0100 Subject: [PATCH 100/140] Update BanCommand.java --- .../tjbot/commands/moderation/BanCommand.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 27f3b00d20..0da8882c08 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -26,6 +26,7 @@ public final class BanCommand extends SlashCommandAdapter { private static final String USER_OPTION = "user"; private static final String DELETE_MESSAGE_HISTORY_DAYS_OPTION = "delete-message-history-days"; private static final String REASON_OPTION = "reason"; + private static final Integer REASON_MAX_LENGTH = 512; private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); /** * Creates an instance of the ban command. @@ -89,6 +90,13 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { private static void banUser(Member user, String reason, int days, long userId, long authorNameId, @NotNull SlashCommandEvent event) { + + if(reason.length() > BanCommand.REASON_MAX_LENGTH) { + event.reply("The reason can not be over 512 characters") + .setEphemeral(true) + .queue(); + } + event.getJDA() .openPrivateChannelById(userId) .flatMap(channel -> channel.sendMessage( From 21059894323c748d52216d2f7a646f3928e3f242 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Fri, 22 Oct 2021 16:26:17 +0100 Subject: [PATCH 101/140] Update BanCommand.java --- .../tjbot/commands/moderation/BanCommand.java | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 0da8882c08..a9fa279d70 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -28,6 +28,7 @@ public final class BanCommand extends SlashCommandAdapter { private static final String REASON_OPTION = "reason"; private static final Integer REASON_MAX_LENGTH = 512; private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); + /** * Creates an instance of the ban command. */ @@ -36,13 +37,9 @@ public BanCommand() { 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_MESSAGE_HISTORY_DAYS_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) - ); + .addOptions(new OptionData(OptionType.INTEGER, DELETE_MESSAGE_HISTORY_DAYS_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 @@ -82,19 +79,17 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { // TODO Implement the same delete message structure for the purge message. int days = Math - .toIntExact(Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) - .getAsLong()); + .toIntExact(Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) + .getAsLong()); long authorId = author.getIdLong(); banUser(user, reason, days, userId, authorId, event); } - private static void banUser(Member user, String reason, - int days, long userId, long authorNameId, @NotNull SlashCommandEvent event) { + private static void banUser(Member user, String reason, int days, long userId, + long authorNameId, @NotNull SlashCommandEvent event) { - if(reason.length() > BanCommand.REASON_MAX_LENGTH) { - event.reply("The reason can not be over 512 characters") - .setEphemeral(true) - .queue(); + if (reason.length() > BanCommand.REASON_MAX_LENGTH) { + event.reply("The reason can not be over 512 characters").setEphemeral(true).queue(); } event.getJDA() From 1532d75b3a6c0b87cc20a6f8c27bd6d2bff42138 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Fri, 22 Oct 2021 16:40:50 +0100 Subject: [PATCH 102/140] Update BanCommand.java --- .../org/togetherjava/tjbot/commands/moderation/BanCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index a9fa279d70..c4ec3ebc55 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -85,7 +85,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { banUser(user, reason, days, userId, authorId, event); } - private static void banUser(Member user, String reason, int days, long userId, + private static void banUser(@NotNull Member user, @NotNull String reason, int days, long userId, long authorNameId, @NotNull SlashCommandEvent event) { if (reason.length() > BanCommand.REASON_MAX_LENGTH) { From bc90bd41dd88cb6f062da85de5b03d721183e151 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Fri, 22 Oct 2021 16:41:16 +0100 Subject: [PATCH 103/140] Update BanCommand.java --- .../tjbot/commands/moderation/BanCommand.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index c4ec3ebc55..07b421ea87 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -81,6 +81,11 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { int days = Math .toIntExact(Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) .getAsLong()); + + if (reason.length() > BanCommand.REASON_MAX_LENGTH) { + event.reply("The reason can not be over 512 characters").setEphemeral(true).queue(); + } + long authorId = author.getIdLong(); banUser(user, reason, days, userId, authorId, event); } @@ -88,10 +93,6 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { private static void banUser(@NotNull Member user, @NotNull String reason, int days, long userId, long authorNameId, @NotNull SlashCommandEvent event) { - if (reason.length() > BanCommand.REASON_MAX_LENGTH) { - event.reply("The reason can not be over 512 characters").setEphemeral(true).queue(); - } - event.getJDA() .openPrivateChannelById(userId) .flatMap(channel -> channel.sendMessage( From f7aac63caea0db659a75147c2ef17214bddb2be6 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Fri, 22 Oct 2021 19:46:23 +0100 Subject: [PATCH 104/140] Fixed everything --- .gitignore | 1 - .../tjbot/commands/moderation/BanCommand.java | 29 ++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index f24a747936..0ce9cdb2ce 100644 --- a/.gitignore +++ b/.gitignore @@ -158,4 +158,3 @@ logviewer/types.d.ts logviewer/pnpm-lock.yaml logviewer/webpack.config.js logviewer/webpack.generated.js -.DS_Store diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 07b421ea87..3931621660 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -1,9 +1,12 @@ 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.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.commands.build.OptionData; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; @@ -24,8 +27,11 @@ */ public final class BanCommand extends SlashCommandAdapter { private static final String USER_OPTION = "user"; - private static final String DELETE_MESSAGE_HISTORY_DAYS_OPTION = "delete-message-history-days"; + private static final String DELETE_HISTORY_OPTION = "delete-history"; private static final String REASON_OPTION = "reason"; + /** + * As stated in {@link Guild#ban(User, int, String)} The reason can be only 512 characters. + */ private static final Integer REASON_MAX_LENGTH = 512; private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); @@ -37,7 +43,7 @@ public BanCommand() { 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_MESSAGE_HISTORY_DAYS_OPTION, + .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)); } @@ -48,8 +54,6 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { Member author = Objects.requireNonNull(event.getMember()); String reason = Objects.requireNonNull(event.getOption(REASON_OPTION)).getAsString(); - long userId = user.getUser().getIdLong(); - if (!author.hasPermission(Permission.BAN_MEMBERS)) { event.reply("You do not have the BAN_MEMBERS permission to ban users from this server.") .setEphemeral(true) @@ -70,24 +74,22 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } + logger.error("The Bot does not have BAN_MEMBERS permissions so it cant ban users"); + if (!bot.canInteract(Objects.requireNonNull(user))) { event.reply("This user is too powerful for me to ban.").setEphemeral(true).queue(); return; } - - // TODO Implement the same delete message structure for the purge message. int days = Math - .toIntExact(Objects.requireNonNull(event.getOption(DELETE_MESSAGE_HISTORY_DAYS_OPTION)) - .getAsLong()); + .toIntExact(Objects.requireNonNull(event.getOption(DELETE_HISTORY_OPTION)).getAsLong()); if (reason.length() > BanCommand.REASON_MAX_LENGTH) { event.reply("The reason can not be over 512 characters").setEphemeral(true).queue(); } - long authorId = author.getIdLong(); - banUser(user, reason, days, userId, authorId, event); + banUser(user, reason, days, user.getUser().getIdLong(), author.getIdLong(), event); } private static void banUser(@NotNull Member user, @NotNull String reason, int days, long userId, @@ -102,14 +104,15 @@ private static void banUser(@NotNull Member user, @NotNull String reason, int da The ban reason is: %s """ .formatted(reason))) - .queue(); + .queue(null, throwable -> { + }); - Member author = Objects.requireNonNull(event.getMember()); + logger.error("I could not send a personal message to '{}' ", userId); event.getGuild() .ban(user, days, reason) .flatMap(v -> event.reply(user.getUser().getAsTag() + " was banned by " - + author.getUser().getAsTag() + " for: " + reason)) + + user.getUser().getAsTag() + " for: " + reason)) .queue(); logger.info( From 08be6987ad19370a256d3ddd879788bb8ecc627f Mon Sep 17 00:00:00 2001 From: Yusuf Date: Fri, 22 Oct 2021 19:58:23 +0100 Subject: [PATCH 105/140] Fixed the loggers --- .../tjbot/commands/moderation/BanCommand.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 3931621660..5aed087e22 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -71,17 +71,17 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { event.reply("I don't have the BAN_MEMBERS permission to ban users from this server.") .setEphemeral(true) .queue(); + + logger.error("The Bot does not have BAN_MEMBERS permissions so it cant ban users"); + return; } - logger.error("The Bot does not have BAN_MEMBERS permissions so it cant ban users"); - if (!bot.canInteract(Objects.requireNonNull(user))) { event.reply("This user is too powerful for me to ban.").setEphemeral(true).queue(); return; } - int days = Math .toIntExact(Objects.requireNonNull(event.getOption(DELETE_HISTORY_OPTION)).getAsLong()); @@ -105,10 +105,9 @@ private static void banUser(@NotNull Member user, @NotNull String reason, int da """ .formatted(reason))) .queue(null, throwable -> { + logger.error("I could not send a personal message to '{}' ", userId); }); - logger.error("I could not send a personal message to '{}' ", userId); - event.getGuild() .ban(user, days, reason) .flatMap(v -> event.reply(user.getUser().getAsTag() + " was banned by " From cdc838931618e74dec43f96578832ad2afc9f790 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Fri, 22 Oct 2021 19:59:06 +0100 Subject: [PATCH 106/140] Update .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 0ce9cdb2ce..563dbf8375 100644 --- a/.gitignore +++ b/.gitignore @@ -158,3 +158,6 @@ logviewer/types.d.ts logviewer/pnpm-lock.yaml logviewer/webpack.config.js logviewer/webpack.generated.js + +### Mac Patch ### +.DS_Store From aa55b9c4a2c8e34ec343c1a0879a31332a50f3a9 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Fri, 22 Oct 2021 20:10:08 +0100 Subject: [PATCH 107/140] Update PurgeCommand.java --- .../togetherjava/tjbot/commands/moderation/PurgeCommand.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java index 9a2897ef71..86f3deb1d5 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java @@ -26,9 +26,9 @@ public final class PurgeCommand extends SlashCommandAdapter { private static final String USER_MESSAGES = "user"; public PurgeCommand() { - super("prune", "Use this command to delete a batch of messages", + super("purge", "Use this command to delete a batch of messages", SlashCommandVisibility.GUILD); - + getData() .addOption(OptionType.INTEGER, NUMBER_OF_MESSAGES_TO_DELETE, "The number of messages you want to delete. 1 to 100", true) @@ -65,6 +65,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { event.reply("You can only delete 1 to 100 messages").setEphemeral(true).queue(); } else { channel.purgeMessages(messageHistory); + event.reply("I have deleted this amount of messages " + messageHistory); logger.info(" '{}' deleted this amount of messages '{}'", author, amount); } } From 0832398446ce24ad125186276bff0c03213e127b Mon Sep 17 00:00:00 2001 From: Yusuf Date: Fri, 22 Oct 2021 20:17:19 +0100 Subject: [PATCH 108/140] Update PurgeCommand.java --- .../tjbot/commands/moderation/PurgeCommand.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java index 86f3deb1d5..63a001bfe1 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java @@ -28,7 +28,7 @@ public final class PurgeCommand extends SlashCommandAdapter { public PurgeCommand() { super("purge", "Use this command to delete a batch of messages", SlashCommandVisibility.GUILD); - + getData() .addOption(OptionType.INTEGER, NUMBER_OF_MESSAGES_TO_DELETE, "The number of messages you want to delete. 1 to 100", true) @@ -61,8 +61,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { Objects.requireNonNull(event.getOption(NUMBER_OF_MESSAGES_TO_DELETE)).getAsLong()); var messageHistory = channel.getHistory().retrievePast(amount).complete(); - if (amount > 100 || amount < 1) { - event.reply("You can only delete 1 to 100 messages").setEphemeral(true).queue(); + if (amount > 200 || amount < 1) { + event.reply("You can only delete 1 to 200 messages").setEphemeral(true).queue(); } else { channel.purgeMessages(messageHistory); event.reply("I have deleted this amount of messages " + messageHistory); From 1f185906d6cb1532bb33c4ee2e4aa0a52e0ee119 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Fri, 22 Oct 2021 20:37:20 +0100 Subject: [PATCH 109/140] Update PurgeCommand.java --- .../togetherjava/tjbot/commands/moderation/PurgeCommand.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java index 63a001bfe1..8a02d6f55b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java @@ -59,11 +59,10 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { int amount = Math.toIntExact( Objects.requireNonNull(event.getOption(NUMBER_OF_MESSAGES_TO_DELETE)).getAsLong()); - var messageHistory = channel.getHistory().retrievePast(amount).complete(); - if (amount > 200 || amount < 1) { event.reply("You can only delete 1 to 200 messages").setEphemeral(true).queue(); } else { + var messageHistory = channel.getHistory().retrievePast(amount).complete(); channel.purgeMessages(messageHistory); event.reply("I have deleted this amount of messages " + messageHistory); logger.info(" '{}' deleted this amount of messages '{}'", author, amount); From 12f6a40e1632e8873e4765a419d2dbfe41578a80 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Fri, 22 Oct 2021 20:40:58 +0100 Subject: [PATCH 110/140] fixed kick and unban command --- .../togetherjava/tjbot/commands/moderation/KickCommand.java | 6 +++--- .../tjbot/commands/moderation/UnbanCommand.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 4cf01f8ddb..672fcf660e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -93,8 +93,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { + author.getUser().getAsTag() + " for: " + reason)) .queue(); - String authorId = author.getId(); - logger.info(" '{}' kicked the user '{}' due to reason being '{}'", authorId, userId, - reason); + logger.info(" '{} ({})' kicked the user '{} ({})' due to reason being '{}'", + author.getUser().getAsTag(), author.getIdLong(), author.getUser().getAsTag(), + userId, reason); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java index a39f1478de..d25ef1973b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java @@ -61,9 +61,9 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } + logger.error("The user '{}' does not exist", userId); event.getGuild().unban(userId).flatMap(v -> event.reply("Unbanned the user")).queue(); - String authorId = author.getId(); - logger.info(" '{}' unbanned user id '{}' ", authorId, userId); + logger.info(" '{}' unbanned user id '{}' ", author.getIdLong(), userId); } } From 75a49161fb1cecac5114c4969cadfd9b20aff3b6 Mon Sep 17 00:00:00 2001 From: Yusuf Arfan Ismail Date: Sat, 23 Oct 2021 10:55:19 +0100 Subject: [PATCH 111/140] Update application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java Co-authored-by: Tais993 <49957334+Tais993@users.noreply.github.com> --- .../togetherjava/tjbot/commands/moderation/PurgeCommand.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java index 8a02d6f55b..8465fed44b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java @@ -62,8 +62,9 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { if (amount > 200 || amount < 1) { event.reply("You can only delete 1 to 200 messages").setEphemeral(true).queue(); } else { - var messageHistory = channel.getHistory().retrievePast(amount).complete(); - channel.purgeMessages(messageHistory); + channel.getHistory().retrievePast(amount).queue(messageHistory -> { + channel.purgeMessages(messageHistory); + }); event.reply("I have deleted this amount of messages " + messageHistory); logger.info(" '{}' deleted this amount of messages '{}'", author, amount); } From 3a1c0457b65da2a2fa300dca3d478c59b994de45 Mon Sep 17 00:00:00 2001 From: Yusuf Arfan Ismail Date: Sat, 23 Oct 2021 10:55:27 +0100 Subject: [PATCH 112/140] Update application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java Co-authored-by: Tais993 <49957334+Tais993@users.noreply.github.com> --- .../togetherjava/tjbot/commands/moderation/KickCommand.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 7154d501d4..d93e3e9d67 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -43,11 +43,8 @@ public KickCommand() { @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { Member user = Objects.requireNonNull(event.getOption(USER_OPTION)).getAsMember(); - Member author = Objects.requireNonNull(event.getMember()); - String reason = Objects.requireNonNull(event.getOption(REASON_OPTION)).getAsString(); - long userId = Objects.requireNonNull(user).getUser().getIdLong(); if (!author.hasPermission(Permission.KICK_MEMBERS)) { From 3a9f44d42bbbb9e6638935068f5d4d7edddfbb69 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 11:21:01 +0100 Subject: [PATCH 113/140] Update PurgeCommand.java --- .../tjbot/commands/moderation/PurgeCommand.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java index 8465fed44b..208fccedde 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java @@ -62,10 +62,9 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { if (amount > 200 || amount < 1) { event.reply("You can only delete 1 to 200 messages").setEphemeral(true).queue(); } else { - channel.getHistory().retrievePast(amount).queue(messageHistory -> { - channel.purgeMessages(messageHistory); - }); - event.reply("I have deleted this amount of messages " + messageHistory); + channel.getHistory().retrievePast(amount).queue(channel::purgeMessages); + event.reply("I have deleted this amount of messages ") + .queue(); logger.info(" '{}' deleted this amount of messages '{}'", author, amount); } } From 22d767426c5562775464afe4b2cb4d1ee31070e1 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 11:22:51 +0100 Subject: [PATCH 114/140] Update PurgeCommand.java --- .../org/togetherjava/tjbot/commands/moderation/PurgeCommand.java | 1 + 1 file changed, 1 insertion(+) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java index 208fccedde..f9a121d1cf 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java @@ -64,6 +64,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { } else { channel.getHistory().retrievePast(amount).queue(channel::purgeMessages); event.reply("I have deleted this amount of messages ") + .setEphemeral(true) .queue(); logger.info(" '{}' deleted this amount of messages '{}'", author, amount); } From e6d523994213ccf5c09b78678f40086583f0bd2c Mon Sep 17 00:00:00 2001 From: Yusuf Arfan Ismail Date: Sat, 23 Oct 2021 11:29:39 +0100 Subject: [PATCH 115/140] Update application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java Co-authored-by: Tais993 <49957334+Tais993@users.noreply.github.com> --- .../togetherjava/tjbot/commands/moderation/KickCommand.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index d93e3e9d67..2fbe5d89eb 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -79,7 +79,11 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { event.getJDA() .openPrivateChannelById(userId) .flatMap(channel -> channel.sendMessage( - "Hey there, sorry to tell you but unfortunately you have been kicked from the guild 'Together Java'. If you think this was a mistake, please contact a moderator or admin of the guild. The kick reason is: " + """ + Hey there, sorry to tell you but unfortunately you have been kicked from the guild 'Together Java'. + If you think this was a mistake, please contact a moderator or admin of the guild. + The kick reason is: + """ + reason)) .queue(); From 4d3f4c10223f3478059e6d81cc8a55970bdecc68 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 11:30:10 +0100 Subject: [PATCH 116/140] Update KickCommand.java --- .../commands/moderation/KickCommand.java | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 672fcf660e..bec942dbaf 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -9,7 +9,6 @@ import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.SlashCommandVisibility; - import java.util.Objects; @@ -30,7 +29,7 @@ public KickCommand() { super("kick", "Kicks a given user", SlashCommandVisibility.GUILD); getData().addOption(OptionType.USER, USER_OPTION, "The user which you want to kick", true) - .addOption(OptionType.STRING, REASON_OPTION, "why the user should be kicked", true); + .addOption(OptionType.STRING, REASON_OPTION, "why the user should be kicked", true); } /** @@ -44,18 +43,15 @@ public KickCommand() { @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { Member user = Objects.requireNonNull(event.getOption(USER_OPTION)).getAsMember(); - Member author = Objects.requireNonNull(event.getMember()); - String reason = Objects.requireNonNull(event.getOption(REASON_OPTION)).getAsString(); - long userId = Objects.requireNonNull(user).getUser().getIdLong(); if (!author.hasPermission(Permission.KICK_MEMBERS)) { event.reply( - "You do not have the KICK_MEMBERS permission to kick users from this server.") - .setEphemeral(true) - .queue(); + "You do not have the KICK_MEMBERS permission to kick users from this server.") + .setEphemeral(true) + .queue(); return; } @@ -67,31 +63,35 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!bot.hasPermission(Permission.KICK_MEMBERS)) { event.reply("I don't have the KICK_MEMBERS permission to kick users from this server.") - .setEphemeral(true) - .queue(); + .setEphemeral(true) + .queue(); return; } if (!bot.canInteract(Objects.requireNonNull(user))) { event.reply( - "This user is too powerful for me to kick because he has more permissions than me.") - .setEphemeral(true) - .queue(); + "This user is too powerful for me to kick because he has more permissions than me.") + .setEphemeral(true) + .queue(); return; } event.getJDA() - .openPrivateChannelById(userId) - .flatMap(channel -> channel.sendMessage( - "Hey there, sorry to tell you but unfortunately you have been kicked from the guild 'Together Java'. If you think this was a mistake, please contact a moderator or admin of the guild. The kick reason is: " - + reason)) - .queue(); + .openPrivateChannelById(userId) + .flatMap(channel -> channel.sendMessage( + """ + Hey there, sorry to tell you but unfortunately you have been kicked from the guild 'Together Java'. + If you think this was a mistake, please contact a moderator or admin of the guild. + The kick reason is: + """ + + reason)) + .queue(); event.getGuild() - .kick(user, reason) - .flatMap(v -> event.reply(user.getUser().getAsTag() + " was kicked by " - + author.getUser().getAsTag() + " for: " + reason)) - .queue(); + .kick(user, reason) + .flatMap(v -> event.reply(user.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(), author.getUser().getAsTag(), From 19e7b5708a6f80791c53b3b821dbd4c87e03e8d0 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 11:31:28 +0100 Subject: [PATCH 117/140] Update UnbanCommand.java --- .../togetherjava/tjbot/commands/moderation/UnbanCommand.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java index d25ef1973b..bf848b70c7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java @@ -41,7 +41,6 @@ public UnbanCommand() { @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { final String userId = Objects.requireNonNull(event.getOption(USER_ID)).getAsString(); - Member author = Objects.requireNonNull(event.getMember()); if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.BAN_MEMBERS)) { @@ -63,7 +62,6 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { logger.error("The user '{}' does not exist", userId); event.getGuild().unban(userId).flatMap(v -> event.reply("Unbanned the user")).queue(); - logger.info(" '{}' unbanned user id '{}' ", author.getIdLong(), userId); } } From 2d7d2df4dbb859b9b403eeab09549ca9a1482cec Mon Sep 17 00:00:00 2001 From: Yusuf Arfan Ismail Date: Sat, 23 Oct 2021 12:09:02 +0100 Subject: [PATCH 118/140] Update application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java Co-authored-by: Tais993 <49957334+Tais993@users.noreply.github.com> --- .../commands/moderation/UnbanCommand.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java index bf848b70c7..fd06f45136 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java @@ -60,8 +60,20 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - logger.error("The user '{}' does not exist", userId); - event.getGuild().unban(userId).flatMap(v -> event.reply("Unbanned the user")).queue(); - logger.info(" '{}' unbanned user id '{}' ", author.getIdLong(), userId); + + event.getGuild().unban(userId)..queue(v -> { + event.reply("Unbanned the user"); + logger.info(" '{}' unbanned user id '{}' ", author.getIdLong(), userId); + }, throwable -> { + if (throwable instanceof ErrorResponseException errorResponseException && + errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) { + + event.reply("the specified user doesn't exist").queue(); + logger.debug("The user '{}' does not exist", userId); + } else { + event.reply("Something went wrong, check the logs or contact a helper/moderator").queue(); + logger.error("Something went wrong in the unban command: {}" + throwable); + } + }); } } From b0bcec7af78ebb791e58b324a4af82530fbbc785 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 12:10:58 +0100 Subject: [PATCH 119/140] Update UnbanCommand.java --- .../tjbot/commands/moderation/UnbanCommand.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java index fd06f45136..701c1809c7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java @@ -3,7 +3,9 @@ import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.exceptions.ErrorResponseException; import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.requests.ErrorResponse; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -61,7 +63,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { } - event.getGuild().unban(userId)..queue(v -> { + event.getGuild().unban(userId).queue(v -> { event.reply("Unbanned the user"); logger.info(" '{}' unbanned user id '{}' ", author.getIdLong(), userId); }, throwable -> { @@ -72,7 +74,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { logger.debug("The user '{}' does not exist", userId); } else { event.reply("Something went wrong, check the logs or contact a helper/moderator").queue(); - logger.error("Something went wrong in the unban command: {}" + throwable); + logger.error("Something went wrong in the unban command: " + throwable); } }); } From 18e05880eb350410668f90a062eabbe6c022f416 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 12:11:14 +0100 Subject: [PATCH 120/140] Update UnbanCommand.java --- .../commands/moderation/UnbanCommand.java | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java index bf848b70c7..355f07a905 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java @@ -3,7 +3,9 @@ import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.exceptions.ErrorResponseException; import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.requests.ErrorResponse; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -45,23 +47,35 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.BAN_MEMBERS)) { event.reply( - "You do not have the BAN_MEMBERS permission to urban users from this server.") - .setEphemeral(true) - .queue(); + "You do not have the BAN_MEMBERS permission to urban users from this server.") + .setEphemeral(true) + .queue(); return; } Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!bot.hasPermission(Permission.BAN_MEMBERS)) { event.reply( - "I don't have the BAN_MEMBERS permission to unban the user from this server.") - .setEphemeral(true) - .queue(); + "I don't have the BAN_MEMBERS permission to unban the user from this server.") + .setEphemeral(true) + .queue(); return; } - logger.error("The user '{}' does not exist", userId); - event.getGuild().unban(userId).flatMap(v -> event.reply("Unbanned the user")).queue(); - logger.info(" '{}' unbanned user id '{}' ", author.getIdLong(), userId); + + event.getGuild().unban(userId).queue(v -> { + event.reply("Unbanned the user"); + logger.info(" '{}' unbanned user id '{}' ", author.getIdLong(), userId); + }, throwable -> { + if (throwable instanceof ErrorResponseException errorResponseException && + errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) { + + event.reply("the specified user doesn't exist").queue(); + logger.debug("The user '{}' does not exist", userId); + } else { + event.reply("Something went wrong, check the logs or contact a helper/moderator").queue(); + logger.error("Something went wrong in the unban command: " + throwable); + } + }); } } From 9e91a864d8b14e68147424a2a4f3a5f8543d852f Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 12:43:21 +0100 Subject: [PATCH 121/140] Update PurgeCommand.java --- .../togetherjava/tjbot/commands/moderation/PurgeCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java index f9a121d1cf..862ad4fe62 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java @@ -17,7 +17,7 @@ /** * When triggered with {@code /prune "number_of_messages"}, the bot will check if the user has * perms. Then it will check if itself has perms to delete messages. If it does the bot will delete - * that amount of messages. {@code Banned User!}. + * that amount of messages. {@code I have deleted this amount of message + amount}. * */ public final class PurgeCommand extends SlashCommandAdapter { @@ -63,7 +63,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { event.reply("You can only delete 1 to 200 messages").setEphemeral(true).queue(); } else { channel.getHistory().retrievePast(amount).queue(channel::purgeMessages); - event.reply("I have deleted this amount of messages ") + event.reply("I have deleted this amount of messages " + amount) .setEphemeral(true) .queue(); logger.info(" '{}' deleted this amount of messages '{}'", author, amount); From 39c8c127a2b9712cbeebef3cef49d8c6108bc9ee Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 15:32:40 +0100 Subject: [PATCH 122/140] Update BanCommand.java --- .../org/togetherjava/tjbot/commands/moderation/BanCommand.java | 1 - 1 file changed, 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 5aed087e22..7157f59af7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -73,7 +73,6 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { .queue(); logger.error("The Bot does not have BAN_MEMBERS permissions so it cant ban users"); - return; } From 7f2389fee018b587860b2e3bd77071eada79f86a Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 15:50:11 +0100 Subject: [PATCH 123/140] Update BanCommand.java --- .../togetherjava/tjbot/commands/moderation/BanCommand.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 7157f59af7..83fddd0765 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -5,9 +5,11 @@ 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.exceptions.ErrorResponseException; import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.commands.build.OptionData; +import net.dv8tion.jda.api.requests.ErrorResponse; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -72,10 +74,9 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { .setEphemeral(true) .queue(); - logger.error("The Bot does not have BAN_MEMBERS permissions so it cant ban users"); + logger.error("The bot does not have BAN_MEMBERS permissions"); return; } - if (!bot.canInteract(Objects.requireNonNull(user))) { event.reply("This user is too powerful for me to ban.").setEphemeral(true).queue(); return; From 431d4881fb078932e87efb5dfebc809a70a01cac Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 15:58:11 +0100 Subject: [PATCH 124/140] Update BanCommand.java --- .../org/togetherjava/tjbot/commands/moderation/BanCommand.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 83fddd0765..bc543da76b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -5,11 +5,8 @@ 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.exceptions.ErrorResponseException; import net.dv8tion.jda.api.interactions.commands.OptionType; -import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.commands.build.OptionData; -import net.dv8tion.jda.api.requests.ErrorResponse; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; From 47f9f289e7a3f0d69317ebc230e52c25c9ddec28 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 15:59:59 +0100 Subject: [PATCH 125/140] Update BanCommand.java --- .../org/togetherjava/tjbot/commands/moderation/BanCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index bc543da76b..86a2ee094f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -52,6 +52,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { Member user = Objects.requireNonNull(event.getOption(USER_OPTION)).getAsMember(); Member author = Objects.requireNonNull(event.getMember()); String reason = Objects.requireNonNull(event.getOption(REASON_OPTION)).getAsString(); + Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!author.hasPermission(Permission.BAN_MEMBERS)) { event.reply("You do not have the BAN_MEMBERS permission to ban users from this server.") @@ -65,7 +66,6 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!bot.hasPermission(Permission.BAN_MEMBERS)) { event.reply("I don't have the BAN_MEMBERS permission to ban users from this server.") .setEphemeral(true) From e3d2a5e0d628ec3ad4ac1dfd8554327b18ba531a Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 16:00:20 +0100 Subject: [PATCH 126/140] Update KickCommand.java --- .../org/togetherjava/tjbot/commands/moderation/KickCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index bec942dbaf..216b14ea6f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -45,6 +45,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { Member user = Objects.requireNonNull(event.getOption(USER_OPTION)).getAsMember(); Member author = Objects.requireNonNull(event.getMember()); String reason = Objects.requireNonNull(event.getOption(REASON_OPTION)).getAsString(); + Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); long userId = Objects.requireNonNull(user).getUser().getIdLong(); if (!author.hasPermission(Permission.KICK_MEMBERS)) { @@ -60,7 +61,6 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!bot.hasPermission(Permission.KICK_MEMBERS)) { event.reply("I don't have the KICK_MEMBERS permission to kick users from this server.") .setEphemeral(true) From 5cef333bdf0e5b75d4f37d13cfa6d856ac335c46 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 16:01:24 +0100 Subject: [PATCH 127/140] Update UnbanCommand.java --- .../tjbot/commands/moderation/UnbanCommand.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java index 355f07a905..d548e4c04c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java @@ -43,7 +43,6 @@ public UnbanCommand() { @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { final String userId = Objects.requireNonNull(event.getOption(USER_ID)).getAsString(); - Member author = Objects.requireNonNull(event.getMember()); if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.BAN_MEMBERS)) { event.reply( @@ -53,19 +52,17 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); - if (!bot.hasPermission(Permission.BAN_MEMBERS)) { + if (!(event.getGuild()).getSelfMember().hasPermission(Permission.BAN_MEMBERS)) { event.reply( "I don't have the BAN_MEMBERS permission to unban the user from this server.") .setEphemeral(true) .queue(); return; } - - + event.getGuild().unban(userId).queue(v -> { event.reply("Unbanned the user"); - logger.info(" '{}' unbanned user id '{}' ", author.getIdLong(), userId); + logger.info(" '{}' unbanned user id '{}' ", event.getMember().getIdLong(), userId); }, throwable -> { if (throwable instanceof ErrorResponseException errorResponseException && errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) { From 7bfb2885e0856510e5068da993095c18e6811195 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 16:26:25 +0100 Subject: [PATCH 128/140] fixed everything that i was told to fix --- .../tjbot/commands/moderation/BanCommand.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 86a2ee094f..f54e1a347e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -71,7 +71,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { .setEphemeral(true) .queue(); - logger.error("The bot does not have BAN_MEMBERS permissions"); + logger.error("The bot does not have BAN_MEMBERS permissions on the server '{}' ", + event.getGuild().getId()); return; } if (!bot.canInteract(Objects.requireNonNull(user))) { @@ -84,13 +85,14 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { if (reason.length() > BanCommand.REASON_MAX_LENGTH) { event.reply("The reason can not be over 512 characters").setEphemeral(true).queue(); + return; } banUser(user, reason, days, user.getUser().getIdLong(), author.getIdLong(), event); } private static void banUser(@NotNull Member user, @NotNull String reason, int days, long userId, - long authorNameId, @NotNull SlashCommandEvent event) { + long authorId, @NotNull SlashCommandEvent event) { event.getJDA() .openPrivateChannelById(userId) @@ -102,7 +104,7 @@ private static void banUser(@NotNull Member user, @NotNull String reason, int da """ .formatted(reason))) .queue(null, throwable -> { - logger.error("I could not send a personal message to '{}' ", userId); + logger.error("I could not dm the user '{}' to inform them they were banned.", userId); }); event.getGuild() @@ -113,6 +115,6 @@ private static void banUser(@NotNull Member user, @NotNull String reason, int da logger.info( " '{}' banned the user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", - authorNameId, userId, days, reason); + authorId, userId, days, reason); } } From 547536f7176e578b39787350d7899a7aaa05b17a Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 16:28:01 +0100 Subject: [PATCH 129/140] Update UnbanCommand.java --- .../togetherjava/tjbot/commands/moderation/UnbanCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java index d548e4c04c..f329847d0c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java @@ -52,7 +52,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } - if (!(event.getGuild()).getSelfMember().hasPermission(Permission.BAN_MEMBERS)) { + if (!(Objects.requireNonNull(event.getGuild())).getSelfMember().hasPermission(Permission.BAN_MEMBERS)) { event.reply( "I don't have the BAN_MEMBERS permission to unban the user from this server.") .setEphemeral(true) From 08c42dcb3ec9ac1143c820283879c28ad02c2934 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 16:28:18 +0100 Subject: [PATCH 130/140] Update UnbanCommand.java --- .../togetherjava/tjbot/commands/moderation/UnbanCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java index f329847d0c..cdebcc7994 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java @@ -1,7 +1,6 @@ package org.togetherjava.tjbot.commands.moderation; import net.dv8tion.jda.api.Permission; -import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import net.dv8tion.jda.api.exceptions.ErrorResponseException; import net.dv8tion.jda.api.interactions.commands.OptionType; @@ -61,7 +60,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { } event.getGuild().unban(userId).queue(v -> { - event.reply("Unbanned the user"); + event.reply("Unbanned the user") + .queue(); logger.info(" '{}' unbanned user id '{}' ", event.getMember().getIdLong(), userId); }, throwable -> { if (throwable instanceof ErrorResponseException errorResponseException && From b44f3af86d0c17582d97c48058ad9e75d91059bb Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 16:29:52 +0100 Subject: [PATCH 131/140] Tweaking the commands --- .../togetherjava/tjbot/commands/moderation/BanCommand.java | 2 +- .../togetherjava/tjbot/commands/moderation/KickCommand.java | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index f54e1a347e..3b113f2ab4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -62,7 +62,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { } if (!author.canInteract(Objects.requireNonNull(user))) { - event.reply("This user is too powerful for you to ban.").setEphemeral(true).queue(); + event.reply("The user" + user + " is too powerful for you to ban.").setEphemeral(true).queue(); return; } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 216b14ea6f..ea05286f33 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -57,7 +57,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { } if (!author.canInteract(Objects.requireNonNull(user))) { - event.reply("\"This user is too powerful for you to kick.").setEphemeral(true).queue(); + event.reply("The user" + user + "is too powerful for you to kick.").setEphemeral(true).queue(); return; } @@ -65,6 +65,9 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { event.reply("I don't have the KICK_MEMBERS permission to kick users from this server.") .setEphemeral(true) .queue(); + + logger.error("The bot does not have KICK_MEMBERS permissions on the server '{}' ", + event.getGuild().getId()); return; } From 6c4f3ed5190f89637dc976ab52e9c16cf603d6a3 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 16:32:57 +0100 Subject: [PATCH 132/140] improving the sentecences --- .../tjbot/commands/moderation/BanCommand.java | 4 ++-- .../tjbot/commands/moderation/KickCommand.java | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 3b113f2ab4..9cdc7aa6c9 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -55,7 +55,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!author.hasPermission(Permission.BAN_MEMBERS)) { - event.reply("You do not have the BAN_MEMBERS permission to ban users from this server.") + event.reply("You do not have the BAN_MEMBERS permission which means you can't unable to ban users in this server.") .setEphemeral(true) .queue(); return; @@ -67,7 +67,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { } if (!bot.hasPermission(Permission.BAN_MEMBERS)) { - event.reply("I don't have the BAN_MEMBERS permission to ban users from this server.") + event.reply("I don't have the BAN_MEMBERS permission which means I am unable to ban users in this server.") .setEphemeral(true) .queue(); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index ea05286f33..9e33ae6ced 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -50,7 +50,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { if (!author.hasPermission(Permission.KICK_MEMBERS)) { event.reply( - "You do not have the KICK_MEMBERS permission to kick users from this server.") + "You do not have the KICK_MEMBERS permission which means you can't unable to kick users in this server.") .setEphemeral(true) .queue(); return; @@ -62,7 +62,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { } if (!bot.hasPermission(Permission.KICK_MEMBERS)) { - event.reply("I don't have the KICK_MEMBERS permission to kick users from this server.") + event.reply("I don't have the KICK_MEMBERS permission which means I am unable to kick users in this server.") .setEphemeral(true) .queue(); @@ -83,9 +83,9 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { .openPrivateChannelById(userId) .flatMap(channel -> channel.sendMessage( """ - Hey there, sorry to tell you but unfortunately you have been kicked from the guild 'Together Java'. - If you think this was a mistake, please contact a moderator or admin of the guild. - The kick reason is: + Hey there, sorry to tell you but unfortunately you have been kicked from the guild 'Together Java'. + If you think this was a mistake, please contact a moderator or admin of the guild. + The kick reason is: """ + reason)) .queue(); From ec26837d9ab7da36983eeefa434a92eb525af316 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 16:50:16 +0100 Subject: [PATCH 133/140] added some loggers --- .../tjbot/commands/moderation/BanCommand.java | 5 ++++- .../tjbot/commands/moderation/KickCommand.java | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 9cdc7aa6c9..7d61dbfb23 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -76,7 +76,10 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } if (!bot.canInteract(Objects.requireNonNull(user))) { - event.reply("This user is too powerful for me to ban.").setEphemeral(true).queue(); + event.reply("The user" + user + "is too powerful for me to ban.").setEphemeral(true).queue(); + + logger.error("The bot does not have enough permissions to ban '{}'", + user.getUser().getIdLong()); return; } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 9e33ae6ced..7a02fb7f68 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -72,10 +72,10 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { } if (!bot.canInteract(Objects.requireNonNull(user))) { - event.reply( - "This user is too powerful for me to kick because he has more permissions than me.") - .setEphemeral(true) - .queue(); + event.reply("The user" + user + "is too powerful for me to kick.").setEphemeral(true).queue(); + + logger.error("The bot does not have enough permissions to kick '{}'", + user.getUser().getIdLong()); return; } From 9d199d95a970e1f2a8ce116d2d9171d6eb66cf20 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 17:43:59 +0100 Subject: [PATCH 134/140] spotlessApply --- .../tjbot/commands/moderation/BanCommand.java | 17 +++++-- .../commands/moderation/KickCommand.java | 49 ++++++++++--------- .../commands/moderation/UnbanCommand.java | 29 ++++++----- 3 files changed, 55 insertions(+), 40 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 7d61dbfb23..f482f40d6d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -55,19 +55,23 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!author.hasPermission(Permission.BAN_MEMBERS)) { - event.reply("You do not have the BAN_MEMBERS permission which means you can't unable to ban users in this server.") + event.reply( + "You do not have the BAN_MEMBERS permission which means you can't unable to ban users in this server.") .setEphemeral(true) .queue(); return; } if (!author.canInteract(Objects.requireNonNull(user))) { - event.reply("The user" + user + " is too powerful for you to ban.").setEphemeral(true).queue(); + event.reply("The user" + user + " is too powerful for you to ban.") + .setEphemeral(true) + .queue(); return; } if (!bot.hasPermission(Permission.BAN_MEMBERS)) { - event.reply("I don't have the BAN_MEMBERS permission which means I am unable to ban users in this server.") + event.reply( + "I don't have the BAN_MEMBERS permission which means I am unable to ban users in this server.") .setEphemeral(true) .queue(); @@ -76,7 +80,9 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { return; } if (!bot.canInteract(Objects.requireNonNull(user))) { - event.reply("The user" + user + "is too powerful for me to ban.").setEphemeral(true).queue(); + event.reply("The user" + user.getUser().getIdLong() + "is too powerful for me to ban.") + .setEphemeral(true) + .queue(); logger.error("The bot does not have enough permissions to ban '{}'", user.getUser().getIdLong()); @@ -107,7 +113,8 @@ private static void banUser(@NotNull Member user, @NotNull String reason, int da """ .formatted(reason))) .queue(null, throwable -> { - logger.error("I could not dm the user '{}' to inform them they were banned.", userId); + logger.error("I could not dm the user '{}' to inform them they were banned.", + userId); }); event.getGuild() diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 7a02fb7f68..a3d51ac6fc 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -29,7 +29,7 @@ public KickCommand() { super("kick", "Kicks a given user", SlashCommandVisibility.GUILD); getData().addOption(OptionType.USER, USER_OPTION, "The user which you want to kick", true) - .addOption(OptionType.STRING, REASON_OPTION, "why the user should be kicked", true); + .addOption(OptionType.STRING, REASON_OPTION, "why the user should be kicked", true); } /** @@ -50,21 +50,24 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { if (!author.hasPermission(Permission.KICK_MEMBERS)) { event.reply( - "You do not have the KICK_MEMBERS permission which means you can't unable to kick users in this server.") - .setEphemeral(true) - .queue(); + "You do not have the KICK_MEMBERS permission which means you can't unable to kick users in this server.") + .setEphemeral(true) + .queue(); return; } if (!author.canInteract(Objects.requireNonNull(user))) { - event.reply("The user" + user + "is too powerful for you to kick.").setEphemeral(true).queue(); + event.reply("The user" + user + "is too powerful for you to kick.") + .setEphemeral(true) + .queue(); return; } if (!bot.hasPermission(Permission.KICK_MEMBERS)) { - event.reply("I don't have the KICK_MEMBERS permission which means I am unable to kick users in this server.") - .setEphemeral(true) - .queue(); + event.reply( + "I don't have the KICK_MEMBERS permission which means I am unable to kick users in this server.") + .setEphemeral(true) + .queue(); logger.error("The bot does not have KICK_MEMBERS permissions on the server '{}' ", event.getGuild().getId()); @@ -72,7 +75,9 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { } if (!bot.canInteract(Objects.requireNonNull(user))) { - event.reply("The user" + user + "is too powerful for me to kick.").setEphemeral(true).queue(); + event.reply("The user" + user.getUser().getIdLong() + "is too powerful for me to kick.") + .setEphemeral(true) + .queue(); logger.error("The bot does not have enough permissions to kick '{}'", user.getUser().getIdLong()); @@ -80,21 +85,21 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { } event.getJDA() - .openPrivateChannelById(userId) - .flatMap(channel -> channel.sendMessage( - """ - Hey there, sorry to tell you but unfortunately you have been kicked from the guild 'Together Java'. - If you think this was a mistake, please contact a moderator or admin of the guild. - The kick reason is: - """ - + reason)) - .queue(); + .openPrivateChannelById(userId) + .flatMap(channel -> channel.sendMessage( + """ + Hey there, sorry to tell you but unfortunately you have been kicked from the guild 'Together Java'. + If you think this was a mistake, please contact a moderator or admin of the guild. + The kick reason is: + """ + + reason)) + .queue(); event.getGuild() - .kick(user, reason) - .flatMap(v -> event.reply(user.getUser().getAsTag() + " was kicked by " - + author.getUser().getAsTag() + " for: " + reason)) - .queue(); + .kick(user, reason) + .flatMap(v -> event.reply(user.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(), author.getUser().getAsTag(), diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java index cdebcc7994..f8724980ab 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java @@ -45,32 +45,35 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.BAN_MEMBERS)) { event.reply( - "You do not have the BAN_MEMBERS permission to urban users from this server.") - .setEphemeral(true) - .queue(); + "You do not have the BAN_MEMBERS permission to urban users from this server.") + .setEphemeral(true) + .queue(); return; } - if (!(Objects.requireNonNull(event.getGuild())).getSelfMember().hasPermission(Permission.BAN_MEMBERS)) { + if (!(Objects.requireNonNull(event.getGuild())).getSelfMember() + .hasPermission(Permission.BAN_MEMBERS)) { event.reply( - "I don't have the BAN_MEMBERS permission to unban the user from this server.") - .setEphemeral(true) - .queue(); + "I don't have the BAN_MEMBERS permission which means I am unable to unban users in this server.") + .setEphemeral(true) + .queue(); + + logger.error("The bot does not have enough permissions to unban '{}'", userId); return; } - + event.getGuild().unban(userId).queue(v -> { - event.reply("Unbanned the user") - .queue(); + event.reply("Unbanned the user").queue(); logger.info(" '{}' unbanned user id '{}' ", event.getMember().getIdLong(), userId); }, throwable -> { - if (throwable instanceof ErrorResponseException errorResponseException && - errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) { + if (throwable instanceof ErrorResponseException errorResponseException + && errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) { event.reply("the specified user doesn't exist").queue(); logger.debug("The user '{}' does not exist", userId); } else { - event.reply("Something went wrong, check the logs or contact a helper/moderator").queue(); + event.reply("Something went wrong, check the logs or contact a helper/moderator") + .queue(); logger.error("Something went wrong in the unban command: " + throwable); } }); From 6cee7d667b6f4010987a013c94e3e3a22a386dbf Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 17:49:49 +0100 Subject: [PATCH 135/140] Update PurgeCommand.java --- .../tjbot/commands/moderation/PurgeCommand.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java index 862ad4fe62..414b457d28 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java @@ -59,13 +59,13 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { int amount = Math.toIntExact( Objects.requireNonNull(event.getOption(NUMBER_OF_MESSAGES_TO_DELETE)).getAsLong()); - if (amount > 200 || amount < 1) { + if (amount > 100 || amount < 1) { event.reply("You can only delete 1 to 200 messages").setEphemeral(true).queue(); } else { channel.getHistory().retrievePast(amount).queue(channel::purgeMessages); event.reply("I have deleted this amount of messages " + amount) - .setEphemeral(true) - .queue(); + .setEphemeral(true) + .queue(); logger.info(" '{}' deleted this amount of messages '{}'", author, amount); } } From 9b12e96ea9844bd2a25ea202fd129c804b2b6dc6 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 17:51:20 +0100 Subject: [PATCH 136/140] Update PurgeCommand.java --- .../tjbot/commands/moderation/PurgeCommand.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java index 414b457d28..ebd1f66af2 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java @@ -31,9 +31,7 @@ public PurgeCommand() { getData() .addOption(OptionType.INTEGER, NUMBER_OF_MESSAGES_TO_DELETE, - "The number of messages you want to delete. 1 to 100", true) - .addOption(OptionType.USER, USER_MESSAGES, - "If you want to delete messages for a specific user.", false); + "The number of messages you want to delete. 1 to 100", true); } @Override @@ -51,9 +49,12 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) { final Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); if (!bot.hasPermission(Permission.MESSAGE_MANAGE)) { - event.reply("I am missing MESSAGE_MANAGE permission to delete these messages") + event.reply("I am missing MESSAGE_MANAGE permission which means I am unable to delete messages in this server.") .setEphemeral(true) .queue(); + + logger.error("The bot does not have MESSAGE_MANAGE permission on the server '{}' ", + event.getGuild().getId()); return; } From c463d991ac3052fc19c946bb31e2a5eb070c0209 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 19:08:47 +0100 Subject: [PATCH 137/140] fixed --- .../togetherjava/tjbot/commands/Commands.java | 3 +- .../tjbot/commands/moderation/BanCommand.java | 130 ------------------ .../commands/moderation/KickCommand.java | 108 --------------- .../commands/moderation/PurgeCommand.java | 2 +- .../commands/moderation/UnbanCommand.java | 81 ----------- 5 files changed, 2 insertions(+), 322 deletions(-) delete mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java delete mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java delete mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java index 483789c4f7..45afe45147 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java @@ -42,7 +42,6 @@ public enum Commands { // available. return List.of(new PingCommand(), new DatabaseCommand(database), new TeXCommand(), new TagCommand(tagSystem), new TagManageCommand(tagSystem), - new TagsCommand(tagSystem), new BanCommand(), new UnbanCommand(), new KickCommand(), - new PurgeCommand()); + new TagsCommand(tagSystem), new PurgeCommand()); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java deleted file mode 100644 index f482f40d6d..0000000000 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ /dev/null @@ -1,130 +0,0 @@ -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.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 him about - * the action and the reason. - *

- * 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"; - /** - * As stated in {@link Guild#ban(User, int, String)} The reason can be only 512 characters. - */ - private static final Integer REASON_MAX_LENGTH = 512; - private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); - - /** - * Creates an instance of the ban command. - */ - public BanCommand() { - super("ban", "Bans a given user", 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) { - Member user = Objects.requireNonNull(event.getOption(USER_OPTION)).getAsMember(); - Member author = Objects.requireNonNull(event.getMember()); - String reason = Objects.requireNonNull(event.getOption(REASON_OPTION)).getAsString(); - Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); - - if (!author.hasPermission(Permission.BAN_MEMBERS)) { - event.reply( - "You do not have the BAN_MEMBERS permission which means you can't unable to ban users in this server.") - .setEphemeral(true) - .queue(); - return; - } - - if (!author.canInteract(Objects.requireNonNull(user))) { - event.reply("The user" + user + " is too powerful for you to ban.") - .setEphemeral(true) - .queue(); - return; - } - - if (!bot.hasPermission(Permission.BAN_MEMBERS)) { - event.reply( - "I don't have the BAN_MEMBERS permission which means I am unable to ban users in this server.") - .setEphemeral(true) - .queue(); - - logger.error("The bot does not have BAN_MEMBERS permissions on the server '{}' ", - event.getGuild().getId()); - return; - } - if (!bot.canInteract(Objects.requireNonNull(user))) { - event.reply("The user" + user.getUser().getIdLong() + "is too powerful for me to ban.") - .setEphemeral(true) - .queue(); - - logger.error("The bot does not have enough permissions to ban '{}'", - user.getUser().getIdLong()); - return; - } - - int days = Math - .toIntExact(Objects.requireNonNull(event.getOption(DELETE_HISTORY_OPTION)).getAsLong()); - - if (reason.length() > BanCommand.REASON_MAX_LENGTH) { - event.reply("The reason can not be over 512 characters").setEphemeral(true).queue(); - return; - } - - banUser(user, reason, days, user.getUser().getIdLong(), author.getIdLong(), event); - } - - private static void banUser(@NotNull Member user, @NotNull String reason, int days, long userId, - long authorId, @NotNull SlashCommandEvent event) { - - event.getJDA() - .openPrivateChannelById(userId) - .flatMap(channel -> channel.sendMessage( - """ - Hey there, sorry to tell you but unfortunately you have been banned from the guild 'Together Java'.\040 - If you think this was a mistake, please contact a moderator or admin of the guild. - The ban reason is: %s - """ - .formatted(reason))) - .queue(null, throwable -> { - logger.error("I could not dm the user '{}' to inform them they were banned.", - userId); - }); - - event.getGuild() - .ban(user, days, reason) - .flatMap(v -> event.reply(user.getUser().getAsTag() + " was banned by " - + user.getUser().getAsTag() + " for: " + reason)) - .queue(); - - logger.info( - " '{}' banned the user '{}' and deleted the message history of the last '{}' days. Reason was '{}'", - authorId, userId, days, reason); - } -} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java deleted file mode 100644 index a3d51ac6fc..0000000000 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ /dev/null @@ -1,108 +0,0 @@ -package org.togetherjava.tjbot.commands.moderation; - -import net.dv8tion.jda.api.Permission; -import net.dv8tion.jda.api.entities.Member; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -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; - - -/** - *

- * The implemented command is {@code /kick @user reason}, upon which the bot will kick the user. - */ -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"; - - - /** - * Creates an instance of the kick command. - */ - public KickCommand() { - super("kick", "Kicks a given user", SlashCommandVisibility.GUILD); - - getData().addOption(OptionType.USER, USER_OPTION, "The user which you want to kick", true) - .addOption(OptionType.STRING, REASON_OPTION, "why the user should be kicked", true); - } - - /** - * When triggered with {@code /kick @user reason}, the bot will check if the user has perms. - * Then it will check if itself has perms to kick. If it does it will check if the user is too - * powerful or not. If the user is not then bot will kick the user and reply with - * {@code Kicked User!}. - * - * @param event the corresponding event - */ - @Override - public void onSlashCommand(@NotNull SlashCommandEvent event) { - Member user = Objects.requireNonNull(event.getOption(USER_OPTION)).getAsMember(); - Member author = Objects.requireNonNull(event.getMember()); - String reason = Objects.requireNonNull(event.getOption(REASON_OPTION)).getAsString(); - Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); - long userId = Objects.requireNonNull(user).getUser().getIdLong(); - - if (!author.hasPermission(Permission.KICK_MEMBERS)) { - event.reply( - "You do not have the KICK_MEMBERS permission which means you can't unable to kick users in this server.") - .setEphemeral(true) - .queue(); - return; - } - - if (!author.canInteract(Objects.requireNonNull(user))) { - event.reply("The user" + user + "is too powerful for you to kick.") - .setEphemeral(true) - .queue(); - return; - } - - if (!bot.hasPermission(Permission.KICK_MEMBERS)) { - event.reply( - "I don't have the KICK_MEMBERS permission which means I am unable to kick users in this server.") - .setEphemeral(true) - .queue(); - - logger.error("The bot does not have KICK_MEMBERS permissions on the server '{}' ", - event.getGuild().getId()); - return; - } - - if (!bot.canInteract(Objects.requireNonNull(user))) { - event.reply("The user" + user.getUser().getIdLong() + "is too powerful for me to kick.") - .setEphemeral(true) - .queue(); - - logger.error("The bot does not have enough permissions to kick '{}'", - user.getUser().getIdLong()); - return; - } - - event.getJDA() - .openPrivateChannelById(userId) - .flatMap(channel -> channel.sendMessage( - """ - Hey there, sorry to tell you but unfortunately you have been kicked from the guild 'Together Java'. - If you think this was a mistake, please contact a moderator or admin of the guild. - The kick reason is: - """ - + reason)) - .queue(); - - event.getGuild() - .kick(user, reason) - .flatMap(v -> event.reply(user.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(), author.getUser().getAsTag(), - userId, reason); - } -} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java index ebd1f66af2..433e96be17 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java @@ -21,7 +21,7 @@ * */ public final class PurgeCommand extends SlashCommandAdapter { - private static final Logger logger = LoggerFactory.getLogger(BanCommand.class); + private static final Logger logger = LoggerFactory.getLogger(PurgeCommand.class); private static final String NUMBER_OF_MESSAGES_TO_DELETE = "number_of_messages"; private static final String USER_MESSAGES = "user"; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java deleted file mode 100644 index f8724980ab..0000000000 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java +++ /dev/null @@ -1,81 +0,0 @@ -package org.togetherjava.tjbot.commands.moderation; - -import net.dv8tion.jda.api.Permission; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.exceptions.ErrorResponseException; -import net.dv8tion.jda.api.interactions.commands.OptionType; -import net.dv8tion.jda.api.requests.ErrorResponse; -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; - -/** - *

- * The implemented command is {@code /unban user_id}, upon which the bot will unban the user. - */ -public final class UnbanCommand extends SlashCommandAdapter { - private static final Logger logger = LoggerFactory.getLogger(UnbanCommand.class); - private static final String USER_ID = "user_id"; - - /** - * Creates an instance of the ban command. - */ - public UnbanCommand() { - super("unban", "Unbans a given user", SlashCommandVisibility.GUILD); - - getData().addOption(OptionType.STRING, USER_ID, - "The user id of the user which you want to unban", true); - - } - - /** - * When triggered with {@code /unban user_id}}, the bot will respond will check if the user has - * perms. Then it will check if itself has perms to unban. If it does it will check if the user - * is the user is too powerful or not. If the user is not then unban will ban the user. - * - * @param event the corresponding event - */ - @Override - public void onSlashCommand(@NotNull SlashCommandEvent event) { - final String userId = Objects.requireNonNull(event.getOption(USER_ID)).getAsString(); - - if (!Objects.requireNonNull(event.getMember()).hasPermission(Permission.BAN_MEMBERS)) { - event.reply( - "You do not have the BAN_MEMBERS permission to urban users from this server.") - .setEphemeral(true) - .queue(); - return; - } - - if (!(Objects.requireNonNull(event.getGuild())).getSelfMember() - .hasPermission(Permission.BAN_MEMBERS)) { - event.reply( - "I don't have the BAN_MEMBERS permission which means I am unable to unban users in this server.") - .setEphemeral(true) - .queue(); - - logger.error("The bot does not have enough permissions to unban '{}'", userId); - return; - } - - event.getGuild().unban(userId).queue(v -> { - event.reply("Unbanned the user").queue(); - logger.info(" '{}' unbanned user id '{}' ", event.getMember().getIdLong(), userId); - }, throwable -> { - if (throwable instanceof ErrorResponseException errorResponseException - && errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) { - - event.reply("the specified user doesn't exist").queue(); - logger.debug("The user '{}' does not exist", userId); - } else { - event.reply("Something went wrong, check the logs or contact a helper/moderator") - .queue(); - logger.error("Something went wrong in the unban command: " + throwable); - } - }); - } -} From 49d692866da18752be30c503677676259c97e599 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 19:10:43 +0100 Subject: [PATCH 138/140] Update V1__Database_Listener.sql --- application/src/main/resources/db/V1__Database_Listener.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/application/src/main/resources/db/V1__Database_Listener.sql b/application/src/main/resources/db/V1__Database_Listener.sql index 8f16bf2dc6..dcbdc58bab 100644 --- a/application/src/main/resources/db/V1__Database_Listener.sql +++ b/application/src/main/resources/db/V1__Database_Listener.sql @@ -1,3 +1,4 @@ + CREATE TABLE storage ( key TEXT NOT NULL PRIMARY KEY, From 323f04a0764af8aa72da9120bc84528b77a8352a Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 19:11:18 +0100 Subject: [PATCH 139/140] Update V1__Database_Listener.sql --- application/src/main/resources/db/V1__Database_Listener.sql | 1 - 1 file changed, 1 deletion(-) diff --git a/application/src/main/resources/db/V1__Database_Listener.sql b/application/src/main/resources/db/V1__Database_Listener.sql index dcbdc58bab..8f16bf2dc6 100644 --- a/application/src/main/resources/db/V1__Database_Listener.sql +++ b/application/src/main/resources/db/V1__Database_Listener.sql @@ -1,4 +1,3 @@ - CREATE TABLE storage ( key TEXT NOT NULL PRIMARY KEY, From 5ba27a7df208abccc3fb3706ffe87124c22e3588 Mon Sep 17 00:00:00 2001 From: Yusuf Date: Sat, 23 Oct 2021 19:11:34 +0100 Subject: [PATCH 140/140] Update V1__Database_Listener.sql --- application/src/main/resources/db/V1__Database_Listener.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/resources/db/V1__Database_Listener.sql b/application/src/main/resources/db/V1__Database_Listener.sql index 8f16bf2dc6..f1956f216e 100644 --- a/application/src/main/resources/db/V1__Database_Listener.sql +++ b/application/src/main/resources/db/V1__Database_Listener.sql @@ -2,4 +2,4 @@ CREATE TABLE storage ( key TEXT NOT NULL PRIMARY KEY, value TEXT NOT NULL -) \ No newline at end of file +)