diff --git a/stoat_map_notifications/python/main.py b/stoat_map_notifications/python/main.py index 626862d..d5a03b1 100644 --- a/stoat_map_notifications/python/main.py +++ b/stoat_map_notifications/python/main.py @@ -21,6 +21,17 @@ def insert_maps(fixed): cur.execute(sql_statement, flattened_values) conn.commit() +def get_author_map_notifcations(author): + with get_connection_unloze_playtime() as conn: + with conn.cursor() as cur: + sql_statement = f""" + SELECT mapname + FROM unloze_playtimestats.map_notifications + WHERE JSON_CONTAINS(users_to_notify, JSON_QUOTE(%s)); + """ + cur.execute(sql_statement, [author]) + return cur.fetchall() + def handle_map_notification(map_notify, author, thing): with get_connection_unloze_playtime() as conn: with conn.cursor() as cur: @@ -55,13 +66,31 @@ def handle_map_notification(map_notify, author, thing): cur.execute(sql_statement, [json.dumps(users), map_notify]) conn.commit() +def stoat_send_map_notification_list(show_map_notifications): + message = f"" + for author in show_map_notifications: + author_notifications = get_author_map_notifcations(author) + message += f"<@{author}> your map notifications are:\n" + for author_notification in author_notifications: + message += f"{author_notification[0]} " + message += "\n" + + headers = { + "x-bot-token": f"{stoat_token}", + "Content-Type": "application/json" + } + data = { + "content": message + } + response = requests.post(stoat_url_map_notifications, headers=headers, json=data) + def stoat_send_invalid_map_notifications(invalid_map_notification_cmds): message = f"" for cmd in invalid_map_notification_cmds: map_notify = cmd[0] author = cmd[1] - message += f"<@{author}> you need to write the full map name correct you dumbass. {map_notify} is not valid input. example: \nadd ze_azathoth_v1_css\nremove ze_azathoth_v1_css\n" + message += f"<@{author}> you need to write the full map name correct you dumbass. {map_notify} is not valid input. example: \nadd ze_azathoth_v1_css\nremove ze_azathoth_v1_css\nshowlist -> lists all your map notifications.\n" headers = { "x-bot-token": f"{stoat_token}", "Content-Type": "application/json" @@ -96,6 +125,7 @@ def check_new_map_notification_status(fixed, last_msg_id): valid_map_notification_cmds_add= [] valid_map_notification_cmds_remove= [] invalid_map_notification_cmds = [] + show_map_notifications = [] #if first time we take the most recent message as starting point if not last_msg_id: url = stoat_url_50_last_messages.replace('/messages?limit=50&include_users=true&sort=Oldest&after=', '/messages?limit=50&include_users=true') @@ -131,7 +161,8 @@ def check_new_map_notification_status(fixed, last_msg_id): valid_map_notification_cmds_remove.append([content, author]) else: invalid_map_notification_cmds.append([content, author]) - + elif content == "showlist": + show_map_notifications.append(author) else: invalid_map_notification_cmds.append([content, author]) except: @@ -143,7 +174,7 @@ def check_new_map_notification_status(fixed, last_msg_id): #print('invalid_map_notification_cmds: ', invalid_map_notification_cmds) #print('last_msg_id') - return valid_map_notification_cmds_add, valid_map_notification_cmds_remove, invalid_map_notification_cmds, last_msg_id + return valid_map_notification_cmds_add, valid_map_notification_cmds_remove, invalid_map_notification_cmds, last_msg_id, show_map_notifications def send_post_notify_message_to_stoat(list_of_people_to_notify, infomap): #print('list_of_people_to_notify: ', list_of_people_to_notify) @@ -209,7 +240,7 @@ def main(): #check last 50 messages for new map notification updates last_msg_id = get_last_msg_id_read() - valid_map_notification_cmds_add, valid_map_notification_cmds_remove, invalid_map_notification_cmds, last_msg_id = check_new_map_notification_status(fixed, last_msg_id) + valid_map_notification_cmds_add, valid_map_notification_cmds_remove, invalid_map_notification_cmds, last_msg_id, show_map_notifications = check_new_map_notification_status(fixed, last_msg_id) if valid_map_notification_cmds_add: stoat_send_valid_map_notifications("added", valid_map_notification_cmds_add) @@ -217,6 +248,8 @@ def main(): stoat_send_valid_map_notifications("removed", valid_map_notification_cmds_remove) if invalid_map_notification_cmds: stoat_send_invalid_map_notifications(invalid_map_notification_cmds) + if show_map_notifications: + stoat_send_map_notification_list(show_map_notifications) update_last_msg_id(last_msg_id)