From ca28f34ce19cec6f5f4a3d00eca77f8192e60abc Mon Sep 17 00:00:00 2001 From: justuser31 Date: Wed, 26 Apr 2023 21:03:05 +0300 Subject: [PATCH] =?UTF-8?q?Tea=20init=20=E2=98=95=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tea.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tea.py diff --git a/tea.py b/tea.py new file mode 100644 index 0000000..3bd4715 --- /dev/null +++ b/tea.py @@ -0,0 +1,70 @@ +import os +import json +import time +from datetime import datetime, timedelta +import telebot + +global USERS_BALANCE +TEA_PRICE = 0.2 # цена чашки чая +USERS_BALANCE = {} # баланс пользователей (user_id: [balance, last_time_teapot]) + +BASE_DIR = os.path.dirname(os.path.abspath(__file__)) +BASE_NAME = 'data.json' + +DB_PATH = os.path.join(BASE_DIR, BASE_NAME) + +# если базы данных нет, то создаем новую +if not os.path.exists(DB_PATH): + with open(DB_PATH, 'w') as f: + json.dump({}, f) + +# загружаем базу данных +with open(DB_PATH, 'r') as f: + USERS_BALANCE = json.load(f) + +#Загружаем токен из базы +bot = telebot.TeleBot(USERS_BALANCE['token']) + +@bot.message_handler(commands=['start']) +def start_message(message): + bot.reply_to(message, 'Привет, я бот для чаепитий! ' + 'Чтобы выпить чашку чая напиши /tea. ' + 'Для проверки баланса напиши /bal.') + +@bot.message_handler(commands=['tea']) +def tea_message(message): + global USERS_BALANCE + user_id = str(message.from_user.id) + user_name = message.from_user.username + + # проверяем, можно ли выпить чай + if user_id in USERS_BALANCE: + last_teapot_time = datetime.strptime(USERS_BALANCE[user_id][1], '%Y-%m-%d %H:%M:%S') + print(last_teapot_time) + time_delta = datetime.now() - last_teapot_time + if time_delta.seconds < 1800: + bot.reply_to(message, f'Ты уже пил чай {time_delta.seconds//30} минут назад. \n' + f'Чай будет готов через {(30 - time_delta.seconds//30)} минут. ☕️') + return + + # обновляем баланс пользователя + balance = USERS_BALANCE[str(user_id)] + balance[0] += TEA_PRICE + balance[1] = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + USERS_BALANCE[user_id] = balance + + with open(DB_PATH, 'w') as f: + json.dump(USERS_BALANCE, f) # записываем изменения в базу данных + + bot.reply_to(message, f'Ты выпил чашку чая. ☕️') + +@bot.message_handler(commands=['bal']) +def balance_message(message): + user_id = message.from_user.id + + # проверяем баланс пользователя + user_balance = USERS_BALANCE.get(user_id, [0, ''])[0] + + bot.reply_to(message, f'Твой баланс: {user_balance} литров. ☕️') + +bot.polling(none_stop=True)