This commit is contained in:
EillesWan 2024-10-22 00:28:52 +08:00
parent 7243dfc527
commit a80df00960
3 changed files with 27 additions and 30 deletions

View File

@ -30,7 +30,7 @@ jieba>=0.42.1
python-dotenv>=1.0.1 python-dotenv>=1.0.1
nonebot_plugin_session nonebot_plugin_session
pypinyin pypinyin
Musicreater>=2.2.0 Musicreater>=2.2.2
librosa==0.10.1 librosa==0.10.1
TrimMCStruct TrimMCStruct
brotli brotli

View File

@ -43,7 +43,7 @@ from Musicreater.plugin.bdx import (
) )
from Musicreater.plugin.archive import compress_zipfile from Musicreater.plugin.archive import compress_zipfile
from src.utils.io import read_file from src.utils.io import read_file, write_file
from src.utils.base.language import get_user_lang from src.utils.base.language import get_user_lang
from src.utils.base.ly_typing import T_Bot, T_MessageEvent from src.utils.base.ly_typing import T_Bot, T_MessageEvent
from src.utils.message.html_tool import md_to_pic from src.utils.message.html_tool import md_to_pic
@ -250,17 +250,18 @@ async def read_file_into_command_lines(
return functionList return functionList
write_2_file = on_alconna( write_2_file = nonebot.on_command(
Alconna( # Alconna(
"写入文本文件", # "写入文本文件",
Option( # Option(
"-n|-f|--file-name", # "-n|-f|--file-name",
default="新建文本文档", # default="新建文本文档",
args=Args["file-name", str, "新建文本文档"], # args=Args["file-name", str, "新建文本文档"],
), # ),
Option("-a|--append", default=False, action=store_true), # Option("-a|--append", default=False, action=store_true),
), # ),
aliases=( "写入文本文件",
aliases={
"write file", "write file",
"write down", "write down",
"写入", "写入",
@ -269,7 +270,7 @@ write_2_file = on_alconna(
"write2file", "write2file",
"写入文件", "写入文件",
"写入文本", "写入文本",
), },
rule=to_me(), rule=to_me(),
# ignorecase=True, # ignorecase=True,
) )
@ -277,12 +278,12 @@ write_2_file = on_alconna(
@write_2_file.handle() @write_2_file.handle()
async def _( async def _(
result: Arparma, # result: Arparma,
event: T_MessageEvent, event: T_MessageEvent,
bot: T_Bot, bot: T_Bot,
): ):
nonebot.logger.info(result.options) # nonebot.logger.info(result.options)
usr_id = event.get_user_id() usr_id = event.get_user_id()
ulang = get_user_lang(usr_id) ulang = get_user_lang(usr_id)
@ -291,19 +292,16 @@ async def _(
if len(whole_texts) < 2: if len(whole_texts) < 2:
await write_2_file.finish(ulang.get("writefile.no_text")) await write_2_file.finish(ulang.get("writefile.no_text"))
file_2_write = ( cmd_arg = whole_texts[0].split(" ",2)
result.options["file-name"].args["file-name"]
if result.options["file-name"].args file_2_write = (cmd_arg[1]+ ".txt") if len(cmd_arg) > 1 else "新建文本文档.txt"
else "新建文本文档"
) + ".txt"
file_path = get_stored_path(usr_id, file_2_write, superuser=False) file_path = get_stored_path(usr_id, file_2_write, superuser=False)
if result.options["append"].value: if "-a" in whole_texts[0]:
if file_2_write in filesaves[usr_id].keys(): if file_2_write in filesaves[usr_id].keys():
with file_path.open(mode="a", encoding="utf-8") as f: await write_file(file_path,content=whole_texts[1], mode="a", encoding="utf-8")
f.write(whole_texts[1])
file_size = os.path.getsize(file_path) file_size = os.path.getsize(file_path)
filesaves[usr_id]["totalSize"] += ( filesaves[usr_id]["totalSize"] += (
file_size - filesaves[usr_id][file_2_write]["size"] file_size - filesaves[usr_id][file_2_write]["size"]
@ -320,8 +318,7 @@ async def _(
else: else:
await write_2_file.finish(ulang.get("writefile.file_not_exist")) await write_2_file.finish(ulang.get("writefile.file_not_exist"))
else: else:
with open(file_path, "w", encoding="utf-8") as f: await write_file(file_path,content=whole_texts[1], mode="w", encoding="utf-8")
f.write(whole_texts[1])
now = zhDateTime.DateTime.now() now = zhDateTime.DateTime.now()
file_size = os.path.getsize(file_path) file_size = os.path.getsize(file_path)
try: try:

View File

@ -1,8 +1,8 @@
import aiofiles import aiofiles
from pathlib import Path
async def write_file( async def write_file(
file_path: str, file_path: str | Path,
content: str | bytes, content: str | bytes,
mode: str = "w", mode: str = "w",
**kws, **kws,
@ -18,7 +18,7 @@ async def write_file(
await f.write(content) await f.write(content)
async def read_file(file_path: str, mode: str = "r", **kws) -> str: async def read_file(file_path: str | Path, mode: str = "r", **kws) -> str:
""" """
读取文件 读取文件
Args: Args: