Skip to content

Commit c7742a3

Browse files
committed
cleanup logic and responses
1 parent 9256f1c commit c7742a3

18 files changed

+138
-165
lines changed

src/main/java/vc/commands/ChatSearchCommand.java

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.time.LocalDate;
2525
import java.util.Optional;
26+
import java.util.concurrent.atomic.AtomicBoolean;
2627

2728
import static discord4j.common.util.TimestampFormat.SHORT_DATE_TIME;
2829
import static org.slf4j.LoggerFactory.getLogger;
@@ -78,32 +79,27 @@ public Mono<Message> resolve(DeferrableInteractionEvent event, String word, int
7879
}
7980
}
8081
if (response == null || response.getChats() == null || response.getChats().isEmpty())
81-
return error(event, "No chats found");
82-
var chatStrings = response.getChats().stream()
83-
.map(c -> SHORT_DATE_TIME.format(c.getTime().toInstant()) + " **" + escape(c.getPlayerName()) + ":** " + escape(c.getChat()))
84-
.toList();
85-
StringBuilder result = new StringBuilder();
86-
for (String s : chatStrings) {
87-
if (result.length() + s.length() > 4090) {
88-
LOGGER.warn("Chat message too long, truncating: {}", s);
89-
break;
90-
}
91-
result.append(s).append("\n");
92-
}
93-
94-
if (!result.isEmpty()) {
95-
result = new StringBuilder(result.substring(0, result.length() - 1));
96-
} else {
9782
return event.createFollowup()
9883
.withEmbeds(EmbedCreateSpec.builder()
99-
.title("Chats")
100-
.color(Color.CYAN)
101-
.description("No chats found")
84+
.color(Color.RUBY)
85+
.description("No chats containing this word were found. That's pretty rare!")
10286
.build());
103-
}
87+
88+
final StringBuilder result = new StringBuilder();
89+
final AtomicBoolean truncated = new AtomicBoolean(false);
90+
response.getChats().stream()
91+
.map(c -> SHORT_DATE_TIME.format(c.getTime().toInstant()) + " " + escape(c.getChat()))
92+
.forEachOrdered(s -> {
93+
if (result.length() + s.length() + 1 > 4090) {
94+
truncated.set(true);
95+
return;
96+
}
97+
result.append(s).append("\n");
98+
});
99+
if (!result.isEmpty()) result.deleteCharAt(result.length() - 1); // cut off the last newline
100+
if (truncated.get()) LOGGER.warn("Truncated chat response");
104101
return event.createFollowup()
105102
.withEmbeds(EmbedCreateSpec.builder()
106-
.title("Chat Search")
107103
.color(Color.CYAN)
108104
.description(result.toString())
109105
.addField("Total", ""+response.getTotal(), true)

src/main/java/vc/commands/ChatsCommand.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import vc.util.PlayerLookup;
2222

2323
import java.time.LocalDate;
24-
import java.util.List;
24+
import java.util.concurrent.atomic.AtomicBoolean;
2525

2626
import static discord4j.common.util.TimestampFormat.SHORT_DATE_TIME;
2727
import static org.slf4j.LoggerFactory.getLogger;
@@ -71,32 +71,27 @@ private Mono<Message> resolveChats(final DeferrableInteractionEvent event, final
7171
}
7272
}
7373
if (chatsResponse == null || chatsResponse.getChats() == null || chatsResponse.getChats().isEmpty())
74-
return error(event, "No chats found");
75-
List<String> chatStrings = chatsResponse.getChats().stream()
76-
.map(c -> SHORT_DATE_TIME.format(c.getTime().toInstant()) + " " + escape(c.getChat()))
77-
.toList();
78-
StringBuilder result = new StringBuilder();
79-
for (String s : chatStrings) {
80-
if (result.length() + s.length() > 4090) {
81-
LOGGER.warn("Chat message too long, truncating: {}", s);
82-
break;
83-
}
84-
result.append(s).append("\n");
85-
}
86-
if (!result.isEmpty()) {
87-
result = new StringBuilder(result.substring(0, result.length() - 1));
88-
} else {
8974
return event.createFollowup()
9075
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
91-
.title("Chats")
92-
.color(Color.CYAN)
76+
.color(Color.RUBY)
9377
.description("No chats found")
9478
.thumbnail(identity.getAvatarURL())
9579
.build());
96-
}
80+
final StringBuilder result = new StringBuilder();
81+
final AtomicBoolean truncated = new AtomicBoolean(false);
82+
chatsResponse.getChats().stream()
83+
.map(c -> SHORT_DATE_TIME.format(c.getTime().toInstant()) + " " + escape(c.getChat()))
84+
.forEachOrdered(s -> {
85+
if (result.length() + s.length() + 1 > 4090) {
86+
truncated.set(true);
87+
return;
88+
}
89+
result.append(s).append("\n");
90+
});
91+
if (!result.isEmpty()) result.deleteCharAt(result.length() - 1); // cut off the last newline
92+
if (truncated.get()) LOGGER.warn("Truncated chat response");
9793
return event.createFollowup()
9894
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
99-
.title("Chats")
10095
.color(Color.CYAN)
10196
.description(result.toString())
10297
.addField("Total", ""+chatsResponse.getTotal(), true)

