Skip to content

Commit 63a9b48

Browse files
committed
improved embeds when starting and stopping games
1 parent d5c4e18 commit 63a9b48

File tree

4 files changed

+68
-23
lines changed

4 files changed

+68
-23
lines changed

src/main/java/pw/chew/mlb/commands/StartGameCommand.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.jagrosh.jdautilities.command.SlashCommand;
44
import com.jagrosh.jdautilities.command.SlashCommandEvent;
5+
import net.dv8tion.jda.api.EmbedBuilder;
6+
import net.dv8tion.jda.api.entities.MessageEmbed;
7+
import net.dv8tion.jda.api.entities.User;
58
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
69
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
710
import net.dv8tion.jda.api.interactions.DiscordLocale;
@@ -16,6 +19,7 @@
1619
import pw.chew.mlb.listeners.GameFeedHandler;
1720
import pw.chew.mlb.objects.ActiveGame;
1821
import pw.chew.mlb.objects.GameState;
22+
import pw.chew.mlb.util.EmbedUtil;
1923

2024
import java.time.OffsetDateTime;
2125
import java.time.format.DateTimeFormatter;
@@ -45,14 +49,18 @@ public StartGameCommand() {
4549
@Override
4650
protected void execute(SlashCommandEvent event) {
4751
String gamePk = event.getOption("game", "0", OptionMapping::getAsString);
48-
String startGame = startGame(gamePk, event.getGuildChannel());
49-
event.reply(startGame).setEphemeral(!startGame.contains("Starting game")).queue();
52+
try {
53+
MessageEmbed startGame = startGame(gamePk, event.getGuildChannel(), event.getUser());
54+
event.replyEmbeds(startGame).queue();
55+
} catch (IllegalStateException e) {
56+
event.replyEmbeds(EmbedUtil.failure(e.getMessage())).setEphemeral(true).queue();
57+
}
5058
}
5159

52-
public static String startGame(String gamePk, GuildMessageChannel channel) {
60+
public static MessageEmbed startGame(String gamePk, GuildMessageChannel channel, User invoker) {
5361
String currentGame = GameFeedHandler.currentGame(channel);
5462
if (currentGame != null) {
55-
return "This channel is already playing a game: " + currentGame + ". Please wait for it to finish, or stop it with `/stopgame`.";
63+
throw new IllegalStateException("This channel is already playing a game: " + currentGame + ". Please wait for it to finish, or stop it with `/stopgame`.");
5664
}
5765

5866
// Start a new thread
@@ -61,22 +69,30 @@ public static String startGame(String gamePk, GuildMessageChannel channel) {
6169

6270
// Refuse to start if the game is already over
6371
if (currentState.isFinal()) {
64-
return "This game is already over. Please start a different game.";
72+
throw new IllegalStateException("This game is already over. Please start a different game.");
6573
}
6674

6775
// We can only start games if the start time is less than 30 minutes away
6876
// E.g. if game starts at 2:30, and the time is 1:59, we cannot start the game
6977
// But, if it's 2:00, we can start the game
7078
// We can also start it if it's like, 2:56, who cares, maybe we forgot to start it
7179
if (OffsetDateTime.now().isBefore(currentState.officialDate().minusMinutes(30))) {
72-
return "This game is not yet ready to start. Please wait until the game is within 30 minutes of starting.";
80+
throw new IllegalStateException("This game is not yet ready to start. Please wait until the game is within 30 minutes of starting.");
7381
}
7482

7583
GameFeedHandler.addGame(activeGame);
7684

77-
return "Starting game with gamePk: " + gamePk + "\n" +
78-
currentState.away().clubName() + " @ " + currentState.home().clubName() + " at " +
79-
TimeFormat.DATE_TIME_SHORT.format(currentState.officialDate());
85+
List<String> description = new ArrayList<>();
86+
description.add("First Pitch: %s".formatted(TimeFormat.RELATIVE.format(currentState.officialDate())));
87+
description.add("\n*Invoked by %s*".formatted(invoker.getAsMention()));
88+
89+
EmbedBuilder embed = new EmbedBuilder()
90+
.setTitle("Starting Game **%s @ %s**".formatted(currentState.away().clubName(), currentState.home().clubName()))
91+
.setDescription(String.join("\n", description))
92+
.setColor(0x4fc94f)
93+
.setFooter("Game PK: %s".formatted(gamePk));
94+
95+
return embed.build();
8096
}
8197

8298
@Override

src/main/java/pw/chew/mlb/commands/StopGameCommand.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package pw.chew.mlb.commands;
22

3-
import com.jagrosh.jdautilities.command.CommandEvent;
43
import com.jagrosh.jdautilities.command.SlashCommand;
54
import com.jagrosh.jdautilities.command.SlashCommandEvent;
5+
import net.dv8tion.jda.api.EmbedBuilder;
66
import net.dv8tion.jda.api.interactions.DiscordLocale;
7+
import net.dv8tion.jda.api.utils.TimeFormat;
78
import pw.chew.mlb.listeners.GameFeedHandler;
9+
import pw.chew.mlb.objects.GameState;
10+
import pw.chew.mlb.util.EmbedUtil;
811

912
import java.util.Map;
1013

@@ -25,19 +28,17 @@ public StopGameCommand() {
2528
protected void execute(SlashCommandEvent event) {
2629
String stoppedGame = GameFeedHandler.stopGame(event.getGuildChannel());
2730
if (stoppedGame == null) {
28-
event.reply("No active game in this channel, please start a game first.").queue();
31+
event.replyEmbeds(EmbedUtil.failure("There is no active game in this channel. Please start a game first.")).setEphemeral(true).queue();
2932
} else {
30-
event.reply("Stopped game " + stoppedGame).queue();
31-
}
32-
}
33+
GameState state = GameState.fromPk(stoppedGame);
3334

34-
@Override
35-
protected void execute(CommandEvent event) {
36-
String stoppedGame = GameFeedHandler.stopGame(event.getGuildChannel());
37-
if (stoppedGame == null) {
38-
event.replyWarning("No active game in this channel, please start a game first.");
39-
} else {
40-
event.replySuccess("Stopped game " + stoppedGame);
35+
EmbedBuilder embed = new EmbedBuilder()
36+
.setTitle("Stopped Game **%s @ %s**".formatted(state.away().clubName(), state.home().clubName()))
37+
.setDescription("Game Date: " + TimeFormat.DATE_LONG.format(state.officialDate()))
38+
.setColor(0xd23d33)
39+
.setFooter("Game PK: %s".formatted(stoppedGame));
40+
41+
event.replyEmbeds(embed.build()).queue();
4142
}
4243
}
4344
}

src/main/java/pw/chew/mlb/listeners/InteractionHandler.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package pw.chew.mlb.listeners;
22

3+
import net.dv8tion.jda.api.entities.MessageEmbed;
34
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
45
import net.dv8tion.jda.api.hooks.ListenerAdapter;
56
import org.jetbrains.annotations.NotNull;
67
import pw.chew.mlb.commands.PlanGameCommand;
78
import pw.chew.mlb.commands.StartGameCommand;
9+
import pw.chew.mlb.util.EmbedUtil;
810
import pw.chew.mlb.util.MLBAPIUtil;
911

1012
import java.util.ArrayList;
@@ -21,8 +23,12 @@ public void onButtonInteraction(@NotNull ButtonInteractionEvent event) {
2123
.editMessageEmbeds(PlanGameCommand.generateGameBlurb(gamePk))
2224
.queue((m) -> event.reply("Refreshed!").setEphemeral(true).queue());
2325
case "start" -> {
24-
String startGame = StartGameCommand.startGame(gamePk, event.getGuildChannel());
25-
event.reply(startGame).setEphemeral(!startGame.contains("Starting game")).queue();
26+
try {
27+
MessageEmbed startGame = StartGameCommand.startGame(gamePk, event.getGuildChannel(), event.getUser());
28+
event.replyEmbeds(startGame).queue();
29+
} catch (IllegalStateException e) {
30+
event.replyEmbeds(EmbedUtil.failure(e.getMessage())).setEphemeral(true).queue();
31+
}
2632
}
2733
case "lineup" -> {
2834
String awayHome = event.getComponentId().split(":")[3];
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package pw.chew.mlb.util;
2+
3+
import net.dv8tion.jda.api.EmbedBuilder;
4+
import net.dv8tion.jda.api.entities.MessageEmbed;
5+
6+
public class EmbedUtil {
7+
private EmbedUtil() {}
8+
9+
/**
10+
* Returns a simple failure embed with the given message
11+
*
12+
* @param message The message to display
13+
* @return The failure embed
14+
*/
15+
public static MessageEmbed failure(String message) {
16+
return new EmbedBuilder()
17+
.setTitle("Time Out!")
18+
.setDescription(message)
19+
.setColor(0xd23d33)
20+
.build();
21+
}
22+
}

0 commit comments

Comments
 (0)