Exemplo n.º 1
0
        public async Task StartAsync()
        {
            Log.Information("Starting BotTerminator");
            Wiki subredditWiki = (await RedditInstance.GetSubredditAsync(SubredditName, false)).GetWiki;

            try
            {
                const String pageName = "botConfig/botTerminator";
                if (!(await subredditWiki.GetPageNamesAsync()).Contains(pageName.ToLower()))
                {
                    GlobalConfig = new GlobalConfig();
                    await subredditWiki.EditPageAsync(pageName, JsonConvert.SerializeObject(GlobalConfig), null, "create BotTerminator configuration");

                    await subredditWiki.SetPageSettingsAsync(pageName, true, WikiPageSettings.WikiPagePermissionLevel.Mods);
                }
                else
                {
                    GlobalConfig = JsonConvert.DeserializeObject <GlobalConfig>((await subredditWiki.GetPageAsync(pageName.ToLower())).MarkdownContent);
                }
            }
            catch (Exception ex)
            {
                Log.Warning("Failed to load or create subreddit configuration for {SubredditName}: {ExceptionMessage}", SubredditName, ex.Message);
                return;
            }
            Statistics = GlobalConfig.MetricIds.ToDictionary(key => key.Key, value => new Statistic()
            {
                MetricId = value.Value
            });
            UserLookup = new CacheableBackedBotDatabase(new SplittableWikiBotDatabase((await RedditInstance.GetSubredditAsync(SubredditName, false)).GetWiki, UsersPageName), new TimeSpan(0, 5, 0));
            await UserLookup.CheckUserAsync(CacheFreshenerUserName, String.Empty);

            await UpdateSubredditCacheAsync();

            this.Modules = new List <BotModule>()
            {
                new CommentScannerModule(this), new PostScannerModule(this),
                new RemovedPostScannerModule(this), new InviteAcceptorModule(this),
                new CacheFreshenerModule(this), new UpdateBanListModule(this),
                new StatisticsPusherModule(this), new StatusPageStatusPusherModule(this),
            };
            RedditInstance.RateLimit = RateLimitMode.SmallBurst;             // we don't need to send lots of requests at once, let's pace ourselves
            await Task.WhenAll(Modules.Select(s => s.RunForeverAsync()));
        }