Initial OutfitCommand refactor. INCOMPLETE

This commit is contained in:
Aleksei 2020-02-27 00:50:43 -05:00
parent 29c30d928e
commit 099f92d0ac
2 changed files with 61 additions and 57 deletions

View File

@ -8,7 +8,7 @@ plugins {
id 'application'
}
group 'studio.spiderling'
group 'dev.salmonllama'
version '1.1.11'
sourceCompatibility = 1.8

View File

@ -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<String> 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 <String tag>"; }
@Override public String category() { return "General"; }
@Override public CommandPermission permission() { return new CommandPermission(PermissionType.NONE); }
@Override public Collection<String> aliases() { return new ArrayList<>(Arrays.asList("outfit", "o")); }
private final DatabaseUtilities db;
public OutfitCommand(DatabaseUtilities db) {
this.db = db;
}
@Override public Collection<String> 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]));
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
}
}
}
for (int i = 0; i < iterator; i++) {
channel.sendMessage(db.getOutfitBySubmitter(userId).generateEmbed());
}
}
else {
channel.sendMessage(db.getOutfitBySubmitter(userId).generateEmbed());
}
}
else if (db.getTags().contains(args[0].toLowerCase())) {
// Outfit needs to be tagged
String tag = args[0];
private void handleNameCommand() {
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());
private Collection<String> initAliases() {
Collection<String> aliases = OutfitController.getDistinctTags().join();
aliases.addAll(NON_TAG_ALIASES);
return aliases;
}
}
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 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;
}
}