Skip to content

Commit 296a8e9

Browse files
committed
部分优化,提升使用时的舒适度
1 parent 448aac6 commit 296a8e9

File tree

2 files changed

+141
-89
lines changed

2 files changed

+141
-89
lines changed

nonebot_plugin_SimpleToWrite/__init__.py

Lines changed: 140 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ast
2+
import os
23
import random
34
import re
45
from nonebot.adapters.onebot.v11 import MessageSegment,GroupMessageEvent,Event, PokeNotifyEvent, NotifyEvent, PrivateMessageEvent, PrivateMessageEvent, GroupIncreaseNoticeEvent, GroupRequestEvent
@@ -35,6 +36,28 @@
3536
f"<yellow>欢迎使用</yellow> <green>简易词库</green> <yellow>插件</yellow> <red>加入开发或反馈bug既可以提issue,也可以联系我</red> QQ:<blue>3791398858</blue>"
3637
)
3738

39+
if os.path.exists("dicpro.txt"):
40+
logger.opt(colors=True).success(
41+
f"<green>dicpro.txt加载成功</green>"
42+
)
43+
else:
44+
logger.opt(colors=True).success(
45+
f"<green>dicpro.txt已自动创建</green>"
46+
)
47+
with open("dicpro.txt", "w", encoding="utf-8") as f:
48+
f.write("")
49+
50+
if os.path.exists("help.txt"):
51+
logger.opt(colors=True).success(
52+
f"<green>help.txt加载成功</green>"
53+
)
54+
else:
55+
logger.opt(colors=True).success(
56+
f"<green>help.txt已自动创建</green>"
57+
)
58+
with open("help.txt", "w", encoding="utf-8") as f:
59+
f.write("")
60+
3861
Event_T = TypeVar("Event_T", bound=Type[Event])
3962

4063

@@ -603,9 +626,16 @@ async def my_function(func, args, event, data):
603626
pass
604627

