forked from LittleSkinCommspt/commspt-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandparser.py
179 lines (152 loc) · 5.78 KB
/
commandparser.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
from typing import List, Optional, Tuple
import re
from graia.application.entry import (At, Group, GroupMessage, MessageChain,
Plain, Quote)
from graia.broadcast.exceptions import ExecutionStop
import settings
from botpermissions import groupPermissions
# 一些 Depend
def filterCafe(group: Group):
'''在咖啡馆中禁用'''
if group.id == settings.specialqq.littleskin_cafe:
raise ExecutionStop()
def exceptGroups(group_list: List[int]):
'''在指定群聊内禁用'''
def wrapper(group: Group):
if group.id in group_list:
raise ExecutionStop()
return wrapper
def adminOnly(gm: GroupMessage):
'''仅管理员'''
cp = CommandParser(gm, settings.commandSymbol)
gp = groupPermissions(cp.sender_id)
if not gp.isAdmin():
raise ExecutionStop()
def onCommand(command_name: str):
'''在特定指令时执行
Args:
command_name: 指令名称(不带标识符)'''
def wrapper(gm: GroupMessage):
cp = CommandParser(gm, settings.commandSymbol)
gp = groupPermissions(cp.sender_id)
if gp.isBlocked():
raise ExecutionStop()
if not cp.plain_message:
raise ExecutionStop()
if cp.Command.cmd != command_name:
raise ExecutionStop()
return wrapper
def onWord(word: str):
'''在消息中有特定文字时执行
Args:
word: 文字'''
def wrapper(gm: GroupMessage):
cp = CommandParser(gm, settings.commandSymbol)
gp = groupPermissions(cp.sender_id)
if gp.isBlocked():
raise ExecutionStop()
if not cp.plain_message:
raise ExecutionStop()
if not word in cp.plain_message:
raise ExecutionStop()
return wrapper
def onWords(words_list: List[str]):
'''在消息中有特定文字时执行
Args:
words_list: 关键字列表'''
def wrapper(gm: GroupMessage):
cp = CommandParser(gm, settings.commandSymbol)
gp = groupPermissions(cp.sender_id)
if gp.isBlocked():
raise ExecutionStop()
inList = False
if not cp.plain_message:
raise ExecutionStop()
for word in words_list:
if word in cp.plain_message:
inList = True
break
if not inList:
raise ExecutionStop()
return wrapper
def onMatch(pattern: str):
'''在消息匹配正则表达式时执行
Args:
pattern: 正则表达式(必须为 str)'''
def wrapper(gm: GroupMessage):
cp = CommandParser(gm, settings.commandSymbol)
gp = groupPermissions(cp.sender_id)
if gp.isBlocked():
raise ExecutionStop()
if not cp.plain_message:
raise ExecutionStop()
if not re.match(pattern, cp.plain_message):
raise ExecutionStop()
return wrapper
class CommandParser(object):
'''指令解析器'''
_commandSymbol: str
messagechain: MessageChain
sender_id: int
plain_message: Optional[str]
quote_plain_message: str
at: List[At]
permission: groupPermissions
class Command(object):
'''指令
`cmd` 为指令,`args` 为参数'''
cmd: Optional[str]
args: Optional[str]
argsList: Optional[List[str]]
def __init__(self, _groupmessage: GroupMessage, _command_symbol: str) -> None:
'''初始化指令解析器
Args:
_groupmessage: `GroupMessage` 对象
_command_symbol: 指令标识符'''
self._commandSymbol = _command_symbol
self.messagechain = _groupmessage.messageChain
self.sender_id = _groupmessage.sender.id
self.plain_message = self._getPlainMessage(self.messagechain)
self.at = self._getAt()
self.Command.cmd, self.Command.args, self.Command.argsList = self._getCommand()
self.quote_plain_message = self._getQuotePlainMessage()
self.permission = groupPermissions(self.sender_id)
def _getPlainMessage(self, _messagechain: MessageChain) -> Optional[str]:
if _messagechain.has(Plain):
_text = str()
for _i in _messagechain[Plain]:
i_text: str = _i.text
if i_text.strip() != '':
_text = f'{_text} {i_text}'
return _text.strip()
else:
return None
def _getAt(self) -> List[At]:
return self.messagechain.get(At)
def _getQuotePlainMessage(self) -> Optional[str]:
if self.messagechain.has(Quote):
_quote_message: Quote = self.messagechain.get(Quote)[0]
_origin_messagechain: MessageChain = _quote_message.origin
_plain_message = self._getPlainMessage(_origin_messagechain)
return _plain_message
else:
return None
def isConstance(self) -> bool:
'''判断此 `GroupMessage` 是否为 Constance 发出'''
return self.sender_id == settings.specialqq.constance
def _getCommand(self) -> Tuple[Optional[str], Optional[str]]:
if not self.plain_message:
return None, None, None
if self._commandSymbol in self.plain_message:
_message_body: str = self.plain_message.split(
':', 1)[1] if self.isConstance() else self.plain_message
_splited_message: list = _message_body.split(' ', 1)
_command: str = _splited_message[0]
_args: Optional[str] = _splited_message[1] if len(
_splited_message) > 1 else None
_argsList: Optional[List[str]] = _args.split() if _args else None
cmd: str = _command.replace(
self._commandSymbol, '') # 只留下命令主体而不考虑命令标识符
return cmd, _args, _argsList
else:
return None, None, None