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."); } }
public async Task LastFmRecent(ICommand command) { try { var(settings, user) = await GetLastFmSettings(command["User"], (IGuildUser)command.Author); var client = new LastFmClient(settings.LastFmUsername, _integrationOptions.Value.LastFmKey); const int NumDisplayed = 100; var userInfoTask = client.GetUserInfo(); var results = await client.GetRecentTracks(count : NumDisplayed); if (!results.Any()) { throw new AbortException("This user hasn't scrobbled anything recently."); } var nowPlaying = results.First().NowPlaying; var pages = new PageCollectionBuilder(); var place = 1; foreach (var track in results.Take(NumDisplayed)) { string when = null; if (nowPlaying) { nowPlaying = false; when = "now playing"; } else if (track.Timestamp.HasValue) { when = (track.Timestamp.Value - DateTimeOffset.UtcNow).SimpleFormat(); } pages.AppendLine($"`{place++}>` **{FormatTrackLink(track.ToTrack(), true)}** by **{FormatArtistLink(track.Artist, true)}**" + (when != null ? $"_ – {when}_" : string.Empty)); } var userInfo = await userInfoTask; var embedFactory = new Func <EmbedBuilder>(() => { var author = new EmbedAuthorBuilder() .WithIconUrl(_websiteWalker.LfIconUrl) .WithName($"{user} last listened to..."); if (!settings.Anonymous) { author.WithUrl($"https://www.last.fm/user/{settings.LastFmUsername}"); } var embed = new EmbedBuilder() .WithColor(0xd9, 0x23, 0x23) .WithAuthor(author); if (userInfo?.Playcount != null) { embed.WithFooter($"{userInfo.Playcount} plays in total"); } return(embed); }); await command.Reply(pages.BuildEmbedCollection(embedFactory, 10), true); } catch (WebException e) when((e.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotFound) { ThrowUserNotFound((await command["User"].AsGuildUserOrName)?.Item2); } 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."); } }
public async Task NowPlaying(ICommand command) { try { var(settings, user) = await GetLastFmSettings(command["User"], (IGuildUser)command.Author); var client = new LastFmClient(settings.LastFmUsername, _integrationOptions.Value.LastFmKey); var topTracksTask = client.GetTopTracks(LastFmDataPeriod.Month, 100); var tracks = (await client.GetRecentTracks(count: 1)).ToList(); if (!tracks.Any()) { await command.Reply(GetNoScrobblesMessage((await command["User"].AsGuildUserOrName)?.Item2)); return; } var nowPlaying = tracks[0].NowPlaying; var current = await client.GetTrackInfo(tracks[0].Artist.Name, tracks[0].Name); // Description var description = new StringBuilder(); description.AppendLine($"**{FormatTrackLink(current ?? tracks[0].ToTrack())}** by **{FormatArtistLink(current?.Artist ?? tracks[0].Artist)}**"); if (!string.IsNullOrEmpty(current?.Album?.Name)) { description.AppendLine($"On {DiscordHelpers.BuildMarkdownUri(current.Album.Name, current.Album.Url)}"); } else if (!string.IsNullOrEmpty(tracks[0].Album?.Name)) { description.AppendLine($"On {tracks[0].Album.Name}"); } var embed = new EmbedBuilder() .WithDescription(description.ToString()) .WithColor(0xd9, 0x23, 0x23); // Image var imageUri = current?.Album?.ImageUri ?? tracks[0]?.Album?.ImageUri; if (imageUri != null) { embed.WithThumbnailUrl(imageUri.AbsoluteUri); } // Title var author = new EmbedAuthorBuilder().WithIconUrl(_websiteWalker.LfIconUrl); if (!settings.Anonymous) { author.WithUrl($"https://www.last.fm/user/{settings.LastFmUsername}"); } if (nowPlaying) { author.WithName($"{user} is now listening to..."); } else { author.WithName($"{user} last listened to..."); } embed.WithAuthor(author); // Playcount var playCount = (current?.Playcount ?? 0) + (nowPlaying ? 1 : 0); if (playCount == 1 && current?.Playcount != null) { embed.WithFooter($"First listen"); } else if (playCount > 1) { embed.WithFooter($"{playCount.ToEnglishOrdinal()} listen"); } // Month placement { var topTracks = await topTracksTask; int counter = 0, placement = 0, placementPlaycount = int.MaxValue; foreach (var track in topTracks) { counter++; if (placementPlaycount > track.Playcount) { placementPlaycount = track.Playcount.Value; placement = counter; } if (string.Compare(track.Url, (string)(current?.Url ?? tracks[0].Url), true) == 0) { var footer = placement == 1 ? "Most played track this month" : $"{placement.ToEnglishOrdinal()} most played this month"; if (embed.Footer != null) { embed.Footer.Text += " • " + footer; } else { embed.WithFooter(footer); } break; } } } // Previous if (nowPlaying && tracks.Count > 1) { var previous = tracks[1]; embed.AddField(x => x.WithName("Previous").WithValue($"{FormatTrackLink(previous?.ToTrack())} by {FormatArtistLink(previous?.Artist)}")); } await command.Reply(embed.Build()); } catch (WebException e) when((e.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotFound) { ThrowUserNotFound((await command["User"].AsGuildUserOrName)?.Item2); } 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."); } }