From 099f92d0ac1d0490e67fc960fc1fe2285e3fe2eb Mon Sep 17 00:00:00 2001 From: Aleksei Date: Thu, 27 Feb 2020 00:50:43 -0500 Subject: [PATCH] Initial OutfitCommand refactor. INCOMPLETE --- build.gradle | 2 +- .../fsbot/commands/general/OutfitCommand.java | 116 +++++++++--------- 2 files changed, 61 insertions(+), 57 deletions(-) diff --git a/build.gradle b/build.gradle index 0f816bb..711a47b 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ plugins { id 'application' } -group 'studio.spiderling' +group 'dev.salmonllama' version '1.1.11' sourceCompatibility = 1.8 diff --git a/src/main/java/dev/salmonllama/fsbot/commands/general/OutfitCommand.java b/src/main/java/dev/salmonllama/fsbot/commands/general/OutfitCommand.java index b152310..98eaee7 100644 --- a/src/main/java/dev/salmonllama/fsbot/commands/general/OutfitCommand.java +++ b/src/main/java/dev/salmonllama/fsbot/commands/general/OutfitCommand.java @@ -5,85 +5,89 @@ package dev.salmonllama.fsbot.commands.general; +import dev.salmonllama.fsbot.database.controllers.OutfitController; import dev.salmonllama.fsbot.guthix.Command; import dev.salmonllama.fsbot.guthix.CommandContext; import dev.salmonllama.fsbot.guthix.CommandPermission; import dev.salmonllama.fsbot.guthix.PermissionType; -import org.javacord.api.entity.channel.TextChannel; -import org.javacord.api.entity.message.Message; -import dev.salmonllama.fsbot.utilities.database.DatabaseUtilities; +import org.javacord.api.util.logging.ExceptionLogger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; public class OutfitCommand extends Command { + private final int MAX_OUTFITS = 5; + private final Collection NON_TAG_ALIASES = new ArrayList<>(Arrays.asList("outfit", "o")); + @Override public String name() { return "Outfit"; } @Override public String description() { return "Generates a random image with the given tag. Use ~tags to see valid tags."; } @Override public String usage() { return "outfit "; } @Override public String category() { return "General"; } @Override public CommandPermission permission() { return new CommandPermission(PermissionType.NONE); } - @Override public Collection aliases() { return new ArrayList<>(Arrays.asList("outfit", "o")); } - - private final DatabaseUtilities db; - - public OutfitCommand(DatabaseUtilities db) { - this.db = db; - } + @Override public Collection aliases() { return initAliases(); } @Override public void onCommand(CommandContext ctx) { + // Parse command used for logic path + // if "outfit" -> look for tags. if no tags -> show random + // if not "outfit" -> match the command to existing tags -> look for numbers + String command = ctx.getUsedAlias(); String[] args = ctx.getArgs(); - TextChannel channel = ctx.getChannel(); - Message message = ctx.getMessage(); - // Find the first arg for tag/submitter/naught, find the next arg for number to send - int MAX_OUTFITS = 5; + OutfitController.getDistinctTags().thenAccept(tags -> { + if (tags.contains(command)) { // TODO: Hey uh, test casing + // args parsing and command logic with tag as caller + handleTagCommand(command, args, ctx); + } else if (NON_TAG_ALIASES.contains(command)) { + // args parsing and command logic with name as caller + } + }); + } - if (args.length == 0) { - channel.sendMessage(db.randomOutfit().generateEmbed()); - } - else if (message.getMentionedUsers().size() > 0) { - // Outfit needs to be from submitter - String userId = message.getMentionedUsers().get(0).getIdAsString(); + private void handleTagCommand(String command, String[] args, CommandContext ctx) { + switch (args.length) { + case 0: + // Send one single random outfit of the given tag + OutfitController.findRandomByTag(command).thenAccept(outfit -> { + ctx.reply(outfit.toString()); + }).exceptionally(ExceptionLogger.get()); // TODO: Exception logging + case 1: + // Send the given number of outfits, not to exceed 5 for ratelimit reasons + if (isNumeric(args[0])) { + int num = Math.min(Integer.parseInt(args[0]), MAX_OUTFITS); - if (args.length == 2) { - // Send a number of outfits - int iterator = ((Integer.parseInt(args[1]) > MAX_OUTFITS) ? MAX_OUTFITS : Integer.parseInt(args[1])); - - for (int i = 0; i < iterator; i++) { - channel.sendMessage(db.getOutfitBySubmitter(userId).generateEmbed()); + OutfitController.findRandomByTag(command, num).thenAccept(outfitsOpt -> { + outfitsOpt.ifPresent(outfits -> { + outfits.forEach(outfit -> ctx.reply(outfit.toString())); + }); + }); + } else { + ctx.reply("Improper command usage"); // TODO: Logging update reminder } - } - else { - channel.sendMessage(db.getOutfitBySubmitter(userId).generateEmbed()); - } - } - else if (db.getTags().contains(args[0].toLowerCase())) { - // Outfit needs to be tagged - String tag = args[0]; - - if (args.length == 2) { - // Send a number of outfits - int iterator = ((Integer.parseInt(args[1]) > MAX_OUTFITS) ? MAX_OUTFITS : Integer.parseInt(args[1])); - - for (int i = 0; i < iterator; i++) { - channel.sendMessage(db.randomTaggedOutfit(tag).generateEmbed()); - } - } - else { - channel.sendMessage(db.randomTaggedOutfit(tag).generateEmbed()); - } - } - else if (args.length == 1 && args[0].matches("\\d")) { - int iterator = ((Integer.parseInt(args[0]) > MAX_OUTFITS) ? MAX_OUTFITS : Integer.parseInt(args[0])); - - for (int i = 0; i < iterator; i++) { - channel.sendMessage(db.randomOutfit().generateEmbed()); - } - } - else { - channel.sendMessage("Something went wrong"); } } + + private void handleNameCommand() { + + } + + private Collection initAliases() { + Collection aliases = OutfitController.getDistinctTags().join(); + aliases.addAll(NON_TAG_ALIASES); + return aliases; + } + + private boolean isNumeric(String input) { // If this is needed elsewhere, it will be moved to a public utility + if (input == null) { + return false; + } + + try { + double number = Double.parseDouble(input); + } catch (NumberFormatException e) { + return false; + } + return true; + } }