mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
✨ 支持mysql,psql,sqlite随机函数
This commit is contained in:
parent
62fd93c1ff
commit
057001f0bd
@ -1,16 +1,15 @@
|
|||||||
import platform
|
import platform
|
||||||
from dataclasses import dataclass
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
import psutil
|
||||||
import cpuinfo
|
import cpuinfo
|
||||||
import nonebot
|
import nonebot
|
||||||
import psutil
|
|
||||||
from httpx import ConnectTimeout, NetworkError
|
|
||||||
from nonebot.utils import run_sync
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
from nonebot.utils import run_sync
|
||||||
|
|
||||||
from zhenxun.configs.config import BotConfig
|
|
||||||
from zhenxun.services.log import logger
|
from zhenxun.services.log import logger
|
||||||
|
from zhenxun.configs.config import BotConfig
|
||||||
from zhenxun.utils.http_utils import AsyncHttpx
|
from zhenxun.utils.http_utils import AsyncHttpx
|
||||||
|
|
||||||
BAIDU_URL = "https://www.baidu.com/"
|
BAIDU_URL = "https://www.baidu.com/"
|
||||||
|
|||||||
@ -32,6 +32,14 @@ class BotSetting(BaseModel):
|
|||||||
return random.choice(platform_superuser)
|
return random.choice(platform_superuser)
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
def get_sql_type(self) -> str:
|
||||||
|
"""获取数据库类型
|
||||||
|
|
||||||
|
返回:
|
||||||
|
str: 数据库类型, postgres, aiomysql, sqlite
|
||||||
|
"""
|
||||||
|
return self.db_url.split(":", 1)[0] if self.db_url else ""
|
||||||
|
|
||||||
|
|
||||||
Config = ConfigsManager(Path() / "data" / "configs" / "plugins2config.yaml")
|
Config = ConfigsManager(Path() / "data" / "configs" / "plugins2config.yaml")
|
||||||
|
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
from zhenxun.services.log import logger
|
||||||
|
from zhenxun.configs.config import BotConfig
|
||||||
from zhenxun.models.task_info import TaskInfo
|
from zhenxun.models.task_info import TaskInfo
|
||||||
from zhenxun.models.ban_console import BanConsole
|
from zhenxun.models.ban_console import BanConsole
|
||||||
from zhenxun.models.group_console import GroupConsole
|
from zhenxun.models.group_console import GroupConsole
|
||||||
@ -34,3 +36,19 @@ class CommonUtils:
|
|||||||
"""群组是否被ban"""
|
"""群组是否被ban"""
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
class SqlUtils:
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def random(cls, query, limit: int = 1) -> str:
|
||||||
|
db_class_name = BotConfig.get_sql_type()
|
||||||
|
if "postgres" in db_class_name or "sqlite" in db_class_name:
|
||||||
|
query = f"{query.sql()} ORDER BY RANDOM() LIMIT {limit};"
|
||||||
|
elif "mysql" in db_class_name:
|
||||||
|
query = f"{query.sql()} ORDER BY RAND() LIMIT {limit};"
|
||||||
|
else:
|
||||||
|
logger.warning(
|
||||||
|
f"Unsupported database type: {db_class_name}", query.__module__
|
||||||
|
)
|
||||||
|
return query
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user