Esempio n. 1
0
        public async Task WhoKnowsAsync(params string[] artistValues)
        {
            if (this._guildService.CheckIfDM(this.Context))
            {
                await ReplyAsync("This command is not supported in DMs.");

                this.Context.LogCommandUsed(CommandResponse.NotSupportedInDm);
                return;
            }

            var userSettings = await this._userService.GetUserSettingsAsync(this.Context.User);

            var prfx = this._prefixService.GetPrefix(this.Context.Guild.Id) ?? ConfigData.Data.Bot.Prefix;

            var lastIndex = await this._guildService.GetGuildIndexTimestampAsync(this.Context.Guild);

            if (lastIndex == null)
            {
                await ReplyAsync("This server hasn't been indexed yet.\n" +
                                 $"Please run `{prfx}index` to index this server.");

                this.Context.LogCommandUsed(CommandResponse.IndexRequired);
                return;
            }
            if (lastIndex < DateTime.UtcNow.AddDays(-50))
            {
                await ReplyAsync("Server index data is out of date, it was last updated over 50 days ago.\n" +
                                 $"Please run `{prfx}index` to re-index this server.");

                this.Context.LogCommandUsed(CommandResponse.IndexRequired);
                return;
            }

            var guildTask = this._guildService.GetGuildAsync(this.Context.Guild.Id);

            _ = this.Context.Channel.TriggerTypingAsync();

            var artistQuery = await GetArtistOrHelp(artistValues, userSettings, "whoknows", prfx);

            if (artistQuery == null)
            {
                this.Context.LogCommandUsed(CommandResponse.NotFound);
                return;
            }

            var queryParams = new Dictionary <string, string>
            {
                { "artist", artistQuery },
                { "username", userSettings.UserNameLastFM },
                { "autocorrect", "1" }
            };

            try
            {
                var artistCall = await this._lastfmApi.CallApiAsync <ArtistResponse>(queryParams, Call.ArtistInfo);

                if (!artistCall.Success)
                {
                    this._embed.ErrorResponse(artistCall.Error.Value, artistCall.Message, this.Context, this._logger);
                    await ReplyAsync("", false, this._embed.Build());

                    this.Context.LogCommandWithLastFmError(artistCall.Error);
                    return;
                }

                var spotifyArtistResultsTask = this._spotifyService.GetOrStoreArtistImageAsync(artistCall.Content, artistQuery);

                var guild = await guildTask;
                var users = guild.GuildUsers.Select(s => s.User).ToList();

                var artist = artistCall.Content;

                var usersWithArtist = await this._whoKnowArtistService.GetIndexedUsersForArtist(this.Context, users, artist.Artist.Name);

                Statistics.LastfmApiCalls.Inc();

                if (artist.Artist.Stats.Userplaycount != 0)
                {
                    var guildUser = await this.Context.Guild.GetUserAsync(this.Context.User.Id);

                    usersWithArtist = WhoKnowsService.AddOrReplaceUserToIndexList(usersWithArtist, userSettings, guildUser, artist.Artist.Name, artist.Artist.Stats.Userplaycount);
                }

                var serverUsers = WhoKnowsService.WhoKnowsListToString(usersWithArtist);
                if (usersWithArtist.Count == 0)
                {
                    serverUsers = "Nobody in this server (not even you) has listened to this artist.";
                }

                this._embed.WithDescription(serverUsers);

                var userTitle = await this._userService.GetUserTitleAsync(this.Context);

                var footer = $"WhoKnows artist requested by {userTitle}";

                var rnd = new Random();
                if (rnd.Next(0, 4) == 1 && lastIndex < DateTime.UtcNow.AddDays(-3))
                {
                    footer += $"\nMissing members? Update with {prfx}index";
                }

                if (guild.GuildUsers.Count < 400)
                {
                    var serverListeners = await this._whoKnowArtistService.GetArtistListenerCountForServer(users, artist.Artist.Name);

                    var serverPlaycount = await this._whoKnowArtistService.GetArtistPlayCountForServer(users, artist.Artist.Name);

                    var avgServerListenerPlaycount = await this._whoKnowArtistService.GetArtistAverageListenerPlaycountForServer(users, artist.Artist.Name);

                    footer += $"\n{serverListeners} listeners - ";
                    footer += $"{serverPlaycount} total plays - ";
                    footer += $"{(int)avgServerListenerPlaycount} median plays";
                }
                else if (guild.GuildUsers.Count < 450)
                {
                    footer += $"\nView server artist averages in `{prfx}artist`";
                }

                this._embed.WithTitle($"Who knows {artist.Artist.Name} in {this.Context.Guild.Name}");

                if (Uri.IsWellFormedUriString(artist.Artist.Url, UriKind.Absolute))
                {
                    this._embed.WithUrl(artist.Artist.Url);
                }

                this._embedFooter.WithText(footer);
                this._embed.WithFooter(this._embedFooter);

                var spotifyImage = await spotifyArtistResultsTask;
                if (spotifyImage != null)
                {
                    this._embed.WithThumbnailUrl(spotifyImage);
                }

                await this.Context.Channel.SendMessageAsync("", false, this._embed.Build());

                this.Context.LogCommandUsed();
            }
            catch (Exception e)
            {
                this.Context.LogCommandException(e);
                await ReplyAsync("Something went wrong while using whoknows. Please let us know as this feature is in beta.");
            }
        }
