This commit is contained in:
Braydon 2024-09-09 14:48:36 -04:00
parent 2785048de8
commit 444ae3f7c0
2 changed files with 78 additions and 18 deletions

@ -121,7 +121,14 @@ public final class DiscordUser {
*/ */
@AllArgsConstructor @Getter @AllArgsConstructor @Getter
public static class UserFlags { public static class UserFlags {
private final EnumSet<User.UserFlag> list; /**
* The list of flags the user has.
*/
@NonNull private final EnumSet<User.UserFlag> list;
/**
* The raw flags the user has.
*/
private final int raw; private final int raw;
} }
@ -130,7 +137,14 @@ public final class DiscordUser {
*/ */
@AllArgsConstructor @Getter @AllArgsConstructor @Getter
public static class Avatar { public static class Avatar {
/**
* The id of the user's avatar.
*/
@NonNull private final String id; @NonNull private final String id;
/**
* The URL of the user's avatar.
*/
@NonNull private final String url; @NonNull private final String url;
} }
@ -139,7 +153,14 @@ public final class DiscordUser {
*/ */
@AllArgsConstructor @Getter @AllArgsConstructor @Getter
public static class Banner { public static class Banner {
/**
* The id of the user's avatar.
*/
@NonNull private final String id; @NonNull private final String id;
/**
* The URL of the user's avatar.
*/
@NonNull private final String url; @NonNull private final String url;
} }
@ -148,11 +169,39 @@ public final class DiscordUser {
*/ */
@AllArgsConstructor @Getter @AllArgsConstructor @Getter
public static class SpotifyActivity { public static class SpotifyActivity {
/**
* The currently playing song.
*/
@NonNull private final String song; @NonNull private final String song;
/**
* The currently playing artist.
*/
@NonNull private final String artist; @NonNull private final String artist;
/**
* The album the song is from.
*/
@NonNull private final String album;
/**
* The current progress of the track.
*/
@NonNull private final String trackProgress; @NonNull private final String trackProgress;
/**
* The total length of the track.
*/
@NonNull private final String trackLength; @NonNull private final String trackLength;
/**
* The unix time of when this track started playing.
*/
private final long started; private final long started;
/**
* The unix time of when this track stops playing.
*/
private final long ends; private final long ends;
/** /**
@ -161,7 +210,7 @@ public final class DiscordUser {
* @param richPresence the raw Discord data * @param richPresence the raw Discord data
* @return the built Spotify activity * @return the built Spotify activity
*/ */
@NonNull @NonNull @SuppressWarnings("DataFlowIssue")
public static SpotifyActivity fromActivity(@NonNull RichPresence richPresence) { public static SpotifyActivity fromActivity(@NonNull RichPresence richPresence) {
SimpleDateFormat dateFormat = new SimpleDateFormat("m:ss"); SimpleDateFormat dateFormat = new SimpleDateFormat("m:ss");
long started = Objects.requireNonNull(richPresence.getTimestamps()).getStart(); long started = Objects.requireNonNull(richPresence.getTimestamps()).getStart();
@ -171,9 +220,9 @@ public final class DiscordUser {
long trackProgress = Math.min(System.currentTimeMillis() - started, trackLength); long trackProgress = Math.min(System.currentTimeMillis() - started, trackLength);
return new SpotifyActivity( return new SpotifyActivity(
Objects.requireNonNull(richPresence.getDetails()), Objects.requireNonNull(richPresence.getState()).replace(";", ","), richPresence.getDetails(), richPresence.getState().replace(";", ","),
dateFormat.format(trackProgress), dateFormat.format(trackLength), dateFormat.format(trackProgress), dateFormat.format(trackLength),
started, ends richPresence.getLargeImage().getText(), started, ends
); );
} }
} }

@ -72,20 +72,9 @@ public final class DiscordService {
throw new BadRequestException("Not a valid snowflake"); throw new BadRequestException("Not a valid snowflake");
} }
try { try {
// First try to locate the user in a guild Member member = getMember(snowflake); // First try to locate the member in a guild
Member member = null;
try { // Then retrieve the user (first try the cache)
for (Guild guild : jda.getGuilds()) {
if ((member = guild.retrieveMemberById(snowflake).complete()) != null) {
break;
}
}
} catch (ErrorResponseException ex) {
if (ex.getErrorCode() != 10007) {
throw ex;
}
}
// Then retrieve the user
CachedDiscordUser cachedUser = cachedUsers.getIfPresent(snowflake); CachedDiscordUser cachedUser = cachedUsers.getIfPresent(snowflake);
boolean fromCache = cachedUser != null; boolean fromCache = cachedUser != null;
if (cachedUser == null) { // No cache, retrieve fresh data if (cachedUser == null) { // No cache, retrieve fresh data
@ -130,4 +119,26 @@ public final class DiscordService {
self.getAsTag(), inviteUrl self.getAsTag(), inviteUrl
); );
} }
/**
* Get a member from a guild by their snowflake.
*
* @param snowflake the user's snowflake
* @return the member, null if none
*/
private Member getMember(long snowflake) {
Member member = null;
try {
for (Guild guild : jda.getGuilds()) {
if ((member = guild.retrieveMemberById(snowflake).complete()) != null) {
break;
}
}
} catch (ErrorResponseException ex) {
if (ex.getErrorCode() != 10007) {
throw ex;
}
}
return member;
}
} }