public async Task CacheSubredditAsync(String subredditName) { Subreddit subreddit = null; // we have to do it this way, otherwise ModPermissions won't be set int count = 0; await RedditInstance.User.GetModeratorSubreddits(-1).ForEachAsync(moderatedSubreddit => { count++; // we can count the modded subs for stats purposes here if (count % 100 == 1) { IncrementStatisticIfExists("requestRate"); } if (moderatedSubreddit.DisplayName == SubredditName) { subreddit = moderatedSubreddit; } }); SubredditLookup[subredditName] = new CachedSubreddit(subreddit, configurationLoader); await SubredditLookup[subredditName].ReloadOptionsAsync(this); }
protected override sealed async Task RunItemAsync(T thing) { Log.Verbose("Scanning for banned users on thing {ThingFullname}", thing.FullName); String subredditName = thing["subreddit"].Value <String>(); if (!bot.SubredditLookup.ContainsKey(subredditName)) { Log.Verbose("Subreddit {SubredditName} not in cache. Adding to cache now.", subredditName); await bot.CacheSubredditAsync(subredditName); } CachedSubreddit subreddit = bot.SubredditLookup[subredditName]; AbstractSubredditOptionSet options = new ShadedOptionSet(new[] { subreddit?.Options, GlobalConfig.GlobalOptions }, true); if (!options.Enabled) { return; } if (!options.ScanPosts && thing is Post) { return; } if (!options.ScanComments && thing is Comment) { return; } bot.IncrementStatisticIfExists("postCommentCount"); IReadOnlyCollection <Group> bannedGroups = await bot.GetBannedGroupsAsync(options); if (await bot.CheckShouldBanAsync(thing, bannedGroups.Select(group => group.Name))) { Log.Debug("Found user {User}. Taking action {Action} based on subreddit setting for {Subreddit}", thing.AuthorName, options.RemovalType.ToString(), subreddit.RedditSubreddit.DisplayName); try { if (options.RemovalType == RemovalType.Spam) { await thing.RemoveSpamAsync(); bot.IncrementStatisticIfExists("requestRate"); } else if (options.RemovalType == RemovalType.Remove) { await thing.RemoveAsync(); bot.IncrementStatisticIfExists("requestRate"); } } catch (RedditHttpException ex) { Log.Error("Could not remove thing {ThingFullname} due to HTTP error from reddit: {ExceptionMessage}", thing.FullName, ex.Message); } if (options.BanDuration > -1) { Log.Verbose("Banning user {User} now.", thing.AuthorName); await subreddit.RedditSubreddit.BanUserAsync(thing.AuthorName, options.BanNote.Trim(), null, options.BanDuration, options.BanMessage.Trim()); bot.IncrementStatisticIfExists("requestRate"); } } }