def new_comment(comment, reddit): # Skip self posts for subreddit_name, config in wiki_config.items(): word_list = check_words(comment.body, config, comment.author) if len(word_list) > 0: message_body = "Word list: %s\nLink: %s" % (str(word_list), comment.permalink) get_subreddit(subreddit_name).send_modmail( "Given word/words has/have been found in a comment", message_body)
def new_post(submission, reddit): # Skip link posts if not submission.is_self: return for subreddit_name, config in wiki_config.items(): word_list = check_words(submission.selftext, config, submission.author) if len(word_list) > 0: message_body = "Word list: %s\nLink: %s" % (str(word_list), submission.shortlink) get_subreddit(subreddit_name).send_modmail( "Given word/words has/have been found in a submission", message_body)
def get_notes(report): """ Returns user notes for the reported item """ subreddit = get_subreddit(report.subreddit_name) wiki_content = subreddit.wiki("usernotes").content unotes = json.loads(wiki_content) if unotes["ver"] != 6: report.author.send_pm( "Usernotes decode error", "Invalid usernotes version found, please upgrade") return data_raw = zlib.decompress(base64.b64decode(unotes["blob"])).decode() data = json.loads(data_raw) if report.author_name not in data: report.author.send_pm( "Usernotes not found for %s" % report.author_name, "No notes found") return reply = "User %s has the following notes:\n\n" % report.author_name for note in data[report.author_name]["ns"]: reply += note["n"] + "\n\n" report.author.send_pm("Usernotes", reply)
def set_sidebar_old_reddit(subreddit_name, choice, local_file): sub = get_subreddit(subreddit_name) # Get sidebar contents wiki = sub.wiki("config/sidebar") content = wiki.get_content() # Find the start/end markers in the sidebar start = content.find(START_MARKER) end = content.find(END_MARKER) if start == -1 or end == -1: logger.error("Could not find markers on %s" % subreddit_name) return # Upload the selected image as 'promsub' sub.stylesheet_upload_image("promsub", local_file) # Generate new sidebar content new_content = "%s\n%s\n\n%s" % ( content[:start] + START_MARKER, "##### [%s](https://www.reddit.com/r/%s \"DailyLink\")" % (choice, choice), content[end:]) # Update the sidebar wiki.edit(new_content) # Do a dummy sidebar edit (because reddit needs this apparently) sub.stylesheet_set_content(sub.stylesheet_get_content()) logger.debug("Done on old reddit")
def set_sidebar_new_reddit(subreddit_name, choice, local_file): sub = get_subreddit(subreddit_name) pic_widget = None for widget in sub.get_sidebar_widgets(): if widget.name == "DailyLink": pic_widget = widget break if not pic_widget: logger.debug("No picture widget found") return pic_widget.set_image(local_file, "https://www.reddit.com/r/" + choice) logger.debug("Done on new reddit")
def __init__(self, bot_inst, master_subreddit, path_list=None, bot_config={}, db_params={}): """ Class that manages plugins from a given list of paths. :param bot_inst: bot instance :param master_subreddit: subreddit where core bot configuration is stored :param path_list: list of folders relative to the location from where to load plugins :param bot_config: bot config data :param db_params: how to log in to the psql server """ self.modules = [] self.plugin_threads = [] self.per_last_exec = {} self.bot = bot_inst self.config = bot_config self.plugin_args = {} self.inbox_cmds = {} self.master_sub = get_subreddit(master_subreddit) # Save the given watched subreddits self.given_watched_subs = {} for sub in get_moderated_subs(): self.given_watched_subs[sub] = True self.watched_subs = dict(self.given_watched_subs) self.db = None if db_params: print("Connecting to DB") # Create DB connection self.db = db_data( "postgresql+psycopg2://{user}:{password}@{host}/{database}".format(**db_params)) # Fill the standard parameter list self.plugin_args["plugin_manager"] = self self.plugin_args["reddit"] = self self.plugin_args["config"] = self.config self.plugin_args["db"] = self.db self.plugin_args["bot_owner"] = get_user( self.config.get("owner", "owner")) # Set start time self.start_time = utils.utcnow() print("[%d] Getting dispatchers" % utils.utcnow()) self.dispatchers = {} self.dispatchers[DISPATCH_ANY] = DispatchAll(self.plugin_args) # Get notifications from the hook module callbacks.append(self.add_plugin_function) print("[%d] Loading plugins" % utils.utcnow()) # Load plugins for path in path_list: self.load_plugins(path) print("[%d] Running on start plugins" % utils.utcnow()) self.dispatchers[DISPATCH_ANY].run_on_start(False) print("[%d] Creating periodic thread" % utils.utcnow()) # Create the periodic thread to trigger periodic events start_tick(1.0, self.periodic_func) print("[%d] Startup done!" % utils.utcnow()) # Start watching subreddits watch_all( self.feed_sub, self.feed_comms, self.feed_inbox, self.feed_reports, self.feed_modlog)
def get_subreddit(self, name): return get_subreddit(name)