Skip to content

Commit

Permalink
Merge pull request #109 from Ailitonia/dev
Browse files Browse the repository at this point in the history
Upgrade: New version for nonebot2 v2.0.0-beta.5
  • Loading branch information
Ailitonia committed Aug 10, 2022
2 parents 4b31228 + f920562 commit c8de61c
Show file tree
Hide file tree
Showing 65 changed files with 1,417 additions and 694 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

_基于 [Nonebot2](https://github.com/nonebot/nonebot2)[go-cqhttp](https://github.com/Mrs4s/go-cqhttp) 的 qq 机器人_

![Nonebot2](https://img.shields.io/badge/Nonebot2-Release_v2.0.0_beta.3-brightgreen)
![go-cqhttp](https://img.shields.io/badge/go--cqhttp-v1.0.0_rc1-brightgreen)
![Nonebot2](https://img.shields.io/badge/Nonebot2-Release_v2.0.0_beta.5-brightgreen)
![go-cqhttp](https://img.shields.io/badge/go--cqhttp-v1.0.0_rc3-brightgreen)
<br>
![GitHub](https://img.shields.io/github/license/Ailitonia/omega-miya)
![Python](https://img.shields.io/badge/Python-3.10+-blue)
Expand All @@ -18,11 +18,11 @@ _基于 [Nonebot2](https://github.com/nonebot/nonebot2) 和 [go-cqhttp](https://

## 当前适配 nonebot2 版本

[Nonebot2 Release v2.0.0-beta.3](https://github.com/nonebot/nonebot2/releases/tag/v2.0.0-beta.3)
[Nonebot2 Release v2.0.0-beta.5](https://github.com/nonebot/nonebot2/releases/tag/v2.0.0-beta.5)

## 当前适配 go-cqhttp 版本

[go-cqhttp v1.0.0-rc1](https://github.com/Mrs4s/go-cqhttp/releases/tag/v1.0.0-rc1)
[go-cqhttp v1.0.0-rc3](https://github.com/Mrs4s/go-cqhttp/releases/tag/v1.0.0-rc3)

## 功能 & 特点

Expand Down
11 changes: 10 additions & 1 deletion omega_miya/database/schemas/pixiv_artwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,18 @@ async def query_all_pid_by_uid(cls, uid: int) -> IntListResult:
return IntListResult(error=False, info='Success', result=(await cls._query_custom_all(stmt=stmt)))

@classmethod
async def count_all(cls, keywords: Optional[List[str]] = None) -> PixivArtworkCountResult:
async def count_all(
cls,
keywords: Optional[List[str]] = None,
*,
classified: Optional[int] = 1
) -> PixivArtworkCountResult:
try:
all_stmt = select(func.count(cls.orm_model.id)).with_for_update(read=True)

if classified is not None:
all_stmt = all_stmt.where(cls.orm_model.classified == classified)

if keywords:
for keyword in keywords:
all_stmt = all_stmt.where(or_(
Expand Down
40 changes: 30 additions & 10 deletions omega_miya/onebot_api/gocq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
class GoCqhttpBot(BaseOnebotApi):
"""go-cqhttp api
适配版本: go-cqhttp v1.0.0-rc2
适配版本: go-cqhttp v1.0.0-rc3
"""

def __init__(self, bot: Bot):
Expand All @@ -54,41 +54,50 @@ async def connecting_db_upgrade(self) -> None:
# 更新群组相关信息
groups_result = await self.get_group_list()
group_upgrade_tasks = [
InternalBotGroup(bot_id=self.self_id, parent_id=self.self_id, entity_id=x.group_id).add_only(
InternalBotGroup(bot_id=self.self_id, parent_id=self.self_id, entity_id=x.group_id).add_upgrade(
parent_entity_name=bot_login_info.nickname,
entity_name=x.group_name,
entity_info=x.group_memo,
related_entity_name=x.group_name
related_entity_name=x.group_name,
parent_entity_info=bot_login_info.nickname,
entity_info=x.group_memo
)
for x in groups_result]
# 更新用户相关信息
users_result = await self.get_friend_list()
user_upgrade_tasks = [
InternalBotUser(bot_id=self.self_id, parent_id=self.self_id, entity_id=x.user_id).add_only(
InternalBotUser(bot_id=self.self_id, parent_id=self.self_id, entity_id=x.user_id).add_upgrade(
parent_entity_name=bot_login_info.nickname,
entity_name=x.nickname,
related_entity_name=x.remark
related_entity_name=x.remark,
parent_entity_info=bot_login_info.nickname,
entity_info=x.remark
)
for x in users_result]
# 更新频道相关信息
guild_profile = await self.get_guild_service_profile()
guild_data = await self.get_guild_list()
guild_upgrade_tasks = [
InternalBotGuild(bot_id=self.self_id, parent_id=guild_profile.tiny_id, entity_id=x.guild_id).add_only(
InternalBotGuild(bot_id=self.self_id, parent_id=guild_profile.tiny_id, entity_id=x.guild_id).add_upgrade(
parent_entity_name=guild_profile.nickname,
entity_name=x.guild_name,
related_entity_name=x.guild_name
related_entity_name=x.guild_name,
parent_entity_info=guild_profile.nickname,
entity_info=x.guild_name
)
for x in guild_data]

channel_upgrade_tasks = []
for guild in guild_data:
channel_data = await self.get_guild_channel_list(guild_id=guild.guild_id)
channel_upgrade_tasks.extend([
InternalGuildChannel(bot_id=self.self_id, parent_id=x.owner_guild_id, entity_id=x.channel_id).add_only(
InternalGuildChannel(
bot_id=self.self_id, parent_id=x.owner_guild_id, entity_id=x.channel_id
).add_upgrade(
parent_entity_name=guild.guild_name,
entity_name=x.channel_name,
related_entity_name=x.channel_name
related_entity_name=x.channel_name,
parent_entity_info=guild.guild_name,
entity_info=x.channel_name
)
for x in channel_data
])
Expand Down Expand Up @@ -486,6 +495,17 @@ async def upload_group_file(self, group_id: int | str, file: str, name: str, fol
"""
return await self.bot.call_api('upload_group_file', group_id=group_id, file=file, name=name, folder=folder)

async def upload_private_file(self, user_id: int | str, file: str, name: str) -> None:
"""上传私聊文件
只能上传本地文件, 需要上传 http 文件的话请先调用 download_file API下载
:param user_id: 好友qq
:param file: 本地文件路径
:param name: 储存名称
"""
return await self.bot.call_api('upload_private_file', user_id=user_id, file=file, name=name)

async def get_group_file_system_info(self, group_id: int | str) -> GroupFileSystemInfo:
system_info_result = await self.bot.call_api('get_group_file_system_info', group_id=group_id)
return GroupFileSystemInfo.parse_obj(system_info_result)
Expand Down
17 changes: 9 additions & 8 deletions omega_miya/plugins/announce/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from nonebot import on_command, logger
from nonebot.log import logger
from nonebot.plugin import on_command, PluginMetadata
from nonebot.rule import to_me
from nonebot.permission import SUPERUSER
from nonebot.typing import T_State
Expand All @@ -12,13 +13,13 @@
from omega_miya.utils.process_utils import run_async_catching_exception, semaphore_gather


# Custom plugin usage text
__plugin_custom_name__ = '公告'
__plugin_usage__ = r'''【公告插件】
快速批量向启用了bot的群组发送通知公告
用法:
/公告 [公告内容]'''
__plugin_meta__ = PluginMetadata(
name="公告",
description="【公告插件】\n"
"快速批量向启用了bot的群组发送通知公告",
usage="/公告 [公告内容]",
extra={"author": "Ailitonia"},
)


# 注册事件响应器
Expand Down
75 changes: 75 additions & 0 deletions omega_miya/plugins/auto_group_sign/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""
@Author : Ailitonia
@Date : 2022/06/27 20:48
@FileName : auto_group_sign.py
@Project : nonebot2_miya
@Description : 自动群打卡 (go-cqhttp v1.0.0-rc3 以上版本可用)
@GitHub : https://github.com/Ailitonia
@Software : PyCharm
"""

from nonebot import get_driver
from nonebot.log import logger
from nonebot.plugin import PluginMetadata
from nonebot.adapters import Bot
from pydantic import BaseModel

from omega_miya.onebot_api import GoCqhttpBot
from omega_miya.utils.apscheduler import scheduler
from omega_miya.utils.process_utils import semaphore_gather


__plugin_meta__ = PluginMetadata(
name="自动群打卡",
description="【自动群打卡插件】\n"
"让机器人参与抢群打卡每日第一",
usage="由管理员配置, 无命令用法",
extra={"author": "Ailitonia"},
)


class AutoGroupSignConfig(BaseModel):
"""自动群打卡插件配置"""
# 启用自动群打卡
enable_auto_group_sign: bool = False

class Config:
extra = "ignore"


_plugin_config = AutoGroupSignConfig.parse_obj(get_driver().config)


async def _bot_group_sign(bot: Bot):
gocq_bot = GoCqhttpBot(bot=bot)
tasks = [gocq_bot.send_group_sign(group_id=group_result.group_id) for group_result in await gocq_bot.get_group_list()]
await semaphore_gather(tasks=tasks, semaphore_num=16)


async def _sign_main() -> None:
logger.debug('AutoGroupSign | Starting sign all groups')
tasks = [_bot_group_sign(bot=bot) for _, bot in get_driver().bots.items()]
await semaphore_gather(tasks=tasks, semaphore_num=8)
logger.debug('AutoGroupSign | Sign tasks completed')


if _plugin_config.enable_auto_group_sign:
scheduler.add_job(
_sign_main,
'cron',
# year=None,
# month=None,
# day='*/1',
# week=None,
# day_of_week=None,
hour='0',
minute='0',
second='0',
# start_date=None,
# end_date=None,
# timezone=None,
id='auto_group_sign',
coalesce=True,
misfire_grace_time=30
)
logger.opt(colors=True).success('<lc>AutoGroupSign</lc> | <lg>自动群打卡已启用</lg>')
23 changes: 12 additions & 11 deletions omega_miya/plugins/bilibili_dynamic_monitor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
@Software : PyCharm
"""

from nonebot import on_command, logger
from nonebot.log import logger
from nonebot.plugin import on_command, PluginMetadata
from nonebot.typing import T_State
from nonebot.matcher import Matcher
from nonebot.permission import SUPERUSER
Expand All @@ -29,16 +30,16 @@
from .monitor import scheduler


# Custom plugin usage text
__plugin_custom_name__ = 'B站动态订阅'
__plugin_usage__ = r'''【B站动态订阅】
订阅并跟踪Bilibili用户动态更新
用法:
仅限私聊或群聊中群管理员使用:
/B站动态订阅 [UID]
/B站动态取消订阅 [UID]
/B站动态订阅列表'''
__plugin_meta__ = PluginMetadata(
name="B站动态订阅",
description="【B站动态订阅插件】\n"
"订阅并跟踪Bilibili用户动态更新",
usage="仅限私聊或群聊中群管理员使用:\n"
"/B站动态订阅 [UID]\n"
"/B站动态取消订阅 [UID]\n"
"/B站动态订阅列表",
extra={"author": "Ailitonia"},
)


add_dynamic_sub = on_command(
Expand Down
35 changes: 23 additions & 12 deletions omega_miya/plugins/bilibili_live_monitor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
from nonebot import on_command, logger
"""
@Author : Ailitonia
@Date : 2022/04/28 20:26
@FileName : bilibili_live_monitor.py
@Project : nonebot2_miya
@Description : Bilibili 直播间订阅
@GitHub : https://github.com/Ailitonia
@Software : PyCharm
"""

from nonebot.log import logger
from nonebot.plugin import on_command, PluginMetadata
from nonebot.typing import T_State
from nonebot.matcher import Matcher
from nonebot.permission import SUPERUSER
Expand All @@ -18,17 +29,17 @@
from .utils import add_bili_live_room_sub, delete_bili_live_room_sub, query_subscribed_bili_live_room_sub_source


# Custom plugin usage text
__plugin_custom_name__ = 'B站直播间订阅'
__plugin_usage__ = r'''【B站直播间订阅】
订阅并监控Bilibili直播间状态
提供开播、下播、直播间换标题提醒
用法:
仅限私聊或群聊中群管理员使用:
/B站直播间订阅 [RoomID]
/B站直播间取消订阅 [RoomID]
/B站直播间订阅列表'''
__plugin_meta__ = PluginMetadata(
name="B站直播间订阅",
description="【B站直播间订阅插件】\n"
"订阅并监控Bilibili直播间状态\n"
"提供开播、下播、直播间换标题提醒",
usage="仅限私聊或群聊中群管理员使用:\n"
"/B站直播间订阅 [RoomID]\n"
"/B站直播间取消订阅 [RoomID]\n"
"/B站直播间订阅列表",
extra={"author": "Ailitonia"},
)


add_live_sub = on_command(
Expand Down
35 changes: 35 additions & 0 deletions omega_miya/plugins/bilibili_live_monitor/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
@Author : Ailitonia
@Date : 2022/07/30 21:31
@FileName : config.py
@Project : nonebot2_miya
@Description : Bilibili 直播间订阅插件配置
@GitHub : https://github.com/Ailitonia
@Software : PyCharm
"""

from nonebot import get_driver, logger
from pydantic import BaseModel, ValidationError


class BilibiliLiveMonitorPluginConfig(BaseModel):
"""BilibiliLiveMonitor 插件配置"""

# 发送消息通知时尝试@全体
bilibili_live_monitor_enable_group_at_all_notice: bool = False

class Config:
extra = "ignore"


try:
bilibili_live_monitor_plugin_config = BilibiliLiveMonitorPluginConfig.parse_obj(get_driver().config)
except ValidationError as e:
import sys
logger.opt(colors=True).critical(f'<r>Bilibili 直播间订阅插件配置格式验证失败</r>, 错误信息:\n{e}')
sys.exit(f'Bilibili 直播间订阅插件配置格式验证失败, {e}')


__all__ = [
'bilibili_live_monitor_plugin_config'
]
14 changes: 13 additions & 1 deletion omega_miya/plugins/bilibili_live_monitor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from omega_miya.utils.process_utils import run_async_catching_exception, semaphore_gather
from omega_miya.utils.message_tools import MessageSender

from .config import bilibili_live_monitor_plugin_config as plugin_config
from .model import (BilibiliLiveRoomStatus, BilibiliLiveRoomTitleChange, BilibiliLiveRoomStartLiving,
BilibiliLiveRoomStartLivingWithUpdateTitle, BilibiliLiveRoomStopLiving,
BilibiliLiveRoomStopLivingWithPlaylist, BilibiliLiveRoomStatusUpdate)
Expand Down Expand Up @@ -164,7 +165,7 @@ async def _get_live_room_update_message(
update_data: BilibiliLiveRoomStatusUpdate
) -> str | Message | None:
"""处理直播间更新为消息"""
send_message = f'【bilibili直播间】\n'
send_message = '【bilibili直播间】\n'
user_name = _LIVE_STATUS.get(live_room_data.uid).live_user_name
need_url = False

Expand Down Expand Up @@ -206,6 +207,17 @@ async def _msg_sender(entity: BaseInternalEntity, message: str | Message) -> int
"""向 entity 发送消息"""
try:
msg_sender = MessageSender.init_from_bot_id(bot_id=entity.bot_id)

# 通知群组时检查能不能@全体成员
if plugin_config.bilibili_live_monitor_enable_group_at_all_notice and entity.relation_type == 'bot_group':
at_all_remain = await run_async_catching_exception(msg_sender.bot.get_group_at_all_remain)(
group_id=entity.entity_id)
if (not isinstance(at_all_remain, Exception)
and at_all_remain.can_at_all
and at_all_remain.remain_at_all_count_for_group
and at_all_remain.remain_at_all_count_for_uin):
message = MessageSegment.at(user_id='all') + message

sent_msg_id = await msg_sender.send_internal_entity_msg(entity=entity, message=message)
except KeyError:
logger.debug(f'BilibiliLiveRoomMonitor | Bot({entity.bot_id}) not online, '
Expand Down
Loading

0 comments on commit c8de61c

Please sign in to comment.