diff --git a/src/main/java/sciwhiz12/janitor/commands/BaseCommand.java b/src/main/java/sciwhiz12/janitor/commands/BaseCommand.java index e953bac..4f115d4 100644 --- a/src/main/java/sciwhiz12/janitor/commands/BaseCommand.java +++ b/src/main/java/sciwhiz12/janitor/commands/BaseCommand.java @@ -3,6 +3,7 @@ package sciwhiz12.janitor.commands; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import sciwhiz12.janitor.JanitorBot; +import sciwhiz12.janitor.msg.Messages; public abstract class BaseCommand { private final CommandRegistry registry; @@ -19,5 +20,9 @@ public abstract class BaseCommand { return registry.getBot(); } + protected Messages messages() { + return getBot().getMessages(); + } + public abstract LiteralArgumentBuilder getNode(); } diff --git a/src/main/java/sciwhiz12/janitor/commands/moderation/KickCommand.java b/src/main/java/sciwhiz12/janitor/commands/moderation/KickCommand.java index f9faf83..c807aee 100644 --- a/src/main/java/sciwhiz12/janitor/commands/moderation/KickCommand.java +++ b/src/main/java/sciwhiz12/janitor/commands/moderation/KickCommand.java @@ -50,7 +50,7 @@ public class KickCommand extends BaseCommand { private int runWithReason(CommandContext ctx, @Nullable String reason) throws CommandSyntaxException { MessageChannel channel = ctx.getSource().getChannel(); if (!ctx.getSource().isFromGuild()) { - getBot().getMessages().GENERAL.guildOnlyCommand(channel).queue(); + messages().GENERAL.guildOnlyCommand(channel).queue(); return 1; } final Guild guild = ctx.getSource().getGuild(); @@ -60,28 +60,26 @@ public class KickCommand extends BaseCommand { return 1; } final Member target = members.get(0); - if (!guild.getSelfMember().hasPermission(KICK_PERMISSION)) { - getBot().getMessages().GENERAL.insufficientPermissions(channel, KICK_PERMISSION).queue(); - return 1; - } - if (!guild.getSelfMember().canInteract(target)) { - getBot().getMessages().GENERAL.cannotInteract(channel, target).queue(); - return 1; - } - if (!performer.hasPermission(KICK_PERMISSION)) { - getBot().getMessages().MODERATION.performerInsufficientPermissions(channel, performer, KICK_PERMISSION).queue(); - return 1; - } - if (!performer.canInteract(target)) { - getBot().getMessages().MODERATION.cannotModerate(channel, performer, target).queue(); - return 1; - } - target.getUser().openPrivateChannel() - .flatMap(dm -> getBot().getMessages().MODERATION.kickedDM(dm, performer, target, reason)) - .mapToResult() - .flatMap(res -> ModerationHelper.kickUser(target.getGuild(), performer, target, reason) - .flatMap(v -> getBot().getMessages().MODERATION.kickUser(channel, performer, target, reason, res.isSuccess()))) - .queue(); + if (guild.getSelfMember().equals(target)) + messages().GENERAL.cannotActionSelf(channel).queue(); + else if (performer.equals(target)) + messages().GENERAL.cannotActionPerformer(channel, performer).queue(); + else if (!guild.getSelfMember().hasPermission(KICK_PERMISSION)) + messages().GENERAL.insufficientPermissions(channel, KICK_PERMISSION).queue(); + else if (!guild.getSelfMember().canInteract(target)) + messages().GENERAL.cannotInteract(channel, target).queue(); + else if (!performer.hasPermission(KICK_PERMISSION)) + messages().MODERATION.performerInsufficientPermissions(channel, performer, KICK_PERMISSION).queue(); + else if (!performer.canInteract(target)) + messages().MODERATION.cannotModerate(channel, performer, target).queue(); + else + target.getUser().openPrivateChannel() + .flatMap(dm -> messages().MODERATION.kickedDM(dm, performer, target, reason)) + .mapToResult() + .flatMap(res -> ModerationHelper.kickUser(target.getGuild(), performer, target, reason) + .flatMap( + v -> messages().MODERATION.kickUser(channel, performer, target, reason, res.isSuccess()))) + .queue(); return 1; } } diff --git a/src/main/java/sciwhiz12/janitor/msg/Messages.java b/src/main/java/sciwhiz12/janitor/msg/Messages.java index 99108b5..5702b84 100644 --- a/src/main/java/sciwhiz12/janitor/msg/Messages.java +++ b/src/main/java/sciwhiz12/janitor/msg/Messages.java @@ -78,6 +78,27 @@ public class Messages { .build() ); } + + public RestAction cannotActionSelf(MessageChannel channel) { + return channel.sendMessage( + new EmbedBuilder() + .setTitle(translate("general.cannot_action_self.title")) + .setDescription(translate("general.cannot_action_self.desc")) + .setColor(General.FAILURE_COLOR) + .build() + ); + } + + public RestAction cannotActionPerformer(MessageChannel channel, Member performer) { + return channel.sendMessage( + new EmbedBuilder() + .setTitle(translate("general.cannot_action_performer.title")) + .setDescription(translate("general.cannot_action_performer.desc")) + .addField(translate("general.cannot_action_performer.field.performer"), performer.getUser().getAsMention(), true) + .setColor(General.FAILURE_COLOR) + .build() + ); + } } public final class Moderation { diff --git a/src/main/resources/english.json b/src/main/resources/english.json index 1e72cd8..f7537c8 100644 --- a/src/main/resources/english.json +++ b/src/main/resources/english.json @@ -1,6 +1,6 @@ { "general.guild_only_command.title": "Guild only command!", - "general.guild_only_command.desc": "The previous command can only be run in a guild channel.", + "general.guild_only_command.desc": "This command can only be run in a guild channel.", "general.insufficient_permissions.title": "I have insufficient permissions!", "general.insufficient_permissions.desc": "I do not have sufficient permissions to carry out this action!\nPlease contact your server admins if you believe this is in error.", "general.insufficient_permissions.field.permissions": "Required permissions", @@ -9,6 +9,11 @@ "general.cannot_interact.title": "Member is higher than me!", "general.cannot_interact.desc": "Cannot perform action on the given member, likely due to me being lower in the role hierarchy.", "general.cannot_interact.field.target": "Target", + "general.cannot_action_self.title": "Cannot act against myself!", + "general.cannot_action_self.desc": "Cannot perform action against myself, as that would be counter-intuitive.", + "general.cannot_action_performer.title": "Performer cannot act against self!", + "general.cannot_action_performer.desc": "You cannot perform this action against yourself.", + "general.cannot_action_performer.field.performer": "Performer/Target", "moderation.insufficient_permissions.title": "Insufficient permissions.", "moderation.insufficient_permissions.desc": "The performer of this command has insufficient permissions to use this command.", "moderation.insufficient_permissions.field.performer": "Performer",