forked from SAGIRI-kawaii/sagiri-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
101 lines (81 loc) · 3.34 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
93
94
95
96
97
98
99
100
101
# -*- coding: utf-8 -*-
import os
import yaml
import traceback
import threading
from loguru import logger
from graia.application import GraiaMiraiApplication
from graia.application.exceptions import AccountMuted
from graia.application.message.elements.internal import *
from graia.application.event.mirai import BotJoinGroupEvent
from graia.application.event.lifecycle import ApplicationLaunched
from graia.application.event.messages import Group, Member, GroupMessage
from WebManager.websocket import set_log
from SAGIRIBOT.Core.AppCore import AppCore
from SAGIRIBOT.utils import online_notice, get_config
from SAGIRIBOT.ORM.AsyncORM import orm, UserPermission, Setting
from SAGIRIBOT.frequency_limit_module import GlobalFrequencyLimitDict
with open('config.yaml', 'r', encoding='utf-8') as f:
configs = yaml.load(f.read())
logger.add(f"{os.getcwd()}/log/common.log", level="INFO", retention=f"{configs['commonRetention']} days", encoding="utf-8")
logger.add(f"{os.getcwd()}/log/error.log", level="ERROR", retention=f"{configs['errorRetention']} days", encoding="utf-8")
logger.add(set_log)
core = AppCore(configs)
app = core.get_app()
bcc = core.get_bcc()
saya = core.get_saya()
ignore = ["__init__", "__pycache__"]
with saya.module_context():
for module in os.listdir("SAGIRIBOT/Handler/Handlers"):
if module in ignore:
continue
try:
if os.path.isdir(module):
saya.require(f"SAGIRIBOT.Handler.Handlers.{module}")
else:
saya.require(f"SAGIRIBOT.Handler.Handlers.{module.split('.')[0]}")
except ModuleNotFoundError:
pass
core.load_saya_modules()
@bcc.receiver(GroupMessage)
async def group_message_handler(app: GraiaMiraiApplication, message: MessageChain, group: Group, member: Member):
message_text_log = message.asDisplay().replace("\n", "\\n")
logger.info(f"收到来自群 <{group.name}> 中成员 <{member.name}> 的消息:{message_text_log}")
@bcc.receiver(BotJoinGroupEvent)
async def bot_join_group(app: GraiaMiraiApplication, group: Group):
logger.info(f"机器人加入群组 <{group.name}>")
try:
await orm.insert_or_update(
Setting,
[Setting.group_id == group.id],
{"group_id": group.id, "group_name": group.name, "active": True}
)
await orm.insert_or_update(
UserPermission,
[UserPermission.member_id == core.get_config()["HostQQ"], UserPermission.group_id == group.id],
{"member_id": core.get_config()["HostQQ"], "group_id": group.id, "level": 4}
)
GlobalFrequencyLimitDict().add_group(group.id)
await app.sendGroupMessage(
group, MessageChain.create([
Plain(text="欸嘿嘿~我来啦!宇宙无敌小可爱纱雾酱华丽登场!")
])
)
except AccountMuted:
pass
except:
logger.error(traceback.format_exc())
@logger.catch
@bcc.receiver(ApplicationLaunched)
async def init():
await core.bot_launch_init()
await online_notice(app)
def management_boot():
from WebManager.web_manager import run_api
run_api()
if get_config("webManagerApi"):
threading.Thread(target=management_boot, args=()).start()
if get_config("webManagerAutoBoot"):
import webbrowser
webbrowser.open(f"{os.getcwd()}/WebManager/index.html")
core.launch()