From 011ec2c9a8e24616fdba8005e5e57d196b18be14 Mon Sep 17 00:00:00 2001 From: notagoat Date: Sun, 19 May 2019 13:31:42 +0100 Subject: [PATCH 1/3] Added the ability to pin and unpin toots with the pin and unpin command. Would like to clean up code as well as possibly toot confirmation and error messages --- main.py | 5 +++-- reply.py | 33 +++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index 011a5b4..613e0de 100755 --- a/main.py +++ b/main.py @@ -11,8 +11,9 @@ import os, sqlite3, signal, sys, json, re, shutil import requests import functions -scopes = ["read:statuses", "read:accounts", "read:follows", "write:statuses", "read:notifications"] +scopes = ["read:statuses", "read:accounts", "read:follows", "write:statuses", "read:notifications", "write:accounts"] #cfg defaults + cfg = { "site": "https://botsin.space", "cw": None, @@ -21,10 +22,10 @@ cfg = { "mention_handling": 1, "max_thread_length": 15 } + try: cfg.update(json.load(open('config.json', 'r'))) except: - shutil.copy2("config.sample.json", "config.json") cfg.update(json.load(open('config.json', 'r'))) diff --git a/reply.py b/reply.py index 060a673..ad982ca 100755 --- a/reply.py +++ b/reply.py @@ -38,6 +38,7 @@ class ReplyListener(mastodon.StreamListener): posts = 0 for post in context['ancestors']: if post['account']['id'] == me: + pin = post["id"] #Only used if pin is called, but easier to call here posts += 1 if posts >= cfg['max_thread_length']: # stop replying @@ -45,14 +46,30 @@ class ReplyListener(mastodon.StreamListener): return mention = extract_toot(notification['status']['content']) - toot = functions.make_toot(True)['toot'] #generate a toot - toot = acct + " " + toot #prepend the @ - print(acct + " says " + mention) #logging - visibility = notification['status']['visibility'] - if visibility == "public": - visibility = "unlisted" - client.status_post(toot, post_id, visibility=visibility, spoiler_text = cfg['cw']) #send toost - print("replied with " + toot) #logging + if (mention == "pin") or (mention == "unpin"): #check for keywords + print("Found pin/unpin") + #get a list of people the bot is following + validusers = client.account_following(me) + for user in validusers: + if user["id"] == notification["account"]["id"]: #user is #valid + print("User is valid") + if mention == "pin": + print("pin received, pinning") + client.status_pin(pin) + else: + print("unpin received, unpinning") + client.status_unpin(pin) + else: + print("User is not valid") + else: + toot = functions.make_toot(True)['toot'] #generate a toot + toot = acct + " " + toot #prepend the @ + print(acct + " says " + mention) #logging + visibility = notification['status']['visibility'] + if visibility == "public": + visibility = "unlisted" + client.status_post(toot, post_id, visibility=visibility, spoiler_text = cfg['cw']) #send toost + print("replied with " + toot) #logging rl = ReplyListener() client.stream_user(rl) #go! From 65cebc37ac18a82876feb872907f1c7847443f14 Mon Sep 17 00:00:00 2001 From: notagoat Date: Sun, 19 May 2019 13:54:59 +0100 Subject: [PATCH 2/3] Added toots! --- reply.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/reply.py b/reply.py index ad982ca..3fe1159 100755 --- a/reply.py +++ b/reply.py @@ -53,11 +53,16 @@ class ReplyListener(mastodon.StreamListener): for user in validusers: if user["id"] == notification["account"]["id"]: #user is #valid print("User is valid") + visibility = notification['status']['visibility'] + if visibility == "public": + visibility = "unlisted" if mention == "pin": print("pin received, pinning") client.status_pin(pin) + client.status_post("Toot pinned!", post_id, visibility=visibility spoiler_text = cfg['cw']) else: print("unpin received, unpinning") + client.status_post("Toot Unpinned!", post_id, visibility=visibility spoiler_text = cfg['cw']) client.status_unpin(pin) else: print("User is not valid") From 8ebef4327d71ddd079a6a12349ed127c3c7eaea1 Mon Sep 17 00:00:00 2001 From: notagoat Date: Sun, 19 May 2019 13:57:20 +0100 Subject: [PATCH 3/3] Fixey fix --- reply.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/reply.py b/reply.py index 3fe1159..d8f20d3 100755 --- a/reply.py +++ b/reply.py @@ -59,10 +59,10 @@ class ReplyListener(mastodon.StreamListener): if mention == "pin": print("pin received, pinning") client.status_pin(pin) - client.status_post("Toot pinned!", post_id, visibility=visibility spoiler_text = cfg['cw']) + client.status_post("Toot pinned!", post_id, visibility=visibility, spoiler_text = cfg['cw']) else: print("unpin received, unpinning") - client.status_post("Toot Unpinned!", post_id, visibility=visibility spoiler_text = cfg['cw']) + client.status_post("Toot unpinned!", post_id, visibility=visibility, spoiler_text = cfg['cw']) client.status_unpin(pin) else: print("User is not valid")