Fix outfit sending

This commit is contained in:
Salmonllama 2020-07-27 23:04:37 -04:00
parent 114630ead5
commit 3a1fbd57fc

View File

@ -6,15 +6,19 @@
package dev.salmonllama.fsbot.commands.general; package dev.salmonllama.fsbot.commands.general;
import dev.salmonllama.fsbot.database.controllers.OutfitController; import dev.salmonllama.fsbot.database.controllers.OutfitController;
import dev.salmonllama.fsbot.database.models.Outfit;
import dev.salmonllama.fsbot.guthix.Command; import dev.salmonllama.fsbot.guthix.Command;
import dev.salmonllama.fsbot.guthix.CommandContext; import dev.salmonllama.fsbot.guthix.CommandContext;
import dev.salmonllama.fsbot.guthix.CommandPermission; import dev.salmonllama.fsbot.guthix.CommandPermission;
import dev.salmonllama.fsbot.guthix.PermissionType; import dev.salmonllama.fsbot.guthix.PermissionType;
import org.javacord.api.entity.message.Message;
import org.javacord.api.entity.message.embed.EmbedBuilder;
import org.javacord.api.util.logging.ExceptionLogger; import org.javacord.api.util.logging.ExceptionLogger;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.concurrent.CompletableFuture;
public class OutfitCommand extends Command { public class OutfitCommand extends Command {
private final int MAX_OUTFITS = 5; private final int MAX_OUTFITS = 5;
@ -36,7 +40,7 @@ public class OutfitCommand extends Command {
String[] args = ctx.getArgs(); String[] args = ctx.getArgs();
OutfitController.getDistinctTags().thenAccept(tags -> { OutfitController.getDistinctTags().thenAccept(tags -> {
if (tags.contains(command)) { // TODO: Hey uh, test casing if (tags.contains(command)) {
// args parsing and command logic with tag as caller // args parsing and command logic with tag as caller
handleTagCommand(command, args, ctx); handleTagCommand(command, args, ctx);
} else if (NON_TAG_ALIASES.contains(command)) { } else if (NON_TAG_ALIASES.contains(command)) {
@ -50,17 +54,45 @@ public class OutfitCommand extends Command {
switch (args.length) { switch (args.length) {
case 0: case 0:
// Send one single random outfit of the given tag // Send one single random outfit of the given tag
OutfitController.findRandomByTag(command).thenAccept(outfit -> { OutfitController.findRandomByTag(command).thenAccept(possibleOutfit -> { // TODO: Add an orElse case
ctx.reply(outfit.toString()); possibleOutfit.ifPresent(outfit -> {
}).exceptionally(ExceptionLogger.get()); // TODO: Exception logging ctx.getApi().getUserById(outfit.getSubmitter()).thenAcceptAsync(user -> {
EmbedBuilder response = new EmbedBuilder()
.setTitle(user.getDiscriminatedName())
.setFooter(String.format("%s | %s", outfit.getTag(), outfit.getId()))
.setImage(outfit.getLink())
.setUrl(outfit.getLink());
if (!outfit.getMeta().equals("")) {
response.setDescription(outfit.getMeta());
}
ctx.reply(response);
});
});
}).exceptionally(ExceptionLogger.get());
case 1: case 1:
// Send the given number of outfits, not to exceed 5 for ratelimit reasons // Send the given number of outfits, not to exceed 5 for ratelimit reasons
if (isNumeric(args[0])) { if (isNumeric(args[0])) {
int num = Math.min(Integer.parseInt(args[0]), MAX_OUTFITS); int num = Math.min(Integer.parseInt(args[0]), MAX_OUTFITS);
OutfitController.findRandomByTag(command, num).thenAccept(outfitsOpt -> { OutfitController.findRandomByTag(command, num).thenAccept(possibleOutfits -> {
outfitsOpt.ifPresent(outfits -> { possibleOutfits.ifPresent(outfits -> {
outfits.forEach(outfit -> ctx.reply(outfit.toString())); outfits.forEach(outfit -> {
ctx.getApi().getUserById(outfit.getSubmitter()).thenAcceptAsync(user -> {
EmbedBuilder response = new EmbedBuilder()
.setTitle(user.getDiscriminatedName())
.setFooter(String.format("%s | %s", outfit.getTag(), outfit.getId()))
.setImage(outfit.getLink())
.setUrl(outfit.getLink());
if (!outfit.getMeta().equals("")) {
response.setDescription(outfit.getMeta());
}
ctx.reply(response);
});
});
}); });
}); });
} else { } else {
@ -73,24 +105,66 @@ public class OutfitCommand extends Command {
switch (args.length) { switch (args.length) {
case 0: case 0:
// Send one truly random outfit // Send one truly random outfit
OutfitController.findRandom().thenAccept(outfit -> { OutfitController.findRandom().thenAccept(possibleOutfit -> {
ctx.reply(outfit.toString()); possibleOutfit.ifPresent(outfit -> {
ctx.getApi().getUserById(outfit.getSubmitter()).thenAcceptAsync(user -> {
EmbedBuilder response = new EmbedBuilder()
.setTitle(user.getDiscriminatedName())
.setFooter(String.format("%s | %s", outfit.getTag(), outfit.getId()))
.setImage(outfit.getLink())
.setUrl(outfit.getLink());
if (!outfit.getMeta().equals("")) {
response.setDescription(outfit.getMeta());
}
ctx.reply(response);
});
});
}); });
case 1: case 1:
// Send the given number of outfits, not to exceed 5 for ratelimit reasons // Send the given number of outfits, not to exceed 5 for ratelimit reasons
if (isNumeric(args[0])) { if (isNumeric(args[0])) {
int num = Math.min(Integer.parseInt(args[0]), MAX_OUTFITS); int num = Math.min(Integer.parseInt(args[0]), MAX_OUTFITS);
OutfitController.findRandom(num).thenAccept(outfitsOpt -> { OutfitController.findRandom(num).thenAccept(possibleOutfits -> {
outfitsOpt.ifPresent(outfits -> { possibleOutfits.ifPresent(outfits -> {
outfits.forEach(outfit -> ctx.reply(outfit.toString())); outfits.forEach(outfit -> {
ctx.getApi().getUserById(outfit.getSubmitter()).thenAcceptAsync(user -> {
EmbedBuilder response = new EmbedBuilder()
.setTitle(user.getDiscriminatedName())
.setFooter(String.format("%s | %s", outfit.getTag(), outfit.getId()))
.setImage(outfit.getLink())
.setUrl(outfit.getLink());
if (!outfit.getMeta().equals("")) {
response.setDescription(outfit.getMeta());
}
ctx.reply(response);
});
});
}); });
}); });
} else { } else {
// First arg is not a number, send an outfit of the given tag // First arg is not a number, send an outfit of the given tag
String tag = args[0]; String tag = args[0];
OutfitController.findRandomByTag(tag).thenAccept(outfitOpt -> { OutfitController.findRandomByTag(tag).thenAccept(possibleOutfit -> {
outfitOpt.ifPresent(outfit -> ctx.reply(outfit.toString())); possibleOutfit.ifPresent(outfit -> {
ctx.getApi().getUserById(outfit.getSubmitter()).thenAcceptAsync(user -> {
EmbedBuilder response = new EmbedBuilder()
.setTitle(user.getDiscriminatedName())
.setFooter(String.format("%s | %s", outfit.getTag(), outfit.getId()))
.setImage(outfit.getLink())
.setUrl(outfit.getLink());
if (!outfit.getMeta().equals("")) {
response.setDescription(outfit.getMeta());
}
ctx.reply(response);
});
});
}); });
} }
case 2: case 2:
@ -98,9 +172,23 @@ public class OutfitCommand extends Command {
if (isNumeric(args[1])) { if (isNumeric(args[1])) {
int num = Math.min(Integer.parseInt(args[1]), MAX_OUTFITS); int num = Math.min(Integer.parseInt(args[1]), MAX_OUTFITS);
OutfitController.findRandomByTag(args[0], num).thenAccept(outfitsOpt -> { OutfitController.findRandomByTag(args[0], num).thenAccept(possibleOutfits -> {
outfitsOpt.ifPresent(outfits -> { possibleOutfits.ifPresent(outfits -> {
outfits.forEach(outfit -> ctx.reply(outfit.toString())); outfits.forEach(outfit -> {
ctx.getApi().getUserById(outfit.getSubmitter()).thenAcceptAsync(user -> {
EmbedBuilder response = new EmbedBuilder()
.setTitle(user.getDiscriminatedName())
.setFooter(String.format("%s | %s", outfit.getTag(), outfit.getId()))
.setImage(outfit.getLink())
.setUrl(outfit.getLink());
if (!outfit.getMeta().equals("")) {
response.setDescription(outfit.getMeta());
}
ctx.reply(response);
});
});
}); });
}); });
} }
@ -125,4 +213,9 @@ public class OutfitCommand extends Command {
} }
return true; return true;
} }
private CompletableFuture<Message> sendOutfit(Outfit outfit) {
// TODO: Replace sending with this
return null;
}
} }