Example #1
0
    public static async Task <string> AddToQueue(string channel, string song, bool channelEqualsTarget = true)
    {
        string?uri = SpotifyHelper.GetSpotifyUri(song);

        if (uri is null)
        {
            return("this isn't a valid track link");
        }

        SpotifyUser?user = await GetSpotifyUser(channel);

        if (user is null)
        {
            return($"can't add the song to the queue of {channel}, they have to register first");
        }

        if (user.SongRequestEnabled == true)
        {
            try
            {
                SpotifyClient client = new(user.AccessToken);
                await client.Player.AddToQueue(new(uri));

                FullTrack item = await client.Tracks.Get(uri.Remove("spotify:track:"));

                string[] artists  = item.Artists.GetArtistNames();
                string   response = $"{item.Name} by {string.Join(", ", artists)} has been added to the queue";

                if (!channelEqualsTarget)
                {
                    response = string.Concat(response, $" of {channel}");
                }
                return(response);
            }
            catch (APIException ex)
            {
                Logger.Log(ex);
                return($"no music playing on any device, {channel} has to start their playback first");
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                return($"an unknown error occurred. It might not be possible to request songs for this user");
            }
        }
        else
        {
            return($"song requests are currently not open, {channel} or a mod has to enable song requests first");
        }
    }