Ejemplo n.º 1
0
        internal static async Task Main(string[] _)
        {
            PrintBuildInformation();

            try {
                BotConfigService cfg = await LoadBotConfigAsync();

                Log.Logger = LogExt.CreateLogger(cfg.CurrentConfiguration);
                Log.Information("Logger created.");

                DbContextBuilder dbb = await InitializeDatabaseAsync(cfg);

                await StartAsync(cfg, dbb);

                Log.Information("Booting complete!");

                CancellationToken token = Bot?.Services.GetRequiredService <BotActivityService>().MainLoopCts.Token
                                          ?? throw new InvalidOperationException("Bot not initialized");
                await Task.Delay(Timeout.Infinite, token);
            } catch (TaskCanceledException) {
                Log.Information("Shutdown signal received!");
            } catch (Exception e) {
                Log.Fatal(e, "Critical exception occurred");
                Environment.ExitCode = 1;
            } finally {
                await DisposeAsync();
            }

            Log.Information("Powering off");
            Log.CloseAndFlush();
            Environment.Exit(Environment.ExitCode);
        }
Ejemplo n.º 2
0
 public LavalinkService(BotConfigService cfg, DiscordShardedClient client)
 {
     this.cfg           = cfg.CurrentConfiguration.LavalinkConfig;
     this.client        = client;
     this.client.Ready += this.InitializeLavalinkAsync;
     this.trackError    = new AsyncEvent <LavalinkGuildConnection, TrackExceptionEventArgs>("LAVALINK_ERROR", TimeSpan.Zero, this.LavalinkErrorHandler);
 }
Ejemplo n.º 3
0
 public GiphyService(BotConfigService cfg)
 {
     if (!string.IsNullOrWhiteSpace(cfg.CurrentConfiguration.GiphyKey))
     {
         this.giphy = new Giphy(cfg.CurrentConfiguration.GiphyKey);
     }
 }
        public void InitializeService()
        {
            var bcs = new BotConfigService();

            this.Configs = new GuildConfigService(bcs, TestDbProvider.Database, false);
            this.Service = new LocalizationService(this.Configs, bcs, false);
            Assume.That(Directory.Exists(this.ValidTestDataPath), "Valid tests dir not present");
            Assume.That(Directory.Exists(this.ThrowsIOTestDataPath), "Invalid tests dir not present");
        }
Ejemplo n.º 5
0
        public SelfService(DiscordSocketClient client, CommandHandler cmdHandler, DbService db,
                           IBotStrings strings, IBotCredentials creds, IDataCache cache, IHttpClientFactory factory,
                           BotConfigService bss)
        {
            _redis       = cache.Redis;
            _cmdHandler  = cmdHandler;
            _db          = db;
            _strings     = strings;
            _client      = client;
            _creds       = creds;
            _cache       = cache;
            _imgs        = cache.LocalImages;
            _httpFactory = factory;
            _bss         = bss;

            var sub = _redis.GetSubscriber();

            if (_client.ShardId == 0)
            {
                sub.Subscribe(_creds.RedisKey() + "_reload_images",
                              delegate { _imgs.Reload(); }, CommandFlags.FireAndForget);
            }

            sub.Subscribe(_creds.RedisKey() + "_leave_guild", async(ch, v) =>
            {
                try
                {
                    var guildStr = v.ToString()?.Trim().ToUpperInvariant();
                    if (string.IsNullOrWhiteSpace(guildStr))
                    {
                        return;
                    }
                    var server = _client.Guilds.FirstOrDefault(g => g.Id.ToString() == guildStr) ??
                                 _client.Guilds.FirstOrDefault(g => g.Name.Trim().ToUpperInvariant() == guildStr);

                    if (server == null)
                    {
                        return;
                    }

                    if (server.OwnerId != _client.CurrentUser.Id)
                    {
                        await server.LeaveAsync().ConfigureAwait(false);
                        Log.Information($"Left server {server.Name} [{server.Id}]");
                    }
                    else
                    {
                        await server.DeleteAsync().ConfigureAwait(false);
                        Log.Information($"Deleted server {server.Name} [{server.Id}]");
                    }
                }
                catch
                {
                }
            }, CommandFlags.FireAndForget);
        }
Ejemplo n.º 6
0
 public GuildConfigService(BotConfigService cfg, DbContextBuilder dbb, bool loadData = true)
 {
     this.cfg  = cfg.CurrentConfiguration;
     this.dbb  = dbb;
     this.gcfg = new ConcurrentDictionary <ulong, CachedGuildConfig>();
     if (loadData)
     {
         this.LoadData();
     }
 }
Ejemplo n.º 7
0
        private static async Task <BotConfigService> LoadBotConfigAsync()
        {
            Console.Write("Loading configuration... ");

            var cfg = new BotConfigService();
            await cfg.LoadConfigAsync();

            Console.Write("\r");
            return(cfg);
        }
Ejemplo n.º 8
0
 public SteamService(BotConfigService cfg)
 {
     this.cacheResetTimer = new Timer(ResetCache, this, TimeSpan.FromSeconds(5), TimeSpan.FromMinutes(15));
     if (!string.IsNullOrWhiteSpace(cfg.CurrentConfiguration.SteamKey))
     {
         var webInterfaceFactory = new SteamWebInterfaceFactory(cfg.CurrentConfiguration.SteamKey);
         this.users = webInterfaceFactory.CreateSteamWebInterface <SteamUser>();
         this.apps  = webInterfaceFactory.CreateSteamWebInterface <SteamApps>();
         this.store = webInterfaceFactory.CreateSteamStoreInterface();
     }
 }
Ejemplo n.º 9
0
 public YtService(BotConfigService cfg)
 {
     this.ytExplode = new YoutubeClient();
     if (!string.IsNullOrWhiteSpace(cfg.CurrentConfiguration.YouTubeKey))
     {
         this.yt = new YouTubeService(new BaseClientService.Initializer {
             ApiKey          = cfg.CurrentConfiguration.YouTubeKey,
             ApplicationName = TheGodfather.ApplicationName
         });
     }
 }
Ejemplo n.º 10
0
        public Help(GlobalPermissionService perms, CommandService cmds, BotConfigService bss,
                    IServiceProvider services, DiscordSocketClient client, IBotStrings strings)
        {
            _cmds     = cmds;
            _bss      = bss;
            _perms    = perms;
            _services = services;
            _client   = client;
            _strings  = strings;

            _lazyClientId = new AsyncLazy <ulong>(async() => (await _client.GetApplicationInfoAsync()).Id);
        }
Ejemplo n.º 11
0
        public HelpService(CommandHandler ch, IBotStrings strings,
                           DiscordPermOverrideService dpos, BotConfigService bss)
        {
            _ch      = ch;
            _strings = strings;
            _dpos    = dpos;
            _bss     = bss;


            _rep = new ReplacementBuilder()
                   .WithOverride("%prefix%", () => _bss.GetRawData().Prefix)
                   .WithOverride("%bot.prefix%", () => _bss.GetRawData().Prefix)
                   .Build();
        }
Ejemplo n.º 12
0
        public PlayingRotateService(DiscordSocketClient client, DbService db, NadekoBot bot,
                                    BotConfigService bss, IEnumerable <IPlaceholderProvider> phProviders)
        {
            _db  = db;
            _bot = bot;
            _bss = bss;

            if (client.ShardId == 0)
            {
                _rep = new ReplacementBuilder()
                       .WithClient(client)
                       .WithProviders(phProviders)
                       .Build();

                _t = new Timer(RotatingStatuses, new TimerState(), TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1));
            }
        }
Ejemplo n.º 13
0
        private static async Task <DbContextBuilder> InitializeDatabaseAsync(BotConfigService cfg)
        {
            Log.Information("Establishing database connection");
            var dbb = new DbContextBuilder(cfg.CurrentConfiguration.DatabaseConfig);

            Log.Information("Testing database context creation");
            using (TheGodfatherDbContext db = dbb.CreateContext()) {
                IEnumerable <string> pendingMigrations = await db.Database.GetPendingMigrationsAsync();

                if (pendingMigrations.Any())
                {
                    Log.Information("Applying pending database migrations: {PendingDbMigrations}", pendingMigrations);
                    await db.Database.MigrateAsync();
                }
            }

            return(dbb);
        }
Ejemplo n.º 14
0
        public Localization(BotConfigService bss, NadekoBot bot, DbService db)
        {
            _bss = bss;
            _db  = db;

            var cultureInfoNames = bot.AllGuildConfigs
                                   .ToDictionary(x => x.GuildId, x => x.Locale);

            GuildCultureInfos = new ConcurrentDictionary <ulong, CultureInfo>(cultureInfoNames.ToDictionary(x => x.Key, x =>
            {
                CultureInfo cultureInfo = null;
                try
                {
                    if (x.Value == null)
                    {
                        return(null);
                    }
                    cultureInfo = new CultureInfo(x.Value);
                }
                catch { }
                return(cultureInfo);
            }).Where(x => x.Value != null));
        }
Ejemplo n.º 15
0
 public OMDbService(BotConfigService cfg)
 {
     this.key = cfg.CurrentConfiguration.OMDbKey;
 }
Ejemplo n.º 16
0
 private static Task StartAsync(BotConfigService cfg, DbContextBuilder dbb)
 {
     Bot             = new TheGodfatherBot(cfg, dbb);
     PeriodicService = new PeriodicTasksService(Bot, cfg.CurrentConfiguration);
     return(Bot.StartAsync());
 }
Ejemplo n.º 17
0
 public ConfigCommands(BotConfigService bss, SelfService selfService, IEnumerable <IConfigService> settingServices)
 {
     _settingServices = settingServices;
     _bss             = bss;
     _selfService     = selfService;
 }
Ejemplo n.º 18
0
 public GlobalPermissionService(BotConfigService bss)
 {
     _bss = bss;
 }
Ejemplo n.º 19
0
 public DesignTimeDbContextFactory()
 {
     this.cfg   = new BotConfigService();
     this.async = new AsyncExecutionService();
 }
Ejemplo n.º 20
0
 public GoodreadsService(BotConfigService cfg)
 {
     this.key = cfg.CurrentConfiguration.GoodreadsKey;
 }
Ejemplo n.º 21
0
 public TheGodfatherBot(BotConfigService cfg, DbContextBuilder dbb)
 {
     this.config   = cfg;
     this.database = dbb;
 }
Ejemplo n.º 22
0
 public WeatherService(BotConfigService cfg)
 {
     this.key = cfg.CurrentConfiguration.WeatherKey;
 }