diff --git a/ChristmasEvent.py b/ChristmasEvent.py index b7fcd01..f487d46 100755 --- a/ChristmasEvent.py +++ b/ChristmasEvent.py @@ -29,6 +29,12 @@ class DatabaseManager: ) ''') + self.cursor.execute(''' + CREATE TABLE IF NOT EXISTS commented_users ( + username TEXT PRIMARY KEY + ) + ''') + # Wishes table self.cursor.execute(''' CREATE TABLE IF NOT EXISTS wishes ( @@ -71,6 +77,22 @@ class DatabaseManager: except sqlite3.IntegrityError: return False + def add_commented_user(self, username): + try: + self.cursor.execute("INSERT INTO commented_users VALUES (?)", + (username, )) + self.conn.commit() + return True + except sqlite3.IntegrityError: + return False + + def get_commented_users(self, username): + try: + self.cursor.execute("SELECT * FROM commented_users WHERE username=?", (username,)) + return self.cursor.fetchone() + except sqlite3.IntegrityError: + return None + def save_wish(self, sender, recipient, message): """Inserts or Replaces a wish""" self.cursor.execute("SELECT username FROM participants WHERE username = ?", (recipient,)) @@ -166,8 +188,8 @@ class SafetyFilter: class ChristmasBot: - # Store the last updated data to prevent unneeded updates. - last_updated_data = [] + # Store the last updated overall count of wishes. + last_updated_count = 0 def __init__(self): print("🎄 Initializing Christmas Helper...") @@ -214,14 +236,18 @@ class ChristmasBot: if now - self.last_dashboard_update < config.update_post_interval: return data = self.db.get_forest_data() + overall_count = 0 + if data: + for username, count in data: + overall_count += count - if len(self.last_updated_data) == len(data) or len(data) == 0: + if overall_count == self.last_updated_count: # Nothing changed. print(f"No change in dashboard, last checked {datetime.datetime.fromtimestamp(now).strftime('%Y-%m-%d %H:%M:%S')}...", end="\r") return print("📝 Updating Forest Dashboard...") - self.last_updated_data = data + self.last_updated_count = overall_count body = "# 🎄 The Christmas Forest 🎄\n\n" body += "### How to Participate:\n" body += "1. Send a DM to u/{}\n".format(config.username) @@ -354,6 +380,14 @@ class ChristmasBot: self.db.clear_conversation(sender) print(f"💾 Wish saved for {target.lower()}") + if self.db.get_commented_users(target.lower()): + print("User already existed before, no need to update") + else: + print("New user, add comment") + self.db.add_commented_user(target.lower()) + submission = self.reddit.submission(id=config.master_post_id) + submission.add_comment(config.comment_base_text.format(username=target.lower())) + if state == "CONFIRM_REPLACE": message.reply(f"✅ Your wish for u/{target.lower()} has been **updated**! 🎄") print(f"{sender} has updated their wish for u/{target.lower()} to **{content}**.") diff --git a/christmas_event.db b/christmas_event.db index f10c22e..38712ba 100644 Binary files a/christmas_event.db and b/christmas_event.db differ diff --git a/config.py b/config.py index 1b9a195..3e179e4 100644 --- a/config.py +++ b/config.py @@ -20,6 +20,8 @@ check_interval=60 #Interval in seconds to update the dashboard if something changed update_post_interval=300 +comment_base_text="(Pinging in comment, as no more than 3 pings per post)\n\nYou got messages: u/{username}" + megathread_username="droid_tect" megathread_password="lwwh1b-9gK-Ds_o"