Exemplo n.º 1
0
    public static async Task <PlayingItem?> GetCurrentlyPlayingTrack(string username)
    {
        SpotifyUser?user = await GetSpotifyUser(username);

        if (user is null)
        {
            return(null);
        }

        CurrentlyPlaying response = await new SpotifyClient(user.AccessToken).Player.GetCurrentlyPlaying(new());
        PlayingItem?     item     = SpotifyHelper.GetPlayingItem(response);

        return(item);
    }
Exemplo n.º 2
0
    public static async Task <string> GetCurrentlyPlaying(string username)
    {
        SpotifyUser?user = await GetSpotifyUser(username);

        if (user is null)
        {
            return($"can't request the current playing song, user {username} has to register first");
        }

        CurrentlyPlaying response = await new SpotifyClient(user.AccessToken).Player.GetCurrentlyPlaying(new());
        PlayingItem?     item     = SpotifyHelper.GetPlayingItem(response);

        if (item is not null)
        {
            return(item.Message);
        }
        else
        {
            return("nothing playing");
        }
    }
Exemplo n.º 3
0
    public static async Task <string> ListenTo(string username, string target)
    {
        SpotifyUser?user = await GetSpotifyUser(username);

        if (user is null)
        {
            return($"can't listen to other's songs, you have to register first");
        }

        if (username == target)
        {
            return($"you can't listen to your own song");
        }

        SpotifyUser?targetUser = await GetSpotifyUser(target);

        if (targetUser is null)
        {
            return($"can't listen to {target}'s songs, they have to register first");
        }

        SpotifyClient    targetClient           = new(targetUser.AccessToken);
        CurrentlyPlaying targetCurrentlyPlaying = await targetClient.Player.GetCurrentlyPlaying(new());

        PlayingItem?playingItem = SpotifyHelper.GetPlayingItem(targetCurrentlyPlaying);

        if (playingItem is null)
        {
            return($"{target} is currently not listening to a song");
        }

        if (playingItem.IsLocal)
        {
            return($"can't listen to {target}'s local file");
        }

        SpotifyClient userClient = new(user.AccessToken);

        try
        {
            await userClient.Player.AddToQueue(new(playingItem.Uri));
        }
        catch (APIException ex)
        {
            Logger.Log(ex);
            return($"no music playing on any device, you have to start your playback first");
        }
        catch (Exception ex)
        {
            Logger.Log(ex);
            return($"an unknown error occurred. It might not be possible to listen to songs");
        }

        try
        {
            await userClient.Player.SkipNext(new());
        }
        catch (Exception ex)
        {
            Logger.Log(ex);
            return($"an error occurred while trying to play the song");
        }

        return($"now playing {playingItem.Title} by {string.Join(", ", playingItem.Artists)} || {playingItem.Uri}");
    }