diff --git a/README.md b/README.md index 187e16c..07a0b35 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,6 @@ After all of that you're good to go! Happy using :) * [x] Get applications .json * [ ] Get application by id and user_id * [x] Age as a DD.MM.YYYY -* [ ] Notify about upcoming birthdays +* [x] Notify about upcoming birthdays * [x] Change the application data * [x] Check if user is already in group \ No newline at end of file diff --git a/config_example.json b/config_example.json index 8d4fa1d..c76be67 100644 --- a/config_example.json +++ b/config_example.json @@ -2,6 +2,8 @@ "locale": "uk", "owner": 0, "age_allowed": 0, + "birthdays_notify": true, + "birthdays_time": "09:00", "admin_group": 0, "destination_group": 0, "admins": [], diff --git a/locale/uk.json b/locale/uk.json index 3db8e0a..e6a2f8e 100644 --- a/locale/uk.json +++ b/locale/uk.json @@ -52,6 +52,7 @@ "sus_refused_by": "❌ **Доступ заборонено**\nАдмін **{0}** заборонив `{1}` доступ до спільноти не за персональним посиланням.", "reapply_forbidden": "❌ **Дія неможлива**\nТвоя минула анкета ще не була схвалена або відхилена.", "reapply_in_progress": "❌ **Дія неможлива**\nТи прямо зараз вже заповнюєш анкету. Якщо в ній є помилка - після заповнення просто натисни **{0}** та почни знову.", + "birthday": "У користувача **{0}** (@{1}) сьогодні день народження! Виповнилось {2} років", "question_titles": { "question1": "Ім'я/звертання:", "question2": "День народження:", diff --git a/main.py b/main.py index fcff820..f7c0977 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,11 @@ from datetime import datetime +from threading import Thread from dateutil.relativedelta import relativedelta from time import time from os import getpid, path +from modules.birthdays import check_birthdays from modules.utils import * +from schedule import run_pending, every from pyrogram.client import Client from pyrogram import filters @@ -558,6 +561,26 @@ if __name__ == "__main__": # I did compare performance, almost no difference and it's much more useful this way. Change my mind. app.start() # type: ignore + if configGet("birthdays_notify"): + + every().day.at(configGet("birthdays_time")).do(check_birthdays, app) + + # Background tasks checker + def background_task(): + try: + while True: + try: + run_pending() + #print('Checked') + time.sleep(1) + except: + pass + except KeyboardInterrupt: + print('\nShutting down') + killProc(pid) + t = Thread(target=background_task) + t.start() + app.send_message(configGet("owner"), f"Starting up with pid `{pid}`") # type: ignore # Registering user commands diff --git a/modules/birthdays.py b/modules/birthdays.py new file mode 100644 index 0000000..04963d9 --- /dev/null +++ b/modules/birthdays.py @@ -0,0 +1,18 @@ +from datetime import datetime +from os import fsdecode, listdir, sep +from modules.utils import configGet, jsonLoad, locale +from dateutil.relativedelta import relativedelta + + +def check_birthdays(app): + for user_file in listdir(f"{configGet('data', 'locations')}{sep}users{sep}"): + filename = fsdecode(f"{configGet('data', 'locations')}{sep}users{sep}{user_file}") + if filename.endswith(".json"): + user = jsonLoad(filename) + if isinstance(user["application"]["2"], str): + try: + if ".".join([((user["application"]["2"]).split("."))[0], ((user["application"]["2"]).split("."))[1]]) == datetime.now().strftime("%d.%m"): + tg_user = app.get_users(int(user_file.replace(".json", ""))) + app.send_message( configGet("admin_group"), locale("birthday", "message").format(str(tg_user.first_name), str(tg_user.username), str(relativedelta(datetime.now(), datetime.strptime(user["application"]["2"], '%d.%m.%Y')).years)) ) + except AttributeError: + continue \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 22fb75d..2eb1b66 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ pyrogram>=2.0.59 tgcrypto>=1.2.4 ujson>=5.5.0 -psutil>=5.9.2 \ No newline at end of file +psutil>=5.9.2 +schedule \ No newline at end of file