src/main/java/vc/commands/ConnectionsCommand.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import vc.util.PlayerLookup;
2525

2626
import java.time.LocalDate;
27-
import java.util.List;
27+
import java.util.concurrent.atomic.AtomicBoolean;
2828

2929
import static discord4j.common.util.TimestampFormat.SHORT_DATE_TIME;
3030
import static org.slf4j.LoggerFactory.getLogger;
@@ -74,32 +74,28 @@ private Mono<Message> resolveConnections(final DeferrableInteractionEvent event,
7474
}
7575
}
7676
if (connectionsResponse == null || connectionsResponse.getConnections() == null || connectionsResponse.getConnections().isEmpty())
77-
return error(event, "No connections found for player");
78-
List<String> connectionStrings = connectionsResponse.getConnections().stream()
79-
.map(c -> c.getConnection().getValue() + " " + SHORT_DATE_TIME.format(c.getTime().toInstant()))
80-
.toList();
81-
StringBuilder result = new StringBuilder();
82-
for (String s : connectionStrings) {
83-
if (result.length() + s.length() > 4090) {
84-
LOGGER.warn("Message too long, truncating: {}", s);
85-
break;
86-
}
87-
result.append(s).append("\n");
88-
}
89-
if (result.length() > 0) {
90-
result = new StringBuilder(result.substring(0, result.length() - 1));
91-
} else {
9277
return event.createFollowup()
9378
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
94-
.title("Connections")
95-
.color(Color.CYAN)
79+
.color(Color.RUBY)
9680
.description("No connections found")
9781
.thumbnail(identity.getAvatarURL())
9882
.build());
99-
}
83+
84+
final StringBuilder result = new StringBuilder();
85+
final AtomicBoolean truncated = new AtomicBoolean(false);
86+
connectionsResponse.getConnections().stream()
87+
.map(c -> c.getConnection().getValue() + " " + SHORT_DATE_TIME.format(c.getTime().toInstant()))
88+
.forEachOrdered(s -> {
89+
if (result.length() + s.length() + 1 > 4090) {
90+
truncated.set(true);
91+
return;
92+
}
93+
result.append(s).append("\n");
94+
});
95+
if (!result.isEmpty()) result.deleteCharAt(result.length() - 1); // cut off the last newline
96+
if (truncated.get()) LOGGER.warn("Truncated connections response");
10097
return event.createFollowup()
10198
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
102-
.title("Connections")
10399
.color(Color.CYAN)
104100
.description(result.toString())
105101
.addField("Total", ""+connectionsResponse.getTotal(), true)

src/main/java/vc/commands/DataCommand.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ public Mono<Message> handle(final ChatInputInteractionEvent event) {
4343
return resolvePlayerDataDump(event, ctx.profileData);
4444
}
4545

