mirror of
https://github.com/ClovertaTheTrilobita/SanYeCao-Nonebot.git
synced 2026-04-01 22:04:51 +00:00
待办表结构调整
This commit is contained in:
parent
94c2f3d0e1
commit
b55b4f2715
7 changed files with 131 additions and 61 deletions
|
|
@ -32,7 +32,7 @@
|
|||
- [x] 天气
|
||||
- [x] 今日运势
|
||||
- [ ] 今日塔罗
|
||||
- [x] 点歌(网易云 需扫码登录 在 src\music 目录下)*PC端 QQ可能播放不出来 原因不明*
|
||||
- [x] 点歌(网易云 需扫码登录 在 src\music 目录下)*目前cookie过期检测有bug,出现播放不了需要吧cookie文件删掉*
|
||||
- [x] 图(返回图库中的图片)
|
||||
- [x] 摸摸头
|
||||
- [x] 接入语言模型
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ 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")
|
||||
user_id = fields.CharField(max_length=64, description="用户member_openid")
|
||||
content = fields.TextField()
|
||||
|
||||
class Meta:
|
||||
|
|
@ -18,17 +18,14 @@ class ToDoList(Model):
|
|||
获取对应用户的待办
|
||||
"""
|
||||
if not user_id:
|
||||
print("用户id为空")
|
||||
return None
|
||||
else:
|
||||
return (
|
||||
await cls.filter(user_id=user_id).order_by("id").values_list("id","user_id","content")
|
||||
)
|
||||
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:
|
||||
|
|
@ -37,10 +34,6 @@ class ToDoList(Model):
|
|||
@classmethod
|
||||
async def insert_todo_list(cls, user_id: str | None, content: str | None) -> bool:
|
||||
|
||||
is_user = await UserList.get_user_data(user_id)
|
||||
if not is_user:
|
||||
await UserList.insert_user(user_id)
|
||||
|
||||
if content.lstrip(" ") == "":
|
||||
return False
|
||||
data = {
|
||||
|
|
@ -64,29 +57,3 @@ class ToDoList(Model):
|
|||
del_id = todo_table[del_line_num - 1][0]
|
||||
await cls.filter(id=del_id).delete()
|
||||
return 0
|
||||
|
||||
|
||||
class UserList(Model):
|
||||
user_id = fields.CharField(max_length=100, description="用户member_openid")
|
||||
|
||||
class Meta:
|
||||
table = "user_list"
|
||||
table_description = "用户表"
|
||||
|
||||
@classmethod
|
||||
async def get_user_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("user_id")
|
||||
)
|
||||
|
||||
|
||||
@classmethod
|
||||
async def insert_user(cls, user_id: str | None):
|
||||
data = {
|
||||
"user_id": user_id
|
||||
}
|
||||
await cls.create(**data)
|
||||
|
||||
|
|
|
|||
111
src/my_sqlite/models/user.py
Normal file
111
src/my_sqlite/models/user.py
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
from datetime import datetime, timedelta
|
||||
import random
|
||||
from tortoise import fields
|
||||
from src.my_sqlite.data_init.db_connect import Model
|
||||
|
||||
class UserList(Model):
|
||||
user_id = fields.CharField(max_length=64, description="用户member_openid")
|
||||
group_id = fields.CharField(max_length=64, description="用户所在群聊id")
|
||||
last_used_time = fields.DateField(auto_now_add=True, description="用户最近一次使用时间")
|
||||
|
||||
class Meta:
|
||||
table = "user_list"
|
||||
table_description = "用户表"
|
||||
|
||||
|
||||
@classmethod
|
||||
async def get_user_id(cls, user_id: str | None, group_id: str | None, days: int = 3) -> str | None:
|
||||
"""
|
||||
获取指定天数内有活动的用户ID,并随机选择一个作为伴侣ID。
|
||||
|
||||
:param user_id: 用户ID
|
||||
:param group_id: 群组ID
|
||||
:param days: 最近活动天数,默认为3天
|
||||
:return: 随机选择的伴侣ID,如果没有符合条件的用户则返回None
|
||||
"""
|
||||
# 检查用户是否已有伴侣
|
||||
has_wife = await Wife.has_wife(user_id=user_id, group_id=group_id)
|
||||
if has_wife:
|
||||
return has_wife
|
||||
|
||||
# 获取最近指定天数内有活动的用户
|
||||
start_date = datetime.now().date() - timedelta(days=days)
|
||||
end_date = datetime.now().date()
|
||||
user_ids = await cls.filter(group_id=group_id, last_used_time__range=(start_date, end_date)) \
|
||||
.exclude(user_id=user_id).values_list("user_id", flat=True)
|
||||
|
||||
if user_ids:
|
||||
wife_id = random.choice(user_ids)
|
||||
await Wife.save_wife(user_id=user_id, group_id=group_id, wife_id=wife_id)
|
||||
return wife_id
|
||||
else:
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
async def insert_user(cls, user_id: str | None, group_id: str | None):
|
||||
"""
|
||||
插入用户数据
|
||||
:param user_id:
|
||||
:param group_id:
|
||||
:return:
|
||||
"""
|
||||
user_table = await cls.filter(user_id=user_id, group_id=group_id).first()
|
||||
if user_table:
|
||||
user_table.use_time = datetime.now().date()
|
||||
await user_table.save()
|
||||
else:
|
||||
await cls.create(user_id=user_id,group_id=group_id,last_used_time=datetime.now().date())
|
||||
|
||||
|
||||
class Wife(Model):
|
||||
"""
|
||||
群今日老婆记录表(包含群老婆和二次元老婆)
|
||||
"""
|
||||
id = fields.IntField(primary_key=True, generated=True, auto_increment=True)
|
||||
user_id = fields.CharField(max_length=64, description="用户id", null=True)
|
||||
group_id = fields.CharField(max_length=64, description="群聊id", null=True)
|
||||
wife_id = fields.CharField(max_length=64, description="对应群聊用户抽取到的id", null=True)
|
||||
wife_name = fields.CharField(max_length=64, description="名称", null=True)
|
||||
wife_description = fields.CharField(max_length=64, description="描述", null=True)
|
||||
create_time = fields.DateField(auto_now_add=True, description="创建时间", null=True)
|
||||
|
||||
class Meta:
|
||||
# 指定表名
|
||||
table = "wife"
|
||||
table_description = "群今日老婆记录表"
|
||||
|
||||
|
||||
|
||||
@classmethod
|
||||
async def has_wife(cls, user_id: str | None, group_id: str | None) -> str | None:
|
||||
"""
|
||||
查询是否有今日群老婆
|
||||
:param user_id:
|
||||
:param group_id:
|
||||
:return:
|
||||
"""
|
||||
|
||||
wife = await cls.filter(user_id=user_id, group_id=group_id, create_time=datetime.now().date()).first()
|
||||
if wife is None:
|
||||
return None
|
||||
else:
|
||||
return wife.wife_id
|
||||
|
||||
@classmethod
|
||||
async def save_wife(cls, user_id: str | None, group_id: str | None, wife_id: str | None):
|
||||
"""
|
||||
保存今日群老婆
|
||||
:param user_id:
|
||||
:param group_id:
|
||||
:param wife_id:
|
||||
:return:
|
||||
"""
|
||||
data = {
|
||||
"user_id":user_id,
|
||||
"group_id":group_id,
|
||||
"wife_id":wife_id,
|
||||
"create_time":datetime.now().date()
|
||||
}
|
||||
await cls.create(**data)
|
||||
|
||||
|
||||
|
|
@ -74,14 +74,14 @@ async def handle_function(message: MessageEvent):
|
|||
await t2.finish("您没有权限使用此功能。")
|
||||
value = message.get_plaintext().strip().split(" ")
|
||||
action, role_name = value[0], value[1]
|
||||
if len(value[1])>10:
|
||||
await t2.finish("角色名称过长,请重新输入")
|
||||
if action == "/删除人设":
|
||||
result = await ChatRole.delete_role(role_name)
|
||||
await t2.finish(result)
|
||||
if action == "/切换人设":
|
||||
result = await GroupChatRole.set_chat_role(group_openid,role_name)
|
||||
await t2.finish(result)
|
||||
if len(value[1])>10:
|
||||
await t2.finish("角色名称过长,请重新输入")
|
||||
if len(value) < 3:
|
||||
await t2.finish("请输入角色名称和设定,格式为:命令 角色名称 角色设定")
|
||||
role_setting = re.sub(r'[\n\\n\s"‘’]', '', ''.join(value[2:]))
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import yaml
|
||||
import random
|
||||
from pathlib import Path
|
||||
from nonebot import on_message
|
||||
from nonebot.rule import Rule, to_me
|
||||
from nonebot.plugin import on_command, on_keyword
|
||||
from nonebot.adapters.qq import Message, MessageEvent, MessageSegment
|
||||
from src.ai_chat import ai_chat
|
||||
from src.my_sqlite.models.chat import GroupChatRole
|
||||
from src.my_sqlite.models.user import UserList
|
||||
|
||||
with open(os.getcwd() + '/src/ai_chat/config/chat_ai.yaml', 'r', encoding='utf-8') as f1:
|
||||
chat = yaml.load(f1, Loader=yaml.FullLoader).get('chat_ai')
|
||||
|
|
@ -21,6 +21,8 @@ menu = ['/今日运势','/图','/点歌','/摸摸头','/群老婆','/今日老
|
|||
|
||||
async def check_value_in_menu(message: MessageEvent) -> bool:
|
||||
value = message.get_plaintext().strip().split(" ")
|
||||
#缓存用户id
|
||||
await UserList.insert_user(message.author.id,message.group_openid)
|
||||
if value[0] in menu:
|
||||
return False
|
||||
else:
|
||||
|
|
@ -58,10 +60,6 @@ test = on_command("test", rule=to_me(), priority=10, block=True)
|
|||
async def bot_on_ready():
|
||||
await test.finish("\nBoost & Magnum, ready fight!!!")
|
||||
|
||||
nai_loong = on_keyword({"奶龙"}, rule=to_me(), priority=1, block=True)
|
||||
@nai_loong.handle()
|
||||
async def not_nai_loong():
|
||||
await nai_loong.finish(message=Message(random.choice(text_list_nailoong)))
|
||||
|
||||
text_list_nailoong = [
|
||||
"我是?你是?😨",
|
||||
|
|
|
|||
|
|
@ -13,19 +13,12 @@ async def show_todo_list(message: MessageEvent):
|
|||
:return:
|
||||
"""
|
||||
member_openid = message.get_user_id()
|
||||
|
||||
# result = get_user_todo_list(member_openid)
|
||||
result = await ToDoList.get_todo_list(member_openid)
|
||||
if result is False:
|
||||
if not result:
|
||||
await get_todo_list.finish("\n您还未创建待办\n快使用 /新建待办 创建一份吧")
|
||||
|
||||
todo_list = ""
|
||||
count = 0
|
||||
for each_content in result:
|
||||
count += 1
|
||||
todo_content = "\n" + str(count) + "." + str(each_content).lstrip("('").rstrip("',)") + "\n"
|
||||
todo_list = "".join([todo_list, todo_content])
|
||||
await get_todo_list.finish("您的待办有如下哦:⭐\n" + todo_list)
|
||||
todo_list = "\n\n".join([f"{i + 1}. {content}" for i, content in enumerate(result)])
|
||||
await get_todo_list.finish(f"您的待办有如下哦:⭐\n\n{todo_list}")
|
||||
|
||||
|
||||
insert_todo = on_command("新建待办", rule=to_me(), priority=10, block=True)
|
||||
|
|
@ -33,7 +26,6 @@ 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)
|
||||
success = await ToDoList.insert_todo_list(member_openid, content)
|
||||
if success:
|
||||
await insert_todo.finish("成功添加待办,今后也要加油哦(ง •_•)ง")
|
||||
|
|
@ -48,9 +40,8 @@ async def del_todo(message: MessageEvent):
|
|||
del_line_str = message.get_plaintext().replace("/删除待办", "").strip(" ")
|
||||
try:
|
||||
del_line_num = int(del_line_str)
|
||||
except:
|
||||
except BaseException:
|
||||
await delete_todo.finish("请检查您的输入是否正确。\n请输入 /删除待办+数字 哦。")
|
||||
# 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("您还未创建过待办哦。")
|
||||
|
|
|
|||
|
|
@ -4,18 +4,21 @@ from nonebot.adapters.qq import MessageSegment
|
|||
from nonebot.plugin import on_command
|
||||
from nonebot.rule import to_me
|
||||
from src.image.get_image import download_qq_image,qq_image_delete
|
||||
from src.my_sqlite.models.user import UserList
|
||||
|
||||
today_group_wife = on_command("群老婆", rule=to_me(), priority=10, block=True)
|
||||
@today_group_wife.handle()
|
||||
async def handle_function(message: MessageEvent):
|
||||
member_openid = message.get_user_id()
|
||||
|
||||
local_image_path = download_qq_image(member_openid)
|
||||
user_id = await UserList.get_user_id(member_openid,message.group_id)
|
||||
if user_id is None:
|
||||
await today_group_wife.finish("潜在老婆太少了,快请群友多多使用吧")
|
||||
local_image_path = download_qq_image(user_id)
|
||||
msg = Message([
|
||||
MessageSegment.text("您的今日群老婆"),
|
||||
MessageSegment.file_image(Path(local_image_path)),
|
||||
MessageSegment.text("@" + member_openid),
|
||||
])
|
||||
|
||||
qq_image_delete()
|
||||
await today_group_wife.finish(msg)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue