-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathhandlers.py
122 lines (93 loc) · 3.78 KB
/
handlers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import random
from aiogram import types
from asyncpg import Connection, Record
from asyncpg.exceptions import UniqueViolationError
from load_all import bot, dp, db
class DBCommands:
pool: Connection = db
ADD_NEW_USER_REFERRAL = "INSERT INTO users(chat_id, username, full_name, referral) " \
"VALUES ($1, $2, $3, $4) RETURNING id"
ADD_NEW_USER = "INSERT INTO users(chat_id, username, full_name) VALUES ($1, $2, $3) RETURNING id"
COUNT_USERS = "SELECT COUNT(*) FROM users"
GET_ID = "SELECT id FROM users WHERE chat_id = $1"
CHECK_REFERRALS = "SELECT chat_id FROM users WHERE referral=" \
"(SELECT id FROM users WHERE chat_id=$1)"
CHECK_BALANCE = "SELECT balance FROM users WHERE chat_id = $1"
ADD_MONEY = "UPDATE users SET balance=balance+$1 WHERE chat_id = $2"
async def add_new_user(self, referral=None):
user = types.User.get_current()
chat_id = user.id
username = user.username
full_name = user.full_name
args = chat_id, username, full_name
if referral:
args += (int(referral),)
command = self.ADD_NEW_USER_REFERRAL
else:
command = self.ADD_NEW_USER
try:
record_id = await self.pool.fetchval(command, *args)
return record_id
except UniqueViolationError:
pass
async def count_users(self):
record: Record = await self.pool.fetchval(self.COUNT_USERS)
return record
async def get_id(self):
command = self.GET_ID
user_id = types.User.get_current().id
return await self.pool.fetchval(command, user_id)
async def check_referrals(self):
user_id = types.User.get_current().id
command = self.CHECK_REFERRALS
rows = await self.pool.fetch(command, user_id)
return ", ".join([
f"{num + 1}. " + (await bot.get_chat(user["chat_id"])).get_mention(as_html=True)
for num, user in enumerate(rows)
])
async def check_balance(self):
command = self.CHECK_BALANCE
user_id = types.User.get_current().id
return await self.pool.fetchval(command, user_id)
async def add_money(self, money):
command = self.ADD_MONEY
user_id = types.User.get_current().id
return await self.pool.fetchval(command, money, user_id)
db = DBCommands()
@dp.message_handler(commands=["start"])
async def register_user(message: types.Message):
chat_id = message.from_user.id
referral = message.get_args()
id = await db.add_new_user(referral=referral)
count_users = await db.count_users()
text = ""
if not id:
id = await db.get_id()
else:
text += "Записал в базу! "
bot_username = (await bot.me).username
bot_link = f"https://t.me/{bot_username}?start={id}"
balance = await db.check_balance()
text += f"""
Сейчас в базе {count_users} человек!
Ваша реферальная ссылка: {bot_link}
Проверить рефералов можно по команде: /referrals
Ваш баланс: {balance} монет.
Добавить монет: /add_money
"""
await bot.send_message(chat_id, text)
@dp.message_handler(commands=["referrals"])
async def check_referrals(message: types.Message):
referrals = await db.check_referrals()
text = f"Ваши рефералы:\n{referrals}"
await message.answer(text)
@dp.message_handler(commands=["add_money"])
async def add_money(message: types.Message):
random_amount = random.randint(1, 100)
await db.add_money(random_amount)
balance = await db.check_balance()
text = f"""
Вам было добавлено {random_amount} монет.
Теперь ваш баланс: {balance}
"""
await message.answer(text)