Skip to content

Commit caaeaa3

Browse files
Added command reference link to messages that want you to take an action
1 parent cc489fd commit caaeaa3

8 files changed

Lines changed: 40 additions & 17 deletions

File tree

backend/src/main/java/dev/jb/befit/backend/discord/commands/CommandHandlerHelper.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import dev.jb.befit.backend.data.models.WebUser;
77
import dev.jb.befit.backend.discord.commands.exceptions.OptionNotFoundException;
88
import dev.jb.befit.backend.discord.commands.exceptions.ValueNotFoundException;
9+
import dev.jb.befit.backend.discord.registration.CommandRegistrarService;
910
import discord4j.common.util.Snowflake;
1011
import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent;
1112
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
@@ -271,4 +272,13 @@ public static String addCongratulationsString(ExerciseLog log, Integer spacing,
271272
}
272273
return "";
273274
}
275+
276+
public static String getCommandReference(String... commandNames) {
277+
var reference = new StringBuilder();
278+
for (int i = 0; i < commandNames.length; i++) {
279+
var commandName = commandNames[i];
280+
reference.append(String.format("</%s:%d> ", commandName, CommandRegistrarService.getCommandId(commandName)));
281+
}
282+
return reference.toString();
283+
}
274284
}

backend/src/main/java/dev/jb/befit/backend/discord/commands/handlers/exercises/ExerciseCreateCommandHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ public Mono<Void> execute(ChatInputInteractionEvent event) {
4141
.title("Your new exercise")
4242
.addField(
4343
String.format("#%d %s", exercise.getId(), exercise.getName()),
44-
String.format("Measurement: %s\nGoal direction: %s",
44+
String.format("Measurement: %s\nGoal direction: %s\n\n_Use %s to add a log for this exercise._\n_Use %s to add a goal for this exercise._",
4545
exercise.getMeasurementType().getLongName(),
46-
exercise.getGoalDirection().name().toLowerCase()
46+
exercise.getGoalDirection().name().toLowerCase(),
47+
CommandHandlerHelper.getCommandReference(CommandConstants.CommandLog),
48+
CommandHandlerHelper.getCommandReference(CommandConstants.CommandGoalsAdd)
4749
),
4850
false)
4951
.color(Color.GREEN)

backend/src/main/java/dev/jb/befit/backend/discord/commands/handlers/goals/GoalCancelCommandHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public Mono<Void> execute(ChatInputInteractionEvent event) {
4141

4242
var embed = EmbedCreateSpec.builder()
4343
.title(":no_entry_sign: Goal cancelled")
44+
.description(String.format("Why would you cancel a goal you are working so hard for? :thinking:\nIf you would like to change the target value of a goal you can just replace it by using the %s command again.", CommandHandlerHelper.getCommandReference(CommandConstants.CommandGoalsAdd)))
4445
.addField(goalsViewCommandHandler.getGoalField(goal))
4546
.color(Color.GREEN)
4647
.build();

backend/src/main/java/dev/jb/befit/backend/discord/commands/handlers/goals/GoalsViewCommandHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public InteractionReplyEditSpec getReplyEditSpec(Snowflake userId, int page) {
5353
.title(":dart: Your goals")
5454
.color(Color.GREEN);
5555

56-
if (goals.isEmpty()) embed.description(String.format("No goals yet, try creating a goal by using the command: `/%s`", CommandConstants.CommandGoalsAdd));
56+
if (goals.isEmpty()) embed.description(String.format("No goals yet. Use the %s command to add a goal.", CommandHandlerHelper.getCommandReference(CommandConstants.CommandGoalsAdd)));
5757

5858
goals.stream().skip((long) pageSize * page).limit(pageSize)
5959
.sorted(Comparator.comparing(Goal::getCreated).reversed())

backend/src/main/java/dev/jb/befit/backend/discord/commands/handlers/habits/HabitsViewAllCommandHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public InteractionReplyEditSpec getReplyEditSpec(Snowflake userId, int page, Hab
7171
return EmbedCreateFields.Field.of(habit.getName(), description.toString(), false);
7272
}).toList()
7373
);
74-
if (habitsPage.isEmpty()) embed.description("You have no habits yet, try adding some!");
74+
if (habitsPage.isEmpty()) embed.description(String.format("You have no habits yet, try adding some by using the command %s!", CommandHandlerHelper.getCommandReference(CommandConstants.CommandHabitsAdd)));
7575
var paginationControls = CommandHandlerHelper.getPaginationComponent(page, habitsPage.getTotalPages(), getCommandNameFilter(), String.valueOf(habitTimeRange));
7676
return InteractionReplyEditSpec.builder().addEmbed(embed.build()).addComponent(paginationControls).build();
7777
}

backend/src/main/java/dev/jb/befit/backend/discord/commands/handlers/help/HelpCommandHandler.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,12 @@ private EmbedCreateSpec getGeneralEmbed(String avatarUrl) {
4545
.title("About this bot")
4646
.description("This bot helps you track your gym progress and motivates you into a better lifestyle.\n\n### Useful commands:")
4747
.addFields(
48-
Field.of("/log", "Create a new log for an exercise. The more logs you create the better the bot can help you improve.", false),
49-
Field.of("/exercises create", "Don't see your favourite exercise inside of our catalogue? Just add it and track your progress for it!", false),
50-
Field.of("/exercises view ...", "View all the exercises, your own exercises or detailed information about a single exercise.", false),
51-
Field.of("/goals add", "Set goals for you to reach and work towards them.", false),
52-
Field.of("/progress", "Get a progress chart on a single exercise.", false),
53-
Field.of("/achievements", "See which achievements you have completed and which are still locked.", false),
54-
Field.of("/motivation", "If you are looking for motivation, just use this command and get some.", false)
48+
Field.of(CommandHandlerHelper.getCommandReference(CommandConstants.CommandLog), "Create a new log for an exercise. The more logs you create the better the bot can help you improve.", false),
49+
Field.of(CommandHandlerHelper.getCommandReference(CommandConstants.CommandExercisesCreate), "Don't see your favourite exercise inside of our catalogue? Just add it and track your progress for it!", false),
50+
Field.of(CommandHandlerHelper.getCommandReference(CommandConstants.CommandExercisesViewAll, CommandConstants.CommandExercisesViewMy, CommandConstants.CommandExercisesViewOne), "View all the exercises, your own exercises or detailed information about a single exercise.", false),
51+
Field.of(CommandHandlerHelper.getCommandReference(CommandConstants.CommandGoalsAdd), "Set goals for you to reach and work towards them.", false),
52+
Field.of(CommandHandlerHelper.getCommandReference(CommandConstants.CommandAchievements), "See which achievements you have completed and which are still locked.", false),
53+
Field.of(CommandHandlerHelper.getCommandReference(CommandConstants.CommandMotivation), "If you are looking for motivation, just use this command and get some.", false)
5554
)
5655
.color(Color.GRAY)
5756
.build();
@@ -75,14 +74,12 @@ private EmbedCreateSpec getCommandEmbed(String avatarUrl, String commandName) {
7574

7675
var fields = command.arguments()
7776
.stream()
78-
.map(a -> {
79-
return Field.of(a.name(), a.description(), false);
80-
})
77+
.map(a -> Field.of(a.name(), a.description(), false))
8178
.toList();
8279

8380
var embed = EmbedCreateSpec.builder()
8481
.author("Befit bot", "https://github.com/jordybronowicki37/befit-bot", avatarUrl)
85-
.title(capitalizeFirstLetter(commandName))
82+
.title(CommandHandlerHelper.getCommandReference(commandName))
8683
.description(description.toString())
8784
.fields(fields)
8885
.color(Color.GRAY);

backend/src/main/java/dev/jb/befit/backend/discord/commands/handlers/sessions/SessionViewOneCommandHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static InteractionReplyEditSpec getReplyEditSpec(ExerciseSession session,
5353
var logsPage = CommandHandlerHelper.getPageForList(page, CommandConstants.PageSize, logs);
5454
var logsDescriptionBuilder = new StringBuilder();
5555

56-
if (logsPage.isEmpty()) logsDescriptionBuilder.append("_You have not added any logs yet._");
56+
if (logsPage.isEmpty()) logsDescriptionBuilder.append(String.format("_You have not added any logs yet.\nUse the %s command to add a log._", CommandHandlerHelper.getCommandReference(CommandConstants.CommandLog)));
5757
else {
5858
logsPage.stream().sorted(Comparator.comparing(ExerciseLog::getCreated)).forEach(log -> {
5959
addGeneralLogString(logsDescriptionBuilder, log);

backend/src/main/java/dev/jb/befit/backend/discord/registration/CommandRegistrarService.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import java.io.IOException;
1919
import java.nio.charset.StandardCharsets;
2020
import java.util.Arrays;
21+
import java.util.HashMap;
2122
import java.util.List;
23+
import java.util.Map;
2224
import java.util.function.Consumer;
2325
import java.util.stream.Stream;
2426

@@ -35,6 +37,8 @@ public class CommandRegistrarService {
3537
@Value("${discord.guilds.management}")
3638
private Long managementGuildId;
3739

40+
private static Map<String, Long> commandIds = new HashMap<>();
41+
3842
private final GatewayDiscordClient discordClient;
3943
private final ResourceLoader resourceLoader;
4044
private final JacksonResources d4jMapper = JacksonResources.create();
@@ -81,11 +85,20 @@ private void bulkUpdateGlobalCommands(List<ApplicationCommandRequest> commands)
8185
assert applicationId != null;
8286

8387
applicationService.bulkOverwriteGlobalApplicationCommand(applicationId, commands)
84-
.doOnNext(cmd -> log.debug("Successfully updated global command: {}", cmd.name()))
88+
.doOnNext(cmd -> {
89+
log.debug("Successfully updated global command: {}", cmd.name());
90+
commandIds.put(cmd.name(), cmd.id().asLong());
91+
})
8592
.doOnError(e -> log.error("Failed to update global command: ", e))
8693
.subscribe();
8794
}
8895

96+
public static Long getCommandId(String commandName) {
97+
var parentName = commandName.split(" ")[0];
98+
if (commandIds.containsKey(parentName)) return commandIds.get(parentName);
99+
return null;
100+
}
101+
89102
public ApplicationCommandRequest getCommandConfigFile(String fileName) throws IOException {
90103
var resource = resourceLoader.getResource(String.format(commandsPath, fileName));
91104
var command = d4jMapper.getObjectMapper().readValue(resource.getContentAsString(StandardCharsets.UTF_8), ApplicationCommandRequest.class);

0 commit comments

Comments
 (0)