Пример #1
0
        public async Task Track(ICommand command)
        {
            try
            {
                var client = await GetClient();

                var       trackIdMatch = SpotifyTrackIdRegex.Match(command["Track"]);
                FullTrack track;
                if (!trackIdMatch.Success)
                {
                    var sclient = await SpotifyClient.Create(_integrationOptions.Value.SpotifyId, _integrationOptions.Value.SpotifyKey);

                    var trackId = await sclient.SearchTrackId(command["Track"]);

                    if (trackId == null)
                    {
                        throw new AbortException($"Search for track `{command["Track"]}` returned no results. Consider passing a Spotify link instead.");
                    }

                    track = await client.GetTrackAsync(trackId);
                }
                else
                {
                    var trackId = trackIdMatch.Groups.Values.Skip(1).First(x => !string.IsNullOrEmpty(x.Value)).Value;
                    track = await client.GetTrackAsync(trackId);

                    if (track.HasError())
                    {
                        throw new AbortException($"Search for track with ID `{trackId}` returned no results. Are you sure the ID is correct?");
                    }
                }

                var embed = await PrepareTrackEmbed(client, track, "Track analysis", true);

                await command.Reply(embed.Build());
            }
            catch (WebException ex) when(ex.Response is HttpWebResponse r && r.StatusCode == HttpStatusCode.ServiceUnavailable)
            {
                await command.Reply($"Spotify API is currently unavailable. Please try again in a few seconds.");
            }
        }
Пример #2
0
        public async Task NowPlayingSpotify(ICommand command)
        {
            try
            {
                var(settings, user) = await GetLastFmSettings(command["User"], (IGuildUser)command.Author);

                var lfm            = new LastFmClient(settings.LastFmUsername, _integrationOptions.Value.LastFmKey);
                var nowPlayingTask = lfm.GetRecentTracks(count: 1);

                var spotify = await SpotifyClient.Create(_integrationOptions.Value.SpotifyId, _integrationOptions.Value.SpotifyKey);

                var nowPlaying = (await nowPlayingTask).FirstOrDefault();
                if (nowPlaying == null)
                {
                    await command.Reply(GetNoScrobblesMessage((await command["User"].AsGuildUserOrName)?.Item2));

                    return;
                }

                var url = await spotify.SearchTrackUrl($"{nowPlaying.Name} artist:{nowPlaying.Artist.Name}");

                if (string.IsNullOrEmpty(url))
                {
                    await command.Reply($"Can't find this track on Spotify...");

                    return;
                }

                await command.Reply($"<:sf:621852106235707392> **{user} is now listening to...**\n" + url);
            }
            catch (WebException e) when((e.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.Forbidden)
            {
                throw new CommandException("The bot can't access your recently listened tracks. \n\nPlease make sure you don't have `Hide recent listening information` checked in your Last.fm settings (Settings -> Privacy -> Recent listening).");
            }
            catch (WebException e)
            {
                await command.Reply($"Last.fm is down (error {(e.Response as HttpWebResponse)?.StatusCode.ToString() ?? e.Status.ToString()}). Please try again in a few seconds.");
            }
        }