为此方 Bot 接入数据库 #49

Merged
Passthem merged 4 commits from database into master 2025-11-19 00:52:02 +08:00
Owner

坏枪开发了一个新的分支,关于数据库的接入。我们可以讨论并考虑这个模块的相关事项,并做出一些进一步的完善。如果没有什么问题,我会摘掉 WIP 标签并合并到主分支。

坏枪开发了一个新的分支,关于数据库的接入。我们可以讨论并考虑这个模块的相关事项,并做出一些进一步的完善。如果没有什么问题,我会摘掉 WIP 标签并合并到主分支。
Passthem added 1 commit 2025-11-18 20:44:54 +08:00
Passthem self-assigned this 2025-11-18 21:31:53 +08:00
Passthem reviewed 2025-11-18 21:32:03 +08:00
Passthem left a comment
Author
Owner

其实挺好的,但是确实有点过度设计了,有点打不中靶子的感觉,好头疼!

坏枪如果有时间可以试着改改,要是帮得上我也可以改改,像是之前想到的更合理的迁移、引入异步 Sqlite 库之类的事情……

附赠:来自 AI 的审核意见

😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭

其实挺好的,但是确实有点**过度设计**了,有点打不中靶子的感觉,好头疼! 坏枪如果有时间可以试着改改,要是帮得上我也可以改改,像是之前想到的更合理的迁移、引入异步 Sqlite 库之类的事情…… ## 附赠:来自 AI 的审核意见 <img src="https://cdn.passthem.top/sharex/2025/11/screenshot-1118-212705.png" /> <img src="https://cdn.passthem.top/sharex/2025/11/screenshot-1118-212940.png" /> 😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭
.gitignore Outdated
@ -4,1 +3,4 @@
__pycache__
*.db
Author
Owner

直接忽略 *.db 吗,要是有人写了个 assets/template/template_db_xxx.db 不就一块忽略了么

直接忽略 `*.db` 吗,要是有人写了个 `assets/template/template_db_xxx.db` 不就一块忽略了么
Passthem marked this conversation as resolved
bot.py Outdated
@ -49,2 +50,4 @@
nonebot.load_plugin("nonebot_plugin_analysis_bilibili")
# 预加载
preinit("konabot/plugins")
Author
Owner

要不同步改一下 scripts/test_plugin_load.py 吧,我可以在 CI 流程里面测试插件能否顺利(在 Linux 环境)加载

要不同步改一下 `scripts/test_plugin_load.py` 吧,我可以在 CI 流程里面测试插件能否顺利(在 Linux 环境)加载
Passthem marked this conversation as resolved
@ -0,0 +1,64 @@
import os
import sqlite3
Author
Owner

Nonebot2 框架是个异步框架来着,如果不支持异步的话或许有点不太好(?

要不坏枪找个异步框架看看

Nonebot2 框架是个异步框架来着,如果不支持异步的话或许有点不太好(? 要不坏枪找个异步框架看看
Passthem marked this conversation as resolved
@ -0,0 +5,4 @@
class DatabaseManager:
"""超级无敌神奇的数据库!"""
@classmethod
Author
Owner

其实有点不那么想用单例模式。我一直想的是,要是可以支持不同插件用不同 db 文件,这样就不会互相干扰了

其实有点不那么想用单例模式。我一直想的是,要是可以支持不同插件用不同 db 文件,这样就不会互相干扰了
Passthem marked this conversation as resolved
@ -0,0 +32,4 @@
cursor.execute(command, params or ())
conn.commit()
cursor.close()
conn.close()
Author
Owner

卧槽,每一条 SQL 都开了关了,那性能瓶颈估计就在文件 IO 上了

有个东西叫连接池来着

卧槽,每一条 SQL 都开了关了,那性能瓶颈估计就在文件 IO 上了 有个东西叫连接池来着
Passthem marked this conversation as resolved
@ -0,0 +35,4 @@
conn.close()
@classmethod
def execute_by_sql_file(cls, file_path: str, params: Optional[tuple] = None) -> None:
Author
Owner

file_path 要不支持一下 pathlib.Path 对象?感觉这个 Path 更好用而且更常用

`file_path` 要不支持一下 `pathlib.Path` 对象?感觉这个 `Path` 更好用而且更常用
Passthem marked this conversation as resolved
@ -0,0 +40,4 @@
with open(file_path, 'r', encoding='utf-8') as f:
command = f.read()
# 按照需要执行多条语句
commands = command.split(';')
Author
Owner

假如有个小可爱在 SQL 里使用了带有分号的字符串,那又该如何应对呢(

假如有个小可爱在 SQL 里使用了带有分号的字符串,那又该如何应对呢(
Passthem marked this conversation as resolved
@ -0,0 +5,4 @@
# 初始化数据库表
DatabaseManager.execute_by_sql_file(
Path(__file__).resolve().parent / "sql" / "create_table.sql"
Author
Owner

草,怎么这里又直接传 pathlib.Path 了,我看你前面定义的只允许 str

草,怎么这里又直接传 `pathlib.Path` 了,我看你前面定义的只允许 `str` 啊
Passthem marked this conversation as resolved
Owner

哦耶

哦耶
Passthem added the
Kind/Feature
Priority
Medium
3
Status
Need More Info
2
labels 2025-11-18 22:31:22 +08:00
Passthem added 1 commit 2025-11-18 23:59:03 +08:00
Passthem added 1 commit 2025-11-19 00:45:55 +08:00
Passthem added 1 commit 2025-11-19 00:47:26 +08:00
Passthem changed title from WIP: 为此方 Bot 接入数据库 to 为此方 Bot 接入数据库 2025-11-19 00:47:33 +08:00
Passthem merged commit 00bdb90e3c into master 2025-11-19 00:52:02 +08:00
Sign in to join this conversation.
No description provided.