forked from UNIkeEN/Little-UNIkeEN-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
290 lines (270 loc) · 12.7 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
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
import os
from flask import Flask, request
from enum import IntEnum
from utils.accountOperation import create_account_sql
from utils.standardPlugin import StandardPlugin, PluginGroupManager, EmptyPlugin, PokeStandardPlugin, AddGroupStandardPlugin, EmptyAddGroupPlugin
from plugins.autoRepoke import AutoRepoke
from plugins.faq_v2 import MaintainFAQ, AskFAQ, HelpFAQ, createFaqDb, createFaqTable
from plugins.groupCalendar import GroupCalendarHelper, GroupCalendarManager
from plugins.greetings import MorningGreet, NightGreet
from plugins.checkCoins import CheckCoins, AddAssignedCoins, CheckTransactions
from plugins.superEmoji import FirecrackersFace, FireworksFace, BasketballFace, HotFace
from plugins.news import ShowNews, YesterdayNews, UpdateNewsAndReport
from plugins.hotSearch import WeiboHotSearch, BaiduHotSearch, ZhihuHotSearch
from plugins.signIn import SignIn
from plugins.stocks import *
from plugins.sjtuInfo import SjtuCanteenInfo, SjtuLibInfo
from plugins.sjmcStatus_v2 import ShowSjmcStatus
from plugins.roulette import RoulettePlugin
from plugins.lottery import LotteryPlugin, createLotterySql
from plugins.show2cyPic import Show2cyPIC, ShowSePIC
from plugins.help_v2 import ShowHelp, ShowStatus, ServerMonitor
from plugins.groupBan import GroupBan
from plugins.privateControl import PrivateControl
from plugins.bilibiliSubscribe import createBilibiliTable, BilibiliSubscribe, BilibiliSubscribeHelper, BilibiliUpSearcher
try:
from plugins.chatWithNLP import ChatWithNLP
except:
ChatWithNLP = EmptyPlugin
from plugins.chatWithAnswerbook import ChatWithAnswerbook
try:
from plugins.getDekt import SjtuDekt, SjtuDektMonitor
except:
SjtuDekt, SjtuDektMonitor = EmptyPlugin, EmptyPlugin
from plugins.getJwc import GetSjtuNews, GetJwc, SjtuJwcMonitor#, SubscribeJwc
from plugins.sjtuBwc import SjtuBwc, SjtuBwcMonitor, createBwcSql
from plugins.canvasSync import CanvasiCalBind, CanvasiCalUnbind, GetCanvas
from plugins.getPermission import GetPermission, AddPermission, DelPermission, ShowPermission, AddGroupAdminToBotAdmin
from plugins.goBang import GoBangPlugin
from plugins.messageRecorder import GroupMessageRecorder
from plugins.addGroupRecorder import AddGroupRecorder
from plugins.fileRecorder import GroupFileRecorder
from plugins.sjmcLive import GetSjmcLive, GetFduMcLive, SjmcLiveMonitor, FduMcLiveMonitor
from plugins.sjtuHesuan import SjtuHesuan
from plugins.groupActReport import ActReportPlugin, ActRankPlugin
from plugins.groupWordCloud import wordCloudPlugin, GenWordCloud
from plugins.randomNum import TarotRandom, RandomNum, ThreeKingdomsRandom
from plugins.sjtuClassroom import SjtuClassroom, SjtuClassroomRecommend, SjtuClassroomPeopleNum
from plugins.makeJoke import MakeJoke
from plugins.uniAgenda import GetUniAgenda
try:
from plugins.notPublished.jile import Chai_Jile, Yuan_Jile
except:
Chai_Jile = EmptyPlugin
Yuan_Jile = EmptyPlugin
try:
from plugins.notPublished.getMddStatus import GetMddStatus, MonitorMddStatus#, SubscribeMdd
GetMddStatus()
except:
GetMddStatus, MonitorMddStatus = EmptyPlugin, EmptyPlugin
try:
from plugins.notPublished.EE0502 import ShowEE0502Comments
ShowEE0502Comments()
except:
ShowEE0502Comments = EmptyPlugin
try:
from plugins.notPublished.sjtuPlusGroupingVerication import SjtuPlusGroupingVerify
except:
SjtuPlusGroupingVerify = EmptyAddGroupPlugin
from plugins.gocqWatchDog import GocqWatchDog
###### end not published plugins
def sqlInit():
createGlobalConfig()
create_account_sql()
createFaqDb()
createBilibiliTable()
createLotterySql()
createBwcSql()
for group in get_group_list():
groupId = group['group_id']
createFaqTable(str(groupId))
sqlInit()
ROOT_PATH = os.path.dirname(os.path.realpath(__file__))
RESOURCES_PATH = os.path.join(ROOT_PATH, "resources")
# 特殊插件需要复用的放在这里
helper = ShowHelp() # 帮助插件
gocqWatchDog = GocqWatchDog(60)
groupMessageRecorder = GroupMessageRecorder() # 群聊消息记录插件
GroupPluginList:List[StandardPlugin]=[ # 指定群启用插件
groupMessageRecorder,
helper,ShowStatus(),ServerMonitor(), # 帮助
GetPermission(),
PluginGroupManager([AddPermission(), DelPermission(), ShowPermission(), AddGroupAdminToBotAdmin()], 'permission'), # 权限
PluginGroupManager([AskFAQ(), MaintainFAQ(), HelpFAQ()],'faq'), # 问答库与维护
PluginGroupManager([GroupCalendarHelper(), GroupCalendarManager()], 'calendar'),
PluginGroupManager([MorningGreet(), NightGreet()], 'greeting'), # 早安晚安
PluginGroupManager([CheckCoins(), AddAssignedCoins(),CheckTransactions()],'money'), # 查询金币,查询记录,增加金币(管理员)
PluginGroupManager([FireworksFace(), FirecrackersFace(), BasketballFace(), HotFace(), MakeJoke()], 'superemoji'), # 超级表情
PluginGroupManager([ShowNews(), YesterdayNews(),
PluginGroupManager([UpdateNewsAndReport()], 'newsreport')],'news'), # 新闻
PluginGroupManager([WeiboHotSearch(), BaiduHotSearch(), ZhihuHotSearch(),], 'hotsearch'),
PluginGroupManager([SjtuCanteenInfo(),SjtuLibInfo(), SjtuClassroom(), SjtuClassroomPeopleNum(),
SjtuClassroomRecommend(), GetMddStatus(), #SubscribeMdd(), # 交大餐厅, 图书馆, 核酸点, 麦当劳
PluginGroupManager([MonitorMddStatus()], 'mddmonitor'),],'sjtuinfo'),
# PluginGroupManager([QueryStocksHelper(), QueryStocks(), BuyStocksHelper(), BuyStocks(), QueryStocksPriceHelper(), QueryStocksPrice()],'stocks'), # 股票
PluginGroupManager([Chai_Jile(), Yuan_Jile()],'jile'), # 柴/元神寄了
PluginGroupManager([SignIn()], 'signin'), # 签到
PluginGroupManager([ShowSjmcStatus(), GetSjmcLive(), GetFduMcLive(),
PluginGroupManager([SjmcLiveMonitor(),FduMcLiveMonitor()], 'mclive')], 'sjmc'), #MC社服务
PluginGroupManager([GetJwc(), SjtuBwc(), #SubscribeJwc() ,
SjtuJwcMonitor(), GetSjtuNews(), SjtuDekt(),# jwc服务, jwc广播, 交大新闻, 第二课堂
PluginGroupManager([SjtuDektMonitor()], 'dekt'),
PluginGroupManager([SjtuBwcMonitor()], 'bwcreport'),], 'jwc'),
PluginGroupManager([RoulettePlugin()],'roulette'), # 轮盘赌
PluginGroupManager([LotteryPlugin()],'lottery'), # 彩票 TODO
# PluginGroupManager([GoBangPlugin()],'gobang'),
PluginGroupManager([Show2cyPIC()], 'anime'), #ShowSePIC(), # 来点图图,来点涩涩(关闭)
PluginGroupManager([ChatWithAnswerbook(), ChatWithNLP()], 'chat'), # 答案之书/NLP
PluginGroupManager([GetCanvas(), GetUniAgenda(), CanvasiCalBind(), CanvasiCalUnbind()], 'canvas'), # 日历馈送
# PluginGroupManager([DropOut()], 'dropout'), # 一键退学
PluginGroupManager([ShowEE0502Comments()], 'izf'), # 张峰
PluginGroupManager([ActReportPlugin(), ActRankPlugin(), wordCloudPlugin(), PluginGroupManager([GenWordCloud()], 'wcdaily')], 'actreport'), #水群报告
PluginGroupManager([RandomNum(), ThreeKingdomsRandom(), TarotRandom()], 'random'),
PluginGroupManager([BilibiliSubscribeHelper(), BilibiliSubscribe()], 'bilibili'),
PrivateControl(),
]
PrivatePluginList:List[StandardPlugin]=[ # 私聊启用插件
helper,
ShowStatus(),ServerMonitor(),
CheckCoins(),AddAssignedCoins(),CheckTransactions(),
ShowNews(), YesterdayNews(),
MorningGreet(), NightGreet(),
SignIn(),
QueryStocksHelper(), QueryStocks(), BuyStocksHelper(), BuyStocks(), QueryStocksPriceHelper(), QueryStocksPrice(),
SjtuCanteenInfo(),SjtuLibInfo(),ShowSjmcStatus(),SjtuDekt(),GetJwc(), SjtuBwc(), #SubscribeJwc(),
GetSjtuNews(),
LotteryPlugin(),
Show2cyPIC(), #ShowSePIC(),
GetCanvas(), CanvasiCalBind(), CanvasiCalUnbind(),
ShowEE0502Comments(),
GetSjmcLive(), GetFduMcLive(),
GetMddStatus(),#SubscribeMdd(),
SjtuHesuan(),
RandomNum(), ThreeKingdomsRandom(), TarotRandom(),
MakeJoke(),
SjtuClassroom(), SjtuClassroomPeopleNum(), SjtuClassroomRecommend(),
PrivateControl(),
]
GroupPokeList:List[PokeStandardPlugin] = [
AutoRepoke(), # 自动回复拍一拍
]
AddGroupVerifyPluginList:List[AddGroupStandardPlugin] = [
AddGroupRecorder(), # place this plugin to the first place
SjtuPlusGroupingVerify('test',[123, 456]),
]
helper.updatePluginList(GroupPluginList, PrivatePluginList)
app = Flask(__name__)
class NoticeType(IntEnum):
NoProcessRequired = 0
GroupMessageNoProcessRequired = 1
GocqHeartBeat = 5
GroupMessage = 11
GroupPoke = 12
GroupRecall = 13
GroupUpload = 14
PrivateMessage = 21
PrivatePoke = 22
PrivateRecall = 23
AddGroup = 31
AddPrivate = 32
def eventClassify(json_data: dict)->NoticeType:
"""事件分类"""
if json_data['post_type'] == 'meta_event' and json_data['meta_event_type'] == 'heartbeat':
return NoticeType.GocqHeartBeat
elif json_data['post_type'] == 'message':
if json_data['message_type'] == 'group':
if json_data['group_id'] in APPLY_GROUP_ID:
return NoticeType.GroupMessage
else:
return NoticeType.GroupMessageNoProcessRequired
elif json_data['message_type'] == 'private':
return NoticeType.PrivateMessage
elif json_data['post_type'] == 'notice':
if json_data['notice_type'] == 'notify':
if json_data['sub_type'] == "poke":
if json_data.get('group_id', None) in APPLY_GROUP_ID:
return NoticeType.GroupPoke
elif json_data['notice_type'] == 'group_recall':
return NoticeType.GroupRecall
elif json_data['notice_type'] == 'group_upload':
return NoticeType.GroupUpload
elif json_data['post_type'] == 'request':
print(json_data)
if json_data['request_type'] == 'friend':
return NoticeType.AddPrivate
elif json_data['request_type'] == 'group':
return NoticeType.AddGroup
else:
return NoticeType.NoProcessRequired
@app.route('/', methods=["POST"])
def post_data():
# 获取事件上报
data = request.get_json()
# 筛选并处理指定事件
flag=eventClassify(data)
# 群消息处理
if flag==NoticeType.GroupMessage:
msg=data['message'].strip()
for event in GroupPluginList:
event: StandardPlugin
try:
if event.judgeTrigger(msg, data):
ret = event.executeEvent(msg, data)
if ret != None:
return ret
except TypeError as e:
warning("type error in main.py: {}\n\n{}".format(e, event))
elif flag == NoticeType.GroupMessageNoProcessRequired:
groupMessageRecorder.executeEvent(data['message'], data)
elif flag == NoticeType.GroupRecall:
for plugin in [groupMessageRecorder]:
plugin.recallMessage(data)
# 私聊消息处理
elif flag == NoticeType.PrivateMessage:
# print(data)
msg=data['message'].strip()
for event in PrivatePluginList:
if event.judgeTrigger(msg, data):
if event.executeEvent(msg, data)!=None:
break
elif flag == NoticeType.AddGroup:
for p in AddGroupVerifyPluginList:
if p.judgeTrigger(data):
if p.addGroupVerication(data) != None:
break
# 上传文件处理
elif flag == NoticeType.GroupUpload:
for event in [GroupFileRecorder()]:
event.uploadFile(data)
# 群内拍一拍回拍
elif flag==NoticeType.GroupPoke:
for p in GroupPokeList:
if p.judgeTrigger(data):
if p.pokeMessage(data)!=None:
break
# 自动加好友
elif flag==NoticeType.AddPrivate:
set_friend_add_request(data['flag'], True)
elif flag==NoticeType.GocqHeartBeat:
gocqWatchDog.feed()
return "OK"
def initCheck():
# do some check
for p in GroupPluginList:
infoDict = p.getPluginInfo()
assert 'name' in infoDict.keys() and 'description' in infoDict.keys() \
and 'commandDescription' in infoDict.keys() and 'usePlace' in infoDict.keys()
if 'group' not in infoDict['usePlace']:
print("plugin [{}] can not be used in group talk!".format(infoDict['name']))
exit(1)
for p in PrivatePluginList:
infoDict = p.getPluginInfo()
assert 'name' in infoDict.keys() and 'description' in infoDict.keys() \
and 'commandDescription' in infoDict.keys() and 'usePlace' in infoDict.keys()
if 'private' not in infoDict['usePlace']:
print("plugin [{}] can not be used in private talk!".format(infoDict['name']))
exit(1)
gocqWatchDog.start()
if __name__ == '__main__':
initCheck()
app.run(host="127.0.0.1", port=5986)