-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_functions.py
229 lines (166 loc) · 7.48 KB
/
block_functions.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import nextcord
import bd
import datetime
import time
# >>> registration
class Registration(nextcord.ui.Modal):
def __init__(self):
super().__init__(
"Регистрация",
timeout=2 * 60,
)
self.name = nextcord.ui.TextInput(
label="Ник, для регистрации в боте",
min_length=2,
max_length=50,
required=True,
)
self.add_item(self.name)
async def callback(self, interaction: nextcord.Interaction) -> None:
author = interaction.user
if author.bot:
await interaction.send('Ты бот, я не могу тебя зарегать')
raise Exception('Bot asked for registration')
connection = bd.get_connection()
cursor = connection.cursor(dictionary=True, buffered=True)
user = cursor.execute('SELECT * FROM `users` WHERE discord_id = %s', (str(author.id),))
if check_for_user_registration(str(author.id)) is False:
now = datetime.datetime.utcnow()
sql_to_create_user = f"INSERT INTO `users` (`discord_id`, `coins`, `opened_cases_count`, `is_admin`, `discord_name`) VALUES (%s, %s, %s, %s, %s)"
cursor.execute(sql_to_create_user, (str(author.id), 0, 0, 0, self.name.value))
connection.commit()
cursor.close()
else:
await interaction.send('Я тебя помню, ты уже зареган', ephemeral=True)
raise Exception('Юзер уже зареган')
response = f'Я запомнил тебя под именем - {self.name.value}. Приятного пользования <a:grinning:856976842158112788>'
await interaction.send(response, ephemeral=True)
# <<< registration
# <<< help
class HelpButtonView(nextcord.ui.View):
def __init__(self):
super().__init__()
self.value = None
@nextcord.ui.button(label="Зарегистрироваться", style=nextcord.ButtonStyle.green)
async def registration_button(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
modal = Registration()
await interaction.response.send_modal(modal)
# >>> help
# >>> balance
async def balance(ctx):
if check_for_user_registration(str(ctx.user.id)) is False:
return await ctx.send('Ты не зареган - /help')
connection = bd.get_connection()
cursor = connection.cursor()
sql_to_check_user_balance = "SELECT coins FROM users WHERE discord_id = {}".format(ctx.user.id)
cursor.execute(sql_to_check_user_balance)
coins = cursor.fetchone()
embed = nextcord.Embed(color=0x4ad420, title=f'Баланс {ctx.user.name}',
description='**' + str(coins[0]) + ' коинов**')
return await ctx.send(embed=embed)
# <<< balance
# >>> pay
async def pay(interaction, member, coins_to_pay):
current_coins_count = int(coins_to_pay)
sql_to_check_valid_coins = f"SELECT coins FROM `users` WHERE discord_id = {interaction.user.id}"
sql_to_add_coins = f"UPDATE `users` SET coins = coins + %s WHERE discord_id = %s"
sql_to_drop_coins = f"UPDATE `users` SET coins = coins - %s WHERE discord_id = %s"
connection = bd.get_connection()
cursor = connection.cursor(buffered=True)
cursor.execute(sql_to_check_valid_coins)
result = cursor.fetchone()
coins_result = result[0]
current_coins_result = int(coins_result)
connection.close()
if current_coins_result < current_coins_count + 10:
return await interaction.send('Недостаточно баланса для перевода (комиссия = 10 коинов)', ephemeral=True)
connection.connect()
cursor.execute(sql_to_add_coins, (current_coins_count, member.id))
cursor.execute(sql_to_drop_coins, (current_coins_count + 10, interaction.user.id))
connection.commit()
connection.close()
embed = nextcord.Embed(color=0x28fc64, title='Перевод коинов',
description='Перевод успешно выполнен. Коммисия за перевод - **10 коинов**')
return await interaction.send(embed=embed, ephemeral=True)
# <<< pay
# >>> update
class UpdateName(nextcord.ui.Modal):
def __init__(self):
super().__init__(
"Изменение имени",
timeout=5 * 60,
)
self.name = nextcord.ui.TextInput(
label="Твой новый ник",
min_length=2,
max_length=50,
)
self.add_item(self.name)
async def callback(self, interaction: nextcord.Interaction) -> None:
author = interaction.user
if check_for_user_registration(author.id) is False:
return await interaction.response.send_message('Ты не зареган')
connection = bd.get_connection()
cursor = connection.cursor(buffered=True, dictionary=True)
cursor.execute("UPDATE `users` SET discord_name = %s WHERE discord_id = %s",
(self.name.value, str(interaction.user.id)))
connection.commit()
cursor.close()
connection.close()
await interaction.send(f"Ваше имя изменено на {self.name.value}", ephemeral=True)
# <<< update
# Block functions
def check_for_user_registration(user_id):
user_id = str(user_id)
connection = bd.get_connection()
cursor = connection.cursor(buffered=True, dictionary=True)
sql_to_check_user_registration = f'SELECT * FROM `users` WHERE discord_id = {user_id}'
cursor.execute(sql_to_check_user_registration)
result = cursor.fetchone()
connection.close()
if result is None:
return False
else:
return True
def check_for_admin(user_id: str):
connection = bd.get_connection()
cursor = connection.cursor(buffered=True, dictionary=True)
sql_to_check_for_admin = f"SELECT is_admin FROM `users` WHERE discord_id = {user_id}"
cursor.execute(sql_to_check_for_admin)
result = cursor.fetchone()
connection.close()
if result is None:
return 0
return int(result['is_admin'])
def check_if_user_has_money(user_id: str, bal: int):
connection = bd.get_connection()
cursor = connection.cursor(buffered=True, dictionary=True)
sql_to_check_registered = "SELECT * FROM `users` WHERE discord_id = {}".format(int(user_id))
cursor.execute(sql_to_check_registered)
user = cursor.fetchone()
if user is None:
return False
if user['coins'] >= bal:
return True
else:
return False
def add_cases_count(discord_id):
discord_id = int(discord_id)
connection = bd.get_connection()
cursor = connection.cursor(buffered=True)
sql_request = f"UPDATE `users` SET opened_cases_count = opened_cases_count + 1 WHERE discord_id = {discord_id}"
cursor.execute(sql_request)
connection.commit()
connection.close()
def opened_cases_count(discord_id):
discord_id = int(discord_id)
connection = bd.get_connection()
cursor = connection.cursor(buffered=True, dictionary=True)
sql_request = f"SELECT opened_cases_count FROM `users` WHERE discord_id = {discord_id}"
cursor.execute(sql_request)
cases_count = cursor.fetchone()
return str(cases_count['opened_cases_count'])
async def validate_registration(interaction: nextcord.Interaction):
if check_for_user_registration(interaction.user.id) is False:
await interaction.send('Необходима регистрация - /help')
raise Exception()