1
0
mirror of https://github.com/sciwhiz12/Janitor.git synced 2024-11-14 04:21:25 +00:00

Prevent kick command from working on performer and on the bot

This commit is contained in:
Arnold Alejo Nunag 2020-09-30 06:17:12 +08:00
parent e0e0cb448a
commit 48f0aeb5b1
Signed by: sciwhiz12
GPG Key ID: 622CF446534317E1
4 changed files with 53 additions and 24 deletions

View File

@ -3,6 +3,7 @@ package sciwhiz12.janitor.commands;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import sciwhiz12.janitor.JanitorBot; import sciwhiz12.janitor.JanitorBot;
import sciwhiz12.janitor.msg.Messages;
public abstract class BaseCommand { public abstract class BaseCommand {
private final CommandRegistry registry; private final CommandRegistry registry;
@ -19,5 +20,9 @@ public abstract class BaseCommand {
return registry.getBot(); return registry.getBot();
} }
protected Messages messages() {
return getBot().getMessages();
}
public abstract LiteralArgumentBuilder<MessageReceivedEvent> getNode(); public abstract LiteralArgumentBuilder<MessageReceivedEvent> getNode();
} }

View File

@ -50,7 +50,7 @@ public class KickCommand extends BaseCommand {
private int runWithReason(CommandContext<MessageReceivedEvent> ctx, @Nullable String reason) throws CommandSyntaxException { private int runWithReason(CommandContext<MessageReceivedEvent> ctx, @Nullable String reason) throws CommandSyntaxException {
MessageChannel channel = ctx.getSource().getChannel(); MessageChannel channel = ctx.getSource().getChannel();
if (!ctx.getSource().isFromGuild()) { if (!ctx.getSource().isFromGuild()) {
getBot().getMessages().GENERAL.guildOnlyCommand(channel).queue(); messages().GENERAL.guildOnlyCommand(channel).queue();
return 1; return 1;
} }
final Guild guild = ctx.getSource().getGuild(); final Guild guild = ctx.getSource().getGuild();
@ -60,27 +60,25 @@ public class KickCommand extends BaseCommand {
return 1; return 1;
} }
final Member target = members.get(0); final Member target = members.get(0);
if (!guild.getSelfMember().hasPermission(KICK_PERMISSION)) { if (guild.getSelfMember().equals(target))
getBot().getMessages().GENERAL.insufficientPermissions(channel, KICK_PERMISSION).queue(); messages().GENERAL.cannotActionSelf(channel).queue();
return 1; else if (performer.equals(target))
} messages().GENERAL.cannotActionPerformer(channel, performer).queue();
if (!guild.getSelfMember().canInteract(target)) { else if (!guild.getSelfMember().hasPermission(KICK_PERMISSION))
getBot().getMessages().GENERAL.cannotInteract(channel, target).queue(); messages().GENERAL.insufficientPermissions(channel, KICK_PERMISSION).queue();
return 1; else if (!guild.getSelfMember().canInteract(target))
} messages().GENERAL.cannotInteract(channel, target).queue();
if (!performer.hasPermission(KICK_PERMISSION)) { else if (!performer.hasPermission(KICK_PERMISSION))
getBot().getMessages().MODERATION.performerInsufficientPermissions(channel, performer, KICK_PERMISSION).queue(); messages().MODERATION.performerInsufficientPermissions(channel, performer, KICK_PERMISSION).queue();
return 1; else if (!performer.canInteract(target))
} messages().MODERATION.cannotModerate(channel, performer, target).queue();
if (!performer.canInteract(target)) { else
getBot().getMessages().MODERATION.cannotModerate(channel, performer, target).queue();
return 1;
}
target.getUser().openPrivateChannel() target.getUser().openPrivateChannel()
.flatMap(dm -> getBot().getMessages().MODERATION.kickedDM(dm, performer, target, reason)) .flatMap(dm -> messages().MODERATION.kickedDM(dm, performer, target, reason))
.mapToResult() .mapToResult()
.flatMap(res -> ModerationHelper.kickUser(target.getGuild(), performer, target, reason) .flatMap(res -> ModerationHelper.kickUser(target.getGuild(), performer, target, reason)
.flatMap(v -> getBot().getMessages().MODERATION.kickUser(channel, performer, target, reason, res.isSuccess()))) .flatMap(
v -> messages().MODERATION.kickUser(channel, performer, target, reason, res.isSuccess())))
.queue(); .queue();
return 1; return 1;
} }

View File

@ -78,6 +78,27 @@ public class Messages {
.build() .build()
); );
} }
public RestAction<Message> 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<Message> 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 { public final class Moderation {

View File

@ -1,6 +1,6 @@
{ {
"general.guild_only_command.title": "Guild only command!", "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.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.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", "general.insufficient_permissions.field.permissions": "Required permissions",
@ -9,6 +9,11 @@
"general.cannot_interact.title": "Member is higher than me!", "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.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_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.title": "Insufficient permissions.",
"moderation.insufficient_permissions.desc": "The performer of this command has insufficient permissions to use this command.", "moderation.insufficient_permissions.desc": "The performer of this command has insufficient permissions to use this command.",
"moderation.insufficient_permissions.field.performer": "Performer", "moderation.insufficient_permissions.field.performer": "Performer",