示例#1
0
 public LastFMService(IConfigurationRoot configuration, ILastfmApi lastfmApi)
 {
     this._key          = configuration.GetSection("LastFm:Key").Value;
     this._secret       = configuration.GetSection("LastFm:Secret").Value;
     this._lastFMClient = new LastfmClient(this._key, this._secret);
     this._lastfmApi    = lastfmApi;
 }
示例#2
0
 public AlbumCommands(Logger.Logger logger,
                      ILastfmApi lastfmApi,
                      IPrefixService prefixService,
                      UserService userService,
                      LastFMService lastFmService,
                      WhoKnowsAlbumService whoKnowsAlbumService,
                      PlayService playService,
                      IIndexService indexService,
                      IUpdateService updateService)
 {
     this._logger               = logger;
     this._lastfmApi            = lastfmApi;
     this._lastFmService        = lastFmService;
     this._whoKnowsAlbumService = whoKnowsAlbumService;
     this._playService          = playService;
     this._indexService         = indexService;
     this._updateService        = updateService;
     this._prefixService        = prefixService;
     this._guildService         = new GuildService();
     this._userService          = userService;
     this._embed = new EmbedBuilder()
                   .WithColor(DiscordConstants.LastFMColorRed);
     this._embedAuthor = new EmbedAuthorBuilder();
     this._embedFooter = new EmbedFooterBuilder();
 }
示例#3
0
 public SpotifyCommands(Logger.Logger logger, IPrefixService prefixService, ILastfmApi lastfmApi)
 {
     this._logger        = logger;
     this._prefixService = prefixService;
     this._userService   = new UserService();
     this._lastFmService = new LastFMService(lastfmApi);
     this._embed         = new EmbedBuilder()
                           .WithColor(Constants.LastFMColorRed);
 }
示例#4
0
 public AlbumCommands(Logger.Logger logger, ILastfmApi lastfmApi)
 {
     this._logger    = logger;
     this._lastfmApi = lastfmApi;
     this._embed     = new EmbedBuilder()
                       .WithColor(Constants.LastFMColorRed);
     this._embedAuthor = new EmbedAuthorBuilder();
     this._embedFooter = new EmbedFooterBuilder();
 }
示例#5
0
 public ArtistCommands(Logger.Logger logger, IndexService indexService, ILastfmApi lastfmApi, IPrefixService prefixService)
 {
     this._logger        = logger;
     this._indexService  = indexService;
     this._lastfmApi     = lastfmApi;
     this._prefixService = prefixService;
     this._embed         = new EmbedBuilder()
                           .WithColor(Constants.LastFMColorRed);
     this._embedAuthor = new EmbedAuthorBuilder();
     this._embedFooter = new EmbedFooterBuilder();
 }
示例#6
0
 public TrackCommands(Logger.Logger logger,
                      IPrefixService prefixService,
                      ILastfmApi lastfmApi,
                      GuildService guildService,
                      UserService userService)
 {
     this._logger        = logger;
     this._prefixService = prefixService;
     this._guildService  = guildService;
     this._userService   = userService;
     this._lastFmService = new LastFMService(lastfmApi);
     this._embed         = new EmbedBuilder()
                           .WithColor(Constants.LastFMColorRed);
     this._embedAuthor = new EmbedAuthorBuilder();
     this._embedFooter = new EmbedFooterBuilder();
 }
示例#7
0
 public UserCommands(TimerService timer,
                     Logger.Logger logger,
                     IPrefixService prefixService,
                     ILastfmApi lastfmApi,
                     GuildService guildService)
 {
     this._timer          = timer;
     this._logger         = logger;
     this._prefixService  = prefixService;
     this._guildService   = guildService;
     this._friendsService = new FriendsService();
     this._userService    = new UserService();
     this._lastFmService  = new LastFMService(lastfmApi);
     this._embed          = new EmbedBuilder()
                            .WithColor(Constants.LastFMColorRed);
     this._embedAuthor = new EmbedAuthorBuilder();
     this._embedFooter = new EmbedFooterBuilder();
 }
