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

event 文本获取错误 #19

Open
paul-sama opened this issue Mar 22, 2024 · 6 comments
Open

event 文本获取错误 #19

paul-sama opened this issue Mar 22, 2024 · 6 comments
Labels
question Further information is requested

Comments

@paul-sama
Copy link

event.get_message()
event.get_plaintext()

输入 123&123<123

结果都是 123&amp;123&lt;123

@CMHopeSunshine CMHopeSunshine added the question Further information is requested label Mar 23, 2024
@Autuamn
Copy link
Contributor

Autuamn commented Apr 19, 2024

找到了,是这里的问题

class TextSegment(MessageSegment):
if TYPE_CHECKING:
type: Literal["text"]
data: TextData
@override
def __str__(self) -> str:
return escape(self.data["text"])

对于 event.get_message()__str__ 函数为

def __str__(self) -> str:
    return "".join(str(seg) for seg in self)

而对于 event.get_plaintext() 则为 self.get_message().extract_plain_text()

def extract_plain_text(self) -> str:
    """提取消息内纯文本消息"""

    return "".join(str(seg) for seg in self if seg.is_text())

两者都会触发 TextSegment__str__ 函数,会被 escape

def escape(s: str) -> str:
return s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")

@Autuamn
Copy link
Contributor

Autuamn commented Apr 19, 2024

我还发现了 Message._construct() 函数根本没有触发,导致消息 content 中所有形如 <@474564749217234954> <#1208348399263285308> <:logo:1227276258447065168> 的文本都只会是 TextSegment

原因是处理 message.content 时直接使用了 msg.append(MessageSegment.text(message.content))

并没有调用 _construct()

我想也许要改为 msg.extend(Message(message.content))

@classmethod
def from_guild_message(cls, message: MessageGet) -> "Message":
msg = Message()
if message.mention_everyone:
msg.append(MessageSegment.mention_everyone())
if message.content:
msg.append(MessageSegment.text(message.content))

@Autuamn
Copy link
Contributor

Autuamn commented Apr 19, 2024

而对于 Message._construct() 函数中的正则也有个小问题

在匹配形如 <.*<@474564749217234954> 的 content 时,会匹配最外层的 <>,而返回 TextSegment

也许正则要改为 <(?P<type>(@!|@&|@|#|/|:|a:|t:))(?P<param>.+?)> 去掉对 type 组的 ? 可选

def _construct(msg: str) -> Iterable[MessageSegment]:
text_begin = 0
for embed in re.finditer(
r"<(?P<type>(@!|@&|@|#|/|:|a:|t:))?(?P<param>.+?)>",
msg,
):

@CMHopeSunshine
Copy link
Member

而对于 Message._construct() 函数中的正则也有个小问题

在匹配形如 <.*<@474564749217234954> 的 content 时,会匹配最外层的 <>,而返回 TextSegment

也许正则要改为 <(?P<type>(@!|@&|@|#|/|:|a:|t:))(?P<param>.+?)> 去掉对 type 组的 ? 可选

def _construct(msg: str) -> Iterable[MessageSegment]:
text_begin = 0
for embed in re.finditer(
r"<(?P<type>(@!|@&|@|#|/|:|a:|t:))?(?P<param>.+?)>",
msg,
):

感谢反馈解决!愿意发个PR吗

CMHopeSunshine pushed a commit that referenced this issue May 8, 2024
* 🐛 修复 event 文本获取错误 #19

* 🐛 修改 `Message._construct()` 的正则
@Autuamn
Copy link
Contributor

Autuamn commented May 27, 2024

@paul-sama 麻烦帮忙试试最新 commit 版本

@paul-sama
Copy link
Author

@paul-sama 麻烦帮忙试试最新 commit 版本

嗯,最新版本修复好了

CMHopeSunshine pushed a commit that referenced this issue Jun 21, 2024
* 🐛 修复 event 文本获取错误 #19

* 🐛 修改 `Message._construct()` 的正则

* ✨ 为 Webhook 中的 source_guild 和 source_channel 添加单独的类

🐛 修正 source_guild, source_channel 的类型标注

* 🐛 为 `icon` 添加默认值

thanks, shoucandanghehe

Co-authored-by: 呵呵です <[email protected]>

---------

Co-authored-by: 呵呵です <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants