Fixed wrong counting, added comment functionality
This commit is contained in:
@@ -29,6 +29,12 @@ class DatabaseManager:
|
|||||||
)
|
)
|
||||||
''')
|
''')
|
||||||
|
|
||||||
|
self.cursor.execute('''
|
||||||
|
CREATE TABLE IF NOT EXISTS commented_users (
|
||||||
|
username TEXT PRIMARY KEY
|
||||||
|
)
|
||||||
|
''')
|
||||||
|
|
||||||
# Wishes table
|
# Wishes table
|
||||||
self.cursor.execute('''
|
self.cursor.execute('''
|
||||||
CREATE TABLE IF NOT EXISTS wishes (
|
CREATE TABLE IF NOT EXISTS wishes (
|
||||||
@@ -71,6 +77,22 @@ class DatabaseManager:
|
|||||||
except sqlite3.IntegrityError:
|
except sqlite3.IntegrityError:
|
||||||
return False
|
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):
|
def save_wish(self, sender, recipient, message):
|
||||||
"""Inserts or Replaces a wish"""
|
"""Inserts or Replaces a wish"""
|
||||||
self.cursor.execute("SELECT username FROM participants WHERE username = ?", (recipient,))
|
self.cursor.execute("SELECT username FROM participants WHERE username = ?", (recipient,))
|
||||||
@@ -166,8 +188,8 @@ class SafetyFilter:
|
|||||||
|
|
||||||
class ChristmasBot:
|
class ChristmasBot:
|
||||||
|
|
||||||
# Store the last updated data to prevent unneeded updates.
|
# Store the last updated overall count of wishes.
|
||||||
last_updated_data = []
|
last_updated_count = 0
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
print("🎄 Initializing Christmas Helper...")
|
print("🎄 Initializing Christmas Helper...")
|
||||||
@@ -214,14 +236,18 @@ class ChristmasBot:
|
|||||||
if now - self.last_dashboard_update < config.update_post_interval: return
|
if now - self.last_dashboard_update < config.update_post_interval: return
|
||||||
|
|
||||||
data = self.db.get_forest_data()
|
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.
|
# Nothing changed.
|
||||||
print(f"No change in dashboard, last checked {datetime.datetime.fromtimestamp(now).strftime('%Y-%m-%d %H:%M:%S')}...", end="\r")
|
print(f"No change in dashboard, last checked {datetime.datetime.fromtimestamp(now).strftime('%Y-%m-%d %H:%M:%S')}...", end="\r")
|
||||||
return
|
return
|
||||||
|
|
||||||
print("📝 Updating Forest Dashboard...")
|
print("📝 Updating Forest Dashboard...")
|
||||||
self.last_updated_data = data
|
self.last_updated_count = overall_count
|
||||||
body = "# 🎄 The Christmas Forest 🎄\n\n"
|
body = "# 🎄 The Christmas Forest 🎄\n\n"
|
||||||
body += "### How to Participate:\n"
|
body += "### How to Participate:\n"
|
||||||
body += "1. Send a DM to u/{}\n".format(config.username)
|
body += "1. Send a DM to u/{}\n".format(config.username)
|
||||||
@@ -354,6 +380,14 @@ class ChristmasBot:
|
|||||||
self.db.clear_conversation(sender)
|
self.db.clear_conversation(sender)
|
||||||
|
|
||||||
print(f"💾 Wish saved for {target.lower()}")
|
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":
|
if state == "CONFIRM_REPLACE":
|
||||||
message.reply(f"✅ Your wish for u/{target.lower()} has been **updated**! 🎄")
|
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}**.")
|
print(f"{sender} has updated their wish for u/{target.lower()} to **{content}**.")
|
||||||
|
|||||||
Binary file not shown.
@@ -20,6 +20,8 @@ check_interval=60
|
|||||||
#Interval in seconds to update the dashboard if something changed
|
#Interval in seconds to update the dashboard if something changed
|
||||||
update_post_interval=300
|
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_username="droid_tect"
|
||||||
megathread_password="lwwh1b-9gK-Ds_o"
|
megathread_password="lwwh1b-9gK-Ds_o"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user