From 6be8841eb9d2d49ad966afe93a8d97f94c4c1538 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Tue, 1 Aug 2017 19:29:45 +0100 Subject: [PATCH] If a subreddit/post/user is mentioned, don't give information about again it for 5 minutes --- dave/modules/reddit.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/dave/modules/reddit.py b/dave/modules/reddit.py index 65fc2e0..9e5e5a4 100644 --- a/dave/modules/reddit.py +++ b/dave/modules/reddit.py @@ -13,6 +13,11 @@ from humanize import naturaltime, naturaldelta, intcomma @dave.module.dont_always_run_if_run() def post(bot, args, sender, source): """Ran whenever a reddit post is sent""" + if dave.config.redis.exists("reddit:post:mentioned:{}:{}".format(args[0], source)): + # if this post was mentioned in the last x seconds (see the setex below), + # don't spam info about it + return + if not dave.config.redis.exists("reddit:post:{}".format(args[0])): req = get("https://reddit.com/{}.json?limit=1".format(args[0]), headers={'user-agent': 'irc bot (https://github.com/w4)'}) @@ -30,6 +35,8 @@ def post(bot, args, sender, source): resp = req[0]["data"]["children"][0]["data"] + dave.config.redis.setex("reddit:post:mentioned:{}:{}".format(args[0], source), 300, 1) + bot.msg(source, assembleFormattedText( A.normal[ A.bold[A.fg.lightRed["[NSFW] "]] if resp["over_18"] else "", @@ -49,6 +56,12 @@ def post(bot, args, sender, source): @dave.module.dont_always_run_if_run() def subreddit(bot, args, sender, source): """Ran whenever a subreddit is mentioned""" + if dave.config.redis.exists("reddit:subreddit:mentioned:{}:{}".format(args[0], + source)): + # if this subreddit was mentioned in the last x seconds (see the setex below), + # don't spam info about it + return + if not dave.config.redis.exists("reddit:subreddit:{}".format(args[0])): req = get("https://reddit.com/r/{}/about.json".format(args[0]), headers={'user-agent': 'irc bot (https://github.com/w4)'}) @@ -70,6 +83,10 @@ def subreddit(bot, args, sender, source): resp = req["data"] + # don't give info about this user again in this channel for 300 seconds + dave.config.redis.setex("reddit:subreddit:mentioned:{}:{}".format(args[0], source), + 300, 1) + bot.msg(source, assembleFormattedText( A.normal[ A.bold[A.fg.lightRed["[NSFW] "]] if resp["over18"] else "", @@ -88,6 +105,11 @@ def subreddit(bot, args, sender, source): @dave.module.ratelimit(1, 1) @dave.module.dont_always_run_if_run() def user(bot, args, sender, source): + if dave.config.redis.exists("reddit:user:mentioned:{}:{}".format(args[0], source)): + # if this user was mentioned in the last x seconds (see the setex below), don't + # spam info about them + return + if not dave.config.redis.exists("reddit:user:{}".format(args[0])): req = get("https://reddit.com/u/{}/about.json".format(args[0]), headers={'user-agent': 'irc bot (https://github.com/w4)'}) @@ -105,6 +127,9 @@ def user(bot, args, sender, source): resp = req["data"] + # don't give info about this user again in this channel for 300 seconds + dave.config.redis.setex("reddit:user:mentioned:{}:{}".format(args[0], source), 300, 1) + bot.msg(source, assembleFormattedText( A.normal[ A.bold[resp["name"]], -- libgit2 1.7.2