diff --git a/konabot/common/path.py b/konabot/common/path.py index 6bea695..e81760d 100644 --- a/konabot/common/path.py +++ b/konabot/common/path.py @@ -12,3 +12,10 @@ DOCS_PATH_MAN1 = DOCS_PATH / "user" DOCS_PATH_MAN3 = DOCS_PATH / "lib" DOCS_PATH_MAN7 = DOCS_PATH / "concepts" DOCS_PATH_MAN8 = DOCS_PATH / "sys" + +if not DATA_PATH.exists(): + DATA_PATH.mkdir() + +if not LOG_PATH.exists(): + LOG_PATH.mkdir() + diff --git a/konabot/plugins/poll/__init__.py b/konabot/plugins/poll/__init__.py index 266a1c6..8885d2a 100644 --- a/konabot/plugins/poll/__init__.py +++ b/konabot/plugins/poll/__init__.py @@ -1,16 +1,19 @@ import json, time -from nonebot_plugin_alconna import (Alconna, Args, Field, MultiVar, UniMessage, - on_alconna) -from nonebot_plugin_alconna.uniseg import UniMsg, At, Reply +from nonebot_plugin_alconna import Alconna, Args, Field, MultiVar, on_alconna from nonebot.adapters.onebot.v11 import Event -poll_json_path = "assets/json/poll.json" +from konabot.common.path import ASSETS_PATH, DATA_PATH -poll_file = open(poll_json_path,"r",encoding="utf-8") -poll_list_raw = poll_file.read() -poll_file.close() -poll_list = json.loads(poll_list_raw)['poll'] + +POLL_TEMPLATE_FILE = ASSETS_PATH / "json" / "poll.json" +POLL_DATA_FILE = DATA_PATH / "poll.json" + +if not POLL_DATA_FILE.exists(): + POLL_DATA_FILE.write_bytes(POLL_TEMPLATE_FILE.read_bytes()) + + +poll_list = json.loads(POLL_DATA_FILE.read_text())['poll'] async def createpoll(title,qqid,options): polllength = len(poll_list) @@ -44,8 +47,11 @@ def getpolldata(pollid_or_title): return [thepoll,polnum] def writeback(): - file = open(poll_json_path,"w",encoding="utf-8") - json.dump({'poll':poll_list},file,ensure_ascii=False,sort_keys=True) + # file = open(poll_json_path,"w",encoding="utf-8") + # json.dump({'poll':poll_list},file,ensure_ascii=False,sort_keys=True) + POLL_DATA_FILE.write_text(json.dumps({ + 'poll': poll_list, + }, ensure_ascii=False, sort_keys=True)) async def pollvote(polnum,optionnum,qqnum): optiond = poll_list[polnum]["polldata"] @@ -157,4 +163,4 @@ async def _(saying: list, event: Event): # 写入项目 else: await pollvote(polnum,optionnum,event.get_user_id()) - await viewpoll.send("投票成功!你投给了 "+saying[1]) \ No newline at end of file + await viewpoll.send("投票成功!你投给了 "+saying[1])