zhenxun_bot/basic_plugins/super_cmd/exec_sql.py

38 lines
1.1 KiB
Python
Raw Normal View History

2022-02-19 18:20:19 +08:00
from nonebot.adapters.onebot.v11 import Message
2021-12-16 11:16:28 +08:00
from nonebot import on_command
from nonebot.permission import SUPERUSER
from nonebot.rule import to_me
from services.db_context import db
2022-02-19 18:20:19 +08:00
from nonebot.params import CommandArg
2021-12-16 11:16:28 +08:00
from services.log import logger
__zx_plugin_name__ = "执行sql [Superuser]"
__plugin_usage__ = """
usage
执行一段sql语句
指令
exec [sql语句]
""".strip()
__plugin_des__ = "执行一段sql语句"
__plugin_cmd__ = [
"exec [sql语句]",
]
__plugin_version__ = 0.1
__plugin_author__ = "HibiKier"
exec_ = on_command("exec", rule=to_me(), permission=SUPERUSER, priority=1, block=True)
@exec_.handle()
2022-02-19 18:20:19 +08:00
async def _(arg: Message = CommandArg()):
sql = arg.extract_plain_text().strip()
2021-12-16 11:16:28 +08:00
async with db.transaction():
try:
query = db.text(sql)
await db.first(query)
await exec_.send("执行 sql 语句成功.")
except Exception as e:
await exec_.send(f"执行 sql 语句失败 {type(e)}{e}")
logger.error(f"执行 sql 语句失败 {type(e)}{e}")