From 207b8857e866886923a53d1500c00e09e4bf0180 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Fri, 2 Sep 2022 07:12:22 -0400 Subject: [PATCH] v11 --- .../tylermurphy/ken/command/Responder.java | 3 + .../tylermurphy/ken/command/main/Help.java | 13 ++-- .../ken/command/moderation/Ban.java | 45 +++++-------- .../ken/command/moderation/History.java | 66 ++++++++++++------- .../ken/command/moderation/Kick.java | 45 +++++-------- .../ken/command/moderation/Mute.java | 48 +++++--------- .../ken/command/moderation/TempBan.java | 47 +++++-------- .../ken/command/moderation/UnBan.java | 30 ++++----- .../ken/command/moderation/UnMute.java | 27 ++++---- .../tylermurphy/ken/command/music/Queue.java | 12 ++-- .../tylermurphy/ken/command/nsfw/nHentai.java | 15 +++-- .../ken/command/selfrole/Roles.java | 13 ++-- .../ken/database/ModerationTable.java | 39 ++++++----- .../java/net/tylermurphy/ken/util/Checks.java | 24 +++++++ .../ken/util/ModerationChecker.java | 17 ++--- 15 files changed, 214 insertions(+), 230 deletions(-) diff --git a/src/main/java/net/tylermurphy/ken/command/Responder.java b/src/main/java/net/tylermurphy/ken/command/Responder.java index 38b78be..62a386d 100644 --- a/src/main/java/net/tylermurphy/ken/command/Responder.java +++ b/src/main/java/net/tylermurphy/ken/command/Responder.java @@ -198,6 +198,7 @@ public class Responder extends ListenerAdapter { .setTitle(":x: **Error**") .setDescription(e.getCause().getMessage()); event.getHook().editOriginalEmbeds(builder.build()).queue(); + event.getHook().editOriginalComponents(new ArrayList<>()).queue(); } } @@ -246,6 +247,7 @@ public class Responder extends ListenerAdapter { .setTitle(":x: **Error**") .setDescription(e.getCause().getMessage()); event.getHook().sendMessageEmbeds(builder.build()).queue(); + event.getHook().editOriginalComponents(new ArrayList<>()).queue(); } } @@ -276,6 +278,7 @@ public class Responder extends ListenerAdapter { .setColor(Color.RED) .setDescription(response.getMessage()); hook.sendMessageEmbeds(builder.build()).queue(); + hook.editOriginalComponents(new ArrayList<>()).queue(); } else if(response.isDelete()) { hook.deleteOriginal().queue(); } else { diff --git a/src/main/java/net/tylermurphy/ken/command/main/Help.java b/src/main/java/net/tylermurphy/ken/command/main/Help.java index 59c69b8..3746ffc 100644 --- a/src/main/java/net/tylermurphy/ken/command/main/Help.java +++ b/src/main/java/net/tylermurphy/ken/command/main/Help.java @@ -3,11 +3,11 @@ package net.tylermurphy.ken.command.main; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Message; -import net.dv8tion.jda.api.entities.MessageEmbed; import net.tylermurphy.ken.Ken; import net.tylermurphy.ken.command.annotation.ButtonCallback; import net.tylermurphy.ken.command.annotation.Command; import net.tylermurphy.ken.command.Response; +import net.tylermurphy.ken.util.Checks; public class Help { @@ -18,13 +18,12 @@ public class Help { @ButtonCallback(name="help") public Response onButton(String id, Message message, Member sender){ - int page; + + try { Checks.hasPagedEmbed(message); } + catch (RuntimeException e) { return Response.error(e.getMessage()); } + + int page = Integer.parseInt(message.getEmbeds().get(0).getFooter().getText().split("/")[0].substring(5)); int pages = pageEmbeds.length; - if (message != null && message.getEmbeds().size() > 0 && message.getEmbeds().get(0) != null) { - page = Integer.parseInt(message.getEmbeds().get(0).getFooter().getText().split("/")[0].substring(5)); - } else { - page = 0; - } if(id.equals("previous")) page = page - 1 < 1 ? pages : page - 1; else diff --git a/src/main/java/net/tylermurphy/ken/command/moderation/Ban.java b/src/main/java/net/tylermurphy/ken/command/moderation/Ban.java index 5262afd..22ca558 100644 --- a/src/main/java/net/tylermurphy/ken/command/moderation/Ban.java +++ b/src/main/java/net/tylermurphy/ken/command/moderation/Ban.java @@ -14,6 +14,7 @@ import net.tylermurphy.ken.command.annotation.Command; import net.tylermurphy.ken.command.annotation.Option; import net.tylermurphy.ken.command.annotation.Requirement; import net.tylermurphy.ken.database.ModerationTable; +import net.tylermurphy.ken.database.UserTable; import net.tylermurphy.ken.util.Checks; import org.json.JSONArray; import org.json.JSONObject; @@ -33,31 +34,15 @@ public class Ban { Member target = (Member) args.get(0); String reason = (String) args.get(1); boolean purge = (boolean) args.get(2); - if(target == sender){ - return Response.error("You are not allowed to do this to yourself"); - } - Role low = Checks.getHighestRole(target); - Role high = Checks.getHighestRole(sender); - if(!Checks.getRolePermission(high, low)){ - return Response.error("You need a higher role than the target"); - } - Role self = Checks.getHighestRole(guild.getSelfMember()); - if(!Checks.getRolePermission(self, low)){ - return Response.error("I need a higher role than the target"); - } - if(guild.getOwner() == target){ - return Response.error("You cannot to this to the server owner"); - } + + try { Checks.hasPermission(sender, target); } + catch (RuntimeException e) { return Response.error(e.getMessage()); } try { guild.retrieveBan(target).complete(); return Response.error("User is already banned from this server"); } catch (ErrorResponseException ignored) {} - ModerationTable table = Ken.getInstance().getDatabase().getModerationTable(); - String data = table.getData(guild.getIdLong(), target.getUser().getIdLong()); - JSONObject json = table.updateData(data); - try { guild.removeTimeout(target).queue(); } catch (Exception ignored){} @@ -68,19 +53,19 @@ public class Ban { return Response.error("Failed to ban user"); } - JSONObject status = json.getJSONObject("status"); - status.put("type", "ban"); - status.put("reason", reason); - status.put("until", 0L); - json.put("status", status); - + ModerationTable table = Ken.getInstance().getDatabase().getModerationTable(); + UserTable userTable = Ken.getInstance().getDatabase().getUserTable(); + JSONObject json = table.getData(guild.getIdLong(), target.getUser().getIdLong()); JSONArray history = json.getJSONArray("history"); - history.put(new JSONObject().put("type", "ban").put("reason", reason).put("date",new Date().getTime()).put("by", sender.getIdLong())); - json.put("history", history); + history.put(new JSONObject() + .put("type", "ban") + .put("reason", reason) + .put("date",new Date().getTime()) + .put("by", sender.getIdLong()) + ); - table.setData(guild.getIdLong(), target.getUser().getIdLong(), json.toString()); - - Ken.getInstance().getDatabase().getUserTable().setData(target.getIdLong(), target.getUser().getName(), Integer.parseInt(target.getUser().getDiscriminator())); + table.setData(guild.getIdLong(), target.getUser().getIdLong(), history, "ban", 0L); + userTable.setData(target.getIdLong(), target.getUser().getName(), Integer.parseInt(target.getUser().getDiscriminator())); PrivateChannel channel = target.getUser().openPrivateChannel().complete(); if(channel != null){ diff --git a/src/main/java/net/tylermurphy/ken/command/moderation/History.java b/src/main/java/net/tylermurphy/ken/command/moderation/History.java index 7a360b3..c0a91c5 100644 --- a/src/main/java/net/tylermurphy/ken/command/moderation/History.java +++ b/src/main/java/net/tylermurphy/ken/command/moderation/History.java @@ -11,12 +11,16 @@ import net.tylermurphy.ken.command.annotation.Command; import net.tylermurphy.ken.command.annotation.Option; import net.tylermurphy.ken.command.annotation.Requirement; import net.tylermurphy.ken.database.ModerationTable; +import net.tylermurphy.ken.util.Checks; import org.json.JSONArray; import org.json.JSONObject; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Date; import java.util.List; +import java.util.Objects; +import java.util.concurrent.TimeUnit; public class History { @@ -39,25 +43,25 @@ public class History { } ModerationTable table = Ken.getInstance().getDatabase().getModerationTable(); - String data = table.getData(guild.getIdLong(), target); - JSONObject json = table.updateData(data); + JSONObject json = table.getData(guild.getIdLong(), target); JSONArray history = json.getJSONArray("history"); - int page = 1; - int pages = (history.length()-1)/4+1; - - return Response.success(generateEmbed(history, page, username, discriminator)).addSecondaryButton("history", "previous", "Previous").addSecondaryButton("history", "next", "Next"); + return Response.success(generateEmbed(history, 1, username, discriminator)).addSecondaryButton("history", "previous", "Previous").addSecondaryButton("history", "next", "Next"); } @ButtonCallback(name="history") public Response onButton(String id, Message message, Guild guild){ - if(message == null || message.getEmbeds().size() < 1){ - return Response.error("Message missing"); - } + + try { Checks.hasPagedEmbed(message); } + catch (RuntimeException e) { return Response.error(e.getMessage()); } MessageEmbed temp = message.getEmbeds().get(0); - String username = temp.getDescription().substring(6).split("#")[0]; - String discriminator = temp.getDescription().substring(6).split("#")[1]; + String description = temp.getDescription(); + assert description != null; + int stop = description.indexOf('\n'); + if(stop == -1) stop = description.length(); + String username = description.substring(6, stop).split("#")[0]; + String discriminator = description.substring(6, stop).split("#")[1]; long target = Ken.getInstance().getDatabase().getUserTable().getData(username, Integer.parseInt(discriminator)); if(target == 0L || temp.getFooter() == null) { @@ -65,8 +69,7 @@ public class History { } ModerationTable table = Ken.getInstance().getDatabase().getModerationTable(); - String data = table.getData(guild.getIdLong(), target); - JSONObject json = table.updateData(data); + JSONObject json = table.getData(guild.getIdLong(), target); JSONArray history = json.getJSONArray("history"); int page = Integer.parseInt(temp.getFooter().getText().split("/")[0].substring(5)); @@ -86,7 +89,7 @@ public class History { .setDescription("User: "+username+"#"+discriminator) .setFooter("Page "+page+"/"+pages); if(history.length() < 1){ - builder.appendDescription("User has no moderation history"); + builder.appendDescription("\nUser has no moderation history"); } else { for(int i =(page-1)*4; i "Banned"; - case "temp-ban" -> "Temp banned"; + case "tempban" -> "Temp banned"; case "unban" -> "Unbanned"; case "kick" -> "Kicked"; case "mute" -> "Muted"; case "unmute" -> "Unmuted"; default -> throw new RuntimeException("Unknown value: " + type); }; - SimpleDateFormat format = new SimpleDateFormat("MMM d, y h:ma zz"); - String date = status.has("until") ? - "Until: " + format.format(new Date(status.getLong("until"))) : - "Date: " + format.format(new Date(status.getLong("date"))); - String by = "By: <@"+status.getLong("by")+">"; - return new MessageEmbed.Field(name, reason+"\n"+date+"\n"+by, false); + builder.append("Reason: ") + .append(status.getString("reason")) + .append("\n") + .append("Date: ") + .append(format.format(new Date(status.getLong("date")))) + .append("\n") + .append("By: <@") + .append(status.getLong("by")) + .append(">\n"); + + if(status.has("duration")){ + Date end = new Date(status.getLong("duration")); + Date start = new Date(status.getLong("date")); + long diff = Math.abs(end.getTime() - start.getTime())+60000; + long days = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS); + long buffer = TimeUnit.MILLISECONDS.convert(days, TimeUnit.DAYS); + long hours = TimeUnit.HOURS.convert(diff-buffer, TimeUnit.MILLISECONDS); + builder.append("Duration: "); + if(days/30 > 0) builder.append(days/30).append("m"); + if(days%30 > 0) builder.append(days%30).append("d"); + if(hours > 0) builder.append(hours).append("h"); + } + + return new MessageEmbed.Field(name, builder.toString(), false); } } diff --git a/src/main/java/net/tylermurphy/ken/command/moderation/Kick.java b/src/main/java/net/tylermurphy/ken/command/moderation/Kick.java index 3dae4eb..9822d23 100644 --- a/src/main/java/net/tylermurphy/ken/command/moderation/Kick.java +++ b/src/main/java/net/tylermurphy/ken/command/moderation/Kick.java @@ -14,6 +14,7 @@ import net.tylermurphy.ken.command.annotation.Command; import net.tylermurphy.ken.command.annotation.Option; import net.tylermurphy.ken.command.annotation.Requirement; import net.tylermurphy.ken.database.ModerationTable; +import net.tylermurphy.ken.database.UserTable; import net.tylermurphy.ken.util.Checks; import org.json.JSONArray; import org.json.JSONObject; @@ -31,31 +32,15 @@ public class Kick { public Response execute(Member sender, List args, Guild guild){ Member target = (Member) args.get(0); String reason = (String) args.get(1); - if(target == sender){ - return Response.error("You are not allowed to do this to yourself"); - } - Role low = Checks.getHighestRole(target); - Role high = Checks.getHighestRole(sender); - if(!Checks.getRolePermission(high, low)){ - return Response.error("You need a higher role than the target"); - } - Role self = Checks.getHighestRole(guild.getSelfMember()); - if(!Checks.getRolePermission(self, low)){ - return Response.error("I need a higher role than the target"); - } - if(guild.getOwner() == target){ - return Response.error("You cannot to this to the server owner"); - } + + try { Checks.hasPermission(sender, target); } + catch (RuntimeException e) { return Response.error(e.getMessage()); } try { guild.retrieveBan(target).complete(); return Response.error("User is currently banned and cannot be kicked"); } catch (ErrorResponseException ignored) {} - ModerationTable table = Ken.getInstance().getDatabase().getModerationTable(); - String data = table.getData(guild.getIdLong(), target.getUser().getIdLong()); - JSONObject json = table.updateData(data); - try { guild.removeTimeout(target).queue(); } catch (Exception ignored){} @@ -66,19 +51,19 @@ public class Kick { return Response.error("Failed to kick user"); } - JSONObject status = json.getJSONObject("status"); - status.put("type", "None"); - status.put("reason", ""); - status.put("until", 0L); - json.put("status", status); - + ModerationTable table = Ken.getInstance().getDatabase().getModerationTable(); + UserTable userTable = Ken.getInstance().getDatabase().getUserTable(); + JSONObject json = table.getData(guild.getIdLong(), target.getUser().getIdLong()); JSONArray history = json.getJSONArray("history"); - history.put(new JSONObject().put("type", "kick").put("reason", reason).put("date",new Date().getTime()).put("by", sender.getIdLong())); - json.put("history", history); + history.put(new JSONObject() + .put("type", "kick") + .put("reason", reason) + .put("date",new Date().getTime()) + .put("by", sender.getIdLong()) + ); - table.setData(guild.getIdLong(), target.getUser().getIdLong(), json.toString()); - - Ken.getInstance().getDatabase().getUserTable().setData(target.getIdLong(), target.getUser().getName(), Integer.parseInt(target.getUser().getDiscriminator())); + table.setData(guild.getIdLong(), target.getUser().getIdLong(), history, "None", 0L); + userTable.setData(target.getIdLong(), target.getUser().getName(), Integer.parseInt(target.getUser().getDiscriminator())); PrivateChannel channel = target.getUser().openPrivateChannel().complete(); if(channel != null){ diff --git a/src/main/java/net/tylermurphy/ken/command/moderation/Mute.java b/src/main/java/net/tylermurphy/ken/command/moderation/Mute.java index 0d32832..3b36c5e 100644 --- a/src/main/java/net/tylermurphy/ken/command/moderation/Mute.java +++ b/src/main/java/net/tylermurphy/ken/command/moderation/Mute.java @@ -14,6 +14,7 @@ import net.tylermurphy.ken.command.annotation.Command; import net.tylermurphy.ken.command.annotation.Option; import net.tylermurphy.ken.command.annotation.Requirement; import net.tylermurphy.ken.database.ModerationTable; +import net.tylermurphy.ken.database.UserTable; import net.tylermurphy.ken.util.Checks; import org.json.JSONArray; import org.json.JSONObject; @@ -36,24 +37,14 @@ public class Mute { Member target = (Member) args.get(0); String timeString = (String) args.get(1); String reason = (String) args.get(2); - if(target == sender){ - return Response.error("You are not allowed to do this to yourself"); - } - Role low = Checks.getHighestRole(target); - Role high = Checks.getHighestRole(sender); - if(!Checks.getRolePermission(high, low)){ - return Response.error("You need a higher role than the target"); - } - Role self = Checks.getHighestRole(guild.getSelfMember()); - if(!Checks.getRolePermission(self, low)){ - return Response.error("I need a higher role than the target"); - } - if(guild.getOwner() == target){ - return Response.error("You cannot to this to the server owner"); - } + + try { Checks.hasPermission(sender, target); } + catch (RuntimeException e) { return Response.error(e.getMessage()); } + if(target.isTimedOut()){ return Response.error("User is already muted"); } + long time = Checks.convertTime(timeString); if(time == 0L){ return Response.error("Time to ban cannot be 0"); @@ -65,29 +56,26 @@ public class Mute { SimpleDateFormat format = new SimpleDateFormat("MMM d, y h:ma zz"); String date = format.format(time); - ModerationTable table = Ken.getInstance().getDatabase().getModerationTable(); - String data = table.getData(guild.getIdLong(), target.getUser().getIdLong()); - JSONObject json = table.updateData(data); - try { guild.timeoutUntil(target, new Date(time).toInstant()).queue(); } catch (Exception e){ return Response.error("Failed to mute user"); } - JSONObject status = json.getJSONObject("status"); - status.put("type", "mute"); - status.put("reason", reason); - status.put("until", time); - json.put("status", status); - + ModerationTable table = Ken.getInstance().getDatabase().getModerationTable(); + UserTable userTable = Ken.getInstance().getDatabase().getUserTable(); + JSONObject json = table.getData(guild.getIdLong(), target.getUser().getIdLong()); JSONArray history = json.getJSONArray("history"); - history.put(new JSONObject().put("type", "mute").put("reason", reason).put("date",new Date().getTime()).put("by", sender.getIdLong())); - json.put("history", history); + history.put(new JSONObject() + .put("type", "mute") + .put("reason", reason) + .put("date",new Date().getTime()) + .put("by", sender.getIdLong()) + .put("duration", time) + ); - table.setData(guild.getIdLong(), target.getUser().getIdLong(), json.toString()); - - Ken.getInstance().getDatabase().getUserTable().setData(target.getIdLong(), target.getUser().getName(), Integer.parseInt(target.getUser().getDiscriminator())); + table.setData(guild.getIdLong(), target.getUser().getIdLong(), history, "mute", time); + userTable.setData(target.getIdLong(), target.getUser().getName(), Integer.parseInt(target.getUser().getDiscriminator())); PrivateChannel channel = target.getUser().openPrivateChannel().complete(); if(channel != null){ diff --git a/src/main/java/net/tylermurphy/ken/command/moderation/TempBan.java b/src/main/java/net/tylermurphy/ken/command/moderation/TempBan.java index 00780fc..dcab6bb 100644 --- a/src/main/java/net/tylermurphy/ken/command/moderation/TempBan.java +++ b/src/main/java/net/tylermurphy/ken/command/moderation/TempBan.java @@ -14,6 +14,7 @@ import net.tylermurphy.ken.command.annotation.Command; import net.tylermurphy.ken.command.annotation.Option; import net.tylermurphy.ken.command.annotation.Requirement; import net.tylermurphy.ken.database.ModerationTable; +import net.tylermurphy.ken.database.UserTable; import net.tylermurphy.ken.util.Checks; import org.json.JSONArray; import org.json.JSONObject; @@ -36,21 +37,10 @@ public class TempBan { String timeString = (String) args.get(1); String reason = (String) args.get(2); boolean purge = (boolean) args.get(3); - if(target == sender){ - return Response.error("You are not allowed to do this to yourself"); - } - Role low = Checks.getHighestRole(target); - Role high = Checks.getHighestRole(sender); - if(!Checks.getRolePermission(high, low)){ - return Response.error("You need a higher role than the target"); - } - Role self = Checks.getHighestRole(guild.getSelfMember()); - if(!Checks.getRolePermission(self, low)){ - return Response.error("I need a higher role than the target"); - } - if(guild.getOwner() == target){ - return Response.error("You cannot to this to the server owner"); - } + + try { Checks.hasPermission(sender, target); } + catch (RuntimeException e) { return Response.error(e.getMessage()); } + try { guild.retrieveBan(target).complete(); return Response.error("User is already banned from this server"); @@ -62,10 +52,6 @@ public class TempBan { SimpleDateFormat format = new SimpleDateFormat("MMM d, y h:ma zz"); String date = format.format(time); - ModerationTable table = Ken.getInstance().getDatabase().getModerationTable(); - String data = table.getData(guild.getIdLong(), target.getUser().getIdLong()); - JSONObject json = table.updateData(data); - try { guild.removeTimeout(target).queue(); } catch (Exception ignored){} @@ -76,19 +62,20 @@ public class TempBan { return Response.error("Failed to temp ban user"); } - JSONObject status = json.getJSONObject("status"); - status.put("type", "temp-ban"); - status.put("reason", reason); - status.put("until", time); - json.put("status", status); - + ModerationTable table = Ken.getInstance().getDatabase().getModerationTable(); + UserTable userTable = Ken.getInstance().getDatabase().getUserTable(); + JSONObject json = table.getData(guild.getIdLong(), target.getUser().getIdLong()); JSONArray history = json.getJSONArray("history"); - history.put(new JSONObject().put("type", "temp-ban").put("reason", reason).put("date",new Date().getTime()).put("by", sender.getIdLong())); - json.put("history", history); + history.put(new JSONObject() + .put("type", "tempban") + .put("reason", reason) + .put("date",new Date().getTime()) + .put("by", sender.getIdLong()) + .put("duration", time) + ); - table.setData(guild.getIdLong(), target.getUser().getIdLong(), json.toString()); - - Ken.getInstance().getDatabase().getUserTable().setData(target.getIdLong(), target.getUser().getName(), Integer.parseInt(target.getUser().getDiscriminator())); + table.setData(guild.getIdLong(), target.getUser().getIdLong(), history, "tempban", time); + userTable.setData(target.getIdLong(), target.getUser().getName(), Integer.parseInt(target.getUser().getDiscriminator())); PrivateChannel channel = target.getUser().openPrivateChannel().complete(); if(channel != null){ diff --git a/src/main/java/net/tylermurphy/ken/command/moderation/UnBan.java b/src/main/java/net/tylermurphy/ken/command/moderation/UnBan.java index 0628131..84bce04 100644 --- a/src/main/java/net/tylermurphy/ken/command/moderation/UnBan.java +++ b/src/main/java/net/tylermurphy/ken/command/moderation/UnBan.java @@ -11,6 +11,7 @@ import net.tylermurphy.ken.command.annotation.Command; import net.tylermurphy.ken.command.annotation.Option; import net.tylermurphy.ken.command.annotation.Requirement; import net.tylermurphy.ken.database.ModerationTable; +import net.tylermurphy.ken.database.UserTable; import org.json.JSONArray; import org.json.JSONObject; @@ -28,12 +29,11 @@ public class UnBan { public Response execute(Member sender, Guild guild, List args){ String username = (String) args.get(0); String discriminator = String.valueOf(args.get(1)); + String reason = (String) args.get(2); if(discriminator.length() != 4) { return Response.error("Invalid discriminator"); } - - String reason = (String) args.get(2); User target = null; for(Guild.Ban ban : guild.retrieveBanList()){ @@ -58,29 +58,25 @@ public class UnBan { return Response.error("User is not banned from this server"); } - ModerationTable table = Ken.getInstance().getDatabase().getModerationTable(); - String data = table.getData(guild.getIdLong(), target.getIdLong()); - JSONObject json = table.updateData(data); - try { guild.unban(target).queue(); } catch (Exception e){ return Response.error("Failed to unban user"); } - JSONObject status = json.getJSONObject("status"); - status.put("type", "None"); - status.put("reason", ""); - status.put("until", 0L); - json.put("status", status); - + ModerationTable table = Ken.getInstance().getDatabase().getModerationTable(); + UserTable userTable = Ken.getInstance().getDatabase().getUserTable(); + JSONObject json = table.getData(guild.getIdLong(), target.getIdLong()); JSONArray history = json.getJSONArray("history"); - history.put(new JSONObject().put("type", "unban").put("reason", reason).put("date",new Date().getTime()).put("by", sender.getIdLong())); - json.put("history", history); + history.put(new JSONObject() + .put("type", "unban") + .put("reason", reason) + .put("date",new Date().getTime()) + .put("by", sender.getIdLong()) + ); - table.setData(guild.getIdLong(), target.getIdLong(), json.toString()); - - Ken.getInstance().getDatabase().getUserTable().setData(target.getIdLong(), target.getName(), Integer.parseInt(target.getDiscriminator())); + table.setData(guild.getIdLong(), target.getIdLong(), history, "None", 0L); + userTable.setData(target.getIdLong(), target.getName(), Integer.parseInt(target.getDiscriminator())); PrivateChannel channel = target.openPrivateChannel().complete(); if(channel != null){ diff --git a/src/main/java/net/tylermurphy/ken/command/moderation/UnMute.java b/src/main/java/net/tylermurphy/ken/command/moderation/UnMute.java index df60a8d..abd34b2 100644 --- a/src/main/java/net/tylermurphy/ken/command/moderation/UnMute.java +++ b/src/main/java/net/tylermurphy/ken/command/moderation/UnMute.java @@ -10,6 +10,7 @@ import net.tylermurphy.ken.command.annotation.Command; import net.tylermurphy.ken.command.annotation.Option; import net.tylermurphy.ken.command.annotation.Requirement; import net.tylermurphy.ken.database.ModerationTable; +import net.tylermurphy.ken.database.UserTable; import org.json.JSONArray; import org.json.JSONObject; @@ -35,29 +36,25 @@ public class UnMute { return Response.error("User is not muted"); } - ModerationTable table = Ken.getInstance().getDatabase().getModerationTable(); - String data = table.getData(guild.getIdLong(), target.getIdLong()); - JSONObject json = table.updateData(data); - try { guild.removeTimeout(target).queue(); } catch (Exception e){ return Response.error("Failed to unmute user"); } - JSONObject status = json.getJSONObject("status"); - status.put("type", "None"); - status.put("reason", ""); - status.put("until", 0L); - json.put("status", status); - + ModerationTable table = Ken.getInstance().getDatabase().getModerationTable(); + UserTable userTable = Ken.getInstance().getDatabase().getUserTable(); + JSONObject json = table.getData(guild.getIdLong(), target.getUser().getIdLong()); JSONArray history = json.getJSONArray("history"); - history.put(new JSONObject().put("type", "unmute").put("reason", reason).put("date",new Date().getTime()).put("by", sender.getIdLong())); - json.put("history", history); + history.put(new JSONObject() + .put("type", "unmute") + .put("reason", reason) + .put("date",new Date().getTime()) + .put("by", sender.getIdLong()) + ); - table.setData(guild.getIdLong(), target.getIdLong(), json.toString()); - - Ken.getInstance().getDatabase().getUserTable().setData(target.getIdLong(), target.getUser().getName(), Integer.parseInt(target.getUser().getDiscriminator())); + table.setData(guild.getIdLong(), target.getUser().getIdLong(), history, "None", 0L); + userTable.setData(target.getIdLong(), target.getUser().getName(), Integer.parseInt(target.getUser().getDiscriminator())); PrivateChannel channel = target.getUser().openPrivateChannel().complete(); if(channel != null){ diff --git a/src/main/java/net/tylermurphy/ken/command/music/Queue.java b/src/main/java/net/tylermurphy/ken/command/music/Queue.java index 874aa1c..36f0763 100644 --- a/src/main/java/net/tylermurphy/ken/command/music/Queue.java +++ b/src/main/java/net/tylermurphy/ken/command/music/Queue.java @@ -12,6 +12,7 @@ import net.tylermurphy.ken.command.annotation.Command; import net.tylermurphy.ken.command.Response; import net.tylermurphy.ken.music.GuildMusicManager; import net.tylermurphy.ken.music.PlayerManager; +import net.tylermurphy.ken.util.Checks; import java.util.ArrayList; import java.util.List; @@ -35,13 +36,12 @@ public class Queue { PlayerManager playerManager = Ken.getInstance().getPlayerManager(); GuildMusicManager musicManager = playerManager.getGuildMusicManager(guild); BlockingQueue queue = musicManager.scheduler.getQueue(); - int page; + + try { Checks.hasPagedEmbed(message); } + catch (RuntimeException e) { return Response.error(e.getMessage()); } + + int page = Integer.parseInt(message.getEmbeds().get(0).getFooter().getText().split("/")[0].substring(5)); int pages = (queue.size()-1)/10+1; - if (message != null && message.getEmbeds().size() > 0 && message.getEmbeds().get(0) != null) { - page = Integer.parseInt(message.getEmbeds().get(0).getFooter().getText().split("/")[0].substring(5)); - } else { - page = 0; - } if(id.equals("previous")) page = page - 1 < 1 ? pages : page - 1; else diff --git a/src/main/java/net/tylermurphy/ken/command/nsfw/nHentai.java b/src/main/java/net/tylermurphy/ken/command/nsfw/nHentai.java index 776e470..4e8e138 100644 --- a/src/main/java/net/tylermurphy/ken/command/nsfw/nHentai.java +++ b/src/main/java/net/tylermurphy/ken/command/nsfw/nHentai.java @@ -12,6 +12,7 @@ import net.tylermurphy.ken.command.Response; import net.tylermurphy.ken.command.annotation.ButtonCallback; import net.tylermurphy.ken.command.annotation.Command; import net.tylermurphy.ken.command.annotation.Option; +import net.tylermurphy.ken.util.Checks; import org.json.JSONArray; import org.json.JSONObject; @@ -51,13 +52,13 @@ public class nHentai { @ButtonCallback(name="nhentai") public Response onButton(String button_id, Message message){ - int page, id; - if (message != null && message.getEmbeds().size() > 0 && message.getEmbeds().get(0) != null) { - page = Integer.parseInt(message.getEmbeds().get(0).getFooter().getText().split("/")[0].substring(5)); - id = Integer.parseInt(message.getEmbeds().get(0).getDescription().substring(4)); - } else { - return Response.error("Embed is missing, please resend command"); - } + + try { Checks.hasPagedEmbed(message); } + catch (RuntimeException e) { return Response.error(e.getMessage()); } + + int page = Integer.parseInt(message.getEmbeds().get(0).getFooter().getText().split("/")[0].substring(5)); + int id = Integer.parseInt(message.getEmbeds().get(0).getDescription().substring(4)); + Comic comic = cache.get(id); if(comic == null) comic = request(id+""); if(comic == null) return Response.error("Failed to fetch comic data"); diff --git a/src/main/java/net/tylermurphy/ken/command/selfrole/Roles.java b/src/main/java/net/tylermurphy/ken/command/selfrole/Roles.java index 238a8de..c294d82 100644 --- a/src/main/java/net/tylermurphy/ken/command/selfrole/Roles.java +++ b/src/main/java/net/tylermurphy/ken/command/selfrole/Roles.java @@ -7,6 +7,7 @@ import net.tylermurphy.ken.command.Response; import net.tylermurphy.ken.command.annotation.ButtonCallback; import net.tylermurphy.ken.command.annotation.Command; import net.tylermurphy.ken.command.annotation.SelectMenuCallback; +import net.tylermurphy.ken.util.Checks; import org.json.JSONArray; import org.json.JSONObject; @@ -32,13 +33,11 @@ public class Roles { @ButtonCallback(name="roles") public Response onButton(Guild guild, String id, Message message){ - int page; - if(message == null) return Response.delete(); - if (message.getEmbeds().size() > 0 && message.getEmbeds().get(0) != null) { - page = Integer.parseInt(message.getEmbeds().get(0).getFooter().getText().split("/")[0].substring(5)); - } else { - page = 1; - } + + try { Checks.hasPagedEmbed(message); } + catch (RuntimeException e) { return Response.error(e.getMessage()); } + + int page = Integer.parseInt(message.getEmbeds().get(0).getFooter().getText().split("/")[0].substring(5)); int pages = Ken.getInstance().getDatabase().getSelfRoleData().getPages(guild.getIdLong()); if(id.equals("previous")) page = page - 1 < 1 ? pages : page - 1; diff --git a/src/main/java/net/tylermurphy/ken/database/ModerationTable.java b/src/main/java/net/tylermurphy/ken/database/ModerationTable.java index 6849fae..8a29eca 100644 --- a/src/main/java/net/tylermurphy/ken/database/ModerationTable.java +++ b/src/main/java/net/tylermurphy/ken/database/ModerationTable.java @@ -19,7 +19,8 @@ public class ModerationTable { CREATE TABLE IF NOT EXISTS moderation_data ( guild_id BIGINT NOT NULL, user_id BIGINT NOT NULL, - data TEXT NOT NULL, + history TEXT NOT NULL, + status VARCHAR(10) NOT NULL, until BIGINT NOT NULL, PRIMARY KEY (guild_id,user_id) );"""; @@ -33,27 +34,39 @@ public class ModerationTable { this.database = database; } - public String getData(long guildId, long userId){ + public JSONObject getData(long guildId, long userId){ String sql = "SELECT * FROM moderation_data WHERE guild_id=? AND user_id=?"; try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) { statement.setLong(1, guildId); statement.setLong(2, userId); ResultSet rs = statement.executeQuery(); - if(rs.next()) return rs.getString("data"); - else return null; + if(rs.next()) { + JSONObject data = new JSONObject(); + data.put("history", new JSONArray(rs.getString("history"))); + data.put("status", rs.getString("status")); + data.put("until", rs.getLong("until")); + return data; + } else { + JSONObject data = new JSONObject(); + data.put("history", new JSONArray()); + data.put("status", "None"); + data.put("until", 0L); + return data; + } } catch (SQLException e) { Ken.getInstance().getLogger().error("SQL Error: " + e.getMessage()); return null; } } - public boolean setData(long guildId, long userId, String data) { - String sql = "INSERT OR REPLACE INTO moderation_data (guild_id, user_id, data, until) VALUES(?,?,?,?)"; + public boolean setData(long guildId, long userId, JSONArray history, String status, long until) { + String sql = "INSERT OR REPLACE INTO moderation_data (guild_id, user_id, history, status, until) VALUES(?,?,?,?,?)"; try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) { statement.setLong(1, guildId); statement.setLong(2, userId); - statement.setString(3, data); - statement.setLong(4, new JSONObject(data).getJSONObject("status").getLong("until")); + statement.setString(3, history.toString()); + statement.setString(4, status); + statement.setLong(5, until); return statement.executeUpdate() != 0; } catch (SQLException e) { Ken.getInstance().getLogger().error("SQL Error: " + e.getMessage()); @@ -68,7 +81,7 @@ public class ModerationTable { statement.setLong(1, date); ResultSet rs = statement.executeQuery(); while (rs.next()){ - String[] data = {String.valueOf(rs.getLong("guild_id")), String.valueOf(rs.getLong("user_id")), rs.getString("data")}; + String[] data = {String.valueOf(rs.getLong("guild_id")), String.valueOf(rs.getLong("user_id")), rs.getString("history"), rs.getString("status"), String.valueOf(rs.getLong("until"))}; store.add(data); } } catch (SQLException e) { @@ -77,12 +90,4 @@ public class ModerationTable { store.forEach(callback); } - public JSONObject updateData(String data) { - JSONObject object = data == null ? new JSONObject() : new JSONObject(data); - if(!object.has("history")) object.put("history", new JSONArray()); - if(!object.has("roles")) object.put("roles", new JSONArray()); - if(!object.has("status")) object.put("status", new JSONObject().put("type","None").put("until", 0L).put("reason","")); - return object; - } - } diff --git a/src/main/java/net/tylermurphy/ken/util/Checks.java b/src/main/java/net/tylermurphy/ken/util/Checks.java index 3609b02..83a7078 100644 --- a/src/main/java/net/tylermurphy/ken/util/Checks.java +++ b/src/main/java/net/tylermurphy/ken/util/Checks.java @@ -1,6 +1,8 @@ package net.tylermurphy.ken.util; import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.Role; import java.util.Calendar; @@ -9,6 +11,28 @@ import java.util.List; public class Checks { + public static void hasPagedEmbed(Message message) { + if(message == null || message.getEmbeds().size() < 1) + throw new RuntimeException("Message is missing"); + MessageEmbed temp = message.getEmbeds().get(0); + if(temp == null || temp.getDescription() == null || temp.getFooter() == null) + throw new RuntimeException("Embed is missing"); + } + + public static void hasPermission(Member sender, Member target) { + if(sender == target) + throw new RuntimeException("You are not allowed to do this to yourself"); + Role low = Checks.getHighestRole(target); + Role high = Checks.getHighestRole(sender); + Role self = Checks.getHighestRole(sender.getGuild().getSelfMember()); + if(!Checks.getRolePermission(high, low)) + throw new RuntimeException("You need a higher role than the target"); + if(!Checks.getRolePermission(self, low)) + throw new RuntimeException("I need a higher role than the target"); + if(sender.getGuild().getOwner() == target) + throw new RuntimeException("You cannot to this to the server owner"); + } + public static Role getHighestRole(Member member) { if(member == null) { return null; diff --git a/src/main/java/net/tylermurphy/ken/util/ModerationChecker.java b/src/main/java/net/tylermurphy/ken/util/ModerationChecker.java index 0999e92..e1577c3 100644 --- a/src/main/java/net/tylermurphy/ken/util/ModerationChecker.java +++ b/src/main/java/net/tylermurphy/ken/util/ModerationChecker.java @@ -24,10 +24,10 @@ public class ModerationChecker { Ken.getInstance().getDatabase().getModerationTable().callOverdue(new Date().getTime(), data -> { long guildId = Long.parseLong(data[0]); long userId = Long.parseLong(data[1]); - JSONObject json = new JSONObject(data[2]); - JSONObject status = json.getJSONObject("status"); - String type = status.getString("type"); - if(!type.equals("temp-ban")) return; + JSONArray history = new JSONArray(data[2]); + String status = data[3]; + long until = Long.parseLong(data[4]); + if(!status.equals("tempban")) return; Guild guild = Ken.getInstance().getGuildById(guildId); if(guild == null) return; List bans = guild.retrieveBanList().complete(); @@ -46,15 +46,8 @@ public class ModerationChecker { .appendDescription("`Reason:` Ban period ended"); channel.sendMessageEmbeds(builder.build()).queue(); } - status.put("type", "None"); - status.put("reason", ""); - status.put("until", 0L); - - JSONArray history = json.getJSONArray("history"); history.put(new JSONObject().put("type", "unban").put("reason", "Ban Period Ended").put("date",new Date().getTime()).put("by", guild.getSelfMember().getIdLong())); - json.put("history", history); - json.put("status", status); - Ken.getInstance().getDatabase().getModerationTable().setData(guildId, userId, json.toString()); + Ken.getInstance().getDatabase().getModerationTable().setData(guildId, userId, history, "None", 0L); } catch (ErrorResponseException ignored) {} }); }