Add some comments; fix incorrect response message

master
Thor 3 years ago
parent d22a6002b8
commit 448e60e81f
  1. 17
      cringebot.py

@ -14,13 +14,16 @@ from bot import Bot, BotClient
class CringeBotClient(BotClient):
def __init__(self, bot, config):
# Initialise HTML-to-Markdown converter
self.h2t = html2text.HTML2Text()
self.h2t.ignore_links = True
# Create scheduler for deferred deletion of posts
self.deletion_scheduler = sched.scheduler(time.time, time.sleep)
super().__init__(bot, config)
# Send DM reply to message, appropriately tagged, and schedules it for deferred deletion
def respond(self, status, message):
self.log("Responding with:")
self.log(message)
@ -32,20 +35,30 @@ class CringeBotClient(BotClient):
self.deletion_report()
def on_poll(self):
# Perform any scheduled deletes
self.deletion_scheduler.run(blocking = False)
def on_status(self, status):
# Ignore statuses from other accounts
if status["account"]["id"] != self.api.me()["id"]:
return
# Ignore statuses this account boosts
if status["reblog"]:
return
# Extract plain text
md_text = self.h2t.handle(status["content"])
# Ignore bot's own statuses
if self.config["tag"] in md_text.split():
return
# Create faux HTML email of status
mail_text = toot_dict_to_mail(status).format()
# Format and log plain-text preview
preview = toot_dict_to_mail(status)
preview.body = md_text
preview_text = preview.format()
@ -54,6 +67,8 @@ class CringeBotClient(BotClient):
self.log(preview_text)
self.log()
# Look for commands in the replies to the bot's notifications
replied_id = status.get("in_reply_to_id", None)
if replied_id:
try:
@ -96,7 +111,7 @@ class CringeBotClient(BotClient):
elif token == "based":
bogofilter.run(target_mail_text, [bogofilter.UNLEARN_HAM])
self.unqueue_deletion(target_status_id)
self.respond(status, "Unlearned as cringe")
self.respond(status, "Unlearned as based")
break
elif token == "relearn":
token = tokens.popleft()

Loading…
Cancel
Save