Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复了消息文本中出现和指令相同的字符串会错误激活指令的问题 #164

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
12 changes: 8 additions & 4 deletions botpy/ext/command_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ async def decorated(*args, **kwargs):
message: BaseMessage = kwargs["message"]
for command in self.commands:
if command in message.content:
# 分割指令后面的指令参数
params = message.content.split(command)[1].strip()
kwargs["params"] = params
return await func(*args, **kwargs)
# 剔除消息文本中@机器人的字符串
content = message.content.replace(f"<@!{(await message.api.me())['id']}>", "")
content_split = content.lstrip().split(command)
# 当指令出现在消息文本(已剔除@机器人的信息)的开头执行指令
if len(content_split[0]) == 0:
# 分割指令后面的指令参数
kwargs["params"] = content_split[1].strip()
return await func(*args, **kwargs)
return False

return decorated
Expand Down
6 changes: 6 additions & 0 deletions botpy/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ def __repr__(self):
return str(self.__dict__)


@property
def api(self):
return self._api



class GroupMessage(BaseMessage):
__slots__ = (
"author",
Expand Down