修复了消息文本中出现和指令相同的字符串会错误激活指令的问题#164
Open
CheckeyZerone wants to merge 7 commits intotencent-connect:masterfrom
Open
修复了消息文本中出现和指令相同的字符串会错误激活指令的问题#164CheckeyZerone wants to merge 7 commits intotencent-connect:masterfrom
CheckeyZerone wants to merge 7 commits intotencent-connect:masterfrom
Conversation
Collaborator
|
PR里没看到代码变更,麻烦看下是不是漏了提交 |
Contributor
Author
刚刚提交了漏掉的代码 |
liaoyanglay
reviewed
Dec 19, 2023
Collaborator
liaoyanglay
left a comment
There was a problem hiding this comment.
频道中机器人收到at消息"@机器人 /指令",message.content的开头是机器人id,而不是指令,会导致频道中无法匹配
Contributor
Author
@机器人的消息文本中出现机器人id信息的位置为用户在频道发送消息中@机器人的位置,这个位置通常在文本开头,但是也有可能出现在文本中间位置。修改后的代码在获取到文本信息后先把机器人id的字符串从文本中剔除,然后再进行切分,判断指令是否出现在文本开头,在频道中经过测试,暂时没有出现@机器人时无法触发指令的问题 |
iatbsky
suggested changes
Jan 11, 2024
iatbsky
left a comment
There was a problem hiding this comment.
可以使用更严格的判断方法,防止误触发
你的pr:
for command in self.commands:
if command in message.content:
# 剔除消息文本中@机器人的字符串
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我的建议:
content_split = message.content.split(' ',3)
bot_id = await message.api.me())['id']
for command in self.commands:
# 保证消息开头为@机器人,且接下来为/cmd,以符合客户端的设计
if content_split[0] == f"<@!{bot_id}>" and content_split[1] == f'/{command}':
# 分割指令后面的指令参数
kwargs["params"] = content_split[2].strip()
return await func(*args, **kwargs)
return False
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
修改前的代码,如果在文本中出现了和指令相同的字符串,会直接执行指令,示例如下:
若向机器人发送消息:@机器人 /cmd_01 123
期望得到回复: /cmd_01 123
实际的到回复: /cmd_01 123
若向机器人发送消息:@机器人 /cmd_02 123
期望得到回复: /cmd_02 123
实际的到回复: /cmd_02 123
若向机器人发送消息:@机器人 123
期望得到回复: 收到!
实际的到回复: 收到!
若向机器人发送消息:@机器人 /cmd_02 /cmd_01
期望得到回复: /cmd_02 /cmd_01
实际的到回复: /cmd_01
若向机器人发送消息:@机器人 命令/cmd_02
期望得到回复: 收到!
实际的到回复: /cmd_02