from typing import cast
from nonebot import get_bot, get_plugin_config, logger
from nonebot.adapters import Event as BaseEvent
from nonebot.adapters.onebot.v11.event import GroupMessageEvent
from nonebot.adapters.onebot.v11.bot import Bot as OnebotBot
from nonebot.rule import Rule
from pydantic import BaseModel


class WZQConflictConfig(BaseModel):
    wzq_bot_qq: int = 0

config = get_plugin_config(WZQConflictConfig)


async def no_wzqbot(evt: BaseEvent):
    if config.wzq_bot_qq <= 0:
        return True
    if not isinstance(evt, GroupMessageEvent):
        return True
    gid = evt.group_id
    sid = evt.self_id
    bot = cast(OnebotBot, get_bot(str(sid)))

    members = await bot.get_group_member_list(group_id=gid)

    members = set((m.get("user_id", -1) for m in members))
    if config.wzq_bot_qq in members:
        return False
    return True


no_wzqbot_rule = Rule(no_wzqbot)

