-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
92 lines (76 loc) · 2.75 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
import time
import threading
import schedule
from gv import Global
import botpy
from botpy import logging
from botpy.message import GroupMessage
from botpy.manage import GroupManageEvent
from server import work_message, work_delay_command
from server.task import rank_task, every_day_task
import asyncio
from server.error import LFError
_log = logging.get_logger()
config = Global.config
async def work(message: GroupMessage):
user_id = message.author.member_openid
content = message.content.strip()
try:
resp, is_need_delay = await work_message(user_id=user_id, content=content)
except LFError as lfe:
resp = lfe.get_msg()
is_need_delay = False
except:
resp = "后台异常"
is_need_delay = False
delay_time = 0
delay_command = ""
if is_need_delay:
current_content, delay_time, delay_command = resp.split("|")
else:
current_content = resp
await message._api.post_group_message(
group_openid=message.group_openid,
msg_type=0,
msg_id=message.id,
content=current_content)
if is_need_delay:
await asyncio.sleep(int(delay_time))
delay_content = await work_delay_command(delay_command, user_id)
await message._api.post_group_message(
group_openid=message.group_openid,
msg_type=0,
msg_id=message.id,
msg_seq=2,
content=delay_content)
class FLClient(botpy.Client):
async def on_ready(self):
_log.info(f"robot 「{self.robot.name}」 on_ready!")
async def on_group_add_robot(self, event: GroupManageEvent):
await self.api.post_group_message(
group_openid=event.group_openid,
msg_type=0,
event_id=event.event_id,
content=f"""欢迎来到幻想世界!🌟
亲爱的冒险者们,欢迎你们踏入这片充满奇迹与梦幻的土地!✨ 在这里,你可以尽情释放你的想象力,探索无尽的奇妙领域,与各种神秘生物相遇,展开一场场惊心动魄的冒险!💪""",
)
async def on_group_at_message_create(self, message: GroupMessage):
await work(message)
def schedule_work():
schedule.clear()
schedule.every().hour.at(":00").do(rank_task)
schedule.every().day.at("04:00").do(every_day_task)
# schedule.run_all()
while True:
schedule.run_pending()
time.sleep(1)
# 运行定时任务
def run_schedule_thread():
job_thread = threading.Thread(target=schedule_work)
job_thread.daemon = True
job_thread.start()
if __name__ == "__main__":
intents = botpy.Intents(public_messages=True)
client = FLClient(intents=intents)
run_schedule_thread()
client.run(appid=str(config["appid"]), secret=str(config["secret"]))