46-
public Mono<Message> resolvePlayerDataDump(ChatInputInteractionEvent event, ProfileData playerIdentity) {
46+
public Mono<Message> resolvePlayerDataDump(ChatInputInteractionEvent event, ProfileData identity) {
4747
String playerDataDump = null;
4848
try {
49-
playerDataDump = vcDataDumpApi.getPlayerDataDump(playerIdentity.uuid(), null);
49+
playerDataDump = vcDataDumpApi.getPlayerDataDump(identity.uuid(), null);
5050
} catch (final Exception e){
5151
if (e instanceof ApiException apiException
5252
&& (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204)) {
@@ -56,15 +56,19 @@ public Mono<Message> resolvePlayerDataDump(ChatInputInteractionEvent event, Prof
5656
}
5757
}
5858
if (playerDataDump == null)
59-
return error(event, "No data found");
59+
return event.createFollowup()
60+
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
61+
.color(Color.RUBY)
62+
.description("No Data")
63+
.thumbnail(identity.getAvatarURL())
64+
.build());
6065
return event.createFollowup()
61-
.withFiles(MessageCreateFields.File.of(playerIdentity.name() + ".csv", new ByteArrayInputStream(playerDataDump.getBytes())))
62-
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), playerIdentity)
63-
.title("Data Dump")
66+
.withFiles(MessageCreateFields.File.of(identity.name() + ".csv", new ByteArrayInputStream(playerDataDump.getBytes())))
67+
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
6468
.addField("Data Count", ""+playerDataDump.lines().count(), true)
6569
.description("CSV Generated!")
6670
.color(Color.CYAN)
67-
.thumbnail(playerIdentity.getAvatarURL())
71+
.thumbnail(identity.getAvatarURL())
6872
.build());
6973
}
7074
}

src/main/java/vc/commands/DeathsCommand.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import vc.util.PlayerLookup;
2525

2626
import java.time.LocalDate;
27-
import java.util.List;
27+
import java.util.concurrent.atomic.AtomicBoolean;
2828

2929
import static discord4j.common.util.TimestampFormat.SHORT_DATE_TIME;
3030
import static org.slf4j.LoggerFactory.getLogger;
@@ -74,32 +74,27 @@ private Mono<Message> resolveDeaths(final DeferrableInteractionEvent event, fina
7474
}
7575
}
7676
if (deathsResponse == null || deathsResponse.getDeaths() == null || deathsResponse.getDeaths().isEmpty())
77-
return error(event, "No deaths found for player");
78-
List<String> deathStrings = deathsResponse.getDeaths().stream()
79-
.map(k -> SHORT_DATE_TIME.format(k.getTime().toInstant()) + " " + escape(k.getDeathMessage()))
80-
.toList();
81-
StringBuilder result = new StringBuilder();
82-
for (String s : deathStrings) {
83-
if (result.length() + s.length() > 4090) {
84-
LOGGER.warn("Message too long, truncating: {}", s);
85-
break;
86-
}
87-
result.append(s).append("\n");
88-
}
89-
if (result.length() > 0) {
90-
result = new StringBuilder(result.substring(0, result.length() - 1));
91-
} else {
9277
return event.createFollowup()
9378
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
94-
.title("Deaths")
95-
.color(Color.CYAN)
79+
.color(Color.RUBY)
9680
.description("No deaths found")
9781
.thumbnail(identity.getAvatarURL())
9882
.build());
99-
}
83+
final StringBuilder result = new StringBuilder();
84+
final AtomicBoolean truncated = new AtomicBoolean(false);
85+
deathsResponse.getDeaths().stream()
86+
.map(k -> SHORT_DATE_TIME.format(k.getTime().toInstant()) + " " + escape(k.getDeathMessage()))
87+
.forEachOrdered(s -> {
88+
if (result.length() + s.length() + 1 > 4090) {
89+
truncated.set(true);
90+
return;
91+
}
92+
result.append(s).append("\n");
93+
});
94+
if (!result.isEmpty()) result.deleteCharAt(result.length() - 1); // cut off the last newline
95+
if (truncated.get()) LOGGER.warn("Truncated deaths response");
10096
return event.createFollowup()
10197
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
102-
.title("Deaths")
10398
.color(Color.CYAN)
10499
.description(result.toString())
105100
.addField("Total", ""+deathsResponse.getTotal(), true)

src/main/java/vc/commands/DeathsTopMonthCommand.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import vc.openapi.handler.DeathsApi;
1111
import vc.openapi.model.PlayerDeathOrKillCountResponse;
1212

13-
import java.util.List;
14-
1513
import static org.slf4j.LoggerFactory.getLogger;
1614

