Skip to content

Commit

Permalink
Player identity as embed field
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Nov 8, 2023
1 parent 17c6849 commit c896f8d
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 57 deletions.
14 changes: 6 additions & 8 deletions src/main/java/vc/commands/ChatsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@
import static org.slf4j.LoggerFactory.getLogger;

@Component
public class ChatsCommand implements SlashCommand {
public class ChatsCommand extends PlayerLookupCommand {
private static final Logger LOGGER = getLogger(ChatsCommand.class);
private final ChatsApi chatsApi;
private final PlayerLookup playerLookup;

public ChatsCommand(final ChatsApi chatsApi, final PlayerLookup playerLookup) {
super(playerLookup);
this.chatsApi = chatsApi;
this.playerLookup = playerLookup;
}

@Override
Expand Down Expand Up @@ -72,16 +70,16 @@ private Mono<Message> resolveChats(final ChatInputInteractionEvent event, final
result = new StringBuilder(result.substring(0, result.length() - 1));
} else {
return event.createFollowup()
.withEmbeds(EmbedCreateSpec.builder()
.title("Chats: " + escape(identity.playerName()))
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.title("Chats")
.color(Color.CYAN)
.description("No chats found")
.thumbnail(playerLookup.getAvatarURL(identity.uuid()).toString())
.build());
}
return event.createFollowup()
.withEmbeds(EmbedCreateSpec.builder()
.title("Chats: " + escape(identity.playerName()))
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.title("Chats")
.color(Color.CYAN)
.description(result.toString())
.addField("Total", ""+chatsResponse.getTotal(), true)
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/vc/commands/ConnectionsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
import static org.slf4j.LoggerFactory.getLogger;

@Component
public class ConnectionsCommand implements SlashCommand {
public class ConnectionsCommand extends PlayerLookupCommand {
private static final Logger LOGGER = getLogger(ConnectionsCommand.class);
private final ConnectionsApi connectionsApi;
private final PlayerLookup playerLookup;

public ConnectionsCommand(final ConnectionsApi connectionsApi, final PlayerLookup playerLookup) {
super(playerLookup);
this.connectionsApi = connectionsApi;
this.playerLookup = playerLookup;
}

@Override
Expand Down Expand Up @@ -72,16 +71,16 @@ private Mono<Message> resolveConnections(final ChatInputInteractionEvent event,
result = new StringBuilder(result.substring(0, result.length() - 1));
} else {
return event.createFollowup()
.withEmbeds(EmbedCreateSpec.builder()
.title("Connections: " + escape(identity.playerName()))
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.title("Connections")
.color(Color.CYAN)
.description("No connections found")
.thumbnail(playerLookup.getAvatarURL(identity.uuid()).toString())
.build());
}
return event.createFollowup()
.withEmbeds(EmbedCreateSpec.builder()
.title("Connections: " + escape(identity.playerName()))
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.title("Connections")
.color(Color.CYAN)
.description(result.toString())
.addField("Total", ""+connectionsResponse.getTotal(), true)
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/vc/commands/DataCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
import java.util.Optional;

@Component
public class DataCommand implements SlashCommand {
private final PlayerLookup playerLookup;
public class DataCommand extends PlayerLookupCommand {
private final DataDumpApi dataDumpApi;

public DataCommand(final PlayerLookup playerLookup, final DataDumpApi dataDumpApi) {
this.playerLookup = playerLookup;
super(playerLookup);
this.dataDumpApi = dataDumpApi;
}

Expand All @@ -48,8 +47,8 @@ public Mono<Message> handle(final ChatInputInteractionEvent event) {
return error(event, "Unable to find player");
return event.createFollowup()
.withFiles(MessageCreateFields.File.of(playerIdentityOptional.get().playerName() + ".csv", new ByteArrayInputStream(playerDataDump.getBytes())))
.withEmbeds(EmbedCreateSpec.builder()
.title("Data Dump: " + escape(playerIdentityOptional.get().playerName()))
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), playerIdentityOptional.get())
.title("Data Dump")
.addField("Data Count", ""+playerDataDump.lines().count(), true)
.description("CSV Generated!")
.color(Color.CYAN)
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/vc/commands/DeathsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
import static org.slf4j.LoggerFactory.getLogger;

@Component
public class DeathsCommand implements SlashCommand {
public class DeathsCommand extends PlayerLookupCommand {
private static final Logger LOGGER = getLogger(DeathsCommand.class);
private final DeathsApi deathsApi;
private final PlayerLookup playerLookup;

public DeathsCommand(final DeathsApi deathsApi, final PlayerLookup playerLookup) {
super(playerLookup);
this.deathsApi = deathsApi;
this.playerLookup = playerLookup;
}

@Override
Expand Down Expand Up @@ -72,16 +71,16 @@ private Mono<Message> resolveDeaths(final ChatInputInteractionEvent event, final
result = new StringBuilder(result.substring(0, result.length() - 1));
} else {
return event.createFollowup()
.withEmbeds(EmbedCreateSpec.builder()
.title("Deaths: " + escape(identity.playerName()))
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.title("Deaths")
.color(Color.CYAN)
.description("No deaths found")
.thumbnail(playerLookup.getAvatarURL(identity.uuid()).toString())
.build());
}
return event.createFollowup()
.withEmbeds(EmbedCreateSpec.builder()
.title("Deaths: " + escape(identity.playerName()))
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.title("Deaths")
.color(Color.CYAN)
.description(result.toString())
.addField("Total", ""+deathsResponse.getTotal(), true)
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/vc/commands/KillsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
import static org.slf4j.LoggerFactory.getLogger;

@Component
public class KillsCommand implements SlashCommand {
public class KillsCommand extends PlayerLookupCommand {
private static final Logger LOGGER = getLogger(KillsCommand.class);
private final DeathsApi deathsApi;
private final PlayerLookup playerLookup;

public KillsCommand(final DeathsApi deathsApi, final PlayerLookup playerLookup) {
super(playerLookup);
this.deathsApi = deathsApi;
this.playerLookup = playerLookup;
}

@Override
Expand Down Expand Up @@ -72,16 +71,16 @@ private Mono<Message> resolveKills(final ChatInputInteractionEvent event, final
result = new StringBuilder(result.substring(0, result.length() - 1));
} else {
return event.createFollowup()
.withEmbeds(EmbedCreateSpec.builder()
.title("Kills: " + escape(identity.playerName()))
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.title("Kills")
.color(Color.CYAN)
.description("No kills found")
.thumbnail(playerLookup.getAvatarURL(identity.uuid()).toString())
.build());
}
return event.createFollowup()
.withEmbeds(EmbedCreateSpec.builder()
.title("Kills: " + escape(identity.playerName()))
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.title("Kills")
.color(Color.CYAN)
.description(result.toString())
.addField("Total", ""+killsResponse.getTotal(), true)
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/vc/commands/NamesCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
import static java.util.Objects.isNull;

@Component
public class NamesCommand implements SlashCommand {
public class NamesCommand extends PlayerLookupCommand {
private final NamesApi namesApi;
private final PlayerLookup playerLookup;

public NamesCommand(final NamesApi namesApi, final PlayerLookup playerLookup) {
super(playerLookup);
this.namesApi = namesApi;
this.playerLookup = playerLookup;
}

@Override
Expand Down Expand Up @@ -69,16 +68,16 @@ private Mono<Message> resolveNames(final ChatInputInteractionEvent event, final
result = new StringBuilder(result.substring(0, result.length() - 1));
} else {
return event.createFollowup()
.withEmbeds(EmbedCreateSpec.builder()
.title("Names: " + escape(identity.playerName()))
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.title("Names")
.color(Color.CYAN)
.description("No names found")
.thumbnail(playerLookup.getAvatarURL(identity.uuid()).toString())
.build());
}
return event.createFollowup()
.withEmbeds(EmbedCreateSpec.builder()
.title("Names: " + escape(identity.playerName()))
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.title("Names")
.color(Color.CYAN)
.description(result.toString())
.thumbnail(playerLookup.getAvatarURL(identity.uuid()).toString())
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/vc/commands/PlayerLookupCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package vc.commands;

import discord4j.core.spec.EmbedCreateSpec;
import vc.util.PlayerLookup;

public abstract class PlayerLookupCommand implements SlashCommand {
protected final PlayerLookup playerLookup;

public PlayerLookupCommand(final PlayerLookup playerLookup) {
this.playerLookup = playerLookup;
}

EmbedCreateSpec.Builder populateIdentity(final EmbedCreateSpec.Builder builder, PlayerLookup.PlayerIdentity identity) {
return builder
.addField("Player", "[" + escape(identity.playerName()) + "](" + playerLookup.getNameMCLink(identity.uuid()) + ")", true)
.addField("\u200B", "\u200B", true)
.addField("\u200B", "\u200B", true);
}
}
10 changes: 4 additions & 6 deletions src/main/java/vc/commands/PlayerStatsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
import static discord4j.common.util.TimestampFormat.SHORT_DATE_TIME;

@Component
public class PlayerStatsCommand implements SlashCommand {

private final PlayerLookup playerLookup;
public class PlayerStatsCommand extends PlayerLookupCommand {
private final StatsApi statsApi;

public PlayerStatsCommand(final PlayerLookup playerLookup, final StatsApi statsApi) {
this.playerLookup = playerLookup;
super(playerLookup);
this.statsApi = statsApi;
}

Expand All @@ -48,8 +46,8 @@ public Mono<Message> handle(final ChatInputInteractionEvent event) {
if (playerStats == null)
return error(event, "Unable to find player");
return event.createFollowup()
.withEmbeds(EmbedCreateSpec.builder()
.title("Player Stats: " + escape(playerNameOptional.get()))
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), playerIdentityOptional.get())
.title("Player Stats")
.color(Color.CYAN)
.addField("Joins", ""+playerStats.getJoinCount(), true)
.addField("Leaves", ""+playerStats.getLeaveCount(), true)
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/vc/commands/PlaytimeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@
import static java.util.Objects.isNull;

@Component
public class PlaytimeCommand implements SlashCommand {
public class PlaytimeCommand extends PlayerLookupCommand {

private final PlaytimeApi playtimeApi;
private final PlayerLookup playerLookup;

public PlaytimeCommand(final PlaytimeApi playtimeApi, final PlayerLookup playerLookup) {
super(playerLookup);
this.playtimeApi = playtimeApi;
this.playerLookup = playerLookup;
}

@Override
Expand All @@ -53,8 +52,8 @@ private Mono<Message> resolvePlaytime(ChatInputInteractionEvent event, final Pla
Integer playtimeSeconds = playtime.getPlaytimeSeconds();
String durationStr = formatDuration(playtimeSeconds);
return event.createFollowup()
.withEmbeds(EmbedCreateSpec.builder()
.title("Playtime: " + escape(identity.playerName()))
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.title("Playtime")
.color(Color.CYAN)
.description(durationStr)
.thumbnail(playerLookup.getAvatarURL(profileUUID).toString())
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/vc/commands/SeenCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
import static java.util.Objects.isNull;

@Component
public class SeenCommand implements SlashCommand {
public class SeenCommand extends PlayerLookupCommand {

private final SeenApi seenApi;
private final PlayerLookup playerLookup;

public SeenCommand(final SeenApi seenApi, final PlayerLookup playerLookup) {
super(playerLookup);
this.seenApi = seenApi;
this.playerLookup = playerLookup;
}

@Override
Expand Down Expand Up @@ -57,8 +56,8 @@ private Mono<Message> resolveSeen(final ChatInputInteractionEvent event, final P
OffsetDateTime lastSeen = seenResponse.getTime();
OffsetDateTime firstSeen = firstSeenResponse.getTime();
return event.createFollowup()
.withEmbeds(EmbedCreateSpec.builder()
.title("Seen: " + escape(identity.playerName()))
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
.title("Seen")
.color(Color.CYAN)
.addField("First seen", SHORT_DATE_TIME.format(firstSeen.toInstant()), false)
.addField("Last seen", SHORT_DATE_TIME.format(lastSeen.toInstant()), false)
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/vc/util/PlayerLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public URL getAvatarURL(String playerName) {
}
}

public String getNameMCLink(UUID uuid) {
return "https://namemc.com/profile/" + uuid.toString();
}

public Optional<UUID> getOrResolveUuid(final UUID uuid, final String username) {
if (uuid != null) return Optional.of(uuid);
return getPlayerIdentity(username.trim()).map(PlayerIdentity::uuid);
Expand Down

0 comments on commit c896f8d

Please sign in to comment.