app/liteyuki/session/models.py

148 lines
4.1 KiB
Python
Raw Permalink Normal View History

2024-10-23 01:04:42 +08:00
"""
本模块使用了[nonebot-plugin-uninfo](https://github.com/RF-Tar-Railt/nonebot-plugin-uninfo)的部分模型定义
MIT License
Copyright (c) 2024 RF-Tar-Railt
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from datetime import datetime
from enum import Enum
from typing import Any
2024-10-19 21:10:18 +08:00
from pydantic import BaseModel
2024-10-23 01:04:42 +08:00
class SceneType(int, Enum):
PRIVATE = 0
"""私聊场景"""
GROUP = 1
"""群聊场景"""
GUILD = 2
"""频道场景"""
CHANNEL_TEXT = 3
"""子频道文本场景"""
CHANNEL_CATEGORY = 4
"""频道分类场景"""
CHANNEL_VOICE = 5
"""子频道语音场景"""
2024-10-19 21:10:18 +08:00
class User(BaseModel):
"""
用户信息
Attributes:
id: 用户ID
name: 用户名
nick: 用户昵称
avatar: 用户头像图链接
"""
2024-10-23 01:04:42 +08:00
2024-10-19 21:10:18 +08:00
id: str
2024-10-23 01:04:42 +08:00
name: str | None = None
nick: str | None = None
avatar: str | None = None
gender: str | None = None
2024-10-19 21:10:18 +08:00
class Scene(BaseModel):
"""
场景信息
Attributes:
id: 场景ID
type: 场景类型
name: 场景名
avatar: 场景头像图链接
parent: 父场景
"""
2024-10-23 01:04:42 +08:00
2024-10-19 21:10:18 +08:00
id: str
2024-10-23 01:04:42 +08:00
type: SceneType
name: str | None = None
avatar: str | None = None
parent: "Scene | None" = None
class Role(BaseModel):
id: str
level: int | None = None
name: str | None = None
class Member(BaseModel):
user: User
nickname: str | None = None
role: Role | None = None
mute: bool | None = None
joined_at: datetime | None = None
2024-10-19 21:10:18 +08:00
class Session(BaseModel):
"""
会话信息
Attributes:
self_id: 机器人ID
adapter: 适配器ID
scope: 会话范围
scene: 场景信息
user: 用户信息
member: 成员信息仅频道及群聊有效
operator: 操作者信息仅频道及群聊有效
2024-10-23 01:04:42 +08:00
session_id: 会话ID精确到会话用于快速标识会话通常为{适配器范围}:{群聊ID(公共)/用户ID(群聊)}
target_id: 目标ID精确到用户用于快速标识会话通常为{适配器范围}:{群聊ID}:{用户ID}(仅公共) {适配器范围}:{用户ID}(仅私聊)
2024-10-19 21:10:18 +08:00
"""
2024-10-23 01:04:42 +08:00
2024-10-19 21:10:18 +08:00
self_id: str
adapter: str
scope: str
scene: Scene
user: User
2024-10-23 01:04:42 +08:00
member: "Member | None" = None
operator: "Member | None" = None
@property
def session_id(self):
if self.scope == SceneType.PRIVATE:
return f"{self.scope}:{self.user.id}"
elif self.scope in (
SceneType.GROUP,
SceneType.GUILD,
SceneType.CHANNEL_TEXT,
SceneType.CHANNEL_VOICE,
SceneType.CHANNEL_CATEGORY,
):
return f"{self.scope}:{self.scene.id}"
else:
raise ValueError("Invalid SceneType")
@property
def target_id(self):
if self.scope == SceneType.PRIVATE:
return f"{self.scope}:{self.user.id}"
elif self.scope in (
SceneType.GROUP,
SceneType.GUILD,
SceneType.CHANNEL_TEXT,
SceneType.CHANNEL_VOICE,
SceneType.CHANNEL_CATEGORY,
):
return f"{self.scope}:{self.scene.id}:{self.user.id}"