1715
@Component
@@ -39,28 +37,23 @@ public Mono<Message> handle(final ChatInputInteractionEvent event) {
3937
if (response == null || response.getPlayers() == null || response.getPlayers().isEmpty()) {
4038
return error(event, "Unable to resolve deaths list");
4139
}
42-
List<String> deathList = response.getPlayers().stream()
43-
.map(death -> "**" + escape(death.getPlayerName()) + "**: " + death.getCount())
44-
.toList();
4540
StringBuilder result = new StringBuilder();
46-
for (int i = 0, deathListSize = Math.min(50, deathList.size()); i < deathListSize; i++) {
47-
final String s = deathList.get(i);
41+
for (int i = 0, deathListSize = Math.min(50, response.getPlayers().size()); i < deathListSize; i++) {
42+
var player = response.getPlayers().get(i);
43+
String s = "**" + escape(player.getPlayerName()) + "**: " + player.getCount();
4844
if (result.length() + s.length() > 4090) {
4945
break;
5046
}
51-
result.append("*#" + (i+1) + ":* ").append(s).append("\n");
47+
result.append("*#").append(i + 1).append(":* ").append(s).append("\n");
5248
}
53-
String resultString = result.toString();
54-
if (resultString.length() > 0) {
55-
resultString = resultString.substring(0, resultString.length() - 1);
56-
} else {
57-
return error(event, "No deaths data found");
49+
if (!result.isEmpty()) {
50+
result.deleteCharAt(result.length() - 1);
5851
}
5952
return event.createFollowup()
6053
.withEmbeds(EmbedCreateSpec.builder()
6154
.title("Top Deaths Count (30 days)")
6255
.color(Color.CYAN)
63-
.description(resultString)
56+
.description(result.toString())
6457
.build());
6558
}
6659
}

src/main/java/vc/commands/KillsCommand.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import vc.util.PlayerLookup;
2525

2626
import java.time.LocalDate;
27-
import java.util.List;
27+
import java.util.concurrent.atomic.AtomicBoolean;
2828

2929
import static discord4j.common.util.TimestampFormat.SHORT_DATE_TIME;
3030
import static org.slf4j.LoggerFactory.getLogger;
@@ -73,32 +73,28 @@ private Mono<Message> resolveKills(final DeferrableInteractionEvent event, final
7373
}
7474
}
7575
if (killsResponse == null || killsResponse.getKills() == null || killsResponse.getKills().isEmpty())
76-
return error(event, "No kills found for player");
77-
List<String> killStrings = killsResponse.getKills().stream()
78-
.map(k -> SHORT_DATE_TIME.format(k.getTime().toInstant()) + " " + escape(k.getDeathMessage()))
79-
.toList();
80-
StringBuilder result = new StringBuilder();
81-
for (String s : killStrings) {
82-
if (result.length() + s.length() > 4090) {
83-
LOGGER.warn("Kills message too long, truncating: {}", s);
84-
break;
85-
}
86-
result.append(s).append("\n");
87-
}
88-
if (result.length() > 0) {
89-
result = new StringBuilder(result.substring(0, result.length() - 1));
90-
} else {
9176
return event.createFollowup()
9277
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
93-
.title("Kills")
94-
.color(Color.CYAN)
78+
.color(Color.RUBY)
9579
.description("No kills found")
9680
.thumbnail(identity.getAvatarURL())
9781
.build());
98-
}
82+
83+
final StringBuilder result = new StringBuilder();
84+
final AtomicBoolean truncated = new AtomicBoolean(false);
85+
killsResponse.getKills().stream()
86+
.map(k -> SHORT_DATE_TIME.format(k.getTime().toInstant()) + " " + escape(k.getDeathMessage()))
87+
.forEachOrdered(s -> {
88+
if (result.length() + s.length() + 1 > 4090) {
89+
truncated.set(true);
90+
return;
91+
}
92+
result.append(s).append("\n");
93+
});
94+
if (!result.isEmpty()) result.deleteCharAt(result.length() - 1); // cut off the last newline
95+
if (truncated.get()) LOGGER.warn("Truncated kills response");
9996
return event.createFollowup()
10097
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
101-
.title("Kills")
10298
.color(Color.CYAN)
10399
.description(result.toString())
104100
.addField("Total", ""+killsResponse.getTotal(), true)

0 commit comments

Comments
 (0)