-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
92 lines (61 loc) · 2.16 KB
/
main.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
"""
Main running module for bot.
Created on 03.03.2021
@author: Ruslan Dolovanyuk
"""
import asyncio
import pickle
import random
from aiogram import executor
from aiogram import types
from bot import bot, dp
import loader
class Compliments:
data = None
class Languages:
supported = None
current = None
@dp.message_handler(commands=['start'])
async def cmd_start(message: types.Message):
name = ''
if message.from_user.username is not None:
name = message.from_user.username
menu_keyboard = await get_menu_keyboard()
await message.answer(local('phrases', 'start').format(name), reply_markup=menu_keyboard, parse_mode="HTML")
@dp.message_handler(commands=['about'])
async def cmd_about(message: types.Message):
await message.answer(local('about', 'author'), parse_mode="HTML")
@dp.message_handler(commands=['count'])
async def cmd_count(message: types.Message):
await message.answer(str(len(Compliments.data)), parse_mode="HTML")
@dp.message_handler(content_types=types.ContentTypes.ANY)
async def cmd_menu(message: types.Message):
cmd = message.text
await message.delete()
if local('btn', 'want') == cmd:
text = get_compliments()
await message.answer(text, parse_mode="HTML")
async def get_menu_keyboard():
menu_keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=False, selective=True)
menu_keyboard.add(types.KeyboardButton(local('btn', 'want')))
return menu_keyboard
def get_compliments():
return random.choice(Compliments.data)
def local(section, phrase):
return Languages.current[section].get(phrase, '')
def load_language():
with open('languages.dat', 'rb') as lang_file:
data = pickle.load(lang_file)
Languages.supported = data['languages']
Languages.current = data[list(Languages.supported.keys())[0]]
def run():
load_language()
if Compliments.data is None:
result = []
dp.loop.run_until_complete(loader.worker(result))
Compliments.data = result
random.seed()
random.shuffle(Compliments.data)
executor.start_polling(dp, skip_updates=True)
if '__main__' == __name__:
run()