Made ChristmasBot case insensitive

This commit is contained in:
Alexander Dörflinger
2025-12-12 12:51:00 +01:00
parent 0ea97cf9f9
commit 21a83bc5ba
2 changed files with 20 additions and 20 deletions

View File

@@ -257,7 +257,7 @@ class ChristmasBot:
first_line = body_lines[0] first_line = body_lines[0]
if self.username_pattern.match(first_line): if self.username_pattern.match(first_line):
target_user = first_line[2:] target_user = first_line[2:].lower()
wish_content = '\n'.join(body_lines[1:]) wish_content = '\n'.join(body_lines[1:])
return target_user, wish_content return target_user, wish_content
@@ -302,7 +302,7 @@ class ChristmasBot:
# ERROR: Empty Body # ERROR: Empty Body
if not wish_content: if not wish_content:
message.reply(f"You didn't write a message for u/{target_user}! Please try again with text in the body.") message.reply(f"You didn't write a message for u/{target_user.lower()}! Please try again with text in the body.")
return return
# SAFETY CHECK # SAFETY CHECK
@@ -314,16 +314,16 @@ class ChristmasBot:
return return
# ERROR: No Flair # ERROR: No Flair
if not self.verify_user_flair(target_user): if not self.verify_user_flair(target_user.lower()):
message.reply(f"Sorry, u/{target_user} does not have a User Flair in r/{config.subreddit_name}.\n" message.reply(f"Sorry, u/{target_user.lower()} does not have a User Flair in r/{config.subreddit_name}.\n"
"We only allow wishes for active, flaired community members.") "We only allow wishes for active, flaired community members.")
return return
# CHECK: Existing Wish # CHECK: Existing Wish
existing_msg = self.db.check_existing_wish(sender, target_user) existing_msg = self.db.check_existing_wish(sender, target_user.lower())
if existing_msg: if existing_msg:
self.db.set_conversation_state(sender, "CONFIRM_REPLACE", target_user, wish_content) self.db.set_conversation_state(sender, "CONFIRM_REPLACE", target_user.lower(), wish_content)
reply = (f"⚠️ **You already sent a wish to u/{target_user}.**\n\n" reply = (f"⚠️ **You already sent a wish to u/{target_user}.**\n\n"
f"**Old Message:** {existing_msg[:100]}...\n\n" f"**Old Message:** {existing_msg[:100]}...\n\n"
f"**Do you want to REPLACE it with:**\n" f"**Do you want to REPLACE it with:**\n"
@@ -331,9 +331,9 @@ class ChristmasBot:
f"Reply **YES** to confirm or **NO** to cancel.") f"Reply **YES** to confirm or **NO** to cancel.")
message.reply(reply) message.reply(reply)
else: else:
self.db.set_conversation_state(sender, "CONFIRM_NEW", target_user, wish_content) self.db.set_conversation_state(sender, "CONFIRM_NEW", target_user.lower(), wish_content)
reply = (f"🎄 **Christmas Confirmation** 🎄\n\n" reply = (f"🎄 **Christmas Confirmation** 🎄\n\n"
f"I extracted that you want to send a wish to **u/{target_user}**.\n\n" f"I extracted that you want to send a wish to **u/{target_user.lower()}**.\n\n"
f"**Message:**\n" f"**Message:**\n"
f"> {wish_content}\n\n" f"> {wish_content}\n\n"
f"Is this correct? Reply **YES** to save or **NO** to cancel.") f"Is this correct? Reply **YES** to save or **NO** to cancel.")
@@ -350,16 +350,16 @@ class ChristmasBot:
return return
if self.yes_pattern.match(user_response): if self.yes_pattern.match(user_response):
self.db.save_wish(sender, target, content) self.db.save_wish(sender, target.lower(), content)
self.db.clear_conversation(sender) self.db.clear_conversation(sender)
print(f"💾 Wish saved for {target}") print(f"💾 Wish saved for {target.lower()}")
if state == "CONFIRM_REPLACE": if state == "CONFIRM_REPLACE":
message.reply(f"✅ Your wish for u/{target} 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} to **{content}**.") print(f"{sender} has updated their wish for u/{target.lower()} to **{content}**.")
else: else:
message.reply(f"✅ Your wish for u/{target} has been **saved**! 🎄") message.reply(f"✅ Your wish for u/{target.lower()} has been **saved**! 🎄")
print(f"{sender} has saved their wish for u/{target}: **{content}**.") print(f"{sender} has saved their wish for u/{target.lower()}: **{content}**.")
elif self.no_pattern.match(user_response): elif self.no_pattern.match(user_response):
self.db.clear_conversation(sender) self.db.clear_conversation(sender)
@@ -400,10 +400,10 @@ class ChristmasBot:
print(f"🎅 HO HO HO! It's Christmas! Distributing gifts to {len(recipients)} users...") print(f"🎅 HO HO HO! It's Christmas! Distributing gifts to {len(recipients)} users...")
for username in recipients: for username in recipients:
wishes = self.db.get_user_wishes(username) wishes = self.db.get_user_wishes(username.lower())
if not wishes: continue if not wishes: continue
print(f"🎁 Preparing gifts for u/{username}...") print(f"🎁 Preparing gifts for u/{username.lower()}...")
# --- PAGINATION LOGIC --- # --- PAGINATION LOGIC ---
messages_to_send = [] messages_to_send = []
@@ -411,7 +411,7 @@ class ChristmasBot:
current_part_num = 1 current_part_num = 1
# Header for first message # Header for first message
header = f"Merry Christmas u/{username}!\n\nThe wait is over. Here are your wishes:\n\n---\n\n" header = f"Merry Christmas u/{username.lower()}!\n\nThe wait is over. Here are your wishes:\n\n---\n\n"
current_part_content += header current_part_content += header
for sender, message_text in wishes: for sender, message_text in wishes:
@@ -446,12 +446,12 @@ class ChristmasBot:
# Small sleep between parts to ensure order and avoid spam trigger # Small sleep between parts to ensure order and avoid spam trigger
time.sleep(2) time.sleep(2)
self.db.mark_delivered(username) self.db.mark_delivered(username.lower())
print(f"✅ Delivered {len(messages_to_send)} part(s) to u/{username}") print(f"✅ Delivered {len(messages_to_send)} part(s) to u/{username.lower()}")
time.sleep(5) # Delay between USERS time.sleep(5) # Delay between USERS
except Exception as e: except Exception as e:
print(f"❌ Failed to send to u/{username}: {e}") print(f"❌ Failed to send to u/{username.lower()}: {e}")
if __name__ == "__main__": if __name__ == "__main__":

Binary file not shown.