Skip to content

Commit

Permalink
✨ 添加非超级用户添加/移除订阅申请的功能 (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
Well2333 committed May 7, 2024
1 parent 74c7db0 commit 582b86a
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 22 deletions.
1 change: 1 addition & 0 deletions nonebot_plugin_bilichat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require("nonebot_plugin_apscheduler")
require("nonebot_plugin_alconna")
require("nonebot_plugin_auto_bot_selector")
require("nonebot_plugin_waiter")

cmd_perfix = f"{raw_config.command_start}{plugin_config.bilichat_cmd_start}{raw_config.command_sep}"

Expand Down
2 changes: 1 addition & 1 deletion nonebot_plugin_bilichat/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from . import functions, login, subs, subs_cfg # noqa: F401
from . import functions, login, subs, subs_cfg, subs_request # noqa: F401
145 changes: 145 additions & 0 deletions nonebot_plugin_bilichat/commands/subs_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
from asyncio import Lock
from typing import TypedDict

from nonebot.adapters import Event, Message
from nonebot.params import CommandArg, Depends
from nonebot.permission import SUPERUSER
from nonebot_plugin_waiter import waiter

from ..config import plugin_config
from ..lib.uid_extract import uid_extract
from ..subscribe.manager import SubscriptionSystem, Uploader, User
from .base import bilichat, check_lock, get_user


class Request(TypedDict):
sub: list[Uploader]
unsub: list[Uploader]


REQUESTS: dict[User, Request] = {}


bili_add_sub_request = bilichat.command("sub", aliases=set(plugin_config.bilichat_cmd_add_sub), priority=2)
bili_remove_sub_request = bilichat.command("unsub", aliases=set(plugin_config.bilichat_cmd_remove_sub), priority=2)
bili_request_handle = bilichat.command("handle", permission=SUPERUSER)


@bili_add_sub_request.handle()
async def add_sub(uid: Message = CommandArg(), user: User = Depends(get_user)):
reqs = REQUESTS.get(user, {"sub": [], "unsub": []})
if not uid:
await bili_add_sub_request.finish("请输入UP主的昵称呢\n`(*>﹏<*)′")
msg = await uid_extract(uid.extract_plain_text())
if isinstance(msg, str):
await bili_add_sub_request.finish(msg)
up = SubscriptionSystem.uploaders.get(msg.mid, Uploader(nickname=msg.nickname, uid=msg.mid))

if len(user.subscriptions) > SubscriptionSystem.config.subs_limit:
await bili_add_sub_request.finish("本群的订阅已经满啦\n删除无用的订阅再试吧\n`(*>﹏<*)′")
elif up.uid in user.subscriptions:
await bili_add_sub_request.finish("本群已经订阅了此UP主呢...\n`(*>﹏<*)′")
elif up in reqs["sub"]:
await bili_add_sub_request.finish(f"已经申请过添加 {up} 啦,无需再次申请\n(*^▽^*)")

reqs["sub"].append(up)
REQUESTS[user] = reqs

await bili_add_sub_request.finish("已记录此次添加申请,请联系管理员处理\n(*^▽^*)")


@bili_remove_sub_request.handle()
async def remove_sub(msg: Message = CommandArg(), user: User = Depends(get_user)):
reqs = REQUESTS.get(user, {"sub": [], "unsub": []})
if not msg:
await bili_remove_sub_request.finish("请输入UP主的昵称或 UID 呢\n`(*>﹏<*)′")
keyword = msg.extract_plain_text().lower()
for up in SubscriptionSystem.uploaders.values():
if up.nickname.lower() == keyword or str(up.uid) == keyword:
if up.uid not in user.subscriptions:
await bili_remove_sub_request.finish("本群并未订阅此UP主呢...\n`(*>﹏<*)′")
elif up in reqs["unsub"]:
await bili_remove_sub_request.finish(f"已经申请过移除 {up} 啦,无需再次申请\n(*^▽^*)")

reqs["unsub"].append(up)
REQUESTS[user] = reqs
await bili_remove_sub_request.finish("已记录此次添加申请,请联系管理员处理\n(*^▽^*)")
await bili_remove_sub_request.finish("未找到该 UP 主呢\n`(*>﹏<*)′")


@bili_request_handle.handle()
async def handle_request(lock: Lock = Depends(check_lock)):
async with lock:
if not REQUESTS or not any(bool(reqs["sub"] or reqs["unsub"]) for reqs in REQUESTS.values()):
await bili_request_handle.finish("没有任何申请记录")
for user, reqs in REQUESTS.copy().items():
REQUESTS.pop(user)
msg = []
index = 0
if not reqs["sub"] and not reqs["unsub"]:
continue
msg.append(f"{user.user_id}({user.platform})")
if reqs["sub"]:
msg.append(" 添加:")
for up in reqs["sub"]:
msg.append(f" {index}. {up.nickname}({up.uid})")
index += 1
if reqs["unsub"]:
msg.append(" 移除:")
for up in reqs["unsub"]:
msg.append(f" {index}. {up.nickname}({up.uid})")
index += 1
msg.extend(["请输入:", "`y` 同意全部请求", "`n` 拒绝全部请求", "`e` 逐条审查"])
await bili_request_handle.send("\n".join(msg))

@waiter(waits=["message"], keep_session=True)
async def check(event: Event):
return event.get_plaintext().lower().strip()

async for resp in check(timeout=120):
if resp == "y":
store: dict[Uploader, str] = {}
for up in reqs["sub"]:
store[up] = user.add_subscription(up) or f"已添加 {up.nickname}({up.uid})"
for up in reqs["unsub"]:
store[up] = await user.remove_subscription(up) or f"已移除 {up.nickname}({up.uid})"
await bili_request_handle.send(
"已同意该用户的全部请求,具体如下:\n"
+ "\n".join([f"{up.nickname}({up.uid}): {m}" for up, m in store.items()])
)
break
elif resp == "n":
await bili_request_handle.send("已拒绝该用户的全部请求")
break
elif resp == "e":
for up in reqs["sub"]:
await bili_request_handle.send(f"是否同意添加 {up.nickname}({up.uid})")

async for resp1 in check(timeout=120):
if resp1 == "y":
await bili_request_handle.send(
user.add_subscription(up) or f"已添加 {up.nickname}({up.uid})"
)
break
elif resp1 == "n":
await bili_request_handle.send(f"已拒绝添加 {up.nickname}({up.uid})")
break
else:
await bili_request_handle.send(f"请输入 y 或 n,而不是 {resp1}")
for up in reqs["unsub"]:
await bili_request_handle.send(f"是否移除 {up.nickname}({up.uid})")

async for resp2 in check(timeout=120):
if resp2 == "y":
await bili_request_handle.send(
await user.remove_subscription(up) or f"已移除 {up.nickname}({up.uid})"
)
break
elif resp2 == "n":
await bili_request_handle.send(f"已拒绝移除 {up.nickname}({up.uid})")
break
else:
await bili_request_handle.send(f"请输入 y 或 n,而不是 {resp2}")
break
else:
await bili_request_handle.send("输入无效,请输入:\n`y` 同意全部请求\n`n` 拒绝全部请求\n`e` 逐条审查")
16 changes: 16 additions & 0 deletions nonebot_plugin_bilichat/subscribe/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ def __init__(self, **values):
if isinstance(up, str):
raise ValueError(f"未找到 uid 为 {self.uid} 的 UP")
self.nickname = up.nickname

def __hash__(self):
return hash(self.uid)

def __eq__(self, other):
if isinstance(other, Uploader):
return self.uid == other.uid
return False

@property
def subscribed_users(self) -> list["User"]:
Expand Down Expand Up @@ -149,6 +157,14 @@ def validate_subscriptions(cls, v: dict[str, dict[str, Any]] | list[dict[str, An
continue

return validated_subs

def __hash__(self):
return hash((self.platform, self.user_id))

def __eq__(self, other):
if isinstance(other, User):
return self.platform == other.platform and self.user_id == other.user_id
return False

@classmethod
def extract_alc_target(cls, target: Target) -> tuple[str, str]:
Expand Down
56 changes: 36 additions & 20 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "nonebot-plugin-bilichat"

version = "5.9.3"
version = "5.10.0"

description = "多种B站链接解析,视频词云,AI总结,你想要的都在 bilichat"
authors = [
Expand All @@ -22,6 +22,7 @@ dependencies = [
"python-multipart>=0.0.6",
"nonebot-plugin-alconna>=0.42.3",
"nonebot-plugin-auto-bot-selector>=0.2.0",
"nonebot-plugin-waiter>=0.4.0",
]
requires-python = ">=3.10,<4.0"
readme = "README.md"
Expand Down

0 comments on commit 582b86a

Please sign in to comment.