mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-02 09:26:12 +08:00
✨ 删除cookie 对话
This commit is contained in:
parent
940301a6fc
commit
7c9e191f40
@ -307,8 +307,16 @@ class DBConfig:
|
||||
cookie_in_db.tags = cookie.tags
|
||||
await sess.commit()
|
||||
|
||||
async def delete_cookie(self, cookie_id: int):
|
||||
async def delete_cookie_by_id(self, cookie_id: int):
|
||||
async with create_session() as sess:
|
||||
cookie = await sess.scalar(
|
||||
select(Cookie)
|
||||
.where(Cookie.id == cookie_id)
|
||||
.outerjoin(CookieTarget)
|
||||
.options(selectinload(Cookie.targets))
|
||||
)
|
||||
if len(cookie.targets) > 0:
|
||||
raise Exception(f"cookie {cookie.id} in use")
|
||||
await sess.execute(delete(Cookie).where(Cookie.id == cookie_id))
|
||||
await sess.commit()
|
||||
|
||||
|
@ -1,9 +1,46 @@
|
||||
from nonebot.typing import T_State
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.params import EventPlainText
|
||||
from nonebot_plugin_saa import MessageFactory
|
||||
|
||||
from .utils import ensure_user_info, gen_handle_cancel
|
||||
from ..config import config
|
||||
from ..utils import parse_text
|
||||
from .utils import gen_handle_cancel
|
||||
from ..platform import platform_manager
|
||||
|
||||
|
||||
def do_del_cookie(del_cookie: type[Matcher]):
|
||||
handle_cancel = gen_handle_cancel(del_cookie, "删除中止")
|
||||
|
||||
del_cookie.handle()(ensure_user_info(del_cookie))
|
||||
@del_cookie.handle()
|
||||
async def send_list(state: T_State):
|
||||
cookies = await config.get_cookie()
|
||||
if not cookies:
|
||||
await del_cookie.finish("暂无已添加 Cookie\n请使用“添加cookie”命令添加")
|
||||
res = "已添加的 Cookie 为:\n"
|
||||
state["cookie_table"] = {}
|
||||
for index, cookie in enumerate(cookies, 1):
|
||||
state["cookie_table"][index] = cookie
|
||||
client_mgr = platform_manager[cookie.platform_name].site.client_mgr
|
||||
friendly_name = await client_mgr.get_cookie_friendly_name(cookie)
|
||||
res += f"{index} {cookie.platform_name} {friendly_name} {len(cookie.targets)}个关联\n"
|
||||
if res[-1] != "\n":
|
||||
res += "\n"
|
||||
res += "请输入要删除的 Cookie 的序号\n输入'取消'中止"
|
||||
await MessageFactory(await parse_text(res)).send()
|
||||
|
||||
@del_cookie.receive(parameterless=[handle_cancel])
|
||||
async def do_del(
|
||||
state: T_State,
|
||||
index_str: str = EventPlainText(),
|
||||
):
|
||||
try:
|
||||
index = int(index_str)
|
||||
cookie = state["cookie_table"][index]
|
||||
if cookie.targets:
|
||||
await del_cookie.reject("只能删除未关联的 Cookie,请使用“取消关联cookie”命令取消关联")
|
||||
await config.delete_cookie_by_id(cookie.id)
|
||||
except Exception:
|
||||
await del_cookie.reject("删除错误")
|
||||
else:
|
||||
await del_cookie.finish("删除成功")
|
||||
|
@ -61,7 +61,7 @@ class CookieClientManager(ClientManager):
|
||||
for cookie in universal_cookies:
|
||||
if not cookie.tags.get("temporary"):
|
||||
continue
|
||||
await config.delete_cookie(cookie.id)
|
||||
await config.delete_cookie_by_id(cookie.id)
|
||||
universal_cookie.id = cookie.id # 保持原有的id
|
||||
await config.add_cookie(universal_cookie)
|
||||
|
||||
|
@ -73,7 +73,7 @@ async def test_cookie_by_user(app: App, init_scheduler):
|
||||
assert cookies[0].status == cookie.status
|
||||
assert cookies[0].tags == cookie.tags
|
||||
|
||||
await config.delete_cookie(cookies[0].id)
|
||||
await config.delete_cookie_by_id(cookies[0].id)
|
||||
cookies = await config.get_cookie(TargetQQGroup(group_id=123))
|
||||
assert len(cookies) == 0
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user