import json import logging import re import random import time import requests from rich import traceback from bots.data import bussyboy_queue, config, db from bots.clients.drama import DramaClient from bots.clients.runpod import RunpodClient traceback.install() logging.basicConfig( filename=f"{config['data_dir']}/logs/bussyboy.log", filemode="a", format="%(name)s - %(levelname)s - %(message)s", level=logging.INFO, ) logger = logging.getLogger("bussyboy") drama_client = DramaClient(config["bussyboy"]["token"], logger=logger) runpod_client = RunpodClient(logger=logger) def queue_reply(comment): try: post, thread = drama_client.fetch_context(comment) except requests.exceptions.HTTPError as err: logger.error(f"Error when fetching context: {err}") return db["bussyboy_id"] += 1 bussyboy_queue[str(db["bussyboy_id"])] = {"post": post, "thread": thread} def queue_random_reply(): comments, newest_id = drama_client.fetch_new_comments( after=db["bussyboy_last_processed"], limit=25 ) db["bussyboy_last_processed"] = newest_id comments = [ c for c in comments if "author_name" in c and not c["is_bot"] and c["author_name"] != config["bussyboy"]["username"] and c["author_name"] != "👻" and c["author_id"] not in config["bussyboy"]["ignore_user_ids"] and c["post_id"] != 0 ] if len(comments) == 0: logger.warn("No comments found.") return random.shuffle(comments) queue_reply(comments[0]) def main(): if "bussyboy_id" not in db: db["bussyboy_id"] = 0 if "bussyboy_last_processed" not in db: db["bussyboy_last_processed"] = 0 if "bussyboy_last_random_reply" not in db: db["bussyboy_last_random_reply"] = 0 # If both the queue and an instance are active, return and let it complete. if bussyboy_queue and runpod_client.is_running("bussyboy"): return time_since_reply = time.time() - db["bussyboy_last_random_reply"] if time_since_reply > config["bussyboy"]["reply_frequency"]: db["bussyboy_last_random_reply"] = time.time() queue_random_reply() for notif in drama_client.fetch_notifications(): if not notif["is_bot"]: queue_reply(notif) # Create instance if there are requests to be fulfilled. if bussyboy_queue: runpod_client.create_instance( f"{config['data_dir']}/bussyboy/create_runpod.json" ) if __name__ == "__main__": main()