-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
86 lines (69 loc) · 2.52 KB
/
bot.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
import importlib
from datetime import datetime
from pathlib import Path
import nonebot
from nonebot import logger
from nonebot.log import default_format
from essentials.libraries.model import PluginListMode
# 初始化
nonebot.init(alconna_use_command_start=True)
driver = nonebot.get_driver()
# 保存日志
from essentials.libraries import config, database
logger_path = Path(config.global_config.user_data_path) / "log"
if not logger_path.exists():
logger_path.mkdir(parents=True)
logger.add(
logger_path / (datetime.now().strftime("%Y-%m-%dT%H%M%S") + ".log"),
level="WARNING",
format=default_format,
rotation="1 week",
)
# 注册适配器
for adapter_name in config.global_config.enabled_adapters:
adapter_fullname = f"nonebot.adapters.{adapter_name}"
try:
adapter_module = importlib.import_module(adapter_fullname)
driver.register_adapter(adapter_module.Adapter)
except ModuleNotFoundError as e:
logger.error(f"Adapter {adapter_fullname} not found")
logger.exception(e)
except AttributeError as e:
logger.error(f"Cannot find Adapter in {adapter_fullname}")
logger.exception(e)
except Exception as e:
logger.error(
f"Unknown error occurred while registering adapter {adapter_fullname}"
)
logger.exception(e)
# 内置插件
nonebot.load_builtin_plugins("echo", "single_session")
# 前置插件
nonebot.load_plugin("nonebot_plugin_apscheduler")
nonebot.load_plugin("nonebot_plugin_alconna")
# 基础插件
essentials_plugins_path = (Path(__file__).parent / "essentials" / "plugins").resolve()
nonebot.load_plugins(str(essentials_plugins_path))
database.create_all_tables()
# 按需加载普通插件
plugins_path = Path(__file__).parent / "plugins"
for plugin_path in plugins_path.iterdir():
plugin_name = plugin_path.stem
# 跳过隐藏文件
if plugin_name.startswith("_"):
continue
# 跳过黑名单插件
if config.global_config.plugin_list_mode == PluginListMode.Blacklist:
if plugin_name in config.global_config.plugin_list:
logger.info(f"Skip loading plugin {plugin_name}")
continue
# 跳过非白名单插件
if config.global_config.plugin_list_mode == PluginListMode.Whitelist:
if plugin_name not in config.global_config.plugin_list:
logger.info(f"Skip loading plugin {plugin_name}")
continue
# 加载插件
nonebot.load_plugin(plugin_path)
database.create_all_tables()
if __name__ == "__main__":
nonebot.run()