Skip to content

Commit

Permalink
cleanup logic and responses
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Oct 28, 2024
1 parent 9256f1c commit c7742a3
Show file tree
Hide file tree
Showing 18 changed files with 138 additions and 165 deletions.
38 changes: 17 additions & 21 deletions src/main/java/vc/commands/ChatSearchCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.time.LocalDate;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;

import static discord4j.common.util.TimestampFormat.SHORT_DATE_TIME;
import static org.slf4j.LoggerFactory.getLogger;
Expand Down Expand Up @@ -78,32 +79,27 @@ public Mono<Message> resolve(DeferrableInteractionEvent event, String word, int
}
}
if (response == null || response.getChats() == null || response.getChats().isEmpty())
return error(event, "No chats found");
var chatStrings = response.getChats().stream()
.map(c -> SHORT_DATE_TIME.format(c.getTime().toInstant()) + " **" + escape(c.getPlayerName()) + ":** " + escape(c.getChat()))
.toList();
StringBuilder result = new StringBuilder();
for (String s : chatStrings) {
if (result.length() + s.length() > 4090) {
LOGGER.warn("Chat message too long, truncating: {}", s);
break;
}
result.append(s).append("\n");
}

if (!result.isEmpty()) {
result = new StringBuilder(result.substring(0, result.length() - 1));
} else {
return event.createFollowup()
.withEmbeds(EmbedCreateSpec.builder()
.title("Chats")
.color(Color.CYAN)
.description("No chats found")
.color(Color.RUBY)
.description("No chats containing this word were found. That's pretty rare!")
.build());
}

final StringBuilder result = new StringBuilder();
final AtomicBoolean truncated = new AtomicBoolean(false);
response.getChats().stream()
.map(c -> SHORT_DATE_TIME.format(c.getTime().toInstant()) + " " + escape(c.getChat()))
.forEachOrdered(s -> {
if (result.length() + s.length() + 1 > 4090) {
truncated.set(true);
return;
}
result.append(s).append("\n");
});
if (!result.isEmpty()) result.deleteCharAt(result.length() - 1); // cut off the last newline
if (truncated.get()) LOGGER.warn("Truncated chat response");
return event.createFollowup()
.withEmbeds(EmbedCreateSpec.builder()
.title("Chat Search")
.color(Color.CYAN)
.description(result.toString())
.addField("Total", ""+response.getTotal(), true)
Expand Down
35 changes: 15 additions & 20 deletions src/main/java/vc/commands/ChatsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import vc.util.PlayerLookup;

import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import static discord4j.common.util.TimestampFormat.SHORT_DATE_TIME;
import static org.slf4j.LoggerFactory.getLogger;
Expand Down Expand Up @@ -71,32 +71,27 @@ private Mono<Message> resolveChats(final DeferrableInteractionEvent event, final
}
}
if (chatsResponse == null || chatsResponse.getChats() == null || chatsResponse.getChats().isEmpty())
return error(event, "No chats found");
List<String> chatStrings = chatsResponse.getChats().stream()
.map(c -> SHORT_DATE_TIME.format(c.getTime().toInstant()) + " " + escape(c.getChat()))
.toList();
StringBuilder result = new StringBuilder();
for (String s : chatStrings) {
if (result.length() + s.length() > 4090) {
LOGGER.warn("Chat message too long, truncating: {}", s);
break;
}
result.append(s).append("\n");
}
if (!result.isEmpty()) {
result = new StringBuilder(result.substring(0, result.length() - 1));
} else {
return event.createFollowup()
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.title("Chats")
.color(Color.CYAN)
.color(Color.RUBY)
.description("No chats found")
.thumbnail(identity.getAvatarURL())
.build());
}
final StringBuilder result = new StringBuilder();
final AtomicBoolean truncated = new AtomicBoolean(false);
chatsResponse.getChats().stream()
.map(c -> SHORT_DATE_TIME.format(c.getTime().toInstant()) + " " + escape(c.getChat()))
.forEachOrdered(s -> {
if (result.length() + s.length() + 1 > 4090) {
truncated.set(true);
return;
}
result.append(s).append("\n");
});
if (!result.isEmpty()) result.deleteCharAt(result.length() - 1); // cut off the last newline
if (truncated.get()) LOGGER.warn("Truncated chat response");
return event.createFollowup()
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.title("Chats")
.color(Color.CYAN)
.description(result.toString())
.addField("Total", ""+chatsResponse.getTotal(), true)
Expand Down
36 changes: 16 additions & 20 deletions src/main/java/vc/commands/ConnectionsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import vc.util.PlayerLookup;

