Added a way to specify the type of history to show

This commit is contained in:
Teriuihi 2022-09-16 05:16:37 +02:00
parent c59746d81f
commit ce9b57efe9

View File

@ -54,30 +54,86 @@ public class CommandHistory extends DiscordCommand {
String username = option.getAsString(); String username = option.getAsString();
if (username.length() == 36) option = event.getInteraction().getOption("type");
histUUID(username, event); if (option != null) {
String type = option.getAsString();
HistoryType historyType;
try {
historyType = HistoryType.valueOf(type);
} catch (IllegalArgumentException e) {
event.replyEmbeds(Util.genericErrorEmbed("Error", "Invalid history type"))
.setEphemeral(true).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
return;
}
if (username.length() == 36) {
UUID uuid = uuidFromName(username, event);
if (uuid == null)
return;
histUUID(uuid, event, historyType);
}
else
histName(username, event, historyType);
return;
}
if (username.length() == 36) {
UUID uuid = uuidFromName(username, event);
if (uuid == null)
return;
histUUID(uuid, event);
}
else else
histName(username, event); histName(username, event);
} }
private void histUUID(String username, SlashCommandInteractionEvent event) { private UUID uuidFromName(String username, SlashCommandInteractionEvent event) {
UUID uuid;
try { try {
uuid = UUID.fromString(username); return UUID.fromString(username);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
event.replyEmbeds(Util.genericErrorEmbed("Error", "Invalid UUID")) event.replyEmbeds(Util.genericErrorEmbed("Error", "Invalid UUID"))
.setEphemeral(true).queue(RestAction.getDefaultSuccess(), Util::handleFailure); .setEphemeral(true).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
return;
} }
return null;
}
List<History> historyList = QueriesHistory.getHistory(uuid); private void histName(String username, SlashCommandInteractionEvent event, HistoryType historyType) {
if (!username.matches("^[a-zA-Z0-9_]{2,16}$")) {
if (historyList == null) { event.replyEmbeds(Util.genericErrorEmbed("Error", "Invalid username"))
event.replyEmbeds(Util.genericErrorEmbed("Error", "Unable to retrieve history for user"))
.setEphemeral(true).queue(RestAction.getDefaultSuccess(), Util::handleFailure); .setEphemeral(true).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
return; return;
} }
sendHistEmbed(event, historyList, uuid);
UUID uuid = QueriesUserUUID.getUUIDByUsername(username);
if (uuid == null) {
event.replyEmbeds(Util.genericErrorEmbed("Error", "Could not find uuid for username"))
.setEphemeral(true).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
return;
}
hist(username, uuid, historyType, event);
}
private void histUUID(UUID uuid, SlashCommandInteractionEvent event, HistoryType historyType) {
String username = QueriesUserUUID.getUsernameByUUID(uuid);
if (username == null) {
event.replyEmbeds(Util.genericErrorEmbed("Error", "Unable to retrieve username for uuid"))
.setEphemeral(true).queue();
return;
}
hist(username, uuid, historyType, event);
}
private void histUUID(UUID uuid, SlashCommandInteractionEvent event) {
String username = QueriesUserUUID.getUsernameByUUID(uuid);
if (username == null) {
event.replyEmbeds(Util.genericErrorEmbed("Error", "Unable to retrieve username for uuid"))
.setEphemeral(true).queue();
return;
}
hist(username, uuid, event);
} }
private void histName(String username, SlashCommandInteractionEvent event) { private void histName(String username, SlashCommandInteractionEvent event) {
@ -94,6 +150,10 @@ public class CommandHistory extends DiscordCommand {
return; return;
} }
hist(username, uuid, event);
}
private void hist(String username, UUID uuid, SlashCommandInteractionEvent event) {
List<History> history = QueriesHistory.getHistory(uuid); List<History> history = QueriesHistory.getHistory(uuid);
if (history == null) { if (history == null) {
@ -104,13 +164,26 @@ public class CommandHistory extends DiscordCommand {
sendHistEmbed(event, history, username); sendHistEmbed(event, history, username);
} }
private void sendHistEmbed(SlashCommandInteractionEvent event, List<History> historyList, UUID uuid) { private void hist(String username, UUID uuid, HistoryType historyType, SlashCommandInteractionEvent event) {
String username = QueriesUserUUID.getUsernameByUUID(uuid); List<History> historyList;
if (username == null) { switch (historyType) {
event.replyEmbeds(Util.genericErrorEmbed("Error", "Unable to retrieve username for uuid")) case BAN -> historyList = QueriesHistory.getBans(uuid);
.setEphemeral(true).queue(); case MUTE -> historyList = QueriesHistory.getMutes(uuid);
case KICK -> historyList = QueriesHistory.getKicks(uuid);
case WARN -> historyList = QueriesHistory.getWarns(uuid);
default -> {
event.replyEmbeds(Util.genericErrorEmbed("Error", "Invalid history type"))
.setEphemeral(true).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
return;
}
}
if (historyList == null) {
event.replyEmbeds(Util.genericErrorEmbed("Error", "Unable to retrieve history for user"))
.setEphemeral(true).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
return; return;
} }
sendHistEmbed(event, historyList, username); sendHistEmbed(event, historyList, username);
} }
@ -155,8 +228,12 @@ public class CommandHistory extends DiscordCommand {
event.replyChoiceStrings(new ArrayList<>()).queue(); event.replyChoiceStrings(new ArrayList<>()).queue();
return; return;
} }
String type = option.getAsString().toUpperCase();
event.replyChoiceStrings(Arrays.stream(HistoryType.values()).map(HistoryType::name).collect(Collectors.toList())) String type = option.getAsString();
event.replyChoiceStrings(Arrays.stream(HistoryType.values())
.map(HistoryType::name)
.filter(value -> value.toUpperCase().startsWith(type.toUpperCase()))
.collect(Collectors.toList()))
.queue(RestAction.getDefaultSuccess(), Util::handleFailure); .queue(RestAction.getDefaultSuccess(), Util::handleFailure);
} }