add checks to the skip command

This commit is contained in:
Lee 2024-07-06 00:41:31 +01:00
parent 27244d0d98
commit 3126935057
2 changed files with 15 additions and 12 deletions

@ -110,19 +110,24 @@ public class SpotifyFeature extends Feature {
if (!profile.hasLinkedAccount() || !spotifyService.hasTrackPlaying(user)) { if (!profile.hasLinkedAccount() || !spotifyService.hasTrackPlaying(user)) {
return checkSpotify(spotifyService, user); return checkSpotify(spotifyService, user);
} }
CurrentlyPlaying currentlyPlaying = spotifyService.getCurrentlyPlayingTrack(user); CurrentlyPlaying currentlyPlaying = spotifyService.getCurrentlyPlayingTrack(user);
Track track = (Track) currentlyPlaying.getItem(); Track track = (Track) currentlyPlaying.getItem();
String trackName = track.getName(); String trackName = track.getName();
for (Action action : currentlyPlaying.getActions().getDisallows().getDisallowedActions()) {
if (action.equals(Action.SKIPPING_NEXT)) {
return EmbedUtils.errorEmbed()
.setDescription("%s Unable to skip the track.".formatted(Emojis.CROSS_MARK_EMOJI));
}
}
spotifyService.skipTrack(user); spotifyService.skipTrack(user); // Skip the track
// Get the new track
CurrentlyPlaying newCurrentlyPlaying = SpotifyUtils.getNewTrack(spotifyService, user, trackName); CurrentlyPlaying newCurrentlyPlaying = SpotifyUtils.getNewTrack(spotifyService, user, trackName);
if (newCurrentlyPlaying == null) { if (newCurrentlyPlaying == null) {
return EmbedUtils.errorEmbed() return EmbedUtils.errorEmbed()
.setDescription("%s There are no more tracks in the queue.".formatted(Emojis.CROSS_MARK_EMOJI)); .setDescription("%s There are no more tracks in the queue.".formatted(Emojis.CROSS_MARK_EMOJI));
} }
Track newTrack = (Track) newCurrentlyPlaying.getItem(); Track newTrack = (Track) newCurrentlyPlaying.getItem();
return EmbedUtils.successEmbed() return EmbedUtils.successEmbed()
.setDescription(""" .setDescription("""
:track_next: Skipped the track: **[%s | %s](%s)** :track_next: Skipped the track: **[%s | %s](%s)**

@ -89,12 +89,10 @@ public class SpotifyService {
* Starts playback for the user. * Starts playback for the user.
* *
* @param user the user to start playback for * @param user the user to start playback for
* @return if the playback was started
*/ */
@SneakyThrows @SneakyThrows
public boolean skipTrack(BatUser user) { public void skipTrack(BatUser user) {
getSpotifyApi(user).skipUsersPlaybackToNextTrack().build().execute(); getSpotifyApi(user).skipUsersPlaybackToNextTrack().build().execute();
return true;
} }
/** /**
@ -105,10 +103,12 @@ public class SpotifyService {
*/ */
@SneakyThrows @SneakyThrows
public CurrentlyPlaying getCurrentlyPlayingTrack(BatUser user) { public CurrentlyPlaying getCurrentlyPlayingTrack(BatUser user) {
if (currentlyPlayingCache.containsKey(user)) { CurrentlyPlaying currentlyPlaying = currentlyPlayingCache.get(user);
return currentlyPlayingCache.get(user); // If the track is still playing return the cache otherwise fetch the track
if (currentlyPlaying != null && currentlyPlaying.getTimestamp() + currentlyPlaying.getProgress_ms() < System.currentTimeMillis()) {
return currentlyPlaying;
} }
CurrentlyPlaying currentlyPlaying = getSpotifyApi(user).getUsersCurrentlyPlayingTrack().build().execute(); currentlyPlaying = getSpotifyApi(user).getUsersCurrentlyPlayingTrack().build().execute();
currentlyPlayingCache.put(user, currentlyPlaying); currentlyPlayingCache.put(user, currentlyPlaying);
return currentlyPlaying; return currentlyPlaying;
} }
@ -127,7 +127,6 @@ public class SpotifyService {
* Pauses playback for the user. * Pauses playback for the user.
* *
* @param user the user to start playback for * @param user the user to start playback for
* @return if the playback was paused
*/ */
@SneakyThrows @SneakyThrows
public void pausePlayback(BatUser user) { public void pausePlayback(BatUser user) {
@ -138,7 +137,6 @@ public class SpotifyService {
* Pauses playback for the user. * Pauses playback for the user.
* *
* @param user the user to start playback for * @param user the user to start playback for
* @return if the playback was paused
*/ */
@SneakyThrows @SneakyThrows
public void resumePlayback(BatUser user) { public void resumePlayback(BatUser user) {
@ -183,7 +181,7 @@ public class SpotifyService {
/** /**
* Ensures the user has a valid Spotify access token. * Ensures the user has a valid Spotify access token.
* <p> * <p>
* If the token is expired, it will be refreshed. * If the token is expired, it will be refreshed.
* </p> * </p>
* *
* @param user the user to get the token for * @param user the user to get the token for