diff options
| author | jwanek <eddie.atten.ea29@gmail.com> | 2020-03-31 11:06:39 +0100 | 
|---|---|---|
| committer | jwanek <eddie.atten.ea29@gmail.com> | 2020-03-31 11:06:39 +0100 | 
| commit | 3dc0d42799b81437409788fdc08cfa03aa98b49d (patch) | |
| tree | d23315c9de1f64d500f17910dadc802941b3444e | |
| parent | b6976fd5bc7d74e3dff8b888a812ecfc4edc3e1f (diff) | |
| download | SmallYTChannelBot-3dc0d42799b81437409788fdc08cfa03aa98b49d.tar.gz SmallYTChannelBot-3dc0d42799b81437409788fdc08cfa03aa98b49d.zip | |
completed refactoring, see README.md
| -rwxr-xr-x | SmallYTChannelDatabase.db | bin | 16392192 -> 16393216 bytes | |||
| -rwxr-xr-x | actions.log | 28 | ||||
| -rwxr-xr-x | readme.md | 4 | ||||
| -rwxr-xr-x | subreddit.py | 118 | 
4 files changed, 100 insertions, 50 deletions
| diff --git a/SmallYTChannelDatabase.db b/SmallYTChannelDatabase.dbBinary files differ index 3098f74..6e6a2dd 100755 --- a/SmallYTChannelDatabase.db +++ b/SmallYTChannelDatabase.db diff --git a/actions.log b/actions.log index 0ff2a76..5694d1f 100755 --- a/actions.log +++ b/actions.log @@ -141273,3 +141273,31 @@  12363	[Mar 30 2020 18:35:39]	There has been a new submission: 'criquie', with flair 'Video Critique'  12363	[Mar 30 2020 18:38:22]	'/u/_a_creative_username' has given '/u/bot-ocr' lambda!  12363	[Mar 30 2020 18:41:12]	A moderator removed -14 lambda from /u/bot-ocr for the reason 'for the purposes of testing' +3710	[Mar 31 2020 08:29:37]	 +#################### +[Mar 31 2020 08:29:37] RESTARTED +#################### + +3710	[Mar 31 2020 08:30:43]	A moderator refunded 20 lambda from /u/bot-ocr for the reason 'For the purposes of testing' +5948	[Mar 31 2020 09:03:08]	 +#################### +[Mar 31 2020 09:03:08] RESTARTED +#################### + +5948	[Mar 31 2020 09:05:51]	'/u/_a_creative_username' has given '/u/EuroSocialistsBot' lambda! +6898	[Mar 31 2020 09:13:33]	 +#################### +[Mar 31 2020 09:13:33] RESTARTED +#################### + +6898	[Mar 31 2020 09:14:40]	A moderator removed 35 lambda from /u/bot-ocr for the reason 'for being a filthy degenerate' +9381	[Mar 31 2020 09:51:11]	 +#################### +[Mar 31 2020 09:51:11] RESTARTED +#################### + +9490	[Mar 31 2020 09:52:52]	 +#################### +[Mar 31 2020 09:52:52] RESTARTED +#################### + @@ -24,11 +24,11 @@ The source code for /u/SmallYTChannelBot. Currently refactoring is in progress.  - [x] Changed license from MIT to GPLv3 -- [ ] Update flair when using the !takelambda command +- [x] Update flair when using the !takelambda command  - [x] Reimplented the `!takelambda` command -- [ ] Change `!takelambda` to `!refundlambda` when negative +- [x] Change `!takelambda` to `!refundlambda` when negative  # About the database's structure diff --git a/subreddit.py b/subreddit.py index 372411b..fc13cb1 100755 --- a/subreddit.py +++ b/subreddit.py @@ -40,10 +40,13 @@ def get_lambda_from_flair(s):      else:          return "" -def update_users_flair(comment): -    username = str(comment.author) -    flairscore = get_lambda_from_flair(str(comment.author_flair_text)) -    flairtext = comment.author_flair_text +def update_users_flair_from_comment(comment): +    #implemented only for legacy +    update_users_flair(str(comment.author)) + +def update_users_flair(username): +    flairtext = next(SUBREDDIT.flair(redditor=username))["flair_text"] +    flairscore = get_lambda_from_flair(flairtext)      if flairtext is None:          flairtext = ""      else: @@ -135,7 +138,7 @@ def handle_mylambda(comment):                  text += "\n\n[%i more...]" % len(links) - count                  break -    update_users_flair(comment) +    update_users_flair_from_comment(comment)      return text  def handle_givelambda(comment): @@ -167,9 +170,8 @@ def handle_givelambda(comment):          # else:          db.give_lambda(parentauthour, submission.permalink) -    update_users_flair(comment) -    #updating the parent comment is now unnessicary since bonus lambda was removed -    # update_users_flair(comment.parent()) +    # update_users_flair_from_comment(comment) +    update_users_flair_from_comment(comment.parent())      return text  def handle_takelambda(comment): @@ -186,7 +188,24 @@ def handle_takelambda(comment):          display("{ERROR while removing λ} %s" % e)          text = r"An error was encountered. Please use the syntax `!takelambda [user] [how much to remove {integer}] [reason]`" + "\n\nThe error was:\n\n" + str(e) -    update_users_flair(comment.parent()) +    update_users_flair(user) +    return text + +def handle_refundlambda(comment): +    try: +        splitted = comment.body.split() +        user = splitted[1].replace("/u/", "") +        toadd = int(splitted[2].replace("\\", "")) +        reason = " ".join(splitted[3:]) +     +        text = "/u/%s has had %iλ refunded for the reason '%s'. /u/%s now has %iλ" % (user, toadd, reason, user, db.get_lambda(user)[0] + toadd) +        db.change_lambda(user, toadd) +        display("A moderator refunded %i lambda from /u/%s for the reason '%s'" % (toadd,  user, reason)) +    except Exception as e: +        display("{ERROR while refunding λ} %s" % e) +        text = r"An error was encountered. Please use the syntax `!refundlambda [user] [how much to add {integer}] [reason]`" + "\n\nThe error was:\n\n" + str(e) + +    update_users_flair(user)      return text  def handle_submission(submission): @@ -261,7 +280,7 @@ Views|%s              except:                  pass -    update_users_flair(submission) +    update_users_flair_from_comment(submission)      return text  def main(): @@ -269,44 +288,47 @@ def main():      submission_stream = SUBREDDIT.stream.submissions(pause_after=-1)      while True: -        # try: -        for comment in comment_stream: -            if comment is None: -                break -            if not db.id_in_blacklist(comment.id): -                db.add_to_blacklist(comment.id) - -                response = None -                if "!mylambda" in comment.body.lower() and str(comment.author) != "SmallYTChannelBot": -                    response = handle_mylambda(comment) - -                if "!givelambda" in comment.body.lower() and str(comment.author) != "SmallYTChannelBot": -                    response = handle_givelambda(comment)         - -                if comment.body[:11] == "!takelambda" and str(comment.author) in get_mods(): -                    response = handle_takelambda(comment) - -                if response is not None: -                    reply = comment.reply(response + COMMENT_TAIL) -                    reply.mod.distinguish(sticky = False) - -        for submission in submission_stream: -            if submission is None: -                break -            if not db.id_in_blacklist(submission.id): -                db.add_to_blacklist(submission.id)                          -                display("There has been a new submission: '%s', with flair '%s'" % (submission.title, submission.link_flair_text)) - -                response = None -                if str(submission.author) not in get_mods(): -                    response = handle_submission(submission) -                    reply = submission.reply(response + COMMENT_TAIL) -                    reply.mod.distinguish(sticky = True) -                    reply.mod.approve() - -        # except Exception as e: -        #     display("{ERROR} %s" % e) -        #     continue +        try: +            for comment in comment_stream: +                if comment is None: +                    break +                if not db.id_in_blacklist(comment.id): +                    db.add_to_blacklist(comment.id) + +                    response = None +                    if "!mylambda" in comment.body.lower() and str(comment.author) != "SmallYTChannelBot": +                        response = handle_mylambda(comment) + +                    if "!givelambda" in comment.body.lower() and str(comment.author) != "SmallYTChannelBot": +                        response = handle_givelambda(comment)         + +                    if comment.body.startswith("!takelambda") and str(comment.author) in get_mods(): +                        response = handle_takelambda(comment) + +                    if comment.body.startswith("!refundlambda") and str(comment.author) in get_mods(): +                        response = handle_refundlambda(comment) + +                    if response is not None: +                        reply = comment.reply(response + COMMENT_TAIL) +                        reply.mod.distinguish(sticky = False) + +            for submission in submission_stream: +                if submission is None: +                    break +                if not db.id_in_blacklist(submission.id): +                    db.add_to_blacklist(submission.id)                          +                    display("There has been a new submission: '%s', with flair '%s'" % (submission.title, submission.link_flair_text)) + +                    response = None +                    if str(submission.author) not in get_mods(): +                        response = handle_submission(submission) +                        reply = submission.reply(response + COMMENT_TAIL) +                        reply.mod.distinguish(sticky = True) +                        reply.mod.approve() + +        except Exception as e: +            display("{ERROR} %s" % e) +            continue  if __name__ == "__main__":      file = open("pid.txt", "w") | 
