-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.py
457 lines (405 loc) · 20.5 KB
/
handlers.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
from typing import Type
import nonebot
from nonebot import get_plugin_config, on_keyword, on_command, require, get_bot
from nonebot.adapters import MessageTemplate, Event
from nonebot.adapters.onebot.v11 import Message, Event, Bot
from nonebot.internal.matcher import Matcher
from nonebot.plugin import PluginMetadata
from nonebot.rule import to_me, ArgumentParser
from nonebot.params import CommandArg, ArgPlainText
from nonebot.typing import T_State
from .services import *
def user_add_handler(matcher: Type[Matcher], service: APIService):
"""添加或更新用户数据消息事件处理函数"""
@matcher.handle()
async def _(state: T_State, event: Event):
state["qq"] = event.get_user_id()
@matcher.got("user_info", prompt=MessageTemplate("⚙️请输入用户信息\n格式如下:\n账号:密码:学校全称"))
async def _(state: T_State, user_info: str = ArgPlainText()):
"""添加或更新用户信息"""
state["username"], state["password"], state["school"] = user_info.split(":") # 获取输入的用户信息
user = await get_user(state["qq"])
if user is None:
res = await add_user(service, int(state["qq"]), state["username"], state["password"], state["school"])
if res == 2:
await matcher.finish(MessageTemplate("账号信息错误"))
elif res == 1:
await matcher.finish(MessageTemplate("账号数据保存异常"))
elif res == 0:
await matcher.finish(MessageTemplate("用户数据添加成功"))
else:
res = await update_user(service, int(state["qq"]), state["username"], state["password"], state["school"])
if res == 2:
await matcher.finish(MessageTemplate("账号信息错误"))
elif res == 0:
await matcher.finish(MessageTemplate("用户数据更新成功"))
def all_activity_handlers(matcher: Type[Matcher], service: APIService):
"""获取活动消息事件处理函数"""
@matcher.handle()
async def _(event: Event):
qq = int(event.get_user_id())
user = await get_user(qq)
if user is not None:
res = await get_activity_list(service, qq)
if res == 1:
await matcher.finish(MessageTemplate("用户数据错误,请检查用户数据"))
elif res == 2:
await update_token(service, qq)
await matcher.finish(MessageTemplate("请求错误,刷新token,请重试"))
elif res is None:
await update_token(service, qq)
await matcher.finish(MessageTemplate("请求错误,刷新token,请重试"))
else:
msg = ""
if len(res) > 0:
for activity in res:
msg += f'✨活动名称:{activity["name"]}\n'
msg += f'🏷️️活动类型:{activity["categoryName"]}\n'
msg += f'💯活动分值:{activity["credit"]}\n'
msg += f'🕐报名开始时间:{activity["joinStartTime"]}\n'
msg += f'🕐报名结束时间:{activity["joinEndTime"]}\n'
msg += f'🕒活动开始时间:{activity["startTime"]}\n'
msg += f'🕓活动结束时间:{activity["endTime"]}\n'
msg += f'👉可参与人数:{activity["allowUserCount"]}\n'
msg += f'🤚已报名人数:{activity["joinUserCount"]}\n'
msg += f'🏫活动院系:'
if len(activity["allowCollege"]) > 0:
msg += '\n'
for allowCollege in activity["allowCollege"]:
msg += f'\t{allowCollege["name"]}\n'
else:
msg += '全部院系\n'
msg += f'🧸活动年级:'
if len(activity["allowYear"]) > 0:
msg += '\n\t'
for allowYear in activity["allowYear"]:
msg += f'{allowYear["name"]}'
if allowYear != activity["allowYear"][len(activity["allowYear"]) - 1]:
msg += ','
else:
msg += '\n'
else:
msg += '全部年级\n'
msg += f'⛱️活动地址:{activity["address"]}\n'
msg += f'⭐活动状态:{activity["statusName"]}\n'
msg += f'🆔活动ID:{activity["id"]}'
if activity != res[len(res) - 1]:
msg += "\n\n"
else:
msg = "暂无可参加活动"
await matcher.finish(MessageTemplate(msg))
else:
await matcher.finish(MessageTemplate("用户数据错误,请检查用户数据"))
def get_can_join_activity_handler(matcher: Type[Matcher], service: APIService):
"""获取可参加活动消息事件处理函数"""
@matcher.handle()
async def _(event: Event):
qq = int(event.get_user_id())
user = await get_user(qq)
if user is not None:
res = await can_join_activity(service, qq)
if res is None:
await update_token(service, qq)
await matcher.finish(MessageTemplate("请求错误,刷新token,请重试"))
elif res == 1:
await matcher.finish(MessageTemplate("用户数据错误,请检查用户数据"))
elif res == 2:
await update_token(service, qq)
await matcher.finish(MessageTemplate("请求错误,刷新token,请重试"))
else:
msg = ""
for activity in res:
msg += f'✨活动名称:{activity["name"]}\n'
msg += f'🏷️️活动类型:{activity["categoryName"]}\n'
msg += f'💯活动分值:{activity["credit"]}\n'
msg += f'🕐报名开始时间:{activity["joinStartTime"]}\n'
msg += f'🕐报名结束时间:{activity["joinEndTime"]}\n'
msg += f'🕒活动开始时间:{activity["startTime"]}\n'
msg += f'🕓活动结束时间:{activity["endTime"]}\n'
msg += f'👉可参与人数:{activity["allowUserCount"]}\n'
msg += f'🤚已报名人数:{activity["joinUserCount"]}\n'
msg += f'🏫活动院系:'
if len(activity["allowCollege"]) > 0:
msg += '\n'
for allowCollege in activity["allowCollege"]:
msg += f'\t{allowCollege["name"]}\n'
else:
msg += '全部院系\n'
msg += f'🧸活动年级:'
if len(activity["allowYear"]) > 0:
msg += '\n\t'
for allowYear in activity["allowYear"]:
msg += f'{allowYear["name"]}'
if allowYear != activity["allowYear"][len(activity["allowYear"]) - 1]:
msg += ','
else:
msg += '\n'
else:
msg += '全部年级\n'
msg += f'⛱️活动地址:{activity["address"]}\n'
msg += f'⭐活动状态:{activity["statusName"]}\n'
msg += f'🆔活动ID:{activity["id"]}'
if activity != res[len(res) - 1]:
msg += "\n\n"
if msg != "":
await matcher.finish(MessageTemplate(msg))
else:
await matcher.finish(Message("暂无可参加活动"))
else:
await matcher.finish(MessageTemplate("用户数据错误,请检查用户数据"))
def auto_push_handler(matcher: Type[Matcher]):
"""活动推送消息事件处理函数"""
@matcher.handle()
async def _(event: Event, args: Message = CommandArg()):
qq = int(event.get_user_id())
if status := args.extract_plain_text():
res = None
if status == "开启":
res = await database_switch_push(qq, 1)
elif status == "关闭":
res = await database_switch_push(qq, 0)
if res == 0:
await matcher.finish(MessageTemplate("操作成功"))
else:
await matcher.finish(MessageTemplate("操作失败"))
def my_activity_handler(matcher: Type[Matcher], service: APIService):
"""我的活动消息事件处理函数"""
@matcher.handle()
async def _(event: Event):
qq = int(event.get_user_id())
user = await get_user(qq)
if user is not None:
res = await get_my_activity(service, qq)
if res is None:
await update_token(service, qq)
await matcher.finish(MessageTemplate("请求错误,刷新token,请重试"))
elif res == 1:
await matcher.finish(MessageTemplate("用户数据错误,请检查用户数据"))
elif res == 2:
await update_token(service, qq)
await matcher.finish(MessageTemplate("请求错误,刷新token,请重试"))
else:
msg = f'📋未开始活动:\n\n'
if len(res[1]) > 0:
for activity in res[1]:
msg += f'✨活动名称:{activity["name"]}\n'
msg += f'🕑开始时间:{activity["startTime"]}\n'
msg += f'🕓结束时间:{activity["endTime"]}\n'
msg += f'⛱️活动地址:{activity["address"]}\n'
msg += f'🆔活动ID:{activity["id"]}\n\n'
else:
msg += "暂无活动\n\n"
msg += f'📝待审核活动:\n\n'
if len(res[0]) > 0:
for activity in res[0]:
msg += f'✨活动名称:{activity["name"]}\n'
msg += f'🕑开始时间:{activity["startTime"]}\n'
msg += f'🕓结束时间:{activity["endTime"]}\n'
msg += f'⛱️活动地址:{activity["address"]}\n'
msg += f'🆔活动ID:{activity["id"]}'
if activity != res[len(res[0]) - 1]:
msg += "\n\n"
else:
msg += "暂无活动"
await matcher.finish(MessageTemplate(msg))
else:
await matcher.finish(MessageTemplate("用户数据错误,请检查用户数据"))
def activity_info_handler(matcher: Type[Matcher], service: APIService):
"""活动信息消息事件处理函数"""
@matcher.handle()
async def _(event: Event, args: Message = CommandArg()):
qq = int(event.get_user_id())
user = await get_user(qq)
if user is not None:
if activity_id := args.extract_plain_text():
res = await get_activity_info(service, qq, int(activity_id))
if res is None:
await update_token(service, qq)
await matcher.finish(MessageTemplate("请求错误,刷新token,请重试"))
elif res == 1:
await matcher.finish(MessageTemplate("用户数据错误,请检查用户数据"))
elif res == 2:
await update_token(service, qq)
await matcher.finish(MessageTemplate("请求错误,刷新token,请重试"))
else:
activity_msg = res["data"]["baseInfo"]
msg = f'✨活动名称:{activity_msg["name"]}\n'
msg += f'🏷️️活动类型:{activity_msg["categoryName"]}\n'
msg += f'💯活动分值:{activity_msg["credit"]}\n'
msg += f'🕐报名开始时间:{activity_msg["joinStartTime"]}\n'
msg += f'🕐报名结束时间:{activity_msg["joinEndTime"]}\n'
msg += f'🕒活动开始时间:{activity_msg["startTime"]}\n'
msg += f'🕓活动结束时间:{activity_msg["endTime"]}\n'
msg += f'👉可参与人数:{activity_msg["allowUserCount"]}\n'
msg += f'🤚已报名人数:{activity_msg["joinUserCount"]}\n'
msg += f'🏫活动院系:'
if len(activity_msg["allowCollege"]) > 0:
msg += '\n'
for allowCollege in activity_msg["allowCollege"]:
msg += f'\t{allowCollege["name"]}\n'
else:
msg += '全部院系\n'
msg += f'🧸活动年级:'
if len(activity_msg["allowYear"]) > 0:
msg += '\n\t'
for allowYear in activity_msg["allowYear"]:
msg += f'{allowYear["name"]}'
if allowYear != activity_msg["allowYear"][len(activity_msg["allowYear"]) - 1]:
msg += ','
else:
msg += '\n'
else:
msg += '全部年级\n'
msg += f'⛱️活动地址:{activity_msg["address"]}\n'
msg += f'⭐活动状态:{activity_msg["statusName"]}'
await matcher.finish(MessageTemplate(msg))
else:
await matcher.finish(MessageTemplate("请输入活动ID"))
else:
await matcher.finish(MessageTemplate("用户数据错误,请检查用户数据"))
def join_activity_handler(matcher: Type[Matcher], service: APIService):
"""报名消息事件处理函数"""
@matcher.handle()
async def _(event: Event, args: Message = CommandArg()):
qq = int(event.get_user_id())
user = await get_user(qq)
if user is not None:
if activity_id := args.extract_plain_text():
res = await join_activity(service, qq, int(activity_id))
if res is None:
await update_token(service, qq)
await matcher.finish(MessageTemplate("请求错误,刷新token,请重试"))
elif res == 2:
await update_token(service, qq)
await matcher.finish(MessageTemplate("请求错误,刷新token,请重试"))
elif res == 1:
await matcher.finish(MessageTemplate("用户数据错误,请检查用户数据"))
else:
await matcher.finish(MessageTemplate(res))
else:
await matcher.finish(MessageTemplate("用户数据错误,请检查用户数据"))
def reservation_handler(matcher: Type[Matcher], service: APIService, scheduler):
"""预约消息事件处理函数"""
@matcher.handle()
async def _(event: Event, args: Message = CommandArg()):
qq = int(event.get_user_id())
user = await get_user(qq)
if user is not None:
if activity_id := args.extract_plain_text():
res = await reservation_add_activity(service, qq, int(activity_id), scheduler)
if res is None:
await matcher.finish(MessageTemplate("添加任务失败"))
elif res == 1:
await matcher.finish(MessageTemplate("任务添加错误,请检查任务信息是否重复"))
else:
await matcher.finish(MessageTemplate("添加成功"))
def find_reservation_handler(matcher: Type[Matcher], scheduler):
"""查询预约消息事件处理函数"""
@matcher.handle()
async def _(event: Event):
jobs = scheduler.get_jobs()
for job in jobs:
nonebot.logger.info(job)
qq = int(event.get_user_id())
res = await get_reservation_qq(qq)
if res is None:
await matcher.finish(MessageTemplate("暂无预约任务"))
else:
await matcher.finish(MessageTemplate(res))
def remove_reservation_handler(matcher: Type[Matcher], scheduler):
"""删除预约"""
@matcher.handle()
async def _(event: Event, args: Message = CommandArg()):
qq = int(event.get_user_id())
if activity_id := args.extract_plain_text():
res = await remove_reservation(qq, int(activity_id), scheduler)
if res is None:
await matcher.finish(MessageTemplate("删除失败,请检查活动信息"))
else:
await matcher.finish(MessageTemplate(f"{activity_id}删除成功"))
def update_token_handler(matcher: Type[Matcher], service: APIService):
"""刷新令牌消息事件处理函数"""
@matcher.handle()
async def _(event: Event):
qq = int(event.get_user_id())
user = await get_user(qq)
if user is not None:
res = await update_token(service, qq)
if res == 0:
await matcher.finish(MessageTemplate("Token刷新成功"))
elif res == 1:
await matcher.finish(MessageTemplate("用户数据错误,请检查用户数据"))
elif res == 2:
await matcher.finish(MessageTemplate("用户数据错误,请检查用户数据"))
else:
await matcher.finish(MessageTemplate("用户数据错误,请检查用户数据"))
def my_credit_handler(matcher: Type[Matcher], service: APIService):
"""查询分数"""
@matcher.handle()
async def _(event: Event):
qq = int(event.get_user_id())
res = await find_my_credit(service, qq)
if res is None:
await update_token(service, qq)
await matcher.finish(MessageTemplate("请求错误,刷新token,请重试"))
elif res == 1:
await matcher.finish(MessageTemplate("用户数据错误,请检查用户数据"))
elif res == 2:
await update_token(service, qq)
await matcher.finish(MessageTemplate("请求错误,刷新token,请重试"))
else:
msg = f'实践学分:{res["credit"]}\n'
msg += f'诚信值:{res["cx"]}'
await matcher.finish(MessageTemplate(msg))
def group_add_handler(matcher: Type[Matcher], service: APIService):
"""添加群推送"""
@matcher.got("school", prompt=MessageTemplate("⚙️请输入学校全称"))
async def _(event: Event, school: str = ArgPlainText()):
if school:
group_id = event.get_session_id().split("_")[1]
nonebot.logger.info("GroupID:" + group_id)
res = await create_group(service, int(group_id), school)
if res == 0:
await matcher.finish(MessageTemplate("添加完成"))
else:
await matcher.finish(MessageTemplate("添加失败,请检查信息"))
def auto_push_group_handler(matcher: Type[Matcher]):
"""切换群推送状态"""
@matcher.handle()
async def _(event: Event, args: Message = CommandArg()):
group_id = int(event.get_session_id().split("_")[1])
nonebot.logger.info("GroupID:" + str(group_id))
if status := args.extract_plain_text():
if status == "开启":
res = await switch_group_push(group_id, 1)
if res is None:
await matcher.finish(MessageTemplate("失败"))
else:
await matcher.finish(MessageTemplate("成功"))
elif status == "关闭":
res = await switch_group_push(group_id, 0)
if res is None:
await matcher.finish(MessageTemplate("失败"))
else:
await matcher.finish(MessageTemplate("成功"))
def help_cmd_handler(matcher: Type[Matcher]):
"""帮助消息事件处理函数"""
@matcher.handle()
async def _():
msg = (
"📝指令列表:\n"
"✨添加用户\n"
"✨获取活动\n"
"✨获取可参加活动\n"
"✨活动推送开启/关闭\n"
"✨我的活动\n"
"✨活动🆔\n"
"✨报名🆔\n"
"✨预约🆔\n"
"✨查询预约\n"
"✨删除预约🆔\n"
"✨刷新token\n"
"✨查询分数\n"
"⚠️注:🆔为活动ID"
)
await matcher.finish(Message(msg))