-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
118 lines (93 loc) · 2.78 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
import tools.track as track
import os
from flask import Flask, request, jsonify
import sqlite3
import json
import tools.config as config
import tools.plugins as plugins
def init_database():
track.init()
conn = sqlite3.connect(track.server_db_path)
cursor = conn.cursor()
try:
cursor.execute("CREATE TABLE USERSINFO(\
ID INTEGER PRIMARY KEY AUTOINCREMENT,\
USER_ID INT NOT NULL,\
GROUP_ID INT NOT NULL,\
PERMISSION INT NOT NULL,\
GAME_PLAYED INT NOT NULL,\
WINS INT NOT NULL\
);")
except:
pass
# gamedata保存游戏进行时的内容
# STATUS 为0的被视为进行时数据
# STATUS 为1的被视为完成时数据
# 每一个group只能有一个进行时的游戏
# 但是进行时的游戏可以在不同的群保持独立
# 用track_id来保存歌曲信息
try:
cursor.execute("CREATE TABLE GAMEDATA(\
ID INTEGER PRIMARY KEY AUTOINCREMENT,\
STATUS INT NOT NULL,\
GROUP_ID INT NOT NULL,\
MODE INT NOT NULL,\
TRACK_ID INT,\
MEMBERS TEXT,\
WINNER_ID INT,\
START_TIME DATE NOT NULL,\
LYRICS_RESERVE TEXT,\
);")
except:
pass
try:
cursor.execute("CREATE TABLE GLOBAL(\
CURSOR_RANGE INT\
);")
except:
pass
conn.commit()
conn.close()
def get_post_msg(request):
data = request.get_data()
json_data = json.loads(data.decode("utf-8"))
return json_data
def is_group_msg(post_msg):
return post_msg['post_type'] == 'message' and post_msg['message_type'] == 'group'
'''
此函数负责解析消息
返回(命令名,参数列表)
'''
def parse_msg(msg):
stripped_msg = str.strip(msg)
res = [x for x in stripped_msg.split(' ') if x]
if len(res) < 2:
return [res[0],None]
return [res[0], res[1:]]
'''
核心方法 treat_group_msg
函数假定传入的一定是群消息 并能正常获得字段 这些要由调用方保证
'''
def treat_group_msg(post_msg):
# 获得三个最主要字段
msg = post_msg['message']
res = []
for f in config.filters:
r = f(msg)
if r != -1:
res = config.cos_parse_msg[r](msg)
break
else:
res = parse_msg(msg)
try:
config.CALL_COMMAND[res[0]](res[1], post_msg)
except KeyError as e:
pass
app = Flask(__name__)
init_database()
@app.route('/watch',methods=['POST'])
def watch():
post_msg = get_post_msg(request)
if is_group_msg(post_msg):
treat_group_msg(post_msg)
return jsonify(block="false")