示例#8
0
 public ArtistCommands(Logger.Logger logger,
                       ILastfmApi lastfmApi,
                       IPrefixService prefixService,
                       ArtistsService artistsService,
                       WhoKnowsService whoKnowsService,
                       GuildService guildService,
                       UserService userService)
 {
     this._logger          = logger;
     this._lastfmApi       = lastfmApi;
     this._lastFmService   = new LastFMService(lastfmApi);
     this._prefixService   = prefixService;
     this._artistsService  = artistsService;
     this._whoKnowsService = whoKnowsService;
     this._guildService    = guildService;
     this._userService     = userService;
     this._embed           = new EmbedBuilder()
                             .WithColor(Constants.LastFMColorRed);
     this._embedAuthor = new EmbedAuthorBuilder();
     this._embedFooter = new EmbedFooterBuilder();
 }
示例#9
0
        public TimerService(DiscordShardedClient client, Logger.Logger logger, ILastfmApi lastfmApi)
        {
            this._logger        = logger;
            this._lastfmApi     = lastfmApi;
            this._client        = client;
            this._lastFMService = new LastFMService(this._lastfmApi);
            this._userService   = new UserService();
            this._guildService  = new GuildService();

            this._timer = new Timer(async _ =>
            {
                var random               = new Random();
                var randomAvatarMode     = random.Next(1, 4);
                var randomAvatarModeDesc = "";

                switch (randomAvatarMode)
                {
                case 1:
                    randomAvatarModeDesc = "Recent listens";
                    break;

                case 2:
                    randomAvatarModeDesc = "Weekly albums";
                    break;

                case 3:
                    randomAvatarModeDesc = "Monthly albums";
                    break;

                case 4:
                    randomAvatarModeDesc = "Overall albums";
                    break;
                }

                Log.Information($"Featured: Picked mode {randomAvatarMode} - {randomAvatarModeDesc}");

                try
                {
                    var lastFMUserName = await this._userService.GetRandomLastFMUserAsync();
                    Log.Information($"Featured: Picked user {lastFMUserName}");

                    switch (randomAvatarMode)
                    {
                    // Recent listens
                    case 1:
                        var tracks    = await this._lastFMService.GetRecentScrobblesAsync(lastFMUserName, 25);
                        var trackList = tracks
                                        .Select(s => new CensoredAlbum(s.ArtistName, s.AlbumName))
                                        .Where(w => !string.IsNullOrEmpty(w.AlbumName) &&
                                               !GlobalVars.CensoredAlbums.Select(s => s.ArtistName).Contains(w.ArtistName) &&
                                               !GlobalVars.CensoredAlbums.Select(s => s.AlbumName).Contains(w.AlbumName));

                        var currentTrack = trackList.First();

                        var albumImages = await this._lastFMService.GetAlbumImagesAsync(currentTrack.ArtistName, currentTrack.AlbumName);

                        this._trackString = $"{currentTrack.AlbumName} \n" +
                                            $"by {currentTrack.ArtistName} \n \n" +
                                            $"{randomAvatarModeDesc} from {lastFMUserName}";

                        Log.Information("Featured: Changing avatar to: " + this._trackString);

                        if (albumImages?.Large != null)
                        {
                            ChangeToNewAvatar(client, albumImages.Large.AbsoluteUri);
                        }
                        else
                        {
                            Log.Information("Featured: Album had no image, switching to alternative avatar mode");
                            goto case 3;
                        }

                        break;

                    // Weekly albums
                    case 2:
                    case 3:
                    case 4:
                        var timespan = LastStatsTimeSpan.Week;
                        switch (randomAvatarMode)
                        {
                        case 3:
                            timespan = LastStatsTimeSpan.Month;
                            break;

                        case 4:
                            timespan = LastStatsTimeSpan.Overall;
                            break;
                        }

                        var albums = await this._lastFMService.GetTopAlbumsAsync(lastFMUserName, timespan, 10);

                        if (!albums.Any())
                        {
                            Log.Information($"Featured: User {lastFMUserName} had no albums, switching to different user.");
                            lastFMUserName = await this._userService.GetRandomLastFMUserAsync();
                            albums         = await this._lastFMService.GetTopAlbumsAsync(lastFMUserName, timespan, 10);
                        }

                        var albumList = albums
                                        .Select(s => new CensoredAlbum(s.ArtistName, s.Name))
                                        .Where(w => !GlobalVars.CensoredAlbums.Select(s => s.ArtistName).Contains(w.ArtistName) &&
                                               !GlobalVars.CensoredAlbums.Select(s => s.AlbumName).Contains(w.AlbumName))
                                        .ToList();

                        var albumFound = false;
                        var i          = 0;

                        while (!albumFound)
                        {
                            var currentAlbum = albumList[i];

                            var albumImage = await this._lastFMService.GetAlbumImagesAsync(currentAlbum.ArtistName, currentAlbum.AlbumName);

                            this._trackString = $"{currentAlbum.AlbumName} \n" +
                                                $"by {currentAlbum.ArtistName} \n \n" +
                                                $"{randomAvatarModeDesc} from {lastFMUserName}";

                            if (albumImage?.Large != null)
                            {
                                Log.Information($"Featured: Album {i} success, changing avatar to: \n" +
                                                $"{this._trackString}");

                                ChangeToNewAvatar(client, albumImage.Large.AbsoluteUri);
                                albumFound = true;
                            }
                            else
                            {
                                Log.Information($"Featured: Album {i} had no image, switching to alternative album");
                                i++;
                            }
                        }

                        break;
                    }
                }
                catch (Exception e)
                {
                    Log.Error("ChangeFeaturedAvatar", e);
                }
            },
                                    null,
                                    TimeSpan.FromSeconds(ConfigData.Data.Bot.BotWarmupTimeInSeconds + ConfigData.Data.Bot.FeaturedTimerStartupDelayInSeconds), // 4) Time that message should fire after the timer is created
                                    TimeSpan.FromMinutes(ConfigData.Data.Bot.FeaturedTimerRepeatInMinutes));                                                   // 5) Time after which message should repeat (use `Timeout.Infinite` for no repeat)

            this._internalStatsTimer = new Timer(async _ =>
            {
                Log.Information("Updating metrics");
                Statistics.DiscordServerCount.Set(client.Guilds.Count);

                try
                {
                    Statistics.RegisteredUsers.Set(await this._userService.GetTotalUserCountAsync());
                    Statistics.RegisteredGuilds.Set(await this._guildService.GetTotalGuildCountAsync());
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

                if (string.IsNullOrEmpty(ConfigData.Data.Bot.Status))
                {
                    Log.Information("Updating status");
                    await client.SetGameAsync($"{ConfigData.Data.Bot.Prefix} | {client.Guilds.Count} servers | fmbot.xyz");
                }
            },
                                                 null,
                                                 TimeSpan.FromSeconds(ConfigData.Data.Bot.BotWarmupTimeInSeconds + 5),
                                                 TimeSpan.FromMinutes(2));

            this._externalStatsTimer = new Timer(async _ =>
            {
                if (client.CurrentUser.Id.Equals(Constants.BotProductionId) && ConfigData.Data.BotLists != null && !string.IsNullOrEmpty(ConfigData.Data.BotLists.TopGgApiToken))
                {
                    Log.Information("Updating top.gg server count");
                    var dblApi = new AuthDiscordBotListApi(Constants.BotProductionId, ConfigData.Data.BotLists.TopGgApiToken);

                    try
                    {
                        var me = await dblApi.GetMeAsync();
                        await me.UpdateStatsAsync(client.Guilds.Count);
                    }
                    catch (Exception e)
                    {
                        Log.Error("Exception while updating top.gg count!", e);
                    }
                }
                else
                {
                    Log.Information("Non-production bot found or top.gg token not entered, cancelling top.gg server count updater");
                    this._externalStatsTimer.Change(Timeout.Infinite, Timeout.Infinite);
                }
            },
                                                 null,
                                                 TimeSpan.FromSeconds(ConfigData.Data.Bot.BotWarmupTimeInSeconds + 10),
                                                 TimeSpan.FromMinutes(5));

            this._timerEnabled = true;
        }
示例#10
0
 public LastFMService(ILastfmApi lastfmApi)
 {
     this._lastfmApi = lastfmApi;
 }
示例#11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ApiBase"/> class.
 /// </summary>
 /// <param name="lastfmApi">
 /// The Last.fm API.
 /// </param>
 protected ApiBase(ILastfmApi lastfmApi)
 {
     this.LastfmApi = lastfmApi;
     this.Client = new RestClientWrapper(BaseUrl, lastfmApi.Configuration.ApiKey);
 }
示例#12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TrackApi"/> class.
 /// </summary>
 /// <param name="lastfmApi">
 /// The Last.fm API instance.
 /// </param>
 public TrackApi(ILastfmApi lastfmApi)
     : base(lastfmApi)
 {
 }