diff options
author | Kyomotoi <[email protected]> | 2022-10-13 15:38:00 +0800 |
---|---|---|
committer | Kyomotoi <[email protected]> | 2022-10-13 15:38:00 +0800 |
commit | d150495dcc963c2bfe5f6033dfe40af6e5b3a362 (patch) | |
tree | 01303075e546abf0b36ee0ad078f742182d222ed | |
parent | b9f92e547c663a6d4a853b1c967e6f3cc2dca641 (diff) | |
download | ATRI-d150495dcc963c2bfe5f6033dfe40af6e5b3a362.tar.gz ATRI-d150495dcc963c2bfe5f6033dfe40af6e5b3a362.tar.bz2 ATRI-d150495dcc963c2bfe5f6033dfe40af6e5b3a362.zip |
🎨 优化长消息构造
-rw-r--r-- | ATRI/plugins/anti_effort/__init__.py | 17 | ||||
-rw-r--r-- | ATRI/plugins/bilibili_dynamic/data_source.py | 14 | ||||
-rw-r--r-- | ATRI/plugins/broadcast.py | 27 | ||||
-rw-r--r-- | ATRI/plugins/console/__init__.py | 13 | ||||
-rw-r--r-- | ATRI/plugins/repo.py | 12 | ||||
-rw-r--r-- | ATRI/plugins/twitter/data_source.py | 12 |
6 files changed, 52 insertions, 43 deletions
diff --git a/ATRI/plugins/anti_effort/__init__.py b/ATRI/plugins/anti_effort/__init__.py index 2417c37..52fea9c 100644 --- a/ATRI/plugins/anti_effort/__init__.py +++ b/ATRI/plugins/anti_effort/__init__.py @@ -14,6 +14,7 @@ from nonebot.adapters.onebot.v11 import ( ) from nonebot.adapters.onebot.v11.helpers import Cooldown +from ATRI.message import MessageBuilder from ATRI.utils.apscheduler import scheduler from .data_source import AntiEffort, PLUGIN_DIR @@ -21,14 +22,14 @@ from .data_source import AntiEffort, PLUGIN_DIR _lmt_notice = choice(["慢...慢一..点❤", "冷静1下", "歇会歇会~~", "呜呜...别急"]) -_GET_URL_MSG = """请键入wakatime share embed URL: -获取方法: - - 前往 wakatime.com/share/embed - - Format 选择 JSON - - Chart Type 选择 Coding Activity - - Date Range 选择 Last 7 Days - - 所需url就在下一栏 HTML 中的 url -""".strip() +_GET_URL_MSG = ( + MessageBuilder("请键入wakatime share embed URL:") + .text("- 前往 wakatime.com/share/embed") + .text("- Format 选择 JSON") + .text("- Chart Type 选择 Coding Activity") + .text("- Date Range 选择 Last 7 Days") + .text("- 所需url为下一栏 HTML 中的 url") +) add_user = AntiEffort().on_command("!我也要卷", "加入卷王统计榜") diff --git a/ATRI/plugins/bilibili_dynamic/data_source.py b/ATRI/plugins/bilibili_dynamic/data_source.py index 8cac1fb..8f1c6f9 100644 --- a/ATRI/plugins/bilibili_dynamic/data_source.py +++ b/ATRI/plugins/bilibili_dynamic/data_source.py @@ -7,6 +7,7 @@ from nonebot.adapters.onebot.v11 import GROUP_OWNER, GROUP_ADMIN from ATRI.service import Service from ATRI.rule import is_in_service +from ATRI.message import MessageBuilder from ATRI.utils import timestamp2datetime from ATRI.exceptions import BilibiliDynamicError @@ -14,12 +15,13 @@ from .db import DB from .api import API -_OUTPUT_FORMAT = """ -{up_nickname} 的{up_dy_type}更新了! -(限制 {limit_content} 字) -{up_dy_content} -链接: {up_dy_link} -""".strip() +_OUTPUT_FORMAT = ( + MessageBuilder("{up_nickname} 的{up_dy_type}更新了!") + .text("(限制 {limit_content} 字)") + .text("{up_dy_content}") + .text("链接: {up_dy_link}") + .done() +) class BilibiliDynamicSubscriptor(Service): diff --git a/ATRI/plugins/broadcast.py b/ATRI/plugins/broadcast.py index c1241f1..10bfa03 100644 --- a/ATRI/plugins/broadcast.py +++ b/ATRI/plugins/broadcast.py @@ -10,23 +10,22 @@ from nonebot.adapters.onebot.v11 import Bot, Message, MessageEvent from nonebot.adapters.onebot.v11 import GROUP_OWNER, GROUP_ADMIN from nonebot.adapters.onebot.v11 import GroupMessageEvent, PrivateMessageEvent - -from ATRI.service import Service from ATRI.rule import to_bot +from ATRI.service import Service +from ATRI.message import MessageBuilder BC_PATH = Path(".") / "data" / "plugins" / "broadcast" BC_PATH.mkdir(parents=True, exist_ok=True) - -_BROADCAST_BACK = """ -广播报告: -信息:{msg} -预计推送个数(群):{len_g} -成功:{su_g} -失败:{fl_g} -失败列表:{f_g} -""".strip() +_BROADCAST_REPO = ( + MessageBuilder("广播报告:") + .text("信息: {msg}") + .text("预计推送群:{len_g} 个") + .text("成功: {su_g} 失败: {fl_g}") + .text("失败群列表: {f_g}") + .done() +) class BroadCast(Service): @@ -72,7 +71,7 @@ async def _(bot: Bot, event: MessageEvent, s_msg: str = ArgPlainText("bc_msg")): await bot.send(event, "正在推送...(每个群延迟1~3s)") - w_msg = f" 来自维护者的信息:\n{s_msg}" + w_msg = Message(f"来自维护者的信息:\n{s_msg}") su_g = list() fl_g = list() @@ -86,12 +85,12 @@ async def _(bot: Bot, event: MessageEvent, s_msg: str = ArgPlainText("bc_msg")): await asyncio.sleep(random.randint(2, 3)) - repo_msg = _BROADCAST_BACK.format( + repo_msg = _BROADCAST_REPO.format( msg=s_msg, len_g=len(w_group), su_g=su_g, fl_g=fl_g, - f_g="、".join(map(str, fl_g)), + f_g=", ".join(map(str, fl_g)), ) await caster.finish(repo_msg) diff --git a/ATRI/plugins/console/__init__.py b/ATRI/plugins/console/__init__.py index 575ecd9..0764ab8 100644 --- a/ATRI/plugins/console/__init__.py +++ b/ATRI/plugins/console/__init__.py @@ -4,6 +4,7 @@ from nonebot.params import ArgPlainText from nonebot.adapters.onebot.v11 import PrivateMessageEvent, GroupMessageEvent from ATRI.config import BotSelfConfig +from ATRI.message import MessageBuilder from ATRI.exceptions import WriteFileError from .data_source import Console, CONSOLE_DIR @@ -34,11 +35,13 @@ async def _(event: PrivateMessageEvent, is_pub_n: str = ArgPlainText("is_pub_n") with open(data_path, "w", encoding="utf-8") as w: w.write(json.dumps(data)) - msg = f"""控制台信息已生成! - 请访问: {host}:{port} - Token: {token} - 该 token 有效时间为 15min - """.strip() + msg = ( + MessageBuilder("控制台信息已生成!") + .text(f"请访问: {host}:{port}") + .text(f"Token: {token}") + .text("该 token 有效时间为 15min") + ) + await gen_console_key.finish(msg) diff --git a/ATRI/plugins/repo.py b/ATRI/plugins/repo.py index 7e0c160..fe92a3f 100644 --- a/ATRI/plugins/repo.py +++ b/ATRI/plugins/repo.py @@ -7,15 +7,17 @@ from nonebot.adapters.onebot.v11.helpers import Cooldown from ATRI.service import Service from ATRI.config import BotSelfConfig +from ATRI.message import MessageBuilder _repo_flmt_notice = choice(["慢...慢一..点❤", "冷静1下", "歇会歇会~~"]) -REPO_FORMAT = """ -来自用户{user}反馈: -{msg} -""".strip() +_REPO_FORMAT = ( + MessageBuilder("来自用户{user}反馈:") + .text("{msg}") + .done() +) class Repo(Service): @@ -40,7 +42,7 @@ async def _deal_repo( repo_msg: str = ArgPlainText("repo"), ): user_id = event.get_user_id() - repo_0 = REPO_FORMAT.format(user=user_id, msg=repo_msg) + repo_0 = _REPO_FORMAT.format(user=user_id, msg=repo_msg) for superuser in BotSelfConfig.superusers: try: diff --git a/ATRI/plugins/twitter/data_source.py b/ATRI/plugins/twitter/data_source.py index c9168a7..18a8fb9 100644 --- a/ATRI/plugins/twitter/data_source.py +++ b/ATRI/plugins/twitter/data_source.py @@ -6,19 +6,21 @@ from nonebot.adapters.onebot.v11 import GROUP_OWNER, GROUP_ADMIN from ATRI import driver from ATRI.service import Service, ServiceTools from ATRI.rule import is_in_service -from ATRI.log import logger as log +from ATRI.log import log from ATRI.utils import request from ATRI.utils.apscheduler import scheduler +from ATRI.message import MessageBuilder from ATRI.exceptions import TwitterDynamicError from .db import DB from .api import API -_DYNAMIC_OUTPUT_FORMAT = """ -{t_nickname} 的推更新了! -{t_dy_content} -""".strip() +_DYNAMIC_OUTPUT_FORMAT = ( + MessageBuilder("{t_nickname} 的推更新了!") + .text("{t_dy_content}") + .done() +) class TwitterDynamicSubscriptor(Service): |