适用于 Nonebot2 的聊天记录插件
Project description
nonebot-plugin-chatrecorder
适用于 Nonebot2 的聊天记录插件。
将聊天消息存至数据库中,方便其他插件使用。
安装
- 使用 nb-cli
nb plugin install nonebot_plugin_chatrecorder
- 使用 pip
pip install nonebot_plugin_chatrecorder
配置项
以下配置项可在
.env.*
文件中设置,具体参考 NoneBot 配置方式
chatrecorder_record_send_msg
- 类型:
bool
- 默认:
True
- 说明:是否记录机器人自己发出的消息
chatrecorder_record_migration_bot_id
- 类型:
Optional[str]
- 默认:
None
- 说明:在旧版本(0.1.x) 时使用的机器人账号(机器人qq号),用于数据库迁移;若使用过此插件的旧版本则必须配置,数据库迁移完成后可删除;未使用过旧版本可不配置
其他说明
插件依赖 nonebot-plugin-datastore 插件来提供数据库支持
nonebot-plugin-datastore
插件默认使用 SQLite 数据库,
消息记录文件会存放在 nonebot-plugin-datastore
插件设置的数据目录
由于在 OneBot V11 适配器中,机器人发送的消息中可能存在 base64 形式的图片、语音等,
为避免消息记录文件体积过大,本插件会将 base64 形式的图片、语音等存成文件,并在消息记录中以文件路径替代。
这些文件会放置在 nonebot-plugin-datastore
插件设置的缓存目录,建议定期清理
使用
其他插件可使用本插件提供的接口获取消息记录
先在插件代码最前面声明依赖:
from nonebot import require
require("nonebot_plugin_chatrecorder")
使用示例:
- 获取当前群内成员 "12345" 和 "54321" 1天之内的消息记录
from nonebot.adapters.onebot.v11 import GroupMessageEvent
from nonebot_plugin_chatrecorder import get_message_records
@matcher.handle()
async def _(event: GroupMessageEvent):
records = await get_message_records(
user_ids=["12345", "54321"],
group_ids=[str(event.group_id)],
time_start=datetime.utcnow() - timedelta(days=1),
)
- 获取所有 OneBot V11 适配器形式的消息
from nonebot.adapters.onebot.v11 import Bot
from nonebot_plugin_chatrecorder import get_messages
@matcher.handle()
async def _(bot: Bot):
msgs = await get_messages(bot)
- 获取本群除机器人发出的消息外,其他消息的纯本文形式
from nonebot.adapters.onebot.v11 import GroupMessageEvent
from nonebot_plugin_chatrecorder import get_messages_plain_text
@matcher.handle()
async def _(event: GroupMessageEvent):
msgs = await get_messages_plain_text(
types=["message"],
group_ids=[str(event.group_id)],
)
详细参数及说明见代码注释
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for nonebot_plugin_chatrecorder-0.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9b7fd5262349cca1cc95e8354531a498d3c0353aeea8cd7a678ccbea95e39d2 |
|
MD5 | e7aad0a113c470147074731bbe0e183c |
|
BLAKE2b-256 | aed5ba46c9ff4499945620359ccbf0425f1114f1149bed4ecdf1b11c675fdc5e |
Close
Hashes for nonebot_plugin_chatrecorder-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b77da7d8101272f56112c60608c65d16ada3b6007ba383b583c66d07362fa441 |
|
MD5 | 35099d770ddaa88308a2c7b96fab874a |
|
BLAKE2b-256 | 4a7a13ce10e8ee1f04cfda637e983b035fd9f191b78b11bef9e948c074c5f6e7 |