public async Task fmAsync(string user = null) { User userSettings = await _userService.GetUserSettingsAsync(Context.User); if (userSettings?.UserNameLastFM == null) { await UsernameNotSetErrorResponseAsync(); return; } if (user == "help") { await ReplyAsync( "Usage: `.fm 'lastfm username/discord user'` \n" + "You can set your default user and your display mode through the `.fmset 'username' 'embedfull/embedmini/textfull/textmini'` command."); this._logger.LogCommandUsed(Context.Guild?.Id, Context.Channel.Id, Context.User.Id, Context.Message.Content); return; } try { string lastFMUserName = userSettings.UserNameLastFM; bool self = true; if (user != null) { if (await _lastFmService.LastFMUserExistsAsync(user)) { lastFMUserName = user; self = false; } else if (!_guildService.CheckIfDM(Context)) { IGuildUser guildUser = await _guildService.FindUserFromGuildAsync(Context, user); if (guildUser != null) { User guildUserLastFM = await _userService.GetUserSettingsAsync(guildUser); if (guildUserLastFM?.UserNameLastFM != null) { lastFMUserName = guildUserLastFM.UserNameLastFM; self = false; } } } } PageResponse <LastTrack> tracks = await _lastFmService.GetRecentScrobblesAsync(lastFMUserName); if (tracks?.Any() != true) { await NoScrobblesErrorResponseFoundAsync(); return; } LastResponse <LastUser> userinfo = await _lastFmService.GetUserInfoAsync(lastFMUserName); LastTrack currentTrack = tracks.Content[0]; LastTrack lastTrack = tracks.Content[1]; const string nullText = "[undefined]"; string trackName = currentTrack.Name; string artistName = currentTrack.ArtistName; string albumName = string.IsNullOrWhiteSpace(currentTrack.AlbumName) ? nullText : currentTrack.AlbumName; string lastTrackName = lastTrack.Name; string lastTrackArtistName = lastTrack.ArtistName; string lastTrackAlbumName = string.IsNullOrWhiteSpace(lastTrack.AlbumName) ? nullText : lastTrack.AlbumName; int playCount = userinfo.Content.Playcount; switch (userSettings.ChartType) { case ChartType.textmini: await Context.Channel.SendMessageAsync(await _userService.GetUserTitleAsync(Context) + "\n**Current** - " + artistName + " - " + trackName + " [" + albumName + "]\n<https://www.last.fm/user/" + userSettings.UserNameLastFM + ">\n" + userSettings.UserNameLastFM + "'s total scrobbles: " + playCount.ToString("N0")); break; case ChartType.textfull: await Context.Channel.SendMessageAsync(await _userService.GetUserTitleAsync(Context) + "\n**Current** - " + artistName + " - " + trackName + " [" + albumName + "]\n**Previous** - " + lastTrackArtistName + " - " + lastTrackName + " [" + lastTrackAlbumName + "]\n<https://www.last.fm/user/" + userSettings.UserNameLastFM + ">\n" + userSettings.UserNameLastFM + "'s total scrobbles: " + playCount.ToString("N0")); break; default: if (!_guildService.CheckIfDM(Context)) { GuildPermissions perms = await _guildService.CheckSufficientPermissionsAsync(Context); if (!perms.EmbedLinks) { await ReplyAsync("Insufficient permissions, I need to the 'Embed links' permission to show you your scrobbles."); break; } } var userTitle = await _userService.GetUserTitleAsync(Context); this._embed.AddField( $"Current: {tracks.Content[0].Name}", $"By **{tracks.Content[0].ArtistName}**" + (string.IsNullOrEmpty(tracks.Content[0].AlbumName) ? "" : $" | {tracks.Content[0].AlbumName}")); if (userSettings.ChartType == ChartType.embedfull) { this._embedAuthor.WithName("Last tracks for " + userTitle); this._embed.AddField( $"Previous: {tracks.Content[1].Name}", $"By **{tracks.Content[1].ArtistName}**" + (string.IsNullOrEmpty(tracks.Content[1].AlbumName) ? "" : $" | {tracks.Content[1].AlbumName}")); } else { this._embedAuthor.WithName("Last track for " + userTitle); } this._embed.WithTitle(tracks.Content[0].IsNowPlaying == true ? "*Now playing*" : $"Last scrobble {tracks.Content[0].TimePlayed?.ToString("g")}"); this._embedAuthor.WithIconUrl(Context.User.GetAvatarUrl()); this._embed.WithAuthor(this._embedAuthor); this._embed.WithUrl("https://www.last.fm/user/" + lastFMUserName); LastImageSet AlbumImages = await _lastFmService.GetAlbumImagesAsync(currentTrack.ArtistName, currentTrack.AlbumName); if (AlbumImages?.Medium != null) { this._embed.WithThumbnailUrl(AlbumImages.Medium.ToString()); } this._embedFooter.WithText($"{userinfo.Content.Name} has {userinfo.Content.Playcount} scrobbles."); this._embed.WithFooter(this._embedFooter); this._embed.WithColor(Constants.LastFMColorRed); await ReplyAsync("", false, this._embed.Build()); break; } this._logger.LogCommandUsed(Context.Guild?.Id, Context.Channel.Id, Context.User.Id, Context.Message.Content); } catch (Exception e) { _logger.LogError(e.Message, Context.Message.Content, Context.User.Username, Context.Guild?.Name, Context.Guild?.Id); await ReplyAsync("Unable to show Last.FM info due to an internal error. Try scrobbling something then use the command again."); } }
public async Task fmArtistsAsync(string time = "weekly", int num = 6, string user = null) { User userSettings = await _userService.GetUserSettingsAsync(Context.User); if (userSettings?.UserNameLastFM == null) { await UsernameNotSetErrorResponseAsync(); return; } if (time == "help") { await ReplyAsync( "Usage: `.fmartists 'weekly/monthly/yearly/alltime' 'number of artists (max 10)'` \n" + "You can set your default user and your display mode through the `.fmset 'username' 'embedfull/embedmini/textfull/textmini'` command."); return; } if (!Enum.TryParse(time, ignoreCase: true, out ChartTimePeriod timePeriod)) { await ReplyAsync("Invalid time period. Please use 'weekly', 'monthly', 'yearly', or 'alltime'."); return; } if (num > 10) { num = 10; } LastStatsTimeSpan timeSpan = _lastFmService.GetLastStatsTimeSpan(timePeriod); try { string lastFMUserName = userSettings.UserNameLastFM; if (user != null) { if (await _lastFmService.LastFMUserExistsAsync(user)) { lastFMUserName = user; } else if (!_guildService.CheckIfDM(Context)) { IGuildUser guildUser = await _guildService.FindUserFromGuildAsync(Context, user); if (guildUser != null) { User guildUserLastFM = await _userService.GetUserSettingsAsync(guildUser); if (guildUserLastFM?.UserNameLastFM != null) { lastFMUserName = guildUserLastFM.UserNameLastFM; } } } } PageResponse <LastArtist> artists = await _lastFmService.GetTopArtistsAsync(lastFMUserName, timeSpan, num); if (artists?.Any() != true) { await ReplyAsync("No artists found on this profile. (" + lastFMUserName + ")"); return; } EmbedAuthorBuilder eab = new EmbedAuthorBuilder { IconUrl = Context.User.GetAvatarUrl(), Name = userSettings.UserNameLastFM }; EmbedBuilder builder = new EmbedBuilder { Color = new Discord.Color(186, 0, 0), }; builder.WithUrl("https://www.last.fm/user/" + lastFMUserName + "/library/artists"); builder.Title = lastFMUserName + " top " + num + " artists (" + timePeriod + ")"; const string nulltext = "[undefined]"; int indexval = (num - 1); for (int i = 0; i <= indexval; i++) { LastArtist artist = artists.Content[i]; string artistName = string.IsNullOrWhiteSpace(artist.Name) ? nulltext : artist.Name; int correctnum = (i + 1); builder.AddField("#" + correctnum + ": " + artist.Name, artist.PlayCount.Value.ToString("N0") + " times scrobbled"); } EmbedFooterBuilder embedFooter = new EmbedFooterBuilder(); LastResponse <LastUser> userinfo = await _lastFmService.GetUserInfoAsync(lastFMUserName); embedFooter.Text = lastFMUserName + "'s total scrobbles: " + userinfo.Content.Playcount.ToString("N0"); builder.WithFooter(embedFooter); await Context.Channel.SendMessageAsync("", false, builder.Build()); this._logger.LogCommandUsed(Context.Guild?.Id, Context.Channel.Id, Context.User.Id, Context.Message.Content); } catch (Exception e) { _logger.LogError(e.Message, Context.Message.Content, Context.User.Username, Context.Guild?.Name, Context.Guild?.Id); await ReplyAsync("Unable to show Last.FM info due to an internal error."); } }