Beispiel #1
0
        public void ReadSettings( Reddit r )
        {
            var wiki = r.GetSubreddit( Subreddit ).Wiki;
            WikiPage settingsPage;
            try {
                settingsPage = wiki.GetPage( WikiPageName );
            }
            catch(WebException ex) {
                if ( ( ex.Response as HttpWebResponse ).StatusCode == HttpStatusCode.NotFound ) {
                    //Page doesn't exist, create it with defaults.
                    CreateWikiPage( wiki );
                    OnSettingsModified?.Invoke( this, EventArgs.Empty );
                    return;
                }
                else if ( ( ex.Response as HttpWebResponse ).StatusCode == HttpStatusCode.Unauthorized){
                    throw new Exception( "Bot needs wiki permissions yo!" );
                }
                else { //todo retry handling?
                    throw;
                }
            }
            if ( string.IsNullOrEmpty( settingsPage.MarkdownContent ) ) {
                CreateWikiPage( wiki );
                OnSettingsModified?.Invoke( this, EventArgs.Empty );
                return;
            }

            if ( settingsPage.RevisionDate != null && settingsPage.RevisionDate.Value > LastModified ) {
                BotSettings sets;
                try {
                    sets = JsonConvert.DeserializeObject<BotSettings>( settingsPage.MarkdownContent );
                }
                catch {
                    throw new Exception( "Wikipage is corrupted. Fix it, clear wiki page, or delete the page to recreate with defaults." );
                }
                Version = sets.Version;
                LastModified = settingsPage.RevisionDate.Value;
                RemoveScoreThreshold = sets.RemoveScoreThreshold;
                ReportScoreThreshold = sets.ReportScoreThreshold;
                RunEveryXMinutes = sets.RunEveryXMinutes;

                var addedDefaults = false;
                /***Module Defaults***/
                if ( sets.LicensingSmasher == null ) {
                    LicensingSmasher = new LicensingSmasherSettings();
                    addedDefaults = true;
                }
                else {
                    LicensingSmasher = sets.LicensingSmasher;
                }
                if (sets.YouTubeSpamDetector == null ) {
                    YouTubeSpamDetector = new YouTubeSpamDetectorSettings();
                    addedDefaults = true;
                }
                else {
                    YouTubeSpamDetector = sets.YouTubeSpamDetector;
                }
                if(sets.UserStalker == null ) {
                    UserStalker = new UserStalkerSettings();
                    addedDefaults = true;
                }
                else {
                    UserStalker = sets.UserStalker;
                }
                if(sets.SelfPromotionCombustor == null ) {
                    SelfPromotionCombustor = new SelfPromotionCombustorSettings();
                    addedDefaults = true;
                }
                else {
                    SelfPromotionCombustor = sets.SelfPromotionCombustor;
                }
                if(sets.HighTechBanHammer == null ) {
                    HighTechBanHammer = new HighTechBanHammerSettings();
                    addedDefaults = true;
                }
                else {
                    HighTechBanHammer = sets.HighTechBanHammer;
                }
                /***End Module Defaults ***/
                if ( addedDefaults ) {
                    wiki.EditPage( WikiPageName, JsonConvert.SerializeObject( this, Formatting.Indented, new StringEnumConverter()).Replace( "\r\n  ", "\r\n\r\n    " ), reason: "Add module default" );
                    LastModified = DateTime.UtcNow.AddMinutes( 1 );
                }
                Console.WriteLine("Settings in wiki changed or read for first time : Revision Date = {0}", LastModified);
                OnSettingsModified?.Invoke( this, EventArgs.Empty );
            }
            else {
                Console.WriteLine( "No updates to settings detected in wiki" );
            }
        }
Beispiel #2
0
 public HighTechBanHammer(HighTechBanHammerSettings sets, RedditSharp.Things.Subreddit subreddit) : this()
 {
     Settings  = sets;
     Subreddit = subreddit;
 }
Beispiel #3
0
 public HighTechBanHammer( HighTechBanHammerSettings sets, RedditSharp.Things.Subreddit subreddit ) : this() {
     Settings = sets;
     Subreddit = subreddit;
 }