none = telebot.types.ReplyKeyboardRemove()
course, price, mon = 0,0,0
# команда /balance
@bot.message_handler(commands=['balance'])
def balance(message):
connect = sqlite3.connect("db.db")
cursor = connect.cursor()
money_query = cursor.execute(f"SELECT `balance` FROM `users` WHERE `id` = {message.chat.id}")
balance = cursor.fetchone()[0]
balance_buttons = types.InlineKeyboardMarkup()
poplnenie_button = types.InlineKeyboardButton("Пополнить счет", callback_data='popolnenie')
balance_buttons.add(poplnenie_button)
bot.send_message(message.chat.id, f"Твой баланс: *{balance}* монет",
reply_markup=balance_buttons)
# Обработчик callback
@bot.callback_query_handler(func=lambda c:True)
def callback(c):
if c.data == 'popolnenie': popolnenie(c.message)
if c.data == 'cancel_to_balance':
from time import sleep
msg = bot.send_message(c.message.chat.id, "Отмена...")
sleep(1)
bot.delete_message(c.message.chat.id, msg.id)
sleep(0.1)
balance(c.message)
if c.data == 'buy_money': do_buy_money(c.message)
def popolnenie(message):
global course
connect = sqlite3.connect("db.db")
cursor = connect.cursor()
score_query = cursor.execute(f"SELECT `score` FROM `users` WHERE `id` = {message.chat.id}")
score = cursor.fetchone()[0]
money_query = cursor.execute(f"SELECT `balance` FROM `users` WHERE `id` = {message.chat.id}")
balance = cursor.fetchone()[0]
course_query = cursor.execute(f"SELECT `course` FROM `users` WHERE `id` = {message.chat.id}")
course = cursor.fetchone()[0]
msg = bot.send_message(message.chat.id, f"<b><i>Напишите сколько монет\nВы хотите купить</i></b>\n\nКурс: <b>{course} очков - 1 монета</b>\nУ вас <b>{score}</b>очков и <b>{balance}</b> монет", parse_mode="html")
bot.register_next_step_handler(msg, buy_money)
def buy_money(message):
try: int(message.text)
except: bot.send_message(message.chat.id, "Введите целое число!", reply_markup=none); popolnenie(message)
else:
global course, price, mon
buy_buttons = types.InlineKeyboardMarkup()
buy_button = types.InlineKeyboardButton("Купить монеты", callback_data='buy_money')
cancel_button = types.InlineKeyboardButton("Отмена", callback_data='cancel_to_balance')
buy_buttons.add(buy_button,cancel_button)
connect = sqlite3.connect("db.db")
cursor = connect.cursor()
course_query = cursor.execute(f"SELECT `course` FROM `users` WHERE `id` = {message.chat.id}")
course = cursor.fetchone()[0]
price = int(course) * int(message.text)
mon = message.text
bot.send_message(message.chat.id, f"Покупка *{message.text}* монет.\nЦена: *{price}* очков\nВы хотите купить?", reply_markup=buy_buttons)
def do_buy_money(message):
global course, price, mon
bot.send_message(message.chat.id, f"{mon}, {price}, {course}", reply_markup=none)