Esempio n. 2
0
        public async Task WhoKnowsAsync(params string[] trackValues)
        {
            if (this._guildService.CheckIfDM(this.Context))
            {
                await ReplyAsync("This command is not supported in DMs.");

                this.Context.LogCommandUsed(CommandResponse.NotSupportedInDm);
                return;
            }

            var userSettings = await this._userService.GetUserSettingsAsync(this.Context.User);

            var prfx = this._prefixService.GetPrefix(this.Context.Guild.Id) ?? ConfigData.Data.Bot.Prefix;

            if (trackValues.Any() && trackValues.First() == "help")
            {
                this._embed.WithTitle($"{prfx}whoknowstrack");
                this._embed.WithDescription($"Shows what members in your server listened to the track you're currently listening to or searching for.");

                this._embed.AddField("Examples",
                                     $"`{prfx}wt` \n" +
                                     $"`{prfx}whoknowstrack` \n" +
                                     $"`{prfx}whoknowstrack Hothouse Flowers Don't Go` \n" +
                                     $"`{prfx}whoknowstrack Natasha Bedingfield | Unwritten`");

                await this.Context.Channel.SendMessageAsync("", false, this._embed.Build());

                this.Context.LogCommandUsed(CommandResponse.Help);
                return;
            }

            var lastIndex = await this._guildService.GetGuildIndexTimestampAsync(this.Context.Guild);

            if (lastIndex == null)
            {
                await ReplyAsync("This server hasn't been indexed yet.\n" +
                                 $"Please run `{prfx}index` to index this server.");

                this.Context.LogCommandUsed(CommandResponse.IndexRequired);
                return;
            }
            if (lastIndex < DateTime.UtcNow.AddDays(-50))
            {
                await ReplyAsync("Server index data is out of date, it was last updated over 50 days ago.\n" +
                                 $"Please run `{prfx}index` to re-index this server.");

                this.Context.LogCommandUsed(CommandResponse.IndexRequired);
                return;
            }

            var guildTask = this._guildService.GetGuildAsync(this.Context.Guild.Id);

            if (trackValues.Any() && trackValues.First() == "help")
            {
                await ReplyAsync(
                    $"Usage: `{prfx}whoknowstrack 'artist and track name'`\n" +
                    "If you don't enter any track name, it will get the info from the track you're currently listening to.");

                this.Context.LogCommandUsed(CommandResponse.Help);
                return;
            }

            _ = this.Context.Channel.TriggerTypingAsync();

            var track = await this.SearchTrack(trackValues, userSettings, prfx);

            if (track == null)
            {
                return;
            }

            var trackName = $"{track.Artist.Name} - {track.Name}";

            try
            {
                var guild = await guildTask;
                var users = guild.GuildUsers.Select(s => s.User).ToList();

                var usersWithArtist = await this._whoKnowsTrackService.GetIndexedUsersForTrack(this.Context, users, track.Artist.Name, track.Name);

                if (track.Userplaycount != 0)
                {
                    var guildUser = await this.Context.Guild.GetUserAsync(this.Context.User.Id);

                    usersWithArtist = WhoKnowsService.AddOrReplaceUserToIndexList(usersWithArtist, userSettings, guildUser, trackName, track.Userplaycount);
                }

                var serverUsers = WhoKnowsService.WhoKnowsListToString(usersWithArtist);
                if (usersWithArtist.Count == 0)
                {
                    serverUsers = "Nobody in this server (not even you) has listened to this track.";
                }

                this._embed.WithDescription(serverUsers);

                var userTitle = await this._userService.GetUserTitleAsync(this.Context);

                var footer = $"WhoKnows track requested by {userTitle} - Users with 3 plays or higher are shown";

                var rnd = new Random();
                if (rnd.Next(0, 4) == 1 && lastIndex < DateTime.UtcNow.AddDays(-3))
                {
                    footer += $"\nMissing members? Update with {prfx}index";
                }

                this._embed.WithTitle($"Who knows {trackName} in {this.Context.Guild.Name}");

                if (Uri.IsWellFormedUriString(track.Url, UriKind.Absolute))
                {
                    this._embed.WithUrl(track.Url);
                }

                this._embedFooter.WithText(footer);
                this._embed.WithFooter(this._embedFooter);

                await this.Context.Channel.SendMessageAsync("", false, this._embed.Build());

                this.Context.LogCommandUsed();
            }
            catch (Exception e)
            {
                this.Context.LogCommandException(e);
                await ReplyAsync("Something went wrong while using whoknows track. Please let us know as this feature is in beta.");
            }
        }
