-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenginebot_telegram.py
More file actions
586 lines (544 loc) · 20.7 KB
/
enginebot_telegram.py
File metadata and controls
586 lines (544 loc) · 20.7 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
import logging
from aiogram import (
Bot,
Dispatcher,
types
)
from aiogram.utils.executor import Executor
from fastapi import (
FastAPI,
Request
)
import asyncio
import uvicorn
from config import *
import api
import utils
logging.basicConfig(level=logging.INFO)
bot = Bot(token=BOT_TOKEN)
dispatcher = Dispatcher(bot)
def parse_args(message):
return message.get_args().replace(' ', ' ').split(' ')
@dispatcher.message_handler(commands=['register'])
async def register_handler(message: types.Message):
args = parse_args(message)
try:
await message.delete()
except:
pass
if len(args) != 2:
await bot.send_message(
chat_id=message.chat.id,
text=f'❌ @{message.from_user.username} Invalid arguments.\n'
' Usage: `/register <username> <password>`',
parse_mode='Markdown'
)
return
else:
username, password = args
if len(username) < 3 or len(username) > 25:
await bot.send_message(
chat_id=message.chat.id,
text=f'❌ @{message.from_user.username}\n'
f'Command usage incorrect: Username must be between 3 and 25 characters long.'
)
return
elif len(password) < 7 or len(password) > 30:
await bot.send_message(
chat_id=message.chat.id,
text=f'❌ @{message.from_user.username}\n'
f'Command usage incorrect: Password must be between 7 and 30 characters long.'
)
return
elif not username.isalnum():
await bot.send_message(
chat_id=message.chat.id,
text=f'❌ @{message.from_user.username}\n'
f'Command usage incorrect: Username must contain only letters and numbers.'
)
return
elif not password.isalnum():
await bot.send_message(
chat_id=message.chat.id,
text=f'❌ @{message.from_user.username}\n'
f'Command usage incorrect: Password must contain only letters and numbers.'
)
return
else:
# Perform register
try:
response_json = await api.user_register(
username=username,
im_id=message.from_user.id,
password_hash=utils.calculate_password_hash(password)
)
if 'success' in response_json:
await bot.send_message(
chat_id=message.chat.id,
text=f'✅ **{username}** (@{message.from_user.username}) '
f'was successfully registered, now you can start playing',
parse_mode='Markdown'
)
else:
if response_json['error_type'] == '035':
await bot.send_message(
chat_id=message.chat.id,
text=f'❌ @{message.from_user.username}, you are already registered.\n'
f'Your username is: **{response_json["username"]}**',
parse_mode='Markdown'
)
elif response_json['error_type'] == '036':
await bot.send_message(
chat_id=message.chat.id,
text=f'❌ @{message.from_user.username}, this username is already taken.',
)
else:
await bot.send_message(
chat_id=message.chat.id,
text=f"❌ @{message.from_user.username}\n"
f"Unknown error occurred.\n"
f"{response_json['error_type']} - {response_json['message']}"
)
except Exception as e:
await bot.send_message(
chat_id=message.chat.id,
text=f'❌ @{message.from_user.username}\n'
f'Error occurred while registering: {e}'
)
@dispatcher.message_handler(commands=['change_password'])
async def change_password_handler(message: types.Message):
args = parse_args(message)
try:
await message.delete()
except:
pass
if len(args) != 1:
await bot.send_message(
chat_id=message.chat.id,
text=f'❌ @{message.from_user.username} Invalid arguments.\n'
' Usage: `/change_password <password>`',
parse_mode='Markdown'
)
return
else:
password: str = args[0]
if len(password) < 7 or len(password) > 30:
await bot.send_message(
chat_id=message.chat.id,
text=f'❌ @{message.from_user.username}\n'
f'Command usage incorrect: Password must be between 7 and 30 characters long.'
)
return
elif not password.isalnum():
await bot.send_message(
chat_id=message.chat.id,
text=f'❌ @{message.from_user.username}\n'
f'Command usage incorrect: Password must contain only letters and numbers.'
)
return
else:
try:
response_json = await api.update_password(
user_identifier=str(message.from_user.id),
im_id=message.from_user.id,
password_hash=utils.calculate_password_hash(password)
)
if 'success' in response_json:
await bot.send_message(
chat_id=message.chat.id,
text=f'✅ @{message.from_user.username}\n'
f'Password changed successfully.'
)
else:
if response_json['error_type'] == '006':
await bot.send_message(
chat_id=message.chat.id,
text=f'❌ @{message.from_user.username},\n'
f'You have not registered yet. Use `/register` command to register.',
parse_mode='Markdown'
)
else:
await bot.send_message(
chat_id=message.chat.id,
text=f"❌ @{message.from_user.username}\n"
f"Unknown error occurred.\n"
f"{response_json['error_type']} - {response_json['message']}"
)
except Exception as e:
await bot.send_message(
chat_id=message.chat.id,
text=f'❌ @{message.from_user.username}\n'
f'Error occurred while changing password: {e}'
)
@dispatcher.message_handler(commands=['levels'])
async def levels_handler(message: types.Message):
reply_message = await message.reply(
'⏰ Loading ...'
)
user_identifier: str = message.get_args()
user_identifier: str = str(message.from_user.id) if user_identifier == '' else user_identifier
response_texts: list[str] = []
user_info_response = await api.user_info(
user_identifier=user_identifier
)
if 'result' not in user_info_response:
await message.reply(
f'❌ {"This user" if user_identifier != str(message.from_user.id) else "You"} are not registered yet.'
)
return
else:
user_info = user_info_response['result']
response_texts.append(
f"**{user_info['username']}** (@{message.from_user.username})"
+ f'{f"**👮 Stage Moderator**" if user_info["is_mod"] else ""}'
+ f'{f"**🚀 Booster**" if user_info["is_booster"] else ""}'
)
response_texts.append(f"📤 Uploads: {user_info['uploads']}")
response_texts.append('⏰ Loading...')
await bot.edit_message_text(
chat_id=message.chat.id,
message_id=reply_message.message_id,
text='\n'.join(response_texts),
parse_mode='Markdown'
)
response_texts.pop()
username = user_info['username']
auth_code = await api.login_session(API_TOKEN)
levels = await api.get_user_levels(
username=username,
auth_code=auth_code,
rows_perpage=user_info['uploads'] + 1
)
for level in levels:
response_texts.append(
f"- {level['name']} "
f"{'✨ ' if level['featured'] == 1 else ''}"
f"`{level['id']}` | "
f"❤️ {level['likes']} "
f"💙 {level['dislikes']}"
)
await bot.edit_message_text(
chat_id=message.chat.id,
message_id=reply_message.message_id,
text='\n'.join(response_texts),
parse_mode='Markdown'
)
return
@dispatcher.message_handler(commands=['server_stats'])
async def server_stats_handler(message: types.Message):
server_stats = await api.server_stats()
reply_message = await message.reply(
'⏰ Loading ...'
)
await bot.edit_message_text(
chat_id=message.chat.id,
message_id=reply_message.message_id,
text=(
f'🗄️ **Server Statistics**\n'
f'🐧 OS: {server_stats.os}\n'
f'🐍 Python version: {server_stats.python}\n'
f'👥 Player count: {server_stats.player_count}\n'
f'🌏 Level count: {server_stats.level_count}\n'
f'🕰️ Uptime: {int(server_stats.uptime / 60)} minutes\n'
f'📊 Connection per minute: {server_stats.connection_per_minute}'
).replace('_', '\\_'),
parse_mode='Markdown'
)
return
def level_details_to_string(level: dict, leading_char: str) -> str:
clears: int = level['victorias']
attempts: int = level['intentos']
clear_rate: str = str(round(clears / attempts * 100, 2)) + '%'
return (
f"{leading_char} **{level['name']}**{' ✨' if level['featured'] == 1 else ''}\n"
f"👤 Author **{level['author']}**\n"
f"ID: `{level['id']}`\n"
f"🏷️ {level['etiquetas']}\n"
f"❤️ {level['likes']} | 💙 {level['dislikes']}\n"
f"⛳ {clears} / 🎮 {attempts} ({clear_rate})\n"
)
@dispatcher.message_handler(commands=['query_level'])
async def query_level_handler(message: types.Message):
def prettify_level_id(raw_level_id: str):
return raw_level_id[0:4] + '-' + raw_level_id[4:8] + '-' + raw_level_id[8:12] + '-' + raw_level_id[12:16]
level_id = message.get_args()
if level_id == '':
await message.reply(
'❌ Please provide a level ID.'
)
return
elif '-' not in level_id:
level_id = prettify_level_id(level_id)
else:
level_id = level_id.upper()
try:
auth_code = await api.login_session(token=API_TOKEN)
response_json = await api.query_level(
level_id=level_id,
auth_code=auth_code
)
if 'error_type' in response_json:
await message.reply(
f'❌ Level not found.'
)
else:
level_data: dict = response_json['result']
await message.reply(
level_details_to_string(level_data, '🔍')
)
except Exception as e:
await message.reply(
f'❌ Error: {e}'
)
@dispatcher.message_handler(commands=['random_level'])
async def random_level_handler(message: types.Message):
difficulty = message.get_args()
if difficulty == '':
difficulty = None
else:
difficulty_ids: dict[str, int] = {
# SMM1 风格的难度名
'easy': 0, 'normal': 1, 'expert': 2, 'super expert': 3,
# SMM2 风格的难度名
'hard': 2, 'very hard': 3,
# TGRCode API 风格的难度 ID
'e': 0, 'n': 1, 'ex': 2, 'sex': 3,
# SMMWE API 风格的难度 ID
'0': 0, '1': 1, '2': 2, '3': 3
}
if difficulty.lower() not in difficulty_ids:
await message.reply(
'❌ Invalid difficulty.\n'
'Valid difficulties: `easy`, `normal`, `expert`, `super expert`',
parse_mode='Markdown'
)
return
difficulty = difficulty_ids[difficulty.lower()]
try:
auth_code = await api.login_session(
token=API_TOKEN
)
response_json = await api.random_level(
auth_code=auth_code,
difficulty=difficulty
)
level_data: dict = response_json['result']
await message.reply(
level_details_to_string(level_data, '🎲')
)
except Exception as e:
await message.reply(
f'❌ Error: {e}'
)
@dispatcher.message_handler(commands=['permission'])
async def permission_handler(message: types.Message):
if (await bot.get_chat_member(
chat_id=message.chat.id,
user_id=message.from_user.id
)).status != 'creator':
await message.reply(
'❌ Permission denied.'
)
return
args = parse_args(message)
if len(args) != 3:
await message.reply(
'❌ Invalid arguments.\n'
'Usage: `/permission <Username\\|ID> <Permission> <true\\|false>`',
parse_mode='Markdown'
)
return
user_identifier: str = args[0]
permission: str = args[1]
value: str = args[2]
if value.lower() not in ['true', 'false']:
await message.reply(
'❌ Invalid value.\n'
'Usage: `/permission <Username\\|ID> <Permission> <true\\|false>`',
parse_mode='Markdown'
)
return
value: bool = value.lower() == 'true'
if permission not in ['mod', 'admin', 'booster', 'valid', 'banned']:
await message.reply(
'❌ Invalid permission.\n'
'Valid permissions: `mod`, `admin`, `booster`, `valid`, `banned`',
parse_mode='Markdown'
)
return
try:
response_json = await api.update_permission(
user_identifier=user_identifier,
permission=permission,
value=value
)
if 'success' in response_json:
await message.reply(
f'✅ Successfully updated permission for `{response_json["username"]}`.',
parse_mode='Markdown'
)
else:
await message.reply(
f'❌ Failed to update permission.\n'
f'{response_json["error_type"]} - {response_json["message"]}'
)
except Exception as e:
await message.reply(
f'❌ Error: {e}'
)
@dispatcher.message_handler(commands=['ban'])
async def ban_handler(message: types.Message):
if (await bot.get_chat_member(
chat_id=message.chat.id,
user_id=message.from_user.id
)).status not in ['creator', 'administrator']:
await message.reply(
'❌ Permission denied.'
)
return
user_identifier: str = message.get_args()
if user_identifier == '':
await message.reply(
'❌ Invalid arguments.\n'
'Usage: `/ban <Username\\|ID>`',
parse_mode='Markdown'
)
return
try:
response_json = await api.update_permission(
user_identifier=user_identifier,
permission='banned',
value=True
)
if 'success' in response_json:
await message.reply(
f'✅ Successfully banned `{response_json["username"]}`.',
parse_mode='Markdown'
)
else:
await message.reply(
f'❌ Failed to ban.\n'
f'{response_json["error_type"]} - {response_json["message"]}'
)
except Exception as e:
await message.reply(
f'❌ Error: {e}'
)
@dispatcher.message_handler(commands=['unban'])
async def unban_handler(message: types.Message):
if (await bot.get_chat_member(
chat_id=message.chat.id,
user_id=message.from_user.id
)).status not in ['creator', 'administrator']:
await message.reply(
'❌ Permission denied.'
)
return
user_identifier: str = message.get_args()
if user_identifier == '':
await message.reply(
'❌ Invalid arguments.\n'
'Usage: `/unban <Username\\|ID>`',
parse_mode='Markdown'
)
return
try:
response_json = await api.update_permission(
user_identifier=user_identifier,
permission='banned',
value=False
)
if 'success' in response_json:
await message.reply(
f'✅ Successfully unbanned `{response_json["username"]}`.',
parse_mode='Markdown'
)
else:
await message.reply(
f'❌ Failed to unban.\n'
f'{response_json["error_type"]} - {response_json["message"]}'
)
except Exception as e:
await message.reply(
f'❌ Error: {e}'
)
'''
@dispatcher.message_handler(commands=['get_id'])
async def get_id(message: types.Message):
await message.reply(
message.chat.id
)
'''
app = FastAPI()
@app.on_event('startup')
async def startup_event():
await dispatcher.skip_updates()
asyncio.create_task(
dispatcher.start_polling()
)
@app.on_event('shutdown')
async def shutdown_event():
await dispatcher.stop_polling()
@app.post('/enginetribe')
async def enginetribe_payload(request: Request):
webhook: dict = await request.json()
message: str = ''
match webhook["type"]:
case 'new_arrival': # new arrival
message = f'📤 {webhook["author"]} uploaded a new level: {webhook["level_name"]}\n' \
f'ID: {webhook["level_id"]}'
case 'new_featured': # new featured
message = f'🌟 {webhook["level_name"]} of {webhook["author"]} ' \
f'has been added to the featured levels, come and play!\n' \
f'ID: {webhook["level_id"]}'
case 'permission_change':
permission_name = {'booster': 'booster', 'mod': 'stage moderator'}[webhook['permission']]
message = f"{'🤗' if webhook['value'] else '😥'} " \
f"{webhook['username']} {'gained' if webhook['value'] else 'lost'} " \
f"{permission_name} role of Engine Tribe!"
case _:
if 'likes' in webhook["type"]: # 10/100/1000 likes
message = f'🎉 Congratulations, {webhook["author"]}\'s level {webhook["level_name"]} has gained ' \
f'{webhook["type"].replace("_likes", "")} likes!\n' \
f'ID: {webhook["level_id"]}'
if 'plays' in webhook["type"]: # 100/1000 plays
message = f'🎉 Congratulations, {webhook["author"]}\'s level {webhook["level_name"]} has been played ' \
f'{webhook["type"].replace("_plays", "")} times!\n' \
f'ID: {webhook["level_id"]}'
if 'deaths' in webhook["type"]: # 100/1000 deaths
message = f'🔪 {webhook["author"]}\'s level {webhook["level_name"]} has obtained ' \
f'{webhook["type"].replace("_deaths", "")} deaths, come and challenge this level!\n' \
f'ID: {webhook["level_id"]}'
if 'clears' in webhook["type"]: # 100/1000 clears
message = f'🎉 Congratulations, {webhook["author"]}\'s level {webhook["level_name"]} has been cleared ' \
f'{webhook["type"].replace("_clears", "")} times, come and play!\n' \
f'ID: {webhook["level_id"]}'
if message != '':
for chat_id in BOT_ENABLED_CHATS:
await bot.send_message(
chat_id=chat_id,
text=message
)
return 'Success'
else:
import json
for chat_id in BOT_ENABLED_CHATS:
await bot.send_message(
chat_id=chat_id,
text=f'{json.dumps(webhook, ensure_ascii=False)}'
)
return 'NotImplemented'
def run():
loop = asyncio.new_event_loop()
webhook_server = uvicorn.Server(
config=uvicorn.Config(
app,
host=WEBHOOK_HOST,
port=WEBHOOK_PORT,
loop="asyncio",
workers=1
)
)
loop.run_until_complete(webhook_server.serve())
if __name__ == '__main__':
run()