605628
def parse_file(file_path):
606-
with open(file_path, 'r', encoding='utf-8') as f:
607-
content = f.read()
608-
return content
629+
if os.path.exists(file_path):
630+
with open(file_path, 'r', encoding='utf-8') as f:
631+
content = f.read()
632+
return content
633+
else:
634+
logger.opt(colors=True).error(
635+
f"<yellow>错误!</yellow> <blue>{file_path}</blue><red> 文件不存在</red>"
636+
)
637+
return None
638+
609639
async def parse_string(s,event,data):
610640
"""
611641
用于解析文件,将文件解析成json\n
@@ -671,111 +701,133 @@ async def _(event: GroupMessageEvent):
671701
qua = str(event.get_message()).strip().replace('\n','\\n')
672702
file_path = 'dicpro.txt'
673703
s = parse_file(file_path)
674-
s = s.replace('$', '&#36;')
675-
result = parse_string(s,event,qua)
676-
for key in result:
677-
match_data = re.search(rf'^{key}$', qua)
678-
if match_data and key not in event_rule:
679-
result = result[key]
680-
data = match_data.groups()
681-
ans = ""
682-
if len(result) != 0:
683-
for i in range(len(result)):
684-
key = result[i]
685-
match = re.search(r'^&#36;(\w+)\s+(.+)&#36;$', key)
686-
if match:
687-
func_name, param = match.groups()
688-
getanstype = await my_function(func_name, param, event,data)
689-
if getanstype:
690-
ans += getanstype
691-
if ans != "":
692-
await grouprequest.send(ans)
693-
else:
694-
pass
704+
if s:
705+
s = s.replace('$', '&#36;')
706+
result = parse_string(s,event,qua)
707+
for key in result:
708+
match_data = re.search(rf'^{key}$', qua)
709+
if match_data and key not in event_rule:
710+
result = result[key]
711+
data = match_data.groups()
712+
ans = ""
713+
if len(result) != 0:
714+
for i in range(len(result)):
715+
key = result[i]
716+
match = re.search(r'^&#36;(\w+)\s+(.+)&#36;$', key)
717+
if match:
718+
func_name, param = match.groups()
719+
getanstype = await my_function(func_name, param, event,data)
720+
if getanstype:
721+
ans += getanstype
722+
if ans != "":
723+
await grouprequest.send(ans)
724+
else:
725+
pass
726+
else:
727+
logger.opt(colors=True).error(
728+
f"<yellow>错误!</yellow> <blue>{file_path}</blue><red> 文件不存在</red>"
729+
)
695730

696731
@privaterequest.handle()
697732
async def _(event: PrivateMessageEvent):
698733
qua = str(event.get_message()).strip()
699734
file_path = 'dicpro.txt'
700735
s = parse_file(file_path)
701-
s = s.replace('$', '&#36;')
702-
result = parse_string(s,event,qua)
703-
for key in result:
704-
match_data = re.search(rf'^{key}$', qua)
705-
if match_data and key not in event_rule:
706-
result = result[key]
707-
data = match_data.groups()
736+
if s:
737+
s = s.replace('$', '&#36;')
738+
result = parse_string(s,event,qua)
739+
for key in result:
740+
match_data = re.search(rf'^{key}$', qua)
741+
if match_data and key not in event_rule:
742+
result = result[key]
743+
data = match_data.groups()
744+
ans = ""
745+
if len(result) != 0:
746+
for i in range(len(result)):
747+
key = result[i]
748+
match = re.search(r'^&#36;(\w+)\s+(.+)&#36;$', key)
749+
if match:
750+
func_name, param = match.groups()
751+
getanstype = await my_function(func_name, param, event,data)
752+
if getanstype:
753+
ans += getanstype
754+
if ans != "":
755+
await privatemassagefix(event, ans)
756+
else:
757+
pass
758+
else:
759+
logger.opt(colors=True).error(
760+
f"<yellow>错误!</yellow> <blue>{file_path}</blue><red> 文件不存在</red>"
761+
)
762+
763+
@pokeevent.handle()
764+
async def _(event: NotifyEvent):
765+
file_path = 'dicpro.txt'
766+
s = parse_file(file_path)
767+
if s:
768+
s = s.replace('$', '&#36;')
769+
result = await parse_string(s,event,data=None)
770+
if "[戳一戳]" in result:
771+
result = result["[戳一戳]"]
708772
ans = ""
709773
if len(result) != 0:
710774
for i in range(len(result)):
711775
key = result[i]
712776
match = re.search(r'^&#36;(\w+)\s+(.+)&#36;$', key)
777+
data = ""
713778
if match:
714779
func_name, param = match.groups()
715-
getanstype = await my_function(func_name, param, event,data)
716-
if getanstype:
717-
ans += getanstype
718-
if ans != "":
719-
await privatemassagefix(event, ans)
720-
else:
721-
pass
722-
723-
@pokeevent.handle()
724-
async def _(event: NotifyEvent):
725-
file_path = 'dicpro.txt'
726-
s = parse_file(file_path)
727-
s = s.replace('$', '&#36;')
728-
result = await parse_string(s,event,data=None)
729-
if "[戳一戳]" in result:
730-
result = result["[戳一戳]"]
731-
ans = ""
732-
if len(result) != 0:
733-
for i in range(len(result)):
734-
key = result[i]
735-
match = re.search(r'^&#36;(\w+)\s+(.+)&#36;$', key)
736-
data = ""
737-
if match:
738-
func_name, param = match.groups()
739-
ans += await my_function(func_name, param, event,data)
740-
await pokeevent.send(ans)
780+
ans += await my_function(func_name, param, event,data)
781+
await pokeevent.send(ans)
782+
else:
783+
logger.opt(colors=True).error(
784+
f"<yellow>错误!</yellow> <blue>{file_path}</blue><red> 文件不存在</red>"
785+
)
741786

742787
@groupaddevent.handle()
743788
async def _(event: GroupIncreaseNoticeEvent):
744789
file_path = 'dicpro.txt'
745790
s = parse_file(file_path)
746-
s = s.replace('$', '&#36;')
747-
748-
result = await parse_string(s,event,data=None)
749-
if "[入群通知]" in result:
750-
result = result["[入群通知]"]
751-
ans = ""
752-
if len(result) != 0:
753-
for i in range(len(result)):
754-
key = result[i]
755-
match = re.search(r'^&#36;(\w+)\s+(.+)&#36;$', key)
756-
data = ""
757-
if match:
758-
func_name, param = match.groups()
759-
ans += await my_function(func_name, param, event,data)
760-
await pokeevent.send(ans)
791+
if s:
792+
s = s.replace('$', '&#36;')
793+
result = await parse_string(s,event,data=None)
794+
if "[入群通知]" in result:
795+
result = result["[入群通知]"]
796+
ans = ""
797+
if len(result) != 0:
798+
for i in range(len(result)):
799+
key = result[i]
800+
match = re.search(r'^&#36;(\w+)\s+(.+)&#36;$', key)
801+
data = ""
802+
if match:
803+
func_name, param = match.groups()
804+
ans += await my_function(func_name, param, event,data)
805+
await pokeevent.send(ans)
806+
else:
807+
logger.opt(colors=True).error(
808+
f"<yellow>错误!</yellow> <blue>{file_path}</blue><red> 文件不存在</red>"
809+
)
761810

762811
@groupwantaddevent.handle()
763812
async def _(event: GroupRequestEvent):
764813
file_path = 'dicpro.txt'
765-
766814
s = parse_file(file_path)
767-
s = s.replace('$', '&#36;')
768-
769-
result = await parse_string(s,event,data=None)
770-
if "[加群请求]" in result:
771-
result = result["[加群请求]"]
772-
ans = ""
773-
if len(result) != 0:
774-
for i in range(len(result)):
775-
key = result[i]
776-
match = re.search(r'^&#36;(\w+)\s+(.+)&#36;$', key)
777-
data = ""
778-
if match:
779-
func_name, param = match.groups()
780-
ans += await my_function(func_name, param, event,data)
781-
await groupwantaddevent.send(ans)
815+
if s:
816+
s = s.replace('$', '&#36;')
817+
result = await parse_string(s,event,data=None)
818+
if "[加群请求]" in result:
819+
result = result["[加群请求]"]
820+
ans = ""
821+
if len(result) != 0:
822+
for i in range(len(result)):
823+
key = result[i]
824+
match = re.search(r'^&#36;(\w+)\s+(.+)&#36;$', key)
825+
data = ""
826+
if match:
827+
func_name, param = match.groups()
828+
ans += await my_function(func_name, param, event,data)
829+
await groupwantaddevent.send(ans)
830+
else:
831+
logger.opt(colors=True).error(
832+
f"<yellow>错误!</yellow> <blue>{file_path}</blue><red> 文件不存在</red>"
833+
)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "nonebot_plugin_SimpleToWrite"
3-
version = "0.1.11"
3+
version = "0.1.12"
44
description = "为0编程基础的小白提供便捷的功能编写"
55
authors = [
66
{ name="STESmly", email="STESmly@mail.com" },

0 commit comments

Comments
 (0)