import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import static discord4j.common.util.TimestampFormat.SHORT_DATE_TIME;
import static org.slf4j.LoggerFactory.getLogger;
Expand Down Expand Up @@ -74,32 +74,28 @@ private Mono<Message> resolveConnections(final DeferrableInteractionEvent event,
}
}
if (connectionsResponse == null || connectionsResponse.getConnections() == null || connectionsResponse.getConnections().isEmpty())
return error(event, "No connections found for player");
List<String> connectionStrings = connectionsResponse.getConnections().stream()
.map(c -> c.getConnection().getValue() + " " + SHORT_DATE_TIME.format(c.getTime().toInstant()))
.toList();
StringBuilder result = new StringBuilder();
for (String s : connectionStrings) {
if (result.length() + s.length() > 4090) {
LOGGER.warn("Message too long, truncating: {}", s);
break;
}
result.append(s).append("\n");
}
if (result.length() > 0) {
result = new StringBuilder(result.substring(0, result.length() - 1));
} else {
return event.createFollowup()
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.title("Connections")
.color(Color.CYAN)
.color(Color.RUBY)
.description("No connections found")
.thumbnail(identity.getAvatarURL())
.build());
}

final StringBuilder result = new StringBuilder();
final AtomicBoolean truncated = new AtomicBoolean(false);
connectionsResponse.getConnections().stream()
.map(c -> c.getConnection().getValue() + " " + SHORT_DATE_TIME.format(c.getTime().toInstant()))
.forEachOrdered(s -> {
if (result.length() + s.length() + 1 > 4090) {
truncated.set(true);
return;
}
result.append(s).append("\n");
});
if (!result.isEmpty()) result.deleteCharAt(result.length() - 1); // cut off the last newline
if (truncated.get()) LOGGER.warn("Truncated connections response");
return event.createFollowup()
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.title("Connections")
.color(Color.CYAN)
.description(result.toString())
.addField("Total", ""+connectionsResponse.getTotal(), true)
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/vc/commands/DataCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public Mono<Message> handle(final ChatInputInteractionEvent event) {
return resolvePlayerDataDump(event, ctx.profileData);
}

public Mono<Message> resolvePlayerDataDump(ChatInputInteractionEvent event, ProfileData playerIdentity) {
public Mono<Message> resolvePlayerDataDump(ChatInputInteractionEvent event, ProfileData identity) {
String playerDataDump = null;
try {
playerDataDump = vcDataDumpApi.getPlayerDataDump(playerIdentity.uuid(), null);
playerDataDump = vcDataDumpApi.getPlayerDataDump(identity.uuid(), null);
} catch (final Exception e){
if (e instanceof ApiException apiException
&& (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204)) {
Expand All @@ -56,15 +56,19 @@ public Mono<Message> resolvePlayerDataDump(ChatInputInteractionEvent event, Prof
}
}
if (playerDataDump == null)
return error(event, "No data found");
return event.createFollowup()
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.color(Color.RUBY)
.description("No Data")
.thumbnail(identity.getAvatarURL())
.build());
return event.createFollowup()
.withFiles(MessageCreateFields.File.of(playerIdentity.name() + ".csv", new ByteArrayInputStream(playerDataDump.getBytes())))
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), playerIdentity)
.title("Data Dump")
.withFiles(MessageCreateFields.File.of(identity.name() + ".csv", new ByteArrayInputStream(playerDataDump.getBytes())))
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.addField("Data Count", ""+playerDataDump.lines().count(), true)
.description("CSV Generated!")
.color(Color.CYAN)
.thumbnail(playerIdentity.getAvatarURL())
.thumbnail(identity.getAvatarURL())
.build());
}
}
35 changes: 15 additions & 20 deletions src/main/java/vc/commands/DeathsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import vc.util.PlayerLookup;

import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import static discord4j.common.util.TimestampFormat.SHORT_DATE_TIME;
import static org.slf4j.LoggerFactory.getLogger;
Expand Down Expand Up @@ -74,32 +74,27 @@ private Mono<Message> resolveDeaths(final DeferrableInteractionEvent event, fina
}
}
if (deathsResponse == null || deathsResponse.getDeaths() == null || deathsResponse.getDeaths().isEmpty())
return error(event, "No deaths found for player");
List<String> deathStrings = deathsResponse.getDeaths().stream()
.map(k -> SHORT_DATE_TIME.format(k.getTime().toInstant()) + " " + escape(k.getDeathMessage()))
.toList();
StringBuilder result = new StringBuilder();
for (String s : deathStrings) {
if (result.length() + s.length() > 4090) {
LOGGER.warn("Message too long, truncating: {}", s);
break;
}
result.append(s).append("\n");
}
if (result.length() > 0) {
result = new StringBuilder(result.substring(0, result.length() - 1));
} else {
return event.createFollowup()
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.title("Deaths")
.color(Color.CYAN)
.color(Color.RUBY)
.description("No deaths found")
.thumbnail(identity.getAvatarURL())
.build());
}
final StringBuilder result = new StringBuilder();
final AtomicBoolean truncated = new AtomicBoolean(false);
deathsResponse.getDeaths().stream()
.map(k -> SHORT_DATE_TIME.format(k.getTime().toInstant()) + " " + escape(k.getDeathMessage()))
.forEachOrdered(s -> {
if (result.length() + s.length() + 1 > 4090) {
truncated.set(true);
return;
}
result.append(s).append("\n");
});
if (!result.isEmpty()) result.deleteCharAt(result.length() - 1); // cut off the last newline
if (truncated.get()) LOGGER.warn("Truncated deaths response");
return event.createFollowup()
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.title("Deaths")
.color(Color.CYAN)
.description(result.toString())
.addField("Total", ""+deathsResponse.getTotal(), true)
Expand Down
21 changes: 7 additions & 14 deletions src/main/java/vc/commands/DeathsTopMonthCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import vc.openapi.handler.DeathsApi;
import vc.openapi.model.PlayerDeathOrKillCountResponse;

import java.util.List;

import static org.slf4j.LoggerFactory.getLogger;

@Component
Expand Down Expand Up @@ -39,28 +37,23 @@ public Mono<Message> handle(final ChatInputInteractionEvent event) {
if (response == null || response.getPlayers() == null || response.getPlayers().isEmpty()) {
return error(event, "Unable to resolve deaths list");
}
List<String> deathList = response.getPlayers().stream()
.map(death -> "**" + escape(death.getPlayerName()) + "**: " + death.getCount())
.toList();
StringBuilder result = new StringBuilder();
for (int i = 0, deathListSize = Math.min(50, deathList.size()); i < deathListSize; i++) {
final String s = deathList.get(i);
for (int i = 0, deathListSize = Math.min(50, response.getPlayers().size()); i < deathListSize; i++) {
var player = response.getPlayers().get(i);
String s = "**" + escape(player.getPlayerName()) + "**: " + player.getCount();
if (result.length() + s.length() > 4090) {
break;
}
result.append("*#" + (i+1) + ":* ").append(s).append("\n");
result.append("*#").append(i + 1).append(":* ").append(s).append("\n");
}
String resultString = result.toString();
if (resultString.length() > 0) {
resultString = resultString.substring(0, resultString.length() - 1);
} else {
return error(event, "No deaths data found");
if (!result.isEmpty()) {
result.deleteCharAt(result.length() - 1);
}
return event.createFollowup()
.withEmbeds(EmbedCreateSpec.builder()
.title("Top Deaths Count (30 days)")
.color(Color.CYAN)
.description(resultString)
.description(result.toString())
.build());
}
}
36 changes: 16 additions & 20 deletions src/main/java/vc/commands/KillsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import vc.util.PlayerLookup;

import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import static discord4j.common.util.TimestampFormat.SHORT_DATE_TIME;
import static org.slf4j.LoggerFactory.getLogger;
Expand Down Expand Up @@ -73,32 +73,28 @@ private Mono<Message> resolveKills(final DeferrableInteractionEvent event, final
}
}
if (killsResponse == null || killsResponse.getKills() == null || killsResponse.getKills().isEmpty())
return error(event, "No kills found for player");
List<String> killStrings = killsResponse.getKills().stream()
.map(k -> SHORT_DATE_TIME.format(k.getTime().toInstant()) + " " + escape(k.getDeathMessage()))
.toList();
StringBuilder result = new StringBuilder();
for (String s : killStrings) {
if (result.length() + s.length() > 4090) {
LOGGER.warn("Kills message too long, truncating: {}", s);
break;
}
result.append(s).append("\n");
}
if (result.length() > 0) {
result = new StringBuilder(result.substring(0, result.length() - 1));
} else {
return event.createFollowup()
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.title("Kills")
.color(Color.CYAN)
.color(Color.RUBY)
.description("No kills found")
.thumbnail(identity.getAvatarURL())
.build());
}

final StringBuilder result = new StringBuilder();
final AtomicBoolean truncated = new AtomicBoolean(false);
killsResponse.getKills().stream()
.map(k -> SHORT_DATE_TIME.format(k.getTime().toInstant()) + " " + escape(k.getDeathMessage()))
.forEachOrdered(s -> {
if (result.length() + s.length() + 1 > 4090) {
truncated.set(true);
return;
}
result.append(s).append("\n");
});
if (!result.isEmpty()) result.deleteCharAt(result.length() - 1); // cut off the last newline
if (truncated.get()) LOGGER.warn("Truncated kills response");
return event.createFollowup()
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.title("Kills")
.color(Color.CYAN)
.description(result.toString())
.addField("Total", ""+killsResponse.getTotal(), true)
Expand Down
Loading

0 comments on commit c7742a3

Please sign in to comment.