diff --git a/src/main/java/wtf/beatrice/hidekobot/Cache.java b/src/main/java/wtf/beatrice/hidekobot/Cache.java index 6e10789..89ba590 100644 --- a/src/main/java/wtf/beatrice/hidekobot/Cache.java +++ b/src/main/java/wtf/beatrice/hidekobot/Cache.java @@ -242,7 +242,7 @@ public class Cache try { Field field = Color.class.getField(colorName); color = (Color)field.get(null); - } catch (Exception e) { + } catch (RuntimeException | NoSuchFieldException | IllegalAccessException e) { logger.log("Unknown color: " + colorName); } return color == null ? defaultColor : color; diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index c5bd5de..ef69e9f 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -138,6 +138,7 @@ public class HidekoBot messageCommandListener.registerCommand(new HelloCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.AliasCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.AvatarCommand()); + messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.BanCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.BannerCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.BotInfoCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.CoinFlipCommand()); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/base/ClearChat.java b/src/main/java/wtf/beatrice/hidekobot/commands/base/ClearChat.java index d732465..17ea6f6 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/base/ClearChat.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/base/ClearChat.java @@ -132,9 +132,8 @@ public class ClearChat which are restricted by discord, and thus has to use a less efficient way that triggers rate-limiting very quickly. */ - } catch (Exception e) + } catch (RuntimeException ignored) { - return -1; } } diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/base/UserPunishment.java b/src/main/java/wtf/beatrice/hidekobot/commands/base/UserPunishment.java new file mode 100644 index 0000000..e5e5eb8 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/commands/base/UserPunishment.java @@ -0,0 +1,157 @@ +package wtf.beatrice.hidekobot.commands.base; + +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.IMentionable; +import net.dv8tion.jda.api.entities.Mentions; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.requests.restaction.AuditableRestAction; +import org.apache.commons.text.WordUtils; +import wtf.beatrice.hidekobot.Cache; +import wtf.beatrice.hidekobot.HidekoBot; +import wtf.beatrice.hidekobot.objects.MessageResponse; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +public class UserPunishment +{ + public static void handle(MessageReceivedEvent event, String[] args, PunishmentType punishmentType) + { + Mentions msgMentions = event.getMessage().getMentions(); + List mentions = msgMentions.getMentions(); + + MessageResponse response = getResponse(event.getAuthor(), + punishmentType, + event.getChannel(), + mentions, + args); + + if(response.embed() != null) + event.getMessage().replyEmbeds(response.embed()).queue(); + else if(response.content() != null) + event.getMessage().reply(response.content()).queue(); + } + + public static MessageResponse getResponse(User author, + PunishmentType punishmentType, + MessageChannelUnion channel, + List mentions, + String[] args) + { + String punishmentTypeName = punishmentType.name().toLowerCase(); + + + if(!(channel instanceof TextChannel)) + { + // todo nicer looking with emojis + return new MessageResponse("Sorry! I can't " + punishmentTypeName + " people in DMs.", null); + } + + if(mentions.isEmpty()) + { + // todo nicer looking with emojis + return new MessageResponse("You have to tell me who to " + punishmentTypeName + "!", null); + } + + String mentionedId = mentions.get(0).getId(); + User mentioned = null; + + try { + mentioned = HidekoBot.getAPI().retrieveUserById(mentionedId).complete(); + } catch (RuntimeException ignored) + { + // todo nicer looking with emojis + return new MessageResponse("I can't " + punishmentTypeName + " that user!", null); + } + + StringBuilder reasonBuilder = new StringBuilder(); + String reason = ""; + if(args.length > 1) + { + for(int i = 1; i < args.length; i++) + { + String arg = args[i]; + reasonBuilder.append(arg); + + if(i + 1 != arg.length()) + reasonBuilder.append(" "); // separate args with a space except on last iteration. + } + + reason = reasonBuilder.toString(); + } + + if(mentioned == null) + { + // todo nicer looking with emojis + return new MessageResponse("I can't " + punishmentTypeName + " that user!", null); + } + + Guild guild = ((TextChannel) channel).getGuild(); + + AuditableRestAction punishmentAction = null; + + try { + switch (punishmentType) { + case BAN -> punishmentAction = guild.ban(mentioned, 0, TimeUnit.SECONDS); + case KICK -> punishmentAction = guild.kick(mentioned); + } + } catch (RuntimeException ignored) { + // todo nicer looking with emojis + return new MessageResponse("Sorry, I couldn't " + punishmentTypeName + " " + mentioned.getAsMention() + "!", + null); + } + + if(!reason.isEmpty() && !reasonBuilder.isEmpty()) punishmentAction.reason(reason); + + try { + punishmentAction.complete(); + } catch (RuntimeException ignored) + { + // todo nicer looking with emojis + return new MessageResponse("Sorry, I couldn't " + punishmentTypeName + " " + mentioned.getAsMention() + "!", + null); + } + + EmbedBuilder embedBuilder = new EmbedBuilder(); + + embedBuilder.setAuthor(author.getAsTag(), null, author.getAvatarUrl()); + embedBuilder.setColor(Cache.getBotColor()); + embedBuilder.setTitle("User " + punishmentType.getPastTense()); + + embedBuilder.addField("\uD83D\uDC64 User", mentioned.getAsMention(), true); + embedBuilder.addField("✂️ By", author.getAsMention(), true); + + if(reason.isEmpty()) + reason = "*No reason specified*"; + + embedBuilder.addField("\uD83D\uDCD6 Reason", reason, false); + + + return new MessageResponse(null, embedBuilder.build()); + + } + + public enum PunishmentType { + KICK("kicked"), + BAN("banned"), + + ; + + private final String pastTense; + + PunishmentType(String pastTense) + { + this.pastTense = pastTense; + } + + public String getPastTense() + { + return pastTense; + } + + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/BanCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/BanCommand.java new file mode 100644 index 0000000..ef3fcef --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/BanCommand.java @@ -0,0 +1,68 @@ +package wtf.beatrice.hidekobot.commands.message; + +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.IMentionable; +import net.dv8tion.jda.api.entities.Mentions; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.requests.restaction.AuditableRestAction; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import wtf.beatrice.hidekobot.Cache; +import wtf.beatrice.hidekobot.HidekoBot; +import wtf.beatrice.hidekobot.commands.base.UserPunishment; +import wtf.beatrice.hidekobot.objects.MessageResponse; +import wtf.beatrice.hidekobot.objects.commands.CommandCategory; +import wtf.beatrice.hidekobot.objects.commands.MessageCommand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +public class BanCommand implements MessageCommand +{ + + @Override + public LinkedList getCommandLabels() { + return new LinkedList<>(Collections.singletonList("ban")); + } + + @Nullable + @Override + public List getPermissions() { + return new ArrayList(Collections.singletonList(Permission.BAN_MEMBERS)); + } + + @Override + public boolean passRawArgs() { + return false; + } + + @NotNull + @Override + public CommandCategory getCategory() { + return CommandCategory.MODERATION; + } + + @NotNull + @Override + public String getDescription() { + return "Ban the mentioned user."; + } + + @Nullable + @Override + public String getUsage() { + return " [reason]"; + } + + @Override + public void runCommand(MessageReceivedEvent event, String label, String[] args) + { + UserPunishment.handle(event, args, UserPunishment.PunishmentType.BAN); + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/InviteCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/InviteCommand.java index 1f95066..53bb1f3 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/InviteCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/InviteCommand.java @@ -69,9 +69,7 @@ public class InviteCommand implements MessageCommand .addActionRow(inviteButton) .queue(); event.getMessage().addReaction(Emoji.fromUnicode("✅")).queue(); - }, (error) -> { - event.getMessage().addReaction(Emoji.fromUnicode("❌")).queue(); - }); + }, error -> event.getMessage().addReaction(Emoji.fromUnicode("❌")).queue()); } else { event.getMessage() .replyEmbeds(inviteEmbed) diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/KickCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/KickCommand.java index c575168..ae9ff7a 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/KickCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/KickCommand.java @@ -12,6 +12,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import wtf.beatrice.hidekobot.Cache; import wtf.beatrice.hidekobot.HidekoBot; +import wtf.beatrice.hidekobot.commands.base.UserPunishment; +import wtf.beatrice.hidekobot.objects.MessageResponse; import wtf.beatrice.hidekobot.objects.commands.CommandCategory; import wtf.beatrice.hidekobot.objects.commands.MessageCommand; @@ -60,87 +62,6 @@ public class KickCommand implements MessageCommand @Override public void runCommand(MessageReceivedEvent event, String label, String[] args) { - if(!(event.getChannel() instanceof TextChannel)) - { - // todo nicer looking with emojis - event.getMessage().reply("Sorry! I can't kick people in DMs.").queue(); - return; - } - - Mentions msgMentions = event.getMessage().getMentions(); - List mentions = msgMentions.getMentions(); - - if(args.length == 0 ||mentions.isEmpty()) - { - // todo nicer looking with emojis - event.getMessage().reply("You have to tell me who to kick!").queue(); - return; - } - - String mentionedId = mentions.get(0).getId(); - User mentioned = null; - - try { - mentioned = HidekoBot.getAPI().retrieveUserById(mentionedId).complete(); - } catch (Exception e) - { - // todo nicer looking with emojis - event.getMessage().reply("I can't kick that user!").queue(); - return; - } - - StringBuilder reasonBuilder = new StringBuilder();; - String reason = ""; - if(args.length > 1) - { - for(int i = 1; i < args.length; i++) - { - String arg = args[i]; - reasonBuilder.append(arg); - - if(i + 1 != arg.length()) - reasonBuilder.append(" "); // separate args with a space except on last iteration. - } - - reason = reasonBuilder.toString(); - } - - if(mentioned == null) - { - // todo nicer looking with emojis - event.getMessage().reply("I can't kick that user!").queue(); - return; - } - - AuditableRestAction kickAction = event.getGuild().kick(mentioned); - if(!reason.isEmpty() && !reasonBuilder.isEmpty()) kickAction.reason(reason); - - final String finalReason = reason; - final IMentionable finalMentioned = mentioned; - - kickAction.queue(success -> { - String embedReason = finalReason; - - EmbedBuilder embedBuilder = new EmbedBuilder(); - - embedBuilder.setAuthor(event.getAuthor().getAsTag(), null,event.getAuthor().getAvatarUrl()); - embedBuilder.setColor(Cache.getBotColor()); - embedBuilder.setTitle("User kicked"); - - embedBuilder.addField("\uD83D\uDC64 User", finalMentioned.getAsMention(), true); - embedBuilder.addField("✂️ By", event.getAuthor().getAsMention(), true); - - if(embedReason.isEmpty()) - embedReason = "*No reason specified*"; - - embedBuilder.addField("\uD83D\uDCD6 Reason", embedReason, false); - - - event.getMessage().replyEmbeds(embedBuilder.build()).queue(); - }, throwable -> { - // todo nicer looking with emojis - event.getMessage().reply("Sorry, I couldn't kick " + finalMentioned.getAsMention() + "!").queue(); - }); - + UserPunishment.handle(event, args, UserPunishment.PunishmentType.KICK); } } diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/LoveCalculatorCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/LoveCalculatorCommand.java index 9152145..de5318b 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/LoveCalculatorCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/LoveCalculatorCommand.java @@ -39,7 +39,7 @@ public class LoveCalculatorCommand extends SlashCommandImpl OptionMapping firsUserArg = event.getOption("first"); if(firsUserArg != null) { - firstUser = firsUserArg.getAsUser(); + firstUser = firsUserArg.getAsUser(); //todo null check? } else { event.reply("\uD83D\uDE22 I need to know who to check! Please mention them.") .setEphemeral(true) @@ -50,7 +50,7 @@ public class LoveCalculatorCommand extends SlashCommandImpl OptionMapping secondUserArg = event.getOption("second"); if(secondUserArg != null) { - secondUser = secondUserArg.getAsUser(); + secondUser = secondUserArg.getAsUser(); //todo null check? } else { secondUser = event.getUser(); } diff --git a/src/main/java/wtf/beatrice/hidekobot/util/CommandUtil.java b/src/main/java/wtf/beatrice/hidekobot/util/CommandUtil.java index 316a329..5d25459 100644 --- a/src/main/java/wtf/beatrice/hidekobot/util/CommandUtil.java +++ b/src/main/java/wtf/beatrice/hidekobot/util/CommandUtil.java @@ -230,8 +230,6 @@ public class CommandUtil databaseSource.untrackExpiredMessage(messageId); }, - (error) -> { - databaseSource.untrackExpiredMessage(messageId); - }); + error -> databaseSource.untrackExpiredMessage(messageId)); } }