mirror of
https://github.com/ClovertaTheTrilobita/SanYeCao-Nonebot.git
synced 2026-04-01 22:04:51 +00:00
将待办编辑从sqlalchemy更新为Tortoise
This commit is contained in:
parent
72ff4e54ac
commit
5fe045a063
4 changed files with 70 additions and 7 deletions
|
|
@ -23,10 +23,9 @@ class Model(Model_):
|
|||
async def init():
|
||||
"""
|
||||
初始化数据库
|
||||
:return:
|
||||
"""
|
||||
await Tortoise.init(
|
||||
db_url="sqlite://chat.db",
|
||||
db_url="sqlite://chat_bot.db",
|
||||
modules={"models": MODELS},
|
||||
timezone="Asia/Shanghai",
|
||||
)
|
||||
|
|
|
|||
60
src/my_sqlite/models/to_do.py
Normal file
60
src/my_sqlite/models/to_do.py
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
from tortoise import fields
|
||||
from typing_extensions import Self
|
||||
from src.my_sqlite.data_init.db_connect import Model
|
||||
|
||||
|
||||
class ToDoList(Model):
|
||||
id = fields.IntField(primary_key=True, generated=True, auto_increment=True)
|
||||
user_id = fields.CharField(max_length=100,description="用户member_openid")
|
||||
content = fields.TextField()
|
||||
|
||||
class Meta:
|
||||
table = "user_todo_list"
|
||||
table_description = "用户待办表"
|
||||
|
||||
@classmethod
|
||||
async def _get_data(cls, user_id: str | None) -> list | None:
|
||||
"""
|
||||
获取对应用户的待办
|
||||
"""
|
||||
if not user_id:
|
||||
print("用户id为空")
|
||||
else:
|
||||
return (
|
||||
await cls.filter(user_id=user_id).order_by("id").values_list("id","user_id","content")
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def get_todo_list(cls, user_id: str | None) -> Self | None:
|
||||
todo_table = await cls._get_data(user_id)
|
||||
todo_list = [row[2] for row in todo_table]
|
||||
# print(todo_list)
|
||||
if todo_list:
|
||||
return todo_list
|
||||
else:
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def insert_todo_list(cls, user_id: str | None, content: str | None):
|
||||
data = {
|
||||
"user_id": user_id,
|
||||
"content": content
|
||||
}
|
||||
await cls.create(**data)
|
||||
|
||||
@classmethod
|
||||
async def delete_user_todo(cls, user_id: str | None, del_line_num: int | None) -> int:
|
||||
todo_table = await cls._get_data(user_id)
|
||||
|
||||
if todo_table is None:
|
||||
return -1
|
||||
|
||||
max_length = len(todo_table)
|
||||
if del_line_num > max_length or del_line_num < 1:
|
||||
return 1
|
||||
|
||||
del_id = todo_table[del_line_num - 1][0]
|
||||
await cls.filter(id=del_id).delete()
|
||||
return 0
|
||||
|
||||
|
||||
|
|
@ -3,6 +3,7 @@ from nonebot.plugin import on_command
|
|||
from nonebot.rule import to_me
|
||||
|
||||
from src.my_sqlite.todo_by_sqlite import *
|
||||
from src.my_sqlite.models.to_do import ToDoList
|
||||
|
||||
get_todo_list = on_command("待办查询", rule=to_me(), priority=10, block=True, aliases={"代办", "daiban"})
|
||||
@get_todo_list.handle()
|
||||
|
|
@ -15,7 +16,8 @@ async def show_todo_list(message: MessageEvent):
|
|||
"""
|
||||
member_openid = message.get_user_id()
|
||||
|
||||
result = get_user_todo_list(member_openid)
|
||||
# result = get_user_todo_list(member_openid)
|
||||
result = await ToDoList.get_todo_list(member_openid)
|
||||
if result is False:
|
||||
await get_todo_list.finish("\n您还未创建待办\n快使用 /新建待办 创建一份吧")
|
||||
|
||||
|
|
@ -33,7 +35,8 @@ insert_todo = on_command("新建待办", rule=to_me(), priority=10, block=True)
|
|||
async def insert_todo_list(message: MessageEvent):
|
||||
member_openid = message.get_user_id()
|
||||
content = message.get_plaintext().replace("/新建待办", "").strip(" ")
|
||||
insert_user_todo_list(member_openid, content)
|
||||
# insert_user_todo_list(member_openid, content)
|
||||
await ToDoList.insert_todo_list(member_openid, content)
|
||||
await insert_todo.finish("成功添加待办,今后也要加油哦(ง •_•)ง")
|
||||
|
||||
|
||||
|
|
@ -46,7 +49,8 @@ async def del_todo(message: MessageEvent):
|
|||
del_line_num = int(del_line_str)
|
||||
except:
|
||||
await delete_todo.finish("请检查您的输入是否正确。\n请输入 /删除待办+数字 哦。")
|
||||
result = delete_user_todo(member_openid, int(del_line_num))
|
||||
# result = delete_user_todo(member_openid, int(del_line_num))
|
||||
result = await ToDoList.delete_user_todo(member_openid, del_line_num)
|
||||
if result == -1:
|
||||
await delete_todo.finish("您还未创建过待办哦。")
|
||||
elif result == 0:
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ from nonebot.plugin import on_command
|
|||
from nonebot.adapters import Message
|
||||
from nonebot.params import CommandArg
|
||||
from nonebot.adapters.qq import MessageEvent
|
||||
|
||||
import datetime
|
||||
|
||||
weather = on_command("天气", rule=to_me(), aliases={"weather", "查天气"}, priority=10, block=True)
|
||||
|
||||
|
|
@ -57,7 +57,7 @@ def format_weather(location):
|
|||
realtime_weather = weather_data['realtime']
|
||||
result = f"实时天气:" + "\n" + f"{realtime_weather['info']}, 温度: {realtime_weather['temperature']}℃, 湿度: {realtime_weather['humidity']}%, 风向: {realtime_weather['direct']}, 风力: {realtime_weather['power']}级, AQI: {realtime_weather['aqi']}"
|
||||
# 未来几天的天气
|
||||
result = result + "\n" + "未来几天的天气:"
|
||||
result = result + "\n" + "未来几天的天气:🌤⛈️☔️"
|
||||
for day in weather_data['future']:
|
||||
result = result + "\n" + f"日期: {day['date']}, 天气: {day['weather']}, 温度: {day['temperature']}, 风向: {day['direct']}"
|
||||
return result
|
||||
|
|
|
|||
Loading…
Reference in a new issue