Esempio n. 3
0
        public async Task WhoKnowsAsync(params string[] albumValues)
        {
            if (this._guildService.CheckIfDM(this.Context))
            {
                await ReplyAsync("This command is not supported in DMs.");

                this.Context.LogCommandUsed(CommandResponse.NotSupportedInDm);
                return;
            }

            var userSettings = await this._userService.GetUserSettingsAsync(this.Context.User);

            var prfx = this._prefixService.GetPrefix(this.Context.Guild.Id) ?? ConfigData.Data.Bot.Prefix;

            if (albumValues.Any() && albumValues.First() == "help")
            {
                this._embed.WithTitle($"{prfx}whoknowsalbum");
                this._embed.WithDescription($"Shows what members in your server listened to the album you're currently listening to or searching for.");

                this._embed.AddField("Examples",
                                     $"`{prfx}wa` \n" +
                                     $"`{prfx}whoknowsalbum` \n" +
                                     $"`{prfx}whoknowsalbum The Beatles Abbey Road` \n" +
                                     $"`{prfx}whoknowsalbum Metallica & Lou Reed | Lulu`");

                await this.Context.Channel.SendMessageAsync("", false, this._embed.Build());

                this.Context.LogCommandUsed(CommandResponse.Help);
                return;
            }

            var lastIndex = await this._guildService.GetGuildIndexTimestampAsync(this.Context.Guild);

            if (lastIndex == null)
            {
                await ReplyAsync("This server hasn't been indexed yet.\n" +
                                 $"Please run `{prfx}index` to index this server.");

                this.Context.LogCommandUsed(CommandResponse.IndexRequired);
                return;
            }
            if (lastIndex < DateTime.UtcNow.AddDays(-50))
            {
                await ReplyAsync("Server index data is out of date, it was last updated over 50 days ago.\n" +
                                 $"Please run `{prfx}index` to re-index this server.");

                this.Context.LogCommandUsed(CommandResponse.IndexRequired);
                return;
            }

            var guildTask = this._guildService.GetGuildAsync(this.Context.Guild.Id);

            if (albumValues.Any() && albumValues.First() == "help")
            {
                await ReplyAsync(
                    $"Usage: `{prfx}whoknowsalbum 'artist and album name'`\n" +
                    "If you don't enter any track name, it will get the info from the track you're currently listening to.");

                this.Context.LogCommandUsed(CommandResponse.Help);
                return;
            }

            _ = this.Context.Channel.TriggerTypingAsync();

            var searchResult = await this.SearchAlbum(albumValues, userSettings, prfx);

            if (!searchResult.AlbumFound)
            {
                this.Context.LogCommandUsed(CommandResponse.NotFound);
                return;
            }

            var queryParams = new Dictionary <string, string>
            {
                { "artist", searchResult.Artist },
                { "album", searchResult.Name },
                { "username", userSettings.UserNameLastFM }
            };

            var albumCall = await this._lastfmApi.CallApiAsync <AlbumResponse>(queryParams, Call.AlbumInfo);

            if (!albumCall.Success)
            {
                this._embed.ErrorResponse(albumCall.Error.Value, albumCall.Message, this.Context, this._logger);
                await ReplyAsync("", false, this._embed.Build());

                this.Context.LogCommandUsed(CommandResponse.Error);
                return;
            }

            var album = albumCall.Content.Album;

            var albumName = $"{album.Artist} - {album.Name}";

            try
            {
                var guild = await guildTask;
                var users = guild.GuildUsers.Select(s => s.User).ToList();

                var usersWithArtist = await this._whoKnowsAlbumService.GetIndexedUsersForAlbum(this.Context, users, album.Artist, album.Name);

                if (album.Userplaycount != 0)
                {
                    var guildUser = await this.Context.Guild.GetUserAsync(this.Context.User.Id);

                    usersWithArtist = WhoKnowsService.AddOrReplaceUserToIndexList(usersWithArtist, userSettings, guildUser, albumName, album.Userplaycount);
                }

                var serverUsers = WhoKnowsService.WhoKnowsListToString(usersWithArtist);
                if (usersWithArtist.Count == 0)
                {
                    serverUsers = "Nobody in this server (not even you) has listened to this album.";
                }

                this._embed.WithDescription(serverUsers);

                var userTitle = await this._userService.GetUserTitleAsync(this.Context);

                var footer = $"WhoKnows album requested by {userTitle}";

                var rnd = new Random();
                if (rnd.Next(0, 4) == 1 && lastIndex < DateTime.UtcNow.AddDays(-3))
                {
                    footer += $"\nMissing members? Update with {prfx}index";
                }

                this._embed.WithTitle($"Who knows {albumName} in {this.Context.Guild.Name}");

                if (Uri.IsWellFormedUriString(album.Url, UriKind.Absolute))
                {
                    this._embed.WithUrl(album.Url);
                }

                this._embedFooter.WithText(footer);
                this._embed.WithFooter(this._embedFooter);

                if (album.Image.Any() && album.Image != null)
                {
                    this._embed.WithThumbnailUrl(album.Image.First(f => f.Size == "mega").Text.ToString());
                }

                await this.Context.Channel.SendMessageAsync("", false, this._embed.Build());

                this.Context.LogCommandUsed();
            }
            catch (Exception e)
            {
                this.Context.LogCommandException(e);
                await ReplyAsync("Something went wrong while using whoknows album. Please let us know as this feature is in beta.");
            }
        }