Skip to content

Commit d6fd4b7

Browse files
committed
start and end date options for paginated commands
1 parent d0ff589 commit d6fd4b7

File tree

13 files changed

+129
-21
lines changed

13 files changed

+129
-21
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import vc.openapi.handler.ChatsApi;
1313
import vc.openapi.model.ChatSearchResponse;
1414

15+
import java.time.LocalDate;
1516
import java.util.Optional;
1617

1718
import static discord4j.common.util.TimestampFormat.SHORT_DATE_TIME;
@@ -43,6 +44,17 @@ public Mono<Message> handle(final ChatInputInteractionEvent event) {
4344
if (word.length() < 4 || word.length() > 50) {
4445
return error(event, "Word must be between 4 and 50 characters");
4546
}
47+
LocalDate startDate;
48+
LocalDate endDate;
49+
try {
50+
startDate = getLocalDateIfPresent(event, "startdate");
51+
endDate = getLocalDateIfPresent(event, "enddate");
52+
if (startDate != null && endDate != null && startDate.isAfter(endDate)) {
53+
return error(event, "Start Date must be before End Date");
54+
}
55+
} catch (Exception e) {
56+
return error(event, "Invalid date. Required format: YYYY-MM-DD");
57+
}
4658
int page = event.getOption("page")
4759
.flatMap(ApplicationCommandInteractionOption::getValue)
4860
.map(ApplicationCommandInteractionOptionValue::asLong)
@@ -53,7 +65,7 @@ public Mono<Message> handle(final ChatInputInteractionEvent event) {
5365
return Mono.defer(() -> {
5466
ChatSearchResponse response;
5567
try {
56-
response = chatsApi.chatSearch(word, null, null, 25, page);
68+
response = chatsApi.chatSearch(word, startDate, endDate, 25, page);
5769
} catch (final Exception e) {
5870
LOGGER.error("Error searching chats for word: {}", word, e);
5971
return error(event, "Error during search");

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import vc.openapi.model.ChatsResponse;
1313
import vc.util.PlayerLookup;
1414

15+
import java.time.LocalDate;
1516
import java.util.List;
1617

1718
import static discord4j.common.util.TimestampFormat.SHORT_DATE_TIME;
@@ -36,10 +37,10 @@ public Mono<Message> handle(final ChatInputInteractionEvent event) {
3637
return resolveData(event, this::resolveChats);
3738
}
3839

39-
private Mono<Message> resolveChats(final ChatInputInteractionEvent event, final ProfileData identity, int page) {
40+
private Mono<Message> resolveChats(final ChatInputInteractionEvent event, final ProfileData identity, int page, LocalDate startDate, LocalDate endDate) {
4041
ChatsResponse chatsResponse = null;
4142
try {
42-
chatsResponse = chatsApi.chats(identity.uuid(), null, null, null, 25, page);
43+
chatsResponse = chatsApi.chats(identity.uuid(), null, startDate, endDate, 25, page);
4344
} catch (final Exception e) {
4445
LOGGER.error("Error processing chats response", e);
4546
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import vc.openapi.model.ConnectionsResponse;
1313
import vc.util.PlayerLookup;
1414

15+
import java.time.LocalDate;
1516
import java.util.List;
1617

1718
import static discord4j.common.util.TimestampFormat.SHORT_DATE_TIME;
@@ -37,10 +38,10 @@ public Mono<Message> handle(final ChatInputInteractionEvent event) {
3738
return resolveData(event, this::resolveConnections);
3839
}
3940

40-
private Mono<Message> resolveConnections(final ChatInputInteractionEvent event, final ProfileData identity, int page) {
41+
private Mono<Message> resolveConnections(final ChatInputInteractionEvent event, final ProfileData identity, int page, LocalDate startDate, LocalDate endDate) {
4142
ConnectionsResponse connectionsResponse = null;
4243
try {
43-
connectionsResponse = connectionsApi.connections(identity.uuid(), null, null, null, 25, page);
44+
connectionsResponse = connectionsApi.connections(identity.uuid(), null, startDate, endDate, 25, page);
4445
} catch (final Exception e){
4546
LOGGER.error("Error processing connections response", e);
4647
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import vc.openapi.model.DeathsResponse;
1313
import vc.util.PlayerLookup;
1414

15+
import java.time.LocalDate;
1516
import java.util.List;
1617

1718
import static discord4j.common.util.TimestampFormat.SHORT_DATE_TIME;
@@ -37,10 +38,10 @@ public Mono<Message> handle(final ChatInputInteractionEvent event) {
3738
return resolveData(event, this::resolveDeaths);
3839
}
3940

40-
private Mono<Message> resolveDeaths(final ChatInputInteractionEvent event, final ProfileData identity, int page) {
41+
private Mono<Message> resolveDeaths(final ChatInputInteractionEvent event, final ProfileData identity, int page, LocalDate startDate, LocalDate endDate) {
4142
DeathsResponse deathsResponse = null;
4243
try {
43-
deathsResponse = deathsApi.deaths(identity.uuid(), null, null, null, 25, page);
44+
deathsResponse = deathsApi.deaths(identity.uuid(), null, startDate, endDate, 25, page);
4445
} catch (final Exception e) {
4546
LOGGER.error("Failed to get deaths", e);
4647
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import vc.openapi.model.KillsResponse;
1313
import vc.util.PlayerLookup;
1414

15+
import java.time.LocalDate;
1516
import java.util.List;
1617

1718
import static discord4j.common.util.TimestampFormat.SHORT_DATE_TIME;
@@ -37,10 +38,10 @@ public Mono<Message> handle(final ChatInputInteractionEvent event) {
3738
return resolveData(event, this::resolveKills);
3839
}
3940

40-
private Mono<Message> resolveKills(final ChatInputInteractionEvent event, final ProfileData identity, int page) {
41+
private Mono<Message> resolveKills(final ChatInputInteractionEvent event, final ProfileData identity, int page, LocalDate startDate, LocalDate endDate) {
4142
KillsResponse killsResponse = null;
4243
try {
43-
killsResponse = deathsApi.kills(identity.uuid(), null, null, null, 25, page);
44+
killsResponse = deathsApi.kills(identity.uuid(), null, startDate, endDate, 25, page);
4445
} catch (final Exception e) {
4546
LOGGER.error("Error resolving kills", e);
4647
}

src/main/java/vc/commands/PlayerLookupCommand.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
import reactor.core.publisher.Mono;
99
import vc.api.model.ProfileData;
1010
import vc.util.PlayerLookup;
11-
import vc.util.TriFunction;
1211
import vc.util.Validator;
1312

13+
import java.time.LocalDate;
1414
import java.util.Optional;
15-
import java.util.function.BiFunction;
1615

1716
public abstract class PlayerLookupCommand implements SlashCommand {
1817
protected final PlayerLookup playerLookup;
@@ -28,7 +27,12 @@ EmbedCreateSpec.Builder populateIdentity(final EmbedCreateSpec.Builder builder,
2827
.addField("\u200B", "\u200B", true);
2928
}
3029

31-
Mono<Message> resolveData(ChatInputInteractionEvent event, BiFunction<ChatInputInteractionEvent, ProfileData, Mono<Message>> resolveFunction) {
30+
@FunctionalInterface
31+
public interface SimpleResolveFunction {
32+
Mono<Message> resolve(ChatInputInteractionEvent event, ProfileData identity);
33+
}
34+
35+
Mono<Message> resolveData(ChatInputInteractionEvent event, SimpleResolveFunction resolveFunction) {
3236
Optional<String> playerNameOptional = event.getOption("player")
3337
.flatMap(ApplicationCommandInteractionOption::getValue)
3438
.map(ApplicationCommandInteractionOptionValue::asString);
@@ -45,11 +49,16 @@ Mono<Message> resolveData(ChatInputInteractionEvent event, BiFunction<ChatInputI
4549
return error(event, "Unable to find player");
4650
}
4751
ProfileData identity = playerIdentityOptional.get();
48-
return resolveFunction.apply(event, identity);
52+
return resolveFunction.resolve(event, identity);
4953
});
5054
}
5155

52-
Mono<Message> resolveData(ChatInputInteractionEvent event, TriFunction<ChatInputInteractionEvent, ProfileData, Integer, Mono<Message>> resolveFunction) {
56+
@FunctionalInterface
57+
public interface PaginatedResolveFunction {
58+
Mono<Message> resolve(ChatInputInteractionEvent event, ProfileData identity, int page, LocalDate startDate, LocalDate endDate);
59+
}
60+
61+
Mono<Message> resolveData(ChatInputInteractionEvent event, PaginatedResolveFunction resolveFunction) {
5362
Optional<String> playerNameOptional = event.getOption("player")
5463
.flatMap(ApplicationCommandInteractionOption::getValue)
5564
.map(ApplicationCommandInteractionOptionValue::asString);
@@ -60,6 +69,17 @@ Mono<Message> resolveData(ChatInputInteractionEvent event, TriFunction<ChatInput
6069
if (!Validator.isValidPlayerName(playerName)) {
6170
return error(event, "Invalid player name");
6271
}
72+
LocalDate startDate;
73+
LocalDate endDate;
74+
try {
75+
startDate = getLocalDateIfPresent(event, "startdate");
76+
endDate = getLocalDateIfPresent(event, "enddate");
77+
if (startDate != null && endDate != null && startDate.isAfter(endDate)) {
78+
return error(event, "Start Date must be before End Date");
79+
}
80+
} catch (Exception e) {
81+
return error(event, "Invalid date. Required format: YYYY-MM-DD");
82+
}
6383
int page = event.getOption("page")
6484
.flatMap(ApplicationCommandInteractionOption::getValue)
6585
.map(ApplicationCommandInteractionOptionValue::asLong)
@@ -73,7 +93,7 @@ Mono<Message> resolveData(ChatInputInteractionEvent event, TriFunction<ChatInput
7393
return error(event, "Unable to find player");
7494
}
7595
ProfileData identity = playerIdentityOptional.get();
76-
return resolveFunction.apply(event, identity, page);
96+
return resolveFunction.resolve(event, identity, page, startDate, endDate);
7797
});
7898
}
7999
}

src/main/java/vc/commands/SlashCommand.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package vc.commands;
22

33
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
4+
import discord4j.core.object.command.ApplicationCommandInteractionOption;
5+
import discord4j.core.object.command.ApplicationCommandInteractionOptionValue;
46
import discord4j.core.object.entity.Message;
57
import discord4j.core.spec.EmbedCreateSpec;
68
import discord4j.rest.util.Color;
9+
import org.springframework.lang.Nullable;
710
import reactor.core.publisher.Mono;
811

12+
import java.time.LocalDate;
13+
914
/**
1015
* A simple interface defining our slash command class contract.
1116
* a getName() method to provide the case-sensitive name of the command.
@@ -29,4 +34,17 @@ default Mono<Message> error(ChatInputInteractionEvent event, final String messag
2934
default String escape(String message) {
3035
return message.replaceAll("_", "\\\\_");
3136
}
37+
38+
// throws runtime exception if date is present but format is invalid
39+
default @Nullable LocalDate getLocalDateIfPresent(ChatInputInteractionEvent event, String argName) {
40+
var inputOptional = event.getOption(argName)
41+
.flatMap(ApplicationCommandInteractionOption::getValue)
42+
.map(ApplicationCommandInteractionOptionValue::asString);
43+
if (inputOptional.isEmpty()) return null;
44+
try {
45+
return LocalDate.parse(inputOptional.get());
46+
} catch (Exception e) {
47+
throw new RuntimeException("Failed parsing date: " + inputOptional.get());
48+
}
49+
}
3250
}

src/main/java/vc/util/TriFunction.java

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/main/resources/commands/chats.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313
"description": "Results page",
1414
"type": 4,
1515
"required": false
16+
},
17+
{
18+
"name": "enddate",
19+
"description": "End Date (YYYY-MM-DD)",
20+
"type": 3,
21+
"required": false
22+
},
23+
{
24+
"name": "startdate",
25+
"description": "Start Date (YYYY-MM-DD)",
26+
"type": 3,
27+
"required": false
1628
}
1729
]
1830
}

src/main/resources/commands/connections.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313
"description": "Results page",
1414
"type": 4,
1515
"required": false
16+
},
17+
{
18+
"name": "enddate",
19+
"description": "End Date (YYYY-MM-DD)",
20+
"type": 3,
21+
"required": false
22+
},
23+
{
24+
"name": "startdate",
25+
"description": "Start Date (YYYY-MM-DD)",
26+
"type": 3,
27+
"required": false
1628
}
1729
]
1830
}

0 commit comments

Comments
 (0)