-
-
Notifications
You must be signed in to change notification settings - Fork 816
feat:发送消息前的At和Reply改用概率添加,同时过滤掉不支持的组件 #1818
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,10 +1,11 @@ | ||||||||||||||||||||||||||
import random | ||||||||||||||||||||||||||
import re | ||||||||||||||||||||||||||
import time | ||||||||||||||||||||||||||
import traceback | ||||||||||||||||||||||||||
from typing import AsyncGenerator, Union | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
from astrbot.core import html_renderer, logger, file_token_service | ||||||||||||||||||||||||||
from astrbot.core.message.components import At, File, Image, Node, Plain, Record, Reply | ||||||||||||||||||||||||||
from astrbot.core.message.components import At, Face, File, Image, Node, Plain, Record, Reply | ||||||||||||||||||||||||||
from astrbot.core.message.message_event_result import ResultContentType | ||||||||||||||||||||||||||
from astrbot.core.platform.astr_message_event import AstrMessageEvent | ||||||||||||||||||||||||||
from astrbot.core.platform.message_type import MessageType | ||||||||||||||||||||||||||
|
@@ -263,7 +264,6 @@ async def process( | |||||||||||||||||||||||||
result.chain = [Image.fromFileSystem(url)] | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# 触发转发消息 | ||||||||||||||||||||||||||
has_forwarded = False | ||||||||||||||||||||||||||
if event.get_platform_name() == "aiocqhttp": | ||||||||||||||||||||||||||
word_cnt = 0 | ||||||||||||||||||||||||||
for comp in result.chain: | ||||||||||||||||||||||||||
|
@@ -274,13 +274,17 @@ async def process( | |||||||||||||||||||||||||
uin=event.get_self_id(), name="AstrBot", content=[*result.chain] | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
result.chain = [node] | ||||||||||||||||||||||||||
has_forwarded = True | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if not has_forwarded: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if any(isinstance(comp, (Plain, Face, Image, At)) for comp in result.chain): | ||||||||||||||||||||||||||
# at 回复 | ||||||||||||||||||||||||||
if ( | ||||||||||||||||||||||||||
self.reply_with_mention | ||||||||||||||||||||||||||
random.random() < self.reply_with_mention | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (bug_risk): 考虑强制执行有效的概率范围 这些概率的无效浮点数值可能会导致意外行为。在加载时限制它们,或者验证配置以确保它们保持在 0.0-1.0 之间。 建议的实施方案: def __init__(self, ..., reply_with_mention, ...):
# Clamp reply_with_mention to [0.0, 1.0]
self.reply_with_mention = min(max(reply_with_mention, 0.0), 1.0)
... 如果在其他地方设置了 Original comment in Englishsuggestion (bug_risk): Consider enforcing valid probability range Invalid float values for these probabilities could cause unintended behavior. Clamp them at load time or validate the config to ensure they stay within 0.0–1.0. Suggested implementation: def __init__(self, ..., reply_with_mention, ...):
# Clamp reply_with_mention to [0.0, 1.0]
self.reply_with_mention = min(max(reply_with_mention, 0.0), 1.0)
... If |
||||||||||||||||||||||||||
and event.get_message_type() != MessageType.FRIEND_MESSAGE | ||||||||||||||||||||||||||
and not ( | ||||||||||||||||||||||||||
isinstance(result.chain[0], At) | ||||||||||||||||||||||||||
and result.chain[0].qq == event.get_sender_id() | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
): | ||||||||||||||||||||||||||
result.chain.insert( | ||||||||||||||||||||||||||
0, At(qq=event.get_sender_id(), name=event.get_sender_name()) | ||||||||||||||||||||||||||
|
@@ -289,6 +293,6 @@ async def process( | |||||||||||||||||||||||||
result.chain[1].text = "\n" + result.chain[1].text | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# 引用回复 | ||||||||||||||||||||||||||
if self.reply_with_quote: | ||||||||||||||||||||||||||
if random.random() < self.reply_with_quote: | ||||||||||||||||||||||||||
if not any(isinstance(item, File) for item in result.chain): | ||||||||||||||||||||||||||
result.chain.insert(0, Reply(id=event.message_obj.message_id)) | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (code-quality): 合并嵌套的 if 条件 (
Suggested change
Explanation过多的嵌套会使代码难以理解,这在 Python 中尤其如此,因为没有括号来帮助区分不同的嵌套级别。阅读深度嵌套的代码令人困惑,因为您必须跟踪哪些条件与哪些级别相关。因此,我们努力尽可能减少嵌套,并且可以使用 Original comment in Englishsuggestion (code-quality): Merge nested if conditions (
Suggested change
ExplanationToo much nesting can make code difficult to understand, and this is especiallytrue in Python, where there are no brackets to help out with the delineation of different nesting levels. Reading deeply nested code is confusing, since you have to keep track of which |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: 为浮点概率添加 JSON 模式范围约束
请将
minimum: 0.0
和maximum: 1.0
添加到这些模式字段,以强制执行有效的概率值。建议的实施方案:
Original comment in English
suggestion: Add JSON schema range constraints for float probabilities
Please add
minimum: 0.0
andmaximum: 1.0
to these schema fields to enforce valid probability values.Suggested implementation: