From b02f38c70f914f37bc421c69afeea668ec4ffd9a Mon Sep 17 00:00:00 2001 From: Arnold Alejo Nunag Date: Tue, 29 Sep 2020 21:04:06 +0800 Subject: [PATCH] Add informative DM when being kicked --- .../java/sciwhiz12/janitor/BotConsole.java | 1 + .../java/sciwhiz12/janitor/JanitorBot.java | 5 +++-- .../commands/moderation/KickCommand.java | 16 +++++++-------- .../janitor/commands/util/CommandHelper.java | 6 +++--- .../commands/util/ModerationHelper.java | 4 ++-- .../java/sciwhiz12/janitor/msg/General.java | 8 ++++---- .../sciwhiz12/janitor/msg/Moderation.java | 20 +++++++++++++++---- 7 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/main/java/sciwhiz12/janitor/BotConsole.java b/src/main/java/sciwhiz12/janitor/BotConsole.java index 7399717..67d0ede 100644 --- a/src/main/java/sciwhiz12/janitor/BotConsole.java +++ b/src/main/java/sciwhiz12/janitor/BotConsole.java @@ -36,6 +36,7 @@ public class BotConsole { String[] parts = input.split(" "); switch (parts[0]) { case "shutdown": { + running = false; bot.shutdown(); break; } diff --git a/src/main/java/sciwhiz12/janitor/JanitorBot.java b/src/main/java/sciwhiz12/janitor/JanitorBot.java index 5ecc7ee..241893e 100644 --- a/src/main/java/sciwhiz12/janitor/JanitorBot.java +++ b/src/main/java/sciwhiz12/janitor/JanitorBot.java @@ -10,6 +10,8 @@ import sciwhiz12.janitor.commands.CommandRegistry; import sciwhiz12.janitor.config.BotConfig; import sciwhiz12.janitor.utils.Util; +import java.util.concurrent.CompletableFuture; + import static sciwhiz12.janitor.Logging.JANITOR; import static sciwhiz12.janitor.Logging.STATUS; @@ -74,8 +76,7 @@ public class JanitorBot { JANITOR .error(STATUS, "Error while sending shutdown message to owner", err) )) - .join() - ); + ).ifPresent(CompletableFuture::join); discord.shutdown(); } } diff --git a/src/main/java/sciwhiz12/janitor/commands/moderation/KickCommand.java b/src/main/java/sciwhiz12/janitor/commands/moderation/KickCommand.java index 8bbd6f3..6ac7a1b 100644 --- a/src/main/java/sciwhiz12/janitor/commands/moderation/KickCommand.java +++ b/src/main/java/sciwhiz12/janitor/commands/moderation/KickCommand.java @@ -5,6 +5,7 @@ import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.MessageChannel; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import org.checkerframework.checker.nullness.qual.Nullable; import sciwhiz12.janitor.commands.BaseCommand; @@ -47,20 +48,19 @@ public class KickCommand extends BaseCommand { } private int runWithReason(CommandContext ctx, @Nullable String reason) throws CommandSyntaxException { + MessageChannel channel = ctx.getSource().getChannel(); if (!ctx.getSource().isFromGuild()) { - General.guildOnlyCommand(ctx.getSource().getTextChannel()).queue(); + General.guildOnlyCommand(channel).queue(); return 1; } Member performer = ctx.getSource().getMember(); if (performer == null) return 1; - List members = getMembers("member", ctx).fromGuild(ctx.getSource().getGuild()); - if (members.size() > 1) { - General.ambiguousMember(ctx.getSource().getTextChannel()).queue(); - return 1; - } + List members = getMembers("member", ctx).fromGuild(performer.getGuild()); Member target = members.get(0); - if (ModerationHelper.ensurePermissions(ctx.getSource().getTextChannel(), performer, target, KICK_PERMISSION)) { - ModerationHelper.kickUser(target.getGuild(), performer, target, reason) + if (ModerationHelper.ensurePermissions(channel, performer, target, KICK_PERMISSION)) { + target.getUser().openPrivateChannel() + .flatMap(dm -> Moderation.kickedDM(dm, performer, target, reason)) + .flatMap(v -> ModerationHelper.kickUser(target.getGuild(), performer, target, reason)) .flatMap(v -> Moderation.kickUser(ctx.getSource().getChannel(), performer, target, reason)) .queue(); } diff --git a/src/main/java/sciwhiz12/janitor/commands/util/CommandHelper.java b/src/main/java/sciwhiz12/janitor/commands/util/CommandHelper.java index 4eb0904..22be29b 100644 --- a/src/main/java/sciwhiz12/janitor/commands/util/CommandHelper.java +++ b/src/main/java/sciwhiz12/janitor/commands/util/CommandHelper.java @@ -6,7 +6,7 @@ import com.mojang.brigadier.builder.RequiredArgumentBuilder; 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.TextChannel; +import net.dv8tion.jda.api.entities.MessageChannel; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import sciwhiz12.janitor.msg.General; @@ -21,7 +21,7 @@ public class CommandHelper { return RequiredArgumentBuilder.argument(command, argument); } - public static boolean canInteract(TextChannel response, Member target) { + public static boolean canInteract(MessageChannel response, Member target) { if (!target.getGuild().getSelfMember().canInteract(target)) { General.cannotInteract(response, target).queue(); return false; @@ -29,7 +29,7 @@ public class CommandHelper { return true; } - public static boolean hasPermission(TextChannel response, Guild guild, EnumSet permissions) { + public static boolean hasPermission(MessageChannel response, Guild guild, EnumSet permissions) { if (!guild.getSelfMember().hasPermission(permissions)) { General.insufficientPermissions(response, permissions).queue(); return false; diff --git a/src/main/java/sciwhiz12/janitor/commands/util/ModerationHelper.java b/src/main/java/sciwhiz12/janitor/commands/util/ModerationHelper.java index a55cb15..6435241 100644 --- a/src/main/java/sciwhiz12/janitor/commands/util/ModerationHelper.java +++ b/src/main/java/sciwhiz12/janitor/commands/util/ModerationHelper.java @@ -3,7 +3,7 @@ package sciwhiz12.janitor.commands.util; 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.TextChannel; +import net.dv8tion.jda.api.entities.MessageChannel; import net.dv8tion.jda.api.requests.restaction.AuditableRestAction; import org.checkerframework.checker.nullness.qual.Nullable; import sciwhiz12.janitor.msg.General; @@ -20,7 +20,7 @@ public class ModerationHelper { return guild.kick(target, auditReason.toString()); } - public static boolean ensurePermissions(TextChannel channel, Member performer, Member target, EnumSet permissions) { + public static boolean ensurePermissions(MessageChannel channel, Member performer, Member target, EnumSet permissions) { if (!CommandHelper.hasPermission(channel, target.getGuild(), permissions)) return false; if (!CommandHelper.canInteract(channel, target)) return false; if (!performer.hasPermission(permissions)) { diff --git a/src/main/java/sciwhiz12/janitor/msg/General.java b/src/main/java/sciwhiz12/janitor/msg/General.java index 99123f6..f3fea43 100644 --- a/src/main/java/sciwhiz12/janitor/msg/General.java +++ b/src/main/java/sciwhiz12/janitor/msg/General.java @@ -14,7 +14,7 @@ public final class General { private General() { } - public static RestAction guildOnlyCommand(TextChannel channel) { + public static RestAction guildOnlyCommand(MessageChannel channel) { return channel.sendMessage( new EmbedBuilder() .setTitle("Guild only command!") @@ -24,7 +24,7 @@ public final class General { ); } - public static RestAction insufficientPermissions(TextChannel channel, EnumSet permissions) { + public static RestAction insufficientPermissions(MessageChannel channel, EnumSet permissions) { return channel.sendMessage( new EmbedBuilder() .setTitle("I have insufficient permissions!") @@ -39,7 +39,7 @@ public final class General { ); } - public static RestAction ambiguousMember(TextChannel channel) { + public static RestAction ambiguousMember(MessageChannel channel) { return channel.sendMessage( new EmbedBuilder() .setTitle("Ambiguous member argument!") @@ -50,7 +50,7 @@ public final class General { ); } - public static RestAction cannotInteract(TextChannel channel, Member target) { + public static RestAction cannotInteract(MessageChannel channel, Member target) { return channel.sendMessage( new EmbedBuilder() .setTitle("Member is higher than me!") diff --git a/src/main/java/sciwhiz12/janitor/msg/Moderation.java b/src/main/java/sciwhiz12/janitor/msg/Moderation.java index 894d0b3..33ec91e 100644 --- a/src/main/java/sciwhiz12/janitor/msg/Moderation.java +++ b/src/main/java/sciwhiz12/janitor/msg/Moderation.java @@ -15,6 +15,7 @@ import static sciwhiz12.janitor.msg.General.nameFor; public final class Moderation { public static final int MODERATION_COLOR = 0xF1BD25; + public static final String GAVEL_ICON_URL = "https://cdn.discordapp.com/attachments/738478941760782526/760463743330549760/gavel.png"; private Moderation() { } @@ -48,14 +49,25 @@ public final class Moderation { public static MessageAction kickUser(MessageChannel channel, Member performer, Member target, @Nullable String reason) { final EmbedBuilder embed = new EmbedBuilder() - .setTitle("Kicked.") - .addField("Performer", nameFor(performer.getUser()), true) + .setAuthor("Kicked user from server.", null, GAVEL_ICON_URL) + .addField("Moderator", nameFor(performer.getUser()), true) .addField("Target", nameFor(target.getUser()), true); if (reason != null) embed.addField("Reason", reason, false); return channel.sendMessage( - embed.setColor(MODERATION_COLOR) - .build() + embed.setColor(MODERATION_COLOR).build() + ); + } + + public static MessageAction kickedDM(MessageChannel channel, Member performer, Member target, @Nullable String reason) { + final EmbedBuilder embed = new EmbedBuilder() + .setAuthor(performer.getGuild().getName(), null, performer.getGuild().getIconUrl()) + .setTitle("You were kicked from this server.") + .addField("Moderator", nameFor(performer.getUser()), true); + if (reason != null) + embed.addField("Reason", reason, false); + return channel.sendMessage( + embed.setColor(MODERATION_COLOR).build() ); } }