-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathbot.py
More file actions
64 lines (50 loc) · 1.8 KB
/
bot.py
File metadata and controls
64 lines (50 loc) · 1.8 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
import asyncio
import pillow_heif
from pyrogram import Client
from pyrogram.handlers import ConnectHandler, DisconnectHandler
from pyrogram.types import BotCommand
from core import bs, on_connect, on_disconnect, ws
from log import logger, setup_logging
from services import parse_cache, persistent_cache
from utils.event_loop import setup_optimized_event_loop
pillow_heif.register_heif_opener()
setup_logging(debug=bs.debug)
setup_optimized_event_loop()
loop = asyncio.new_event_loop()
class Bot(Client):
def __init__(self):
self.cfg = bs
super().__init__(
f"{self.cfg.bot_token.split(':')[0]}_bot",
api_id=self.cfg.api_id,
api_hash=self.cfg.api_hash,
bot_token=self.cfg.bot_token,
plugins={"root": "plugins"},
proxy=self.cfg.bot_proxy,
loop=loop,
workdir=self.cfg.sessions_path,
)
async def start(self, *args, **kwargs):
self.init_watchdog()
parse_cache.start_cleanup()
persistent_cache.start_cleanup()
await super().start()
await self.set_menu()
async def stop(self, *args, **kwargs):
ws.exit_flag = True
await super().stop()
def init_watchdog(self):
self.add_handler(ConnectHandler(on_connect))
self.add_handler(DisconnectHandler(on_disconnect))
async def set_menu(self):
commands = {
"start": "开始",
"jx": "解析",
"raw": "不处理媒体, 发送原始文件",
"zip": "不处理媒体, 保存解析结果, 发送压缩包",
}
await self.set_bot_commands([BotCommand(command=k, description=v) for k, v in commands.items()])
logger.debug(f"菜单已设置: {commands}")
if __name__ == "__main__":
bot = Bot()
bot.run()