-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
84 lines (66 loc) · 2.48 KB
/
main.py
File metadata and controls
84 lines (66 loc) · 2.48 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
import asyncio
import logging
import sys
from os import environ
from aiogram import Bot, Dispatcher
from aiogram.client.default import DefaultBotProperties
from aiogram.types import BotCommand
from dotenv import load_dotenv
from pydantic import ValidationError
from db.database import Database
from handlers import callbacks, commands, errors, messages
from models.servers import ServerModel
from modules.middlewares import LoggingMiddleware, AuthCheckMiddleware, ServerCreateMiddleware
from modules.storages import SQLiteStorage
from servers.servers_file_loader import load_servers_from_file
load_dotenv()
async def main():
admins = [int(admin_id) for admin_id in environ['ADMIN_ID'].split(',')]
try:
servers = load_servers_from_file()
except FileNotFoundError:
logging.info('Servers file not found, local WireGuard server will be used')
servers = [ServerModel(name='WireGuard')]
except ValidationError as e:
logging.critical(f'Validation error while loading servers: {e.errors()}')
sys.exit(1)
except ValueError as e:
logging.critical(f'Servers file contains invalid data: {e}')
sys.exit(1)
default = DefaultBotProperties(parse_mode='HTML')
bot = Bot(token=environ['TOKEN'], default=default)
dp = Dispatcher(
storage=SQLiteStorage(),
admins=admins,
servers=servers,
)
dp.update.middleware(LoggingMiddleware())
dp.update.middleware(AuthCheckMiddleware())
dp.update.middleware(ServerCreateMiddleware())
dp.include_routers(
commands.router,
callbacks.router,
messages.router,
errors.router,
)
await bot.set_my_commands([
BotCommand(command='start', description='start'),
BotCommand(command='servers', description='server list'),
BotCommand(command='settings', description='bot settings'),
])
await bot.delete_webhook(drop_pending_updates=True)
await dp.start_polling(bot)
if __name__ == '__main__':
database = Database()
database.init_db()
log_level_str = database.get_log_level()
log_level = logging.getLevelName(log_level_str)
logging.basicConfig(
level=log_level,
stream=sys.stdout,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
)
# Set the logging level of "aiogram.event" to "WARNING"
# because there is too much spam coming in with the INFO level
logging.getLogger('aiogram.event').setLevel(logging.WARNING)
asyncio.run(main())