From 4c82784344a86c73d4f3704123268c3100f4edd9 Mon Sep 17 00:00:00 2001 From: Hugo Bois Date: Wed, 17 Jan 2024 23:51:21 +0100 Subject: [PATCH] chore: updated JDA library and ClashAPI versions --- pom.xml | 25 +-- .../clashbot/commands/PlayerCommand.java | 14 +- .../clashbot/commands/clan/ClanCommand.java | 31 +-- .../clashbot/commands/clan/WarCommand.java | 178 +++++++++--------- .../commands/clan/WarLeagueCommand.java | 34 ++-- .../clashbot/commands/clan/WarlogCommand.java | 36 ++-- .../clashbot/commands/misc/ClearCommand.java | 7 +- .../clashbot/commands/misc/HelpCommand.java | 8 +- .../clashbot/commands/misc/InfoCommand.java | 16 +- .../clashbot/commands/misc/InviteCommand.java | 7 +- .../clashbot/commands/misc/LangCommand.java | 8 +- .../commands/settings/SetCommand.java | 36 ++-- .../lycoon/clashbot/core/ClashBotMain.java | 47 +++-- .../lycoon/clashbot/core/EventListener.java | 30 ++- .../com/lycoon/clashbot/utils/CoreUtils.java | 34 ++-- .../com/lycoon/clashbot/utils/ErrorUtils.java | 15 +- .../com/lycoon/clashbot/utils/FileUtils.java | 49 ++--- 17 files changed, 300 insertions(+), 275 deletions(-) diff --git a/pom.xml b/pom.xml index 5783bd5..ec4c2ec 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.lycoon clashbot - 1.4.3 + 2.0.0 17 @@ -15,30 +15,17 @@ UTF-8 - - - github - https://maven.pkg.github.com/Lycoon/clash-api - - - - dv8tion - m2-dv8tion - https://m2.dv8tion.net/releases - - - net.dv8tion JDA - 4.3.0_277 + 5.0.0-beta.19 - com.lycoon - clashapi - 3.0.2 + io.github.lycoon + clash-api + 5.1.4 @@ -62,7 +49,7 @@ ch.qos.logback logback-classic - 1.2.7 + 1.4.14 diff --git a/src/main/java/com/lycoon/clashbot/commands/PlayerCommand.java b/src/main/java/com/lycoon/clashbot/commands/PlayerCommand.java index 236a3ab..5a5c6dc 100644 --- a/src/main/java/com/lycoon/clashbot/commands/PlayerCommand.java +++ b/src/main/java/com/lycoon/clashbot/commands/PlayerCommand.java @@ -7,13 +7,13 @@ import static com.lycoon.clashbot.utils.CoreUtils.*; import static com.lycoon.clashbot.utils.GameUtils.*; +import com.lycoon.clashapi.core.exceptions.ClashAPIException; import com.lycoon.clashapi.models.player.Player; import com.lycoon.clashapi.models.player.Troop; -import com.lycoon.clashapi.core.exception.ClashAPIException; import com.lycoon.clashbot.core.CacheComponents; import com.lycoon.clashbot.core.ClashBotMain; import com.lycoon.clashbot.lang.LangUtils; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import java.awt.*; import java.awt.image.BufferedImage; @@ -51,7 +51,7 @@ public class PlayerCommand { "Log Launcher", "Flame Flinger"}; private final static String[] PETS = {"L.A.S.S.I", "Electro Owl", "Mighty Yak", "Unicorn"}; - public static void call(SlashCommandEvent event) { + public static void call(SlashCommandInteractionEvent event) { CompletableFuture.runAsync(() -> { if (event.getOptions().isEmpty()) execute(event); @@ -61,7 +61,7 @@ public static void call(SlashCommandEvent event) { } public static void drawSuperTroop(Graphics2D g2d, Troop troop, String troopName, int x, int y) { - if (troop == null || !troop.isSuperTroopActive()) + if (troop == null || !troop.getSuperTroopIsActive()) g2d.drawImage(getImageFromFile("troops/locked/" + troopName + ".png"), x, y, 44, 44, null); else g2d.drawImage(getImageFromFile("troops/" + troop.getName() + ".png"), x, y, 44, 44, null); @@ -120,7 +120,7 @@ public static void drawPets(Graphics2D g2d, Font font, List pets, int y) drawTroop(g2d, font, getTroopByName(pets, PETS[i]), PETS[i], (i % COLUMNS) * 50 + 710, y + j * 50); } - public static void execute(SlashCommandEvent event, String... args) { + public static void execute(SlashCommandInteractionEvent event, String... args) { Locale lang = LangUtils.getLanguage(event.getMember().getIdLong()); ResourceBundle i18n = LangUtils.getTranslations(lang); NumberFormat nf = NumberFormat.getInstance(lang); @@ -140,7 +140,7 @@ public static void execute(SlashCommandEvent event, String... args) { try { player = ClashBotMain.clashAPI.getPlayer(tag); - } catch (ClashAPIException | IOException e) { + } catch (ClashAPIException e) { sendExceptionError(event, i18n, e, tag, "player"); return; } @@ -196,7 +196,7 @@ public static void execute(SlashCommandEvent event, String... args) { Rectangle clanNameRect = new Rectangle(775, 130, 148, 30); Rectangle clanRoleRect = new Rectangle(775, 151, 148, 30); drawCenteredString(g2d, clanNameRect, font.deriveFont(FONT_SIZE + 2f), player.getClan().getName()); - drawCenteredString(g2d, clanRoleRect, font.deriveFont(FONT_SIZE - 2f), i18n.getString(player.getRole())); + drawCenteredString(g2d, clanRoleRect, font.deriveFont(FONT_SIZE - 2f), i18n.getString(String.valueOf(player.getRole()))); } else { Rectangle noClanRect = new Rectangle(775, 130, 148, 30); drawCenteredString(g2d, noClanRect, font.deriveFont(FONT_SIZE + 2f), i18n.getString("no.clan")); diff --git a/src/main/java/com/lycoon/clashbot/commands/clan/ClanCommand.java b/src/main/java/com/lycoon/clashbot/commands/clan/ClanCommand.java index b1da1cb..c1bf056 100644 --- a/src/main/java/com/lycoon/clashbot/commands/clan/ClanCommand.java +++ b/src/main/java/com/lycoon/clashbot/commands/clan/ClanCommand.java @@ -6,25 +6,26 @@ import static com.lycoon.clashbot.utils.DatabaseUtils.*; import static com.lycoon.clashbot.utils.CoreUtils.*; +import com.lycoon.clashapi.core.exceptions.ClashAPIException; import com.lycoon.clashapi.models.clan.ClanMember; import com.lycoon.clashapi.models.clan.Clan; import com.lycoon.clashapi.models.common.Label; -import com.lycoon.clashapi.core.exception.ClashAPIException; +import com.lycoon.clashapi.models.player.enums.Role; import com.lycoon.clashbot.commands.Command; import com.lycoon.clashbot.core.ClashBotMain; import com.lycoon.clashbot.lang.LangUtils; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import java.awt.*; import java.awt.image.BufferedImage; -import java.io.IOException; import java.text.MessageFormat; import java.text.NumberFormat; import java.util.List; import java.util.*; import java.util.concurrent.CompletableFuture; -public class ClanCommand { +public class ClanCommand +{ private final static int WIDTH = 932; private final static int HEIGHT = 322; private final static float FONT_SIZE = 12f; @@ -66,7 +67,8 @@ public class ClanCommand { put("Clan Capital", "label.capital"); }}; - public static void call(SlashCommandEvent event) { + public static void call(SlashCommandInteractionEvent event) + { CompletableFuture.runAsync(() -> { if (event.getOptions().isEmpty()) execute(event); @@ -75,15 +77,16 @@ public static void call(SlashCommandEvent event) { }); } - public static String getClanChief(List members) { + public static String getClanChief(List members) + { for (ClanMember member : members) - if (member.getRole().equals("leader")) + if (member.getRole() == Role.LEADER) return member.getName(); - return ""; } - public static Clan getClan(SlashCommandEvent event, Locale lang, String[] args) { + public static Clan getClan(SlashCommandInteractionEvent event, Locale lang, String[] args) + { // Checking rate limitation if (!checkThrottle(event, lang)) return null; @@ -100,7 +103,6 @@ public static Clan getClan(SlashCommandEvent event, Locale lang, String[] args) try { clan = ClashBotMain.clashAPI.getClan(tag); - } catch (IOException ignored) { } catch (ClashAPIException e) { sendExceptionError(event, i18n, e, tag, "clan"); return null; @@ -108,7 +110,8 @@ public static Clan getClan(SlashCommandEvent event, Locale lang, String[] args) return clan; } - public static void execute(SlashCommandEvent event, String... args) { + public static void execute(SlashCommandInteractionEvent event, String... args) + { Locale lang = LangUtils.getLanguage(event.getMember().getIdLong()); ResourceBundle i18n = LangUtils.getTranslations(lang); NumberFormat nf = NumberFormat.getInstance(lang); @@ -157,9 +160,9 @@ public static void execute(SlashCommandEvent event, String... args) { drawShadowedStringLeft(g2d, i18n.getString("undefined"), 905, 33, 14f, 2); // Invitation type - switch (clan.getType()) { - case "inviteOnly" -> drawShadowedStringLeft(g2d, i18n.getString("clan.type.inviteonly"), 905, 67, 12f, 2, clanTypeInviteOnlyColor); - case "open" -> drawShadowedStringLeft(g2d, i18n.getString("clan.type.open"), 905, 67, 12f, 2, clanTypeOpenColor); + switch (clan.getInviteType()) { + case INVITE_ONLY -> drawShadowedStringLeft(g2d, i18n.getString("clan.type.inviteonly"), 905, 67, 12f, 2, clanTypeInviteOnlyColor); + case OPEN -> drawShadowedStringLeft(g2d, i18n.getString("clan.type.open"), 905, 67, 12f, 2, clanTypeOpenColor); default -> drawShadowedStringLeft(g2d, i18n.getString("clan.type.closed"), 905, 67, 12f, 2, clanTypeClosedColor); } diff --git a/src/main/java/com/lycoon/clashbot/commands/clan/WarCommand.java b/src/main/java/com/lycoon/clashbot/commands/clan/WarCommand.java index b6398b8..3296818 100644 --- a/src/main/java/com/lycoon/clashbot/commands/clan/WarCommand.java +++ b/src/main/java/com/lycoon/clashbot/commands/clan/WarCommand.java @@ -7,16 +7,20 @@ import static com.lycoon.clashbot.utils.CoreUtils.*; import static com.lycoon.clashbot.utils.GameUtils.*; +import com.lycoon.clashapi.core.ClashAPI; +import com.lycoon.clashapi.core.exceptions.ClashAPIException; import com.lycoon.clashapi.models.war.WarAttack; import com.lycoon.clashapi.models.war.WarMember; import com.lycoon.clashapi.models.war.War; import com.lycoon.clashapi.core.exception.ClashAPIException; +import com.lycoon.clashapi.models.war.enums.WarState; import com.lycoon.clashbot.commands.Command; import com.lycoon.clashbot.core.CacheComponents; import com.lycoon.clashbot.core.ClashBotMain; import com.lycoon.clashbot.lang.LangUtils; import net.dv8tion.jda.api.entities.MessageChannel; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import java.awt.*; import java.awt.image.BufferedImage; @@ -60,7 +64,7 @@ public int compare(WarAttack a, WarAttack b) { } } - public static void call(SlashCommandEvent event) { + public static void call(SlashCommandInteractionEvent event) { CompletableFuture.runAsync(() -> { if (event.getOptions().isEmpty()) { i18n = LangUtils.getTranslations(event.getMember().getIdLong()); @@ -195,7 +199,7 @@ public static void drawMapPosition(Graphics2D g2d, WarMember member, WarMember e drawCenteredString(g2d, starRect2, g2d.getFont().deriveFont(26f), String.valueOf(drawMemberResults(g2d, enemy, members, sortedAttacks, true, y))); } - public static War getWar(SlashCommandEvent event, Locale lang, String[] args) { + public static War getWar(SlashCommandInteractionEvent event, Locale lang, String[] args) { // If rate limitation has exceeded if (!checkThrottle(event, lang)) return null; @@ -211,7 +215,6 @@ public static War getWar(SlashCommandEvent event, Locale lang, String[] args) { try { war = ClashBotMain.clashAPI.getCurrentWar(tag); - } catch (IOException ignored) { } catch (ClashAPIException e) { sendExceptionError(event, i18n, e, tag, "war"); return null; @@ -219,103 +222,104 @@ public static War getWar(SlashCommandEvent event, Locale lang, String[] args) { return war; } - public static void execute(SlashCommandEvent event, String... args) { + public static void execute(SlashCommandInteractionEvent event, String... args) { lang = LangUtils.getLanguage(event.getMember().getIdLong()); i18n = LangUtils.getTranslations(lang); War war = getWar(event, lang, args); - if (war == null) + if (Objects.requireNonNull(war).getState() == WarState.NOT_IN_WAR) + { + sendError(event, MessageFormat.format(i18n.getString("exception.404.war"), tag)); return; + } - if (!war.getState().equals("notInWar")) { - // Checking index validity - int index = checkIndex(event, i18n, args[0], war.getTeamSize() / 5); - if (index == -1) - return; + // Checking index validity + int index = checkIndex(event, i18n, args[0], war.getTeamSize() / 5); + if (index == -1) + return; - // Initializing image - BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); - Graphics2D g2d = initGraphics(WIDTH, HEIGHT, image); - Font font = getFont("Supercell.ttf").deriveFont(FONT_SIZE); - g2d.setFont(font); - - members = war.getClan().getMembers(); - enemyMembers = war.getOpponent().getMembers(); - - members.sort(new SortMemberByOrder()); - enemyMembers.sort(new SortMemberByOrder()); - sortedAttacks = getAttacksByOrder(members); - sortedEnemyAttacks = getAttacksByOrder(enemyMembers); - - // Color background - g2d.setColor(backgroundColor); - g2d.fillRect(0, 0, WIDTH, HEIGHT); - - // Image background - g2d.drawImage(getImageFromFile("backgrounds/clanwar/war-background.png"), 0, 0, null); - - // Clan badges - g2d.drawImage(getImageFromUrl(war.getClan().getBadgeUrls().getSmall()), 20, 20, 135, 135, null); - g2d.drawImage(getImageFromUrl(war.getOpponent().getBadgeUrls().getSmall()), 1050, 20, 135, 135, null); - - // Clan names - drawShadowedString(g2d, war.getClan().getName(), 165, 70, 32f, 2, clanNameColor); - drawShadowedStringLeft(g2d, war.getOpponent().getName(), 1030, 70, 32f, 2, clanNameColor); - - // Status - Rectangle statusRect = new Rectangle(0, 55, WIDTH, 20); - Rectangle timeRect = new Rectangle(0, 95, WIDTH, 20); - int[] timeLeft; - switch (war.getState()) { - case "inWar" -> { - timeLeft = getTimeLeft(war.getEndTime()); - drawSimpleCenteredString(g2d, i18n.getString("war.inwar"), statusRect, 30f, Color.BLACK); - drawSimpleCenteredString(g2d, - MessageFormat.format(i18n.getString("war.timeleft"), - MessageFormat.format(i18n.getString("war.date"), timeLeft[0], timeLeft[1], timeLeft[2])), - timeRect, 22f, Color.BLACK); - } - case "preparation" -> { - timeLeft = getTimeLeft(war.getStartTime()); - drawSimpleCenteredString(g2d, i18n.getString("war.preparation"), statusRect, 30f, Color.BLACK); - drawSimpleCenteredString(g2d, - MessageFormat.format(i18n.getString("war.timeleft"), - MessageFormat.format(i18n.getString("war.date"), timeLeft[0], timeLeft[1], timeLeft[2])), - timeRect, 22f, Color.BLACK); - } - default -> { - //warEnded - timeLeft = getTimeLeft(war.getEndTime()); - drawSimpleCenteredString(g2d, i18n.getString("war.ended"), statusRect, 30f, Color.BLACK); - drawSimpleCenteredString(g2d, - MessageFormat.format(i18n.getString("war.since"), - MessageFormat.format(i18n.getString("war.date"), timeLeft[0], timeLeft[1], timeLeft[2])), - timeRect, 22f, Color.BLACK); - } + // Initializing image + BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); + Graphics2D g2d = initGraphics(WIDTH, HEIGHT, image); + Font font = getFont("Supercell.ttf").deriveFont(FONT_SIZE); + g2d.setFont(font); + + members = war.getClan().getMembers(); + enemyMembers = war.getOpponent().getMembers(); + + members.sort(new SortMemberByOrder()); + enemyMembers.sort(new SortMemberByOrder()); + sortedAttacks = getAttacksByOrder(members); + sortedEnemyAttacks = getAttacksByOrder(enemyMembers); + + // Color background + g2d.setColor(backgroundColor); + g2d.fillRect(0, 0, WIDTH, HEIGHT); + + // Image background + g2d.drawImage(getImageFromFile("backgrounds/clanwar/war-background.png"), 0, 0, null); + + // Clan badges + g2d.drawImage(getImageFromUrl(war.getClan().getBadgeUrls().getSmall()), 20, 20, 135, 135, null); + g2d.drawImage(getImageFromUrl(war.getOpponent().getBadgeUrls().getSmall()), 1050, 20, 135, 135, null); + + // Clan names + drawShadowedString(g2d, war.getClan().getName(), 165, 70, 32f, 2, clanNameColor); + drawShadowedStringLeft(g2d, war.getOpponent().getName(), 1030, 70, 32f, 2, clanNameColor); + + // Status + Rectangle statusRect = new Rectangle(0, 55, WIDTH, 20); + Rectangle timeRect = new Rectangle(0, 95, WIDTH, 20); + int[] timeLeft; + switch (war.getState()) + { + case IN_WAR -> { + timeLeft = getTimeLeft(war.getEndTime()); + drawSimpleCenteredString(g2d, i18n.getString("war.inwar"), statusRect, 30f, Color.BLACK); + drawSimpleCenteredString(g2d, + MessageFormat.format(i18n.getString("war.timeleft"), + MessageFormat.format(i18n.getString("war.date"), timeLeft[0], timeLeft[1], timeLeft[2])), + timeRect, 22f, Color.BLACK); + } + case PREPARATION -> { + timeLeft = getTimeLeft(war.getStartTime()); + drawSimpleCenteredString(g2d, i18n.getString("war.preparation"), statusRect, 30f, Color.BLACK); + drawSimpleCenteredString(g2d, + MessageFormat.format(i18n.getString("war.timeleft"), + MessageFormat.format(i18n.getString("war.date"), timeLeft[0], timeLeft[1], timeLeft[2])), + timeRect, 22f, Color.BLACK); + } + default -> { + //warEnded + timeLeft = getTimeLeft(war.getEndTime()); + drawSimpleCenteredString(g2d, i18n.getString("war.ended"), statusRect, 30f, Color.BLACK); + drawSimpleCenteredString(g2d, + MessageFormat.format(i18n.getString("war.since"), + MessageFormat.format(i18n.getString("war.date"), timeLeft[0], timeLeft[1], timeLeft[2])), + timeRect, 22f, Color.BLACK); } + } - for (int i = 0; i < SIZE; i++) - drawMapPosition(g2d, members.get(i + (index - 1) * SIZE), enemyMembers.get(i + (index - 1) * SIZE), 168 + i * PADDING); + for (int i = 0; i < SIZE; i++) + drawMapPosition(g2d, members.get(i + (index - 1) * SIZE), enemyMembers.get(i + (index - 1) * SIZE), 168 + i * PADDING); - // Total clan stars - Rectangle clanStarRect1 = new Rectangle(110, 100, 200, 20); - Rectangle clanStarRect2 = new Rectangle(890, 100, 200, 20); + // Total clan stars + Rectangle clanStarRect1 = new Rectangle(110, 100, 200, 20); + Rectangle clanStarRect2 = new Rectangle(890, 100, 200, 20); - drawCenteredString(g2d, clanStarRect1, font.deriveFont(28f), String.valueOf(war.getClan().getStars())); - drawCenteredString(g2d, clanStarRect2, font.deriveFont(28f), String.valueOf(war.getOpponent().getStars())); + drawCenteredString(g2d, clanStarRect1, font.deriveFont(28f), String.valueOf(war.getClan().getStars())); + drawCenteredString(g2d, clanStarRect2, font.deriveFont(28f), String.valueOf(war.getOpponent().getStars())); - // Total destruction percentage - Rectangle destructionRect1 = new Rectangle(290, 102, 150, 20); - Rectangle destructionRect2 = new Rectangle(758, 102, 150, 20); + // Total destruction percentage + Rectangle destructionRect1 = new Rectangle(290, 102, 150, 20); + Rectangle destructionRect2 = new Rectangle(758, 102, 150, 20); - DecimalFormatSymbols dfs = new DecimalFormatSymbols(lang); - DecimalFormat df = new DecimalFormat("#.#", dfs); - drawSimpleCenteredString(g2d, df.format(war.getClan().getDestructionPercentage()) + "%", destructionRect1, 26f, Color.BLACK); - drawSimpleCenteredString(g2d, df.format(war.getOpponent().getDestructionPercentage()) + "%", destructionRect2, 26f, Color.BLACK); + DecimalFormatSymbols dfs = new DecimalFormatSymbols(lang); + DecimalFormat df = new DecimalFormat("#.#", dfs); + drawSimpleCenteredString(g2d, df.format(war.getClan().getDestructionPercentage()) + "%", destructionRect1, 26f, Color.BLACK); + drawSimpleCenteredString(g2d, df.format(war.getOpponent().getDestructionPercentage()) + "%", destructionRect2, 26f, Color.BLACK); - sendImage(event, image); - g2d.dispose(); - } else - sendError(event, MessageFormat.format(i18n.getString("exception.404.war"), tag)); + sendImage(event, image); + g2d.dispose(); } } diff --git a/src/main/java/com/lycoon/clashbot/commands/clan/WarLeagueCommand.java b/src/main/java/com/lycoon/clashbot/commands/clan/WarLeagueCommand.java index b6478d2..5c78485 100644 --- a/src/main/java/com/lycoon/clashbot/commands/clan/WarLeagueCommand.java +++ b/src/main/java/com/lycoon/clashbot/commands/clan/WarLeagueCommand.java @@ -7,8 +7,10 @@ import static com.lycoon.clashbot.utils.CoreUtils.*; import static com.lycoon.clashbot.utils.GameUtils.*; +import com.lycoon.clashapi.core.exceptions.ClashAPIException; import com.lycoon.clashapi.models.war.War; import com.lycoon.clashapi.models.war.WarClan; +import com.lycoon.clashapi.models.war.enums.WarState; import com.lycoon.clashapi.models.warleague.WarLeagueClan; import com.lycoon.clashapi.models.warleague.WarLeagueRound; import com.lycoon.clashapi.models.warleague.WarLeagueGroup; @@ -19,6 +21,7 @@ import com.lycoon.clashbot.core.RoundWarInfo; import com.lycoon.clashbot.lang.LangUtils; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import java.awt.*; import java.awt.image.BufferedImage; @@ -35,7 +38,7 @@ public class WarLeagueCommand { private static ResourceBundle i18n; - public static void call(SlashCommandEvent event) { + public static void call(SlashCommandInteractionEvent event) { Locale lang = LangUtils.getLanguage(event.getMember().getIdLong()); i18n = LangUtils.getTranslations(lang); @@ -72,7 +75,7 @@ public static List getWars(WarLeagueGroup leagueGroup, int roundIndex) { return wars; } - public static void drawRound(Graphics2D g2d, SlashCommandEvent event, List wars, ResourceBundle i18n, int roundIndex) { + public static void drawRound(Graphics2D g2d, SlashCommandInteractionEvent event, List wars, ResourceBundle i18n, int roundIndex) { Font font = g2d.getFont().deriveFont(FONT_SIZE); // Round label @@ -190,7 +193,7 @@ public static void drawStats(List rounds) { } } - public static WarLeagueGroup getLeagueGroup(SlashCommandEvent event, Locale lang, String[] args) { + public static WarLeagueGroup getLeagueGroup(SlashCommandInteractionEvent event, Locale lang, String[] args) { // If rate limitation has exceeded if (!checkThrottle(event, lang)) return null; @@ -207,7 +210,6 @@ public static WarLeagueGroup getLeagueGroup(SlashCommandEvent event, Locale lang try { leagueGroup = ClashBotMain.clashAPI.getWarLeagueGroup(tag); - } catch (IOException ignored) { } catch (ClashAPIException e) { sendExceptionError(event, i18n, e, tag, "warleague"); return null; @@ -215,7 +217,7 @@ public static WarLeagueGroup getLeagueGroup(SlashCommandEvent event, Locale lang return leagueGroup; } - public static void executeRound(SlashCommandEvent event, String... args) { + public static void executeRound(SlashCommandInteractionEvent event, String... args) { Locale lang = LangUtils.getLanguage(event.getMember().getIdLong()); ResourceBundle i18n = LangUtils.getTranslations(lang); @@ -237,14 +239,17 @@ public static void executeRound(SlashCommandEvent event, String... args) { // Rounds List roundWars = getWars(warLeague, index - 1); drawRound(g2d, event, roundWars, i18n, index - 1); - if (roundWars.get(0).getState().equals("notInWar")) + + WarState state = roundWars.get(0).getState(); + if (state == WarState.NOT_IN_WAR) return; sendImage(event, image); g2d.dispose(); } - public static WarClan getWinner(WarClan clan1, WarClan clan2) { + public static WarClan getWinner(WarClan clan1, WarClan clan2) + { if (clan1.getStars() > clan2.getStars()) return clan1; else if (clan1.getStars() == clan2.getStars()) { @@ -269,7 +274,8 @@ public static int getWinStars(WarClan clan1, WarClan clan2, War war) { return 0; } - public static void executeAll(SlashCommandEvent event, String... args) { + public static void executeAll(SlashCommandInteractionEvent event, String... args) + { Locale lang = LangUtils.getLanguage(event.getMember().getIdLong()); ResourceBundle i18n = LangUtils.getTranslations(lang); @@ -286,11 +292,14 @@ public static void executeAll(SlashCommandEvent event, String... args) { HashMap stars = new HashMap<>(); HashMap destruction = new HashMap<>(); - for (WarLeagueClan clan : warLeague.getClans()) { + for (WarLeagueClan clan : warLeague.getClans()) + { stars.put(clan.getName(), 0); destruction.put(clan.getName(), 0); } - for (int i = 0; i < 7; i++) { + + for (int i = 0; i < 7; i++) + { List wars = getWars(warLeague, i); for (War war : wars) { WarClan clan1 = war.getClan(); @@ -306,15 +315,14 @@ public static void executeAll(SlashCommandEvent event, String... args) { System.out.println(); } - for (WarLeagueClan clan : warLeague.getClans()) { + for (WarLeagueClan clan : warLeague.getClans()) System.out.println(clan.getName() + ": " + stars.get(clan.getName()) + " étoiles (" + destruction.get(clan.getName()) + "%)"); - } sendImage(event, image); g2d.dispose(); } - public static void executeClan(SlashCommandEvent event, String... args) { + public static void executeClan(SlashCommandInteractionEvent event, String... args) { } } diff --git a/src/main/java/com/lycoon/clashbot/commands/clan/WarlogCommand.java b/src/main/java/com/lycoon/clashbot/commands/clan/WarlogCommand.java index 174a0e2..85f1252 100644 --- a/src/main/java/com/lycoon/clashbot/commands/clan/WarlogCommand.java +++ b/src/main/java/com/lycoon/clashbot/commands/clan/WarlogCommand.java @@ -7,14 +7,17 @@ import static com.lycoon.clashbot.utils.CoreUtils.*; import static com.lycoon.clashbot.utils.GameUtils.*; +import com.lycoon.clashapi.core.exceptions.ClashAPIException; import com.lycoon.clashapi.models.war.WarlogClan; import com.lycoon.clashapi.models.war.WarlogEntry; import com.lycoon.clashapi.core.exception.ClashAPIException; +import com.lycoon.clashapi.models.war.enums.WarResult; import com.lycoon.clashbot.commands.Command; import com.lycoon.clashbot.core.CacheComponents; import com.lycoon.clashbot.core.ClashBotMain; import com.lycoon.clashbot.lang.LangUtils; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import java.awt.*; import java.awt.image.BufferedImage; @@ -45,7 +48,7 @@ public class WarlogCommand { private final static Color versusColor = new Color(0xffffc0); private final static Color percentageColor = new Color(0x5e5d60); - public static void call(SlashCommandEvent event) { + public static void call(SlashCommandInteractionEvent event) { CompletableFuture.runAsync(() -> { if (event.getOptions().isEmpty()) { @@ -63,18 +66,20 @@ public static void call(SlashCommandEvent event) { }); } - public static Image getImageStatus(String result) { + public static Image getImageStatus(WarResult result) + { if (result == null) return CacheComponents.warlogCWL; // CWL - else if (result.equals("win")) + else if (result == WarResult.WIN) return CacheComponents.warlogWon; // won - else if (result.equals("lose")) + else if (result == WarResult.LOSE) return CacheComponents.warlogLost; // lost return CacheComponents.warlogTie; // tie } - public static void drawWar(Graphics2D g2d, WarlogEntry war, int y) { + public static void drawWar(Graphics2D g2d, WarlogEntry war, int y) + { boolean isCWL = war.getResult() == null; // Member background @@ -122,7 +127,8 @@ public static void drawWar(Graphics2D g2d, WarlogEntry war, int y) { drawCenteredString(g2d, teamSizeRect, g2d.getFont().deriveFont(14f), MessageFormat.format(i18n.getString("team.size.versus"), war.getTeamSize()), versusColor); } - public static List getWarlog(SlashCommandEvent event, Locale lang, String[] args) { + public static List getWarlog(SlashCommandInteractionEvent event, Locale lang, String[] args) + { // If rate limitation has exceeded if (!checkThrottle(event, lang)) return null; @@ -147,7 +153,8 @@ public static List getWarlog(SlashCommandEvent event, Locale lang, return warlog; } - public static void execute(SlashCommandEvent event, String... args) { + public static void execute(SlashCommandInteractionEvent event, String... args) + { lang = LangUtils.getLanguage(event.getMember().getIdLong()); i18n = LangUtils.getTranslations(lang); @@ -161,21 +168,22 @@ public static void execute(SlashCommandEvent event, String... args) { return; // Checking if there are any clan wars - if (warlog.size() <= 0) { + if (warlog.isEmpty()) { sendError(event, i18n.getString("no.warlog")); return; } // Computing stats - int total, wins, losses, draws; - wins = losses = draws = 0; - for (WarlogEntry war : warlog) { + int total = 0, wins = 0, losses = 0, draws = 0; + for (WarlogEntry war : warlog) + { if (war.getResult() == null) continue; - switch (war.getResult()) { - case "win" -> wins++; - case "lose" -> losses++; + switch (war.getResult()) + { + case WIN -> wins++; + case LOSE -> losses++; default -> draws++; } } diff --git a/src/main/java/com/lycoon/clashbot/commands/misc/ClearCommand.java b/src/main/java/com/lycoon/clashbot/commands/misc/ClearCommand.java index d41bc44..14c387f 100644 --- a/src/main/java/com/lycoon/clashbot/commands/misc/ClearCommand.java +++ b/src/main/java/com/lycoon/clashbot/commands/misc/ClearCommand.java @@ -4,19 +4,18 @@ import com.lycoon.clashbot.utils.CoreUtils; import com.lycoon.clashbot.utils.DatabaseUtils; import net.dv8tion.jda.api.EmbedBuilder; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import java.util.ResourceBundle; public class ClearCommand { - public static void call(SlashCommandEvent event) + public static void call(SlashCommandInteractionEvent event) { execute(event); } - public static void execute(SlashCommandEvent event) + public static void execute(SlashCommandInteractionEvent event) { long id = event.getMember().getIdLong(); ResourceBundle i18n = LangUtils.getTranslations(id); diff --git a/src/main/java/com/lycoon/clashbot/commands/misc/HelpCommand.java b/src/main/java/com/lycoon/clashbot/commands/misc/HelpCommand.java index e7c6b1b..dff9faf 100644 --- a/src/main/java/com/lycoon/clashbot/commands/misc/HelpCommand.java +++ b/src/main/java/com/lycoon/clashbot/commands/misc/HelpCommand.java @@ -4,10 +4,8 @@ import com.lycoon.clashbot.commands.CommandCategory; import com.lycoon.clashbot.lang.LangUtils; import com.lycoon.clashbot.utils.CoreUtils; -import com.lycoon.clashbot.utils.DatabaseUtils; import net.dv8tion.jda.api.EmbedBuilder; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import java.awt.*; import java.util.ResourceBundle; @@ -17,7 +15,7 @@ public class HelpCommand private static ResourceBundle i18n; private static EmbedBuilder builder; - public static void call(SlashCommandEvent event) + public static void call(SlashCommandInteractionEvent event) { execute(event); } @@ -33,7 +31,7 @@ public static void drawCategory(CommandCategory category, Command[] commands) builder.addField(i18n.getString(category.toString()), categoryField.toString(), false); } - public static void execute(SlashCommandEvent event) + public static void execute(SlashCommandInteractionEvent event) { i18n = LangUtils.getTranslations(event.getMember().getIdLong()); builder = new EmbedBuilder(); diff --git a/src/main/java/com/lycoon/clashbot/commands/misc/InfoCommand.java b/src/main/java/com/lycoon/clashbot/commands/misc/InfoCommand.java index ee2a384..fb8766d 100644 --- a/src/main/java/com/lycoon/clashbot/commands/misc/InfoCommand.java +++ b/src/main/java/com/lycoon/clashbot/commands/misc/InfoCommand.java @@ -1,24 +1,19 @@ package com.lycoon.clashbot.commands.misc; -import com.lycoon.clashbot.core.CacheComponents; import com.lycoon.clashbot.core.ClashBotMain; import com.lycoon.clashbot.lang.LangUtils; -import com.lycoon.clashbot.utils.CoreUtils; import net.dv8tion.jda.api.EmbedBuilder; -import net.dv8tion.jda.api.entities.Emoji; import net.dv8tion.jda.api.entities.Guild; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.events.message.MessageReceivedEvent; -import net.dv8tion.jda.api.interactions.components.Button; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import java.awt.*; -import java.text.MessageFormat; import java.text.NumberFormat; import java.util.List; import java.util.Locale; import java.util.ResourceBundle; -public class InfoCommand { +public class InfoCommand +{ private static final String TWITTER = "@LycoonMC"; private static final String DISCORD = "Lycoon#7542"; private static final String WEBSITE = "https://clashbot.app/"; @@ -29,11 +24,12 @@ public class InfoCommand { static final String PATREON_EMOJI = "<:patreon:909058096503062528>"; static final String CLASHBOT_EMOJI = "<:clashbot:909058781583917107>"; - public static void call(SlashCommandEvent event) { + public static void call(SlashCommandInteractionEvent event) { execute(event); } - public static void execute(SlashCommandEvent event) { + public static void execute(SlashCommandInteractionEvent event) + { Locale lang = LangUtils.getLanguage(event.getMember().getIdLong()); ResourceBundle i18n = LangUtils.getTranslations(lang); NumberFormat nf = NumberFormat.getInstance(lang); diff --git a/src/main/java/com/lycoon/clashbot/commands/misc/InviteCommand.java b/src/main/java/com/lycoon/clashbot/commands/misc/InviteCommand.java index 612d3bf..06d43fd 100644 --- a/src/main/java/com/lycoon/clashbot/commands/misc/InviteCommand.java +++ b/src/main/java/com/lycoon/clashbot/commands/misc/InviteCommand.java @@ -4,8 +4,7 @@ import com.lycoon.clashbot.lang.LangUtils; import com.lycoon.clashbot.utils.CoreUtils; import net.dv8tion.jda.api.EmbedBuilder; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import java.awt.*; import java.text.MessageFormat; @@ -15,12 +14,12 @@ public class InviteCommand { static final String INVITE_EMOJI = "<:invite:825345488152690718>"; - public static void call(SlashCommandEvent event) + public static void call(SlashCommandInteractionEvent event) { execute(event); } - public static void execute(SlashCommandEvent event) + public static void execute(SlashCommandInteractionEvent event) { ResourceBundle i18n = LangUtils.getTranslations(event.getMember().getIdLong()); EmbedBuilder builder = new EmbedBuilder(); diff --git a/src/main/java/com/lycoon/clashbot/commands/misc/LangCommand.java b/src/main/java/com/lycoon/clashbot/commands/misc/LangCommand.java index 91fc953..c69a754 100644 --- a/src/main/java/com/lycoon/clashbot/commands/misc/LangCommand.java +++ b/src/main/java/com/lycoon/clashbot/commands/misc/LangCommand.java @@ -3,10 +3,8 @@ import com.lycoon.clashbot.commands.Command; import com.lycoon.clashbot.lang.LangUtils; import com.lycoon.clashbot.utils.CoreUtils; -import com.lycoon.clashbot.utils.DatabaseUtils; import net.dv8tion.jda.api.EmbedBuilder; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import java.awt.*; import java.text.MessageFormat; @@ -15,12 +13,12 @@ public class LangCommand { - public static void call(SlashCommandEvent event, String... args) + public static void call(SlashCommandInteractionEvent event, String... args) { execute(event); } - public static void execute(SlashCommandEvent event) + public static void execute(SlashCommandInteractionEvent event) { Locale lang = LangUtils.getLanguage(event.getMember().getIdLong()); ResourceBundle i18n = LangUtils.getTranslations(lang); diff --git a/src/main/java/com/lycoon/clashbot/commands/settings/SetCommand.java b/src/main/java/com/lycoon/clashbot/commands/settings/SetCommand.java index aacc25b..a87c471 100644 --- a/src/main/java/com/lycoon/clashbot/commands/settings/SetCommand.java +++ b/src/main/java/com/lycoon/clashbot/commands/settings/SetCommand.java @@ -1,6 +1,6 @@ package com.lycoon.clashbot.commands.settings; -import com.lycoon.clashapi.core.exception.ClashAPIException; +import com.lycoon.clashapi.core.exceptions.ClashAPIException; import com.lycoon.clashbot.commands.Command; import com.lycoon.clashbot.core.ClashBotMain; import com.lycoon.clashbot.lang.LangUtils; @@ -8,18 +8,17 @@ import com.lycoon.clashbot.utils.DatabaseUtils; import com.lycoon.clashbot.utils.ErrorUtils; import net.dv8tion.jda.api.EmbedBuilder; -import net.dv8tion.jda.api.Permission; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import java.io.IOException; import java.text.MessageFormat; import java.util.Locale; -import java.util.Objects; import java.util.ResourceBundle; -public class SetCommand { - public static void call(SlashCommandEvent event) { +public class SetCommand +{ + public static void call(SlashCommandInteractionEvent event) + { if (event.getSubcommandName() == null) { ResourceBundle i18n = LangUtils.getTranslations(event.getMember().getIdLong()); ErrorUtils.sendError(event, @@ -29,7 +28,8 @@ public static void call(SlashCommandEvent event) { } String type = event.getSubcommandName(); - switch (type) { + switch (type) + { case "player" -> executePlayer(event, event.getOption("player_tag").getAsString()); case "clan" -> executeClan(event, event.getOption("clan_tag").getAsString()); case "lang" -> executeLang(event, event.getOption("language").getAsString()); @@ -42,7 +42,7 @@ public static void call(SlashCommandEvent event) { } } - public static void executePlayer(SlashCommandEvent event, String tag) { + public static void executePlayer(SlashCommandInteractionEvent event, String tag) { ResourceBundle i18n = LangUtils.getTranslations(event.getMember().getIdLong()); try { @@ -62,13 +62,14 @@ public static void executePlayer(SlashCommandEvent event, String tag) { CoreUtils.sendMessage(event, i18n, builder); } - public static void executeClan(SlashCommandEvent event, String tag) { + public static void executeClan(SlashCommandInteractionEvent event, String tag) + { ResourceBundle i18n = LangUtils.getTranslations(event.getMember().getIdLong()); try { // Checks if the clan exists ClashBotMain.clashAPI.getClan(tag); - } catch (ClashAPIException | IOException e) { + } catch (ClashAPIException e) { ErrorUtils.sendExceptionError(event, i18n, e, tag, "clan"); return; } @@ -82,9 +83,11 @@ public static void executeClan(SlashCommandEvent event, String tag) { CoreUtils.sendMessage(event, i18n, builder); } - public static void executeLang(SlashCommandEvent event, String language) { + public static void executeLang(SlashCommandInteractionEvent event, String language) + { long id = event.getMember().getIdLong(); - if (LangUtils.isSupportedLanguage(language)) { + if (LangUtils.isSupportedLanguage(language)) + { DatabaseUtils.setUserLang(id, language); Locale lang = new Locale(language); ResourceBundle i18n = LangUtils.getTranslations(lang); @@ -98,7 +101,9 @@ public static void executeLang(SlashCommandEvent event, String language) { Command.SET_LANG.formatCommand())); CoreUtils.sendMessage(event, i18n, builder); - } else { + } + else + { Locale lang = LangUtils.getLanguage(id); ResourceBundle i18n = LangUtils.getTranslations(lang); @@ -110,7 +115,8 @@ public static void executeLang(SlashCommandEvent event, String language) { int length = LangUtils.LANGUAGES.length; double perColumn = Math.ceil(length / 3D); StringBuilder str = new StringBuilder(); - for (int i = 0; i < length; i++) { + for (int i = 0; i < length; i++) + { if (i != 0 && i % perColumn == 0) { builder.addField(str.toString(), "", true); str = new StringBuilder(); diff --git a/src/main/java/com/lycoon/clashbot/core/ClashBotMain.java b/src/main/java/com/lycoon/clashbot/core/ClashBotMain.java index 6125c68..36ac737 100644 --- a/src/main/java/com/lycoon/clashbot/core/ClashBotMain.java +++ b/src/main/java/com/lycoon/clashbot/core/ClashBotMain.java @@ -13,38 +13,57 @@ import java.io.IOException; import java.util.Properties; -public class ClashBotMain { - static final String CONFIG = "tokens.properties"; +public class ClashBotMain +{ + private static final String CONFIG = "tokens.properties"; + private static Properties tokens; public static long[] owners = {138282927502000128L, 198485955701768192L}; - public static final String VERSION = "1.4.3"; + public static final String VERSION = "2.0.0"; public static final String INVITE = "https://discord.com/api/oauth2/authorize?client_id=734481969630543883&permissions=2147534848&scope=bot%20applications.commands"; public static Logger LOGGER = LoggerFactory.getLogger(ClashBotMain.class.getName()); + // Following attributes are initialized on launch public static ClashAPI clashAPI; public static CacheComponents cached; public static JDA jda; /* - * Clashbot entry point + * ############################## Clashbot ############################## + * Author: Hugo BOIS + * All rights reserved 2024 + * ###################################################################### */ - public static void main(String[] args) throws LoginException, InterruptedException { - Properties tokens = new Properties(); - try { - // Loading secret tokens + public static void main(String[] args) throws InterruptedException { + loadTokensFromConfig(); + buildDiscordInstance(); + + clashAPI = new ClashAPI(tokens.getProperty("clash-of-clans")); + cached = CacheComponents.getInstance(); + } + + /* + * Load secret tokens from config file for Clash of Clans and Discord APIs + */ + private static void loadTokensFromConfig() + { + try + { tokens.load(new FileInputStream(CONFIG)); LOGGER.info("Secret tokens loaded"); - } catch (IOException e) { - LOGGER.error(e.getMessage()); } - JDABuilder builder = JDABuilder.createDefault(tokens.getProperty("discord")); + catch (IOException e) { LOGGER.error(e.getMessage()); } + } + /* + * Build Discord instance from previous loaded token + */ + private static void buildDiscordInstance() throws InterruptedException + { + JDABuilder builder = JDABuilder.createDefault(tokens.getProperty("discord")); builder.addEventListeners(new EventListener()); builder.setStatus(OnlineStatus.ONLINE); builder.setActivity(Activity.playing("Clash of Clans")); jda = builder.build().awaitReady(); - - clashAPI = new ClashAPI(tokens.getProperty("clash-of-clans")); - cached = CacheComponents.getInstance(); } } diff --git a/src/main/java/com/lycoon/clashbot/core/EventListener.java b/src/main/java/com/lycoon/clashbot/core/EventListener.java index ccc08a7..b93a24e 100644 --- a/src/main/java/com/lycoon/clashbot/core/EventListener.java +++ b/src/main/java/com/lycoon/clashbot/core/EventListener.java @@ -6,34 +6,27 @@ import com.lycoon.clashbot.commands.clan.WarLeagueCommand; import com.lycoon.clashbot.commands.clan.WarlogCommand; import com.lycoon.clashbot.commands.misc.*; -import com.lycoon.clashbot.commands.settings.AdminCommand; import com.lycoon.clashbot.commands.settings.SetCommand; -import com.lycoon.clashbot.lang.LangUtils; -import com.lycoon.clashbot.utils.CoreUtils; -import com.lycoon.clashbot.utils.DatabaseUtils; import net.dv8tion.jda.api.EmbedBuilder; -import net.dv8tion.jda.api.entities.ChannelType; import net.dv8tion.jda.api.entities.MessageEmbed; -import net.dv8tion.jda.api.entities.TextChannel; +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.entities.channel.unions.DefaultGuildChannelUnion; import net.dv8tion.jda.api.events.guild.GuildJoinEvent; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.exceptions.MissingAccessException; import net.dv8tion.jda.api.hooks.ListenerAdapter; import java.awt.*; -import java.text.MessageFormat; import java.util.Objects; -import java.util.ResourceBundle; -import static com.lycoon.clashbot.core.ClashBotMain.LOGGER; - -public class EventListener extends ListenerAdapter { +public class EventListener extends ListenerAdapter +{ static boolean isCommand(String arg, Command cmd) { return arg.equalsIgnoreCase(cmd.toString()); } - static MessageEmbed warnNotInGuild() { + static MessageEmbed warnNotInGuild() + { EmbedBuilder builder = new EmbedBuilder(); builder.setColor(Color.YELLOW); @@ -44,7 +37,7 @@ static MessageEmbed warnNotInGuild() { } @Override - public void onSlashCommand(SlashCommandEvent event) { + public void onSlashCommandInteraction(SlashCommandInteractionEvent event) { if (!event.isFromGuild()) { event.replyEmbeds(warnNotInGuild()).queue(); @@ -85,14 +78,15 @@ else if (isCommand(cmd, Command.INVITE)) } @Override - public void onGuildJoin(GuildJoinEvent event) { - TextChannel defaultChannel = event.getGuild().getDefaultChannel(); + public void onGuildJoin(GuildJoinEvent event) + { + DefaultGuildChannelUnion defaultChannel = event.getGuild().getDefaultChannel(); EmbedBuilder builder = new EmbedBuilder(); builder.setColor(Color.GRAY); builder.setTitle("Hi, thanks for inviting me!"); builder.setDescription("Run `/help` to get the list of all available commands :scroll:"); - try { Objects.requireNonNull(defaultChannel).sendMessage(builder.build()).queue(); } + try { Objects.requireNonNull(defaultChannel).asTextChannel().sendMessage(builder.build()).queue(); } catch (MissingAccessException ignored) {} } } diff --git a/src/main/java/com/lycoon/clashbot/utils/CoreUtils.java b/src/main/java/com/lycoon/clashbot/utils/CoreUtils.java index 0fef91e..d03bfff 100644 --- a/src/main/java/com/lycoon/clashbot/utils/CoreUtils.java +++ b/src/main/java/com/lycoon/clashbot/utils/CoreUtils.java @@ -9,6 +9,7 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.exceptions.InsufficientPermissionException; import java.awt.*; @@ -46,7 +47,8 @@ public static long getLastTimeDifference(long id) { return threshold; } - public static boolean checkThrottle(SlashCommandEvent event, Locale lang) { + public static boolean checkThrottle(SlashCommandInteractionEvent event, Locale lang) + { ResourceBundle i18n = LangUtils.getTranslations(lang); NumberFormat nf = NumberFormat.getNumberInstance(lang); @@ -64,7 +66,8 @@ public static boolean checkThrottle(SlashCommandEvent event, Locale lang) { return isValid; } - public static boolean isOwner(long id) { + public static boolean isOwner(long id) + { for (int i = 0; i < ClashBotMain.owners.length; i++) if (id == ClashBotMain.owners[i]) return true; @@ -72,21 +75,22 @@ public static boolean isOwner(long id) { return false; } - public static void sendMessage(SlashCommandEvent event, ResourceBundle i18n, EmbedBuilder builder) { - try { - event.getHook().sendMessageEmbeds(builder.build()).queue(); - } catch (InsufficientPermissionException e) { + public static void sendMessage(SlashCommandInteractionEvent event, ResourceBundle i18n, EmbedBuilder builder) + { + try { event.getHook().sendMessageEmbeds(builder.build()).queue(); } + catch (InsufficientPermissionException e) + { LOGGER.debug(e.getMessage()); event.getMember().getUser().openPrivateChannel().queue( - // Success - (channel) -> - ErrorUtils.sendError(event, INFO_EMOJI + " " + - i18n.getString("exception.permission.title"), MessageFormat.format( - i18n.getString("exception.permission.tip"), - event.getGuild().getName(), ClashBotMain.INVITE)), - - // Failure - (err) -> LOGGER.debug(err.getMessage())); + // Success + (channel) -> ErrorUtils.sendError(event, INFO_EMOJI + " " + + i18n.getString("exception.permission.title"), MessageFormat.format( + i18n.getString("exception.permission.tip"), + event.getGuild().getName(), ClashBotMain.INVITE)), + + // Failure + (err) -> LOGGER.debug(err.getMessage()) + ); } builder.clear(); diff --git a/src/main/java/com/lycoon/clashbot/utils/ErrorUtils.java b/src/main/java/com/lycoon/clashbot/utils/ErrorUtils.java index ee610fe..2ddc79e 100644 --- a/src/main/java/com/lycoon/clashbot/utils/ErrorUtils.java +++ b/src/main/java/com/lycoon/clashbot/utils/ErrorUtils.java @@ -1,17 +1,17 @@ package com.lycoon.clashbot.utils; import net.dv8tion.jda.api.EmbedBuilder; -import net.dv8tion.jda.api.entities.MessageChannel; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import java.text.MessageFormat; import java.util.ResourceBundle; -public class ErrorUtils { +public class ErrorUtils +{ private static final String OFFICIAL_TWITTER = "https://twitter.com/ClashofClans"; - public static void sendExceptionError(SlashCommandEvent event, ResourceBundle i18n, Exception e, String... args) { + public static void sendExceptionError(SlashCommandInteractionEvent event, ResourceBundle i18n, Exception e, String... args) + { switch (e.getMessage()) { case "400" -> sendError(event, i18n.getString("exception.other")); case "403" -> sendError(event, i18n.getString("exception.403")); @@ -28,7 +28,8 @@ public static void sendExceptionError(SlashCommandEvent event, ResourceBundle i1 } } - public static int checkIndex(SlashCommandEvent event, ResourceBundle i18n, String arg, int max) { + public static int checkIndex(SlashCommandInteractionEvent event, ResourceBundle i18n, String arg, int max) + { int index; try { index = Integer.parseInt(arg); @@ -57,7 +58,7 @@ public static void throwCommandError(MessageChannel channel, ResourceBundle i18n } */ - public static void sendError(SlashCommandEvent event, String title, String... args) { + public static void sendError(SlashCommandInteractionEvent event, String title, String... args) { EmbedBuilder error = new EmbedBuilder(); error.setColor(CoreUtils.invalidColor); error.setTitle(title); diff --git a/src/main/java/com/lycoon/clashbot/utils/FileUtils.java b/src/main/java/com/lycoon/clashbot/utils/FileUtils.java index 90b2154..c327c66 100644 --- a/src/main/java/com/lycoon/clashbot/utils/FileUtils.java +++ b/src/main/java/com/lycoon/clashbot/utils/FileUtils.java @@ -2,6 +2,7 @@ import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import static com.lycoon.clashbot.core.ClashBotMain.LOGGER; import static com.lycoon.clashbot.utils.CoreUtils.addUserToGenerating; @@ -17,15 +18,14 @@ import java.util.Objects; import java.util.function.Consumer; -public class FileUtils { - public static void sendImage(SlashCommandEvent event, BufferedImage image) { +public class FileUtils +{ + public static void sendImage(SlashCommandInteractionEvent event, BufferedImage image) + { // Generating the picture ByteArrayOutputStream bos = new ByteArrayOutputStream(); - try { - ImageIO.write(image, "png", bos); - } catch (IOException e) { - e.printStackTrace(); - } + try { ImageIO.write(image, "png", bos); } + catch (IOException e) { LOGGER.error("Error while generating picture: " + e.getMessage()); } // Callback function long id = event.getMember().getIdLong(); @@ -35,11 +35,8 @@ public static void sendImage(SlashCommandEvent event, BufferedImage image) { Consumer sendingCallback = (res) -> { // Bot rate limitation - try { - Thread.sleep(CoreUtils.threshold); - } catch (InterruptedException e) { - e.printStackTrace(); - } + try { Thread.sleep(CoreUtils.threshold); } + catch (InterruptedException e) { LOGGER.error("Error while sending picture: " + e.getMessage()); } removeUserFromGenerating(id); }; @@ -47,25 +44,29 @@ public static void sendImage(SlashCommandEvent event, BufferedImage image) { LOGGER.info("Picture sent to " + event.getMember().getUser().getAsTag() + " on " + event.getGuild().getIdLong()); } - public static Image getImageFromFile(String file) { - Image image = null; + public static Image getImageFromFile(String file) + { try { URL imageUrl = Objects.requireNonNull(FileUtils.class.getResource("/" + file)); - image = ImageIO.read(imageUrl); - } catch (IOException e) { - e.printStackTrace(); + return ImageIO.read(imageUrl); + } + catch (IOException e) { + LOGGER.error("Error while reading file: " + e.getMessage()); } - return image; + + return null; } - public static Image getImageFromUrl(String link) { - Image image = null; + public static Image getImageFromUrl(String link) + { try { URL url = new URL(link); - image = ImageIO.read(url); - } catch (IOException e) { - e.printStackTrace(); + return ImageIO.read(url); + } + catch (IOException e) { + LOGGER.error("Error while reading URL: " + e.getMessage()); } - return image; + + return null; } }