Skip to content

Commit

Permalink
Feature/add rebirth plugin (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
wei-z-git authored Apr 11, 2024
1 parent 5f32d1f commit ef1c497
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 73 deletions.
16 changes: 16 additions & 0 deletions pyproject copy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[project]
name = "little_dove"
version = "0.1.0"
description = "little_dove"
readme = "README.md"
requires-python = ">=3.10, <4.0"

[tool.nonebot]
adapters = [
{ name = "OneBot V11", module_name = "nonebot.adapters.onebot.v11" }
]
plugins = ["qq-group-admin-plugin","nonebot_plugin_gocqhttp","haruka_bot","nonebot_plugin_wordcloud","nonebot_plugin_easy_group_manager"]


plugin_dirs = ["src/plugins"]
builtin_plugins = []
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ requires-python = ">=3.10, <4.0"
adapters = [
{ name = "OneBot V11", module_name = "nonebot.adapters.onebot.v11" }
]
plugins = ["qq-group-admin-plugin","nonebot_plugin_gocqhttp","haruka_bot","nonebot_plugin_wordcloud","nonebot_plugin_easy_group_manager"]
plugins = ["qq-group-admin-plugin","nonebot_plugin_gocqhttp","qq-group-rebirth"]


plugin_dirs = ["src/plugins"]
Expand Down
2 changes: 1 addition & 1 deletion qq-group-admin-plugin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import nonebot

from . import (
clear,scheduler
clear
)

driver = nonebot.get_driver()
Expand Down
63 changes: 1 addition & 62 deletions qq-group-admin-plugin/clear.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
import random
from io import BytesIO
# Custom utils
from .utils import order_member_by_time, read_accounts_from_file, order_member_by_time_dsa
from .SDUtils import SDUtils
from .utils import order_member_by_time, read_accounts_from_file

from .config import plugin_config

Expand Down Expand Up @@ -69,48 +68,6 @@ async def kicker_group_mem(bot: Bot, matcher: Matcher, event: GroupMessageEvent)
await matcher.finish(f"机器人权限不足捏")


async def send_ai_image(bot: Bot, matcher: Matcher, event: GroupMessageEvent) -> None:
"""查询活跃成员,并随机获取1人,返回其qq头像
经过处理,返回ai生成的头像
"""
sd = SDUtils(sd_host=plugin_config.sd_host, sd_port=plugin_config.sd_port)
gid = int(event.group_id)
try:
member_list = await get_bot("1141560393").call_api("get_group_member_list", group_id=gid, no_cache=True)
# 选择最近发言10人
top10_list = order_member_by_time_dsa(member_list)

# 随机抽取一人
selected_user = random.choice(top10_list)
avatar_url = f"https://q2.qlogo.cn/headimg_dl?dst_uin={selected_user['user_id']}&spec=640"
# x = str([(user['user_id'], user['nickname']) for user in top10_list])
# await matcher.send)
pil_image, image_base64 = await sd.download_img(avatar_url)
tags = await sd.img2tags(image_base64)
images = []
# 生成4张图片
for _ in range(4):
img_new = await sd.img2img(tags, pil_image)
images.append(img_new)
image_BytesIO = await sd.combine4p(images)
text = (f"""信徒抽取中(10/10)...:
{[(user['user_id'], user['nickname']) for user in top10_list]}
""")
# 消息段只接受BytesIO, 理论上也支持bytes,但懒得看了
img_byte_array = BytesIO()
pil_image.save(img_byte_array, format='PNG')
msg = Message([MessageSegment.text(text)])
msg.append(MessageSegment.text(
f"\n少女祈祷中...\n将被重生的人为: {selected_user['nickname']}\n"))
msg.append(MessageSegment.image(img_byte_array))
msg.append(MessageSegment.text(f"\n重生中...\n\n新造的人:\n"))
msg.append(MessageSegment.image(image_BytesIO))
# [TODO]temp solution!!!!
await bot.send(event,msg)

except ActionFailed:
await matcher.finish(f"error")

# 交互式
matcher_call_robot = on_command('call', aliases={'召唤小鸠机器人', '召唤'}, priority=1,
permission=GROUP_ADMIN | GROUP_OWNER | SUPERUSER)
Expand All @@ -122,19 +79,6 @@ async def send_ai_image(bot: Bot, matcher: Matcher, event: GroupMessageEvent) ->
# # Interative command
question = "洒家来啦~~,哥哥有何吩咐:\n 1.查询冷库食材\n 2.查询冷库食材详细\n 3.发送食材!!!\n 0.退出\n请哥哥选择!\n ---消息来自小鸠Joe机器人"

# 查询尊贵vip名单
matcher_vip_list = on_command(
'vip', priority=4, permission=GROUP_ADMIN | GROUP_OWNER | SUPERUSER)

matcher_rebir = on_command(
'重生', priority=1,permission=GROUP_ADMIN | GROUP_OWNER | SUPERUSER)


@matcher_rebir.handle()
async def _(bot: Bot, matcher: Matcher, event: GroupMessageEvent, args: Message = CommandArg()):
bot = get_bot("3320741388")
await send_ai_image(bot, matcher, event)


@matcher_ac_message.handle()
async def _(bot:Bot,matcher: Matcher,event:Event, args: Message = CommandArg()):
Expand Down Expand Up @@ -166,8 +110,3 @@ async def call_robot(bot: Bot, matcher: Matcher, event: GroupMessageEvent, optio
# 可以使用平台的 Message 类直接构造模板消息
await matcher_call_robot.reject(option.template("哥哥,泥在说些甚么!洒家听不懂!请再说一遍!ok?"))


@matcher_vip_list.handle()
async def _(matcher: Matcher):
vip_list = read_accounts_from_file()
await matcher.send(f"💎尊享svip年费会员列表:{vip_list}")
6 changes: 0 additions & 6 deletions qq-group-admin-plugin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ class Config(BaseModel, extra=Extra.ignore):
ban_rand_time_min: int = 60 # 随机禁言最短时间(s) default: 1分钟
ban_rand_time_max: int = 2591999 # 随机禁言最长时间(s) default: 30天: 60*60*24*30
group_recall: bool = False # 是否开启防撤回功能 #TODO: 加到开关管理?
sd_host: str = 'frp.runtime20.space' # sd的host地址
sd_port: int = 52097 # sd server的端口
sd_groups: list = ["1170115778", "617875321",
"190842825", "689583064","715025692"] # 每天需要发送sd ai图片的groups
sd_hour: int = 2 # 每天需要发送sd ai图片时间,小时
sd_minute: int = 59 # 每天需要发送sd ai图片时间,分钟


driver = get_driver()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from PIL.JpegImagePlugin import JpegImageFile
from typing import Tuple, List
from nonebot.adapters import Message
from nonebot.adapters.onebot.v11 import Bot, MessageSegment
from nonebot.adapters.onebot.v11 import MessageSegment
from .utils import order_member_by_time_dsa
import random
from nonebot import get_bot
Expand Down Expand Up @@ -62,7 +62,7 @@ async def img2tags(self, img_base64: str) -> str:
"threshold": self.threshold
}
# Get tags and serialize
json_data = httpx.post(url, json=data).json()
json_data = httpx.post(url, json=data,timeout=60).json()
# 取出tags, 并拼接为str
caption_dict = json_data['caption']['tag'].keys()
caption_str = ', '.join(caption_dict)
Expand Down
27 changes: 27 additions & 0 deletions qq-group-rebirth/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import nonebot

from . import (
rebirth,scheduler
)

driver = nonebot.get_driver()


@driver.on_bot_connect
async def _():
bot = nonebot.get_bot("3320741388")


"""
qq重生
"""

__usage__ = """
"""
__help_plugin_name__ = 'qq重生'

__permission__ = 1
__help__version__ = '0.1.0'


17 changes: 17 additions & 0 deletions qq-group-rebirth/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# from typing import Optional
from nonebot import get_driver
from pydantic import BaseModel, Extra


class Config(BaseModel, extra=Extra.ignore):
sd_host: str = 'frp.runtime20.space' # sd的host地址
sd_port: int = 52097 # sd server的端口
sd_groups: list = ["1170115778", "617875321",
"190842825", "689583064","715025692"] # 每天需要发送sd ai图片的groups
sd_hour: int = 2 # 每天需要发送sd ai图片时间,小时
sd_minute: int = 59 # 每天需要发送sd ai图片时间,分钟


driver = get_driver()
global_config = driver.config
plugin_config = Config.parse_obj(global_config)
7 changes: 7 additions & 0 deletions qq-group-rebirth/configs/vip_list.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1141560393
1766718182
2508486536
3320741388
2024085613
810575702
173392947
40 changes: 40 additions & 0 deletions qq-group-rebirth/rebirth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from nonebot import on_command, get_bot
from nonebot.adapters import Message
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent
from nonebot.adapters.onebot.v11.permission import GROUP_ADMIN, GROUP_OWNER
from nonebot.matcher import Matcher
from nonebot.params import CommandArg
from nonebot.permission import SUPERUSER
# Custom utils
from .utils import read_accounts_from_file
from .SDUtils import SDUtils
from nonebot.exception import ActionFailed

from .config import plugin_config


# 查询尊贵vip名单
matcher_vip_list = on_command(
'vip', priority=4, permission=GROUP_ADMIN | GROUP_OWNER | SUPERUSER)

matcher_rebir = on_command(
'重生', priority=1, permission=GROUP_ADMIN | GROUP_OWNER | SUPERUSER)


@matcher_rebir.handle()
async def _(bot: Bot, event: GroupMessageEvent):
# make sure bot is bot
bot = get_bot("3320741388")
sd = SDUtils(sd_host=plugin_config.sd_host, sd_port=plugin_config.sd_port)
gid = int(event.group_id)
try:
msg = await sd.generate_ai_image_msg(group_id=gid)
except ActionFailed as e:
await bot.send(event, e)
await bot.send(event, message=msg)


@matcher_vip_list.handle()
async def _(matcher: Matcher):
vip_list = read_accounts_from_file()
await matcher.send(f"💎尊享svip年费会员列表:{vip_list}")
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import nonebot
from .config import plugin_config
from .SDUtils import SDUtils
from nonebot import require
require("nonebot_plugin_apscheduler")
from nonebot_plugin_apscheduler import scheduler
from nonebot import require



Expand Down
37 changes: 37 additions & 0 deletions qq-group-rebirth/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
utils
"""

def read_accounts_from_file() -> str:
"""
从文件中读取每一行账号信息
"""
try:
with open("qq-group-admin-plugin/configs/vip_list.txt", 'r', encoding='utf-8') as file:
accounts = [int(line.strip()) for line in file.readlines()]
return accounts
except FileNotFoundError:
print(f"File not found:")
return []

def order_member_by_time_dsa(data: str):
"""查询成员按最后一次发言时间排序
"""
try:

vip_lists = read_accounts_from_file()
# Only keep fields:user_id, sex, nickname, last_sent_time, join_time
new_data = [{'user_id': d['user_id'], 'sex': d['sex'], 'nickname': d['nickname'],
'last_sent_time': d['last_sent_time'], 'join_time': d['join_time']} for d in data]
# Exclude accounts from the file
filtered_data = [
item for item in new_data if item['user_id'] not in vip_lists]
# Ordered by last_sent_time
json_data_sorted = sorted(
filtered_data, key=lambda x: x['last_sent_time'])
# Return bottom 10 items
final_data = json_data_sorted[-10:]

return final_data
except KeyError:
return []

0 comments on commit ef1c497

Please sign in to comment.