diff --git a/src/main/java/me/kub94ek/Main.java b/src/main/java/me/kub94ek/Main.java index 2e889d7..9d68356 100644 --- a/src/main/java/me/kub94ek/Main.java +++ b/src/main/java/me/kub94ek/Main.java @@ -4,6 +4,7 @@ import me.kub94ek.card.CardType; import me.kub94ek.command.CommandManager; import me.kub94ek.command.impl.commands.CardCommand; +import me.kub94ek.command.impl.commands.CoinCommand; import me.kub94ek.command.impl.executors.CardCommandExecutor; import me.kub94ek.data.database.Database; import me.kub94ek.data.stats.Stats; @@ -296,15 +297,13 @@ public void onModalInteraction(ModalInteractionEvent event) { } private void registerGuildCommands() { - jda.getGuilds().forEach(guild -> guild.retrieveCommands().queue(commands -> { - commands.forEach(command -> { - if (command.getApplicationId().equals("1274368616179175485")) { - command.delete().queue(); - } - }); - })); + jda.getGuilds().forEach(guild -> guild.retrieveCommands().queue(commands -> commands.forEach(command -> { + if (command.getApplicationId().equals("1274368616179175485")) { + command.delete().queue(); + } + }))); - commandManager.registerCommand(new CardCommand()); + commandManager.registerCommands(List.of(new CardCommand(), new CoinCommand())); } public static JDA getJda() { diff --git a/src/main/java/me/kub94ek/command/CommandManager.java b/src/main/java/me/kub94ek/command/CommandManager.java index 3c38637..b2bda00 100644 --- a/src/main/java/me/kub94ek/command/CommandManager.java +++ b/src/main/java/me/kub94ek/command/CommandManager.java @@ -19,13 +19,14 @@ public CommandManager(JDA jda) { this.jda = jda; } - public void registerCommand(Command command) { - registerCommand(command.getCommand(), command.getExecutor()); - } - - public void registerCommand(SlashCommandData command, CommandExecutor commandExecutor) { - jda.getGuilds().forEach(guild -> jda.updateCommands().addCommands(command).queue()); - commandExecutors.put(command.getName(), commandExecutor); + public void registerCommands(List commands) { + List slashCommandData = new ArrayList<>(); + commands.forEach(command -> { + slashCommandData.add(command.getCommand()); + commandExecutors.put(command.getCommand().getName(), command.getExecutor()); + }); + + jda.getGuilds().forEach(guild -> jda.updateCommands().addCommands(slashCommandData).queue()); } @Override diff --git a/src/main/java/me/kub94ek/command/impl/commands/CoinCommand.java b/src/main/java/me/kub94ek/command/impl/commands/CoinCommand.java new file mode 100644 index 0000000..da8902f --- /dev/null +++ b/src/main/java/me/kub94ek/command/impl/commands/CoinCommand.java @@ -0,0 +1,69 @@ +package me.kub94ek.command.impl.commands; + +import me.kub94ek.command.Command; +import me.kub94ek.command.CommandExecutor; +import me.kub94ek.command.impl.executors.CoinCommandExecutor; +import net.dv8tion.jda.api.interactions.DiscordLocale; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.Commands; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; +import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData; +import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; + +import java.util.Map; + +public class CoinCommand implements Command { + private static final CoinCommandExecutor executor = new CoinCommandExecutor(); + + @Override + public CommandExecutor getExecutor() { + return executor; + } + + @Override + public SlashCommandData getCommand() { + SlashCommandData command = Commands.slash("coin", "Command for managing coins"); + command.addSubcommands( + new SubcommandData("bal", "Shows you your coin balance") + .setDescriptionLocalizations( + Map.of( + DiscordLocale.CZECH, "Ukáže vám váš zůstatek mincí", + DiscordLocale.SPANISH, "Muestra el saldo de monedas" + ) + ), + new SubcommandData("give", "Gives coins to somebody") + .addOptions( + new OptionData( + OptionType.USER, + "user", "The user to give the coins to", + true + ).setDescriptionLocalizations( + Map.of( + DiscordLocale.CZECH, "Komu mince darovat", + DiscordLocale.SPANISH, "A quién regalar las monedas" + ) + ), + new OptionData( + OptionType.INTEGER, + "coins", "How many coins to give", + true + ).setDescriptionLocalizations( + Map.of( + DiscordLocale.CZECH, "Kolik mincí chcete darovat", + DiscordLocale.SPANISH, "Cuántas monedas desea regalar" + ) + ) + ) + .setDescriptionLocalizations( + Map.of( + DiscordLocale.CZECH, "Darujte někomu vaše mince", + DiscordLocale.SPANISH, "Regala sus monedas a alguien" + ) + ) + ); + + + return command; + } + +} diff --git a/src/main/java/me/kub94ek/command/impl/executors/CardCommandExecutor.java b/src/main/java/me/kub94ek/command/impl/executors/CardCommandExecutor.java index bee7f9b..e8ac351 100644 --- a/src/main/java/me/kub94ek/command/impl/executors/CardCommandExecutor.java +++ b/src/main/java/me/kub94ek/command/impl/executors/CardCommandExecutor.java @@ -34,106 +34,106 @@ public void executeCommand(SlashCommandInteractionEvent event) { Database database = Main.getDatabase(); switch (Objects.requireNonNull(event.getSubcommandName())) { - case "list" -> { - if (database.getUserCards(memberId).isEmpty()) { - event.reply("You don't have any card yet.").setEphemeral(true).queue(); - return; - } - - - var message = createListMessage( - event.reply("Listing all your cards:"), - database.getUserCards(memberId), - 0 - ); - message.setEphemeral(true).queue(sentMessage -> pages.put(memberId, 0)); + case "list" -> { + if (database.getUserCards(memberId).isEmpty()) { + event.reply("You don't have any card yet.").setEphemeral(true).queue(); + return; } - case "give" -> { - User user = Objects.requireNonNull(event.getOption("user")).getAsUser(); - String cardId = Objects.requireNonNull(event.getOption("id")).getAsString(); - - if (!database.cardExists(cardId)) { - event.reply("Unknown card").setEphemeral(true).queue(); - return; - } - - if (user.isBot() || user.isSystem()) { - event.reply("Invalid user").setEphemeral(true).queue(); - return; - } - - /*if ((battleData.containsKey(memberId) && battleData.get(memberId).containsCard(cardId)) - || (battles.containsKey(memberId) - && startedBattleData.get(battles.get(memberId)).cardHealth.containsKey(cardId))) { - event.reply("This card is currently not available").setEphemeral(true).queue(); - return; - }*/ - - boolean[] owns = {false}; - - database.getUserCards(memberId).forEach(card -> { - if (card.getId().equals(cardId)) { - owns[0] = true; - } - }); - - if (!owns[0]) { - event.reply("You don't own this card").setEphemeral(true).queue(); - return; - } - - try { - database.moveCard(cardId, user.getId()); - } catch (SQLException e) { - e.printStackTrace(); - event.reply(""" - Unexpected database error occurred.\ - - Contact <@1064940406560788540> if this continues happening. \ - Don't forget to provide the error ID.\ - - Error ID: jurhOd4zf9gUaAUP5X\s""" - ).setEphemeral(true).queue(); - } - - event.reply("You gave the card " + database.getCard(cardId).getListString() + - " to " + user.getAsMention()).setEphemeral(true).queue(); - event.getChannel().sendMessage(event.getMember().getAsMention() + " gave the card `" - + database.getCard(cardId).getListString() + "` to " + user.getAsMention()).queue(); - + + + var message = createListMessage( + event.reply("Listing all your cards:"), + database.getUserCards(memberId), + 0 + ); + message.setEphemeral(true).queue(sentMessage -> pages.put(memberId, 0)); + } + case "give" -> { + User user = Objects.requireNonNull(event.getOption("user")).getAsUser(); + String cardId = Objects.requireNonNull(event.getOption("id")).getAsString(); + + if (!database.cardExists(cardId)) { + event.reply("Unknown card").setEphemeral(true).queue(); + return; } - case "last" -> { - if (database.getUserCards(memberId).isEmpty()) { - event.reply("You don't have any card yet.").setEphemeral(true).queue(); - return; - } - - Card card = database.getUserCards(memberId).getLast(); - event.deferReply(true).queue(); - - try { - CardCreator.createCardImage(new Card("id", "owner", card.getType(), - card.getAtkBonus(), card.getHpBonus())); - } catch (IOException | FontFormatException e) { - e.printStackTrace(); - event.reply(""" - Unexpected database error occurred.\ - - Contact <@1064940406560788540> if this continues happening. \ - Don't forget to provide the error ID.\ - - Error ID: papsq6Z8VTFCZKjmst\s""" - ).setEphemeral(true).queue(); + + if (user.isBot() || user.isSystem()) { + event.reply("Invalid user").setEphemeral(true).queue(); + return; + } + + /*if ((battleData.containsKey(memberId) && battleData.get(memberId).containsCard(cardId)) + || (battles.containsKey(memberId) + && startedBattleData.get(battles.get(memberId)).cardHealth.containsKey(cardId))) { + event.reply("This card is currently not available").setEphemeral(true).queue(); + return; + }*/ + + boolean[] owns = {false}; + + database.getUserCards(memberId).forEach(card -> { + if (card.getId().equals(cardId)) { + owns[0] = true; } - - event.getHook().sendMessage(card.toString()) - .addFiles(FileUpload.fromData( - new File("image.jpg") - )) - .queue(); - + }); + + if (!owns[0]) { + event.reply("You don't own this card").setEphemeral(true).queue(); + return; + } + + try { + database.moveCard(cardId, user.getId()); + } catch (SQLException e) { + e.printStackTrace(); + event.reply(""" + Unexpected database error occurred.\ + + Contact <@1064940406560788540> if this continues happening. \ + Don't forget to provide the error ID.\ + + Error ID: jurhOd4zf9gUaAUP5X\s""" + ).setEphemeral(true).queue(); + } + + event.reply("You gave the card " + database.getCard(cardId).getListString() + + " to " + user.getAsMention()).setEphemeral(true).queue(); + event.getChannel().sendMessage(event.getMember().getAsMention() + " gave the card `" + + database.getCard(cardId).getListString() + "` to " + user.getAsMention()).queue(); + + } + case "last" -> { + if (database.getUserCards(memberId).isEmpty()) { + event.reply("You don't have any card yet.").setEphemeral(true).queue(); + return; + } + + Card card = database.getUserCards(memberId).getLast(); + event.deferReply(true).queue(); + + try { + CardCreator.createCardImage(new Card("id", "owner", card.getType(), + card.getAtkBonus(), card.getHpBonus())); + } catch (IOException | FontFormatException e) { + e.printStackTrace(); + event.reply(""" + Unexpected database error occurred.\ + + Contact <@1064940406560788540> if this continues happening. \ + Don't forget to provide the error ID.\ + + Error ID: papsq6Z8VTFCZKjmst\s""" + ).setEphemeral(true).queue(); } + + event.getHook().sendMessage(card.toString()) + .addFiles(FileUpload.fromData( + new File("image.jpg") + )) + .queue(); + } + } } public static ReplyCallbackAction createListMessage(ReplyCallbackAction message, List cards, int page) { diff --git a/src/main/java/me/kub94ek/command/impl/executors/CoinCommandExecutor.java b/src/main/java/me/kub94ek/command/impl/executors/CoinCommandExecutor.java new file mode 100644 index 0000000..895e9e5 --- /dev/null +++ b/src/main/java/me/kub94ek/command/impl/executors/CoinCommandExecutor.java @@ -0,0 +1,43 @@ +package me.kub94ek.command.impl.executors; + +import me.kub94ek.Main; +import me.kub94ek.command.CommandExecutor; +import me.kub94ek.data.database.Database; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.interactions.commands.OptionMapping; + +public class CoinCommandExecutor implements CommandExecutor { + @Override + public void executeCommand(SlashCommandInteractionEvent event) { + String memberId = event.getMember().getId(); + Database database = Main.getDatabase(); + + switch (event.getSubcommandName()) { + case "bal" -> event.reply( + "Your balance: " + database.getCoins(memberId) + " coins" + ).setEphemeral(true).queue(); + case "give" -> { + OptionMapping userOption = event.getOption("user"); + OptionMapping coinsOption = event.getOption("coins"); + User user = userOption.getAsUser(); + int coins = coinsOption.getAsInt(); + + if (coins > database.getCoins(memberId)) { + event.reply("You don't have enough coins!").setEphemeral(true).queue(); + return; + } + + if (user.isBot() || user.isSystem() || user.getId().equals(memberId)) { + event.reply("Invalid user!").setEphemeral(true).queue(); + return; + } + + database.giveCoins(memberId, user.getId(), coins); + event.reply("Gave " + coins + " coins to <@" + user.getId() + ">").setEphemeral(true).queue(); + + } + } + } + +}