79 lines
2.6 KiB
Python
79 lines
2.6 KiB
Python
"""今日老婆 —— 触发器(Trigger + View)层。"""
|
|||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import base64
|
||
|
|
import datetime
|
||
|
|
from io import BytesIO
|
||
|
|
from random import choice
|
||
|
|
|
||
|
|
from nonebot import on_command
|
||
|
|
from nonebot.adapters.onebot.v11 import MessageEvent, Bot, MessageSegment
|
||
|
|
from nonebot.plugin import PluginMetadata
|
||
|
|
from PIL import Image
|
||
|
|
|
||
|
|
from ..services.store import get_member_list, load_group_config, write_group_config
|
||
|
|
from ..utils import download_avatar, png_resize
|
||
|
|
|
||
|
|
__plugin_meta__ = PluginMetadata(
|
||
|
|
name="今日老婆",
|
||
|
|
description="随机抓取群友作为老婆",
|
||
|
|
usage="发送【今日老婆】",
|
||
|
|
type="application",
|
||
|
|
)
|
||
|
|
|
||
|
|
wife = on_command("今日老婆", aliases={"今日老婆"})
|
||
|
|
|
||
|
|
|
||
|
|
async def get_wife_info(member_info, qq_id):
|
||
|
|
img = await download_avatar(qq_id)
|
||
|
|
avatar = Image.open(BytesIO(img)).convert("RGBA")
|
||
|
|
avatar = png_resize(avatar, new_width=145, new_height=145)
|
||
|
|
b_io = BytesIO()
|
||
|
|
avatar.save(b_io, format="PNG")
|
||
|
|
avatar_str = "base64://" + base64.b64encode(b_io.getvalue()).decode()
|
||
|
|
member_name = member_info["card"] or member_info["nickname"]
|
||
|
|
return (
|
||
|
|
MessageSegment.text("你今天的群友老婆是:"),
|
||
|
|
MessageSegment.image(avatar_str),
|
||
|
|
MessageSegment.text(f"{member_name}({qq_id})"),
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
@wife.handle()
|
||
|
|
async def wife_handle(bot: Bot, ev: MessageEvent):
|
||
|
|
group_id = ev.group_id
|
||
|
|
user_id = ev.user_id
|
||
|
|
bot_id = ev.self_id
|
||
|
|
wife_id = None
|
||
|
|
today = str(datetime.date.today())
|
||
|
|
config = load_group_config(group_id)
|
||
|
|
|
||
|
|
if config is not None:
|
||
|
|
if str(user_id) in list(config):
|
||
|
|
if config[str(user_id)][1] == today:
|
||
|
|
wife_id = config[str(user_id)][0]
|
||
|
|
else:
|
||
|
|
del config[str(user_id)]
|
||
|
|
|
||
|
|
if wife_id is None:
|
||
|
|
all_list = await bot.get_group_member_list(group_id=group_id)
|
||
|
|
id_list = get_member_list(all_list)
|
||
|
|
id_list.remove(bot_id)
|
||
|
|
id_list.remove(user_id)
|
||
|
|
if config is not None:
|
||
|
|
for record_id in list(config):
|
||
|
|
if config[record_id][1] != today:
|
||
|
|
del config[record_id]
|
||
|
|
else:
|
||
|
|
try:
|
||
|
|
id_list.remove(int(config[record_id][0]))
|
||
|
|
except Exception:
|
||
|
|
del config[record_id]
|
||
|
|
wife_id = choice(id_list)
|
||
|
|
|
||
|
|
write_group_config(group_id, user_id, wife_id, today, config)
|
||
|
|
member_info = await bot.get_group_member_info(group_id=group_id, user_id=wife_id)
|
||
|
|
result = await get_wife_info(member_info, wife_id)
|
||
|
|
await bot.send(ev, result, at_sender=True)
|