Skip to content

Commit

Permalink
合并拉取请求 #5
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
Cypas authored Jul 24, 2023
2 parents cef960d + 9b7bd5d commit 33e3de8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
15 changes: 12 additions & 3 deletions nonebot_plugin_disconnect_notice/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from nonebot.permission import SUPERUSER

from .config import driver, plugin_config, global_config
from .dataClass import BotParams
from .utils import send_notice, mail_config, send_mail

__plugin_meta__ = PluginMetadata(
Expand All @@ -31,7 +32,11 @@
async def _(matcher: Matcher):
"""主动触发掉线通知测试"""
msg = "已发送测试邮件,如未收到请检查邮件垃圾箱"
res = await send_mail("114514", test=True)
bot_params = BotParams(
adapter_name="114514",
bot_id="114514"
)
res = await send_mail(bot_params, test=True)
if res is not None:
msg = f"测试邮件发送失败,请重新检查配置项参数正确性,错误信息为: {res}"
await matcher.finish(msg)
Expand All @@ -42,7 +47,11 @@ async def disconnect(bot: Bot):
"""bot断连触发器"""
# 开发者模式下不生效
if not plugin_config.disconnect_notice_dev_mode:
await send_notice(bot)
bot_params = BotParams(
adapter_name=bot.adapter.get_name(),
bot_id=bot.self_id
)
await send_notice(bot_params)


@driver.on_bot_connect
Expand All @@ -54,6 +63,6 @@ async def connect(bot: Bot):
super_user: str = list(global_config.superusers)[0]
if super_user:
msg = "【插件nonebot-plugin-disconnect-notice】\n缺少mail配置项,请按照" \
"https://github.com/Cypas/nonebot_plugin_disconnect_notice#%EF%B8%8F-%E9%85%8D%E7%BD%AE"\
"https://github.com/Cypas/nonebot_plugin_disconnect_notice#%EF%B8%8F-%E9%85%8D%E7%BD%AE" \
"\n添加配置项后,再重新载入插件"
await bot.send_private_msg(user_id=int(super_user), message=msg)
9 changes: 9 additions & 0 deletions nonebot_plugin_disconnect_notice/dataClass.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ def check_params(self) -> bool:
return True
else:
return False


class BotParams:
def __init__(self,
adapter_name: str,
bot_id: str,
):
self.adapter_name = adapter_name
self.bot_id = bot_id
16 changes: 5 additions & 11 deletions nonebot_plugin_disconnect_notice/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from nonebot import logger, Bot
from .config import plugin_config
from .dataClass import MailConfig
from .dataClass import MailConfig, BotParams

mail_config: MailConfig = MailConfig(
user=plugin_config.disconnect_notice_smtp_user,
Expand All @@ -16,13 +16,12 @@
)


async def send_notice(bot: Bot):
async def send_notice(bot_params: BotParams):
"""整合发送通知消息"""
bot_id = get_bot_id(bot)
await send_mail(bot_id)
await send_mail(bot_params)


async def send_mail(bot_id: str, test: bool = False):
async def send_mail(bot_params: BotParams, test: bool = False):
"""发送邮件通知"""
# 邮箱凭据
global mail_config
Expand All @@ -33,7 +32,7 @@ async def send_mail(bot_id: str, test: bool = False):
return error
# 邮件正文
subject = f"你的bot掉线了"
content = f"你的bot账号: {bot_id} 掉线了,可能是被风控了,赶快去看看吧"
content = f"你的 {bot_params.adapter_name} 适配器的bot账号: {bot_params.bot_id} 掉线了,可能是被风控了,赶快去看看吧"
if test:
subject = f"掉线通知测试"
content = f"这是一封掉线通知测试邮件,你bot并没有掉线"
Expand All @@ -58,8 +57,3 @@ async def send_mail(bot_id: str, test: bool = False):
logger.info("通知邮件发送成功!")
return


def get_bot_id(bot) -> str:
"""获取bot_id"""
bot_id = bot.self_id
return bot_id
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nonebot-plugin-disconnect-notice"
version = "0.1.3"
version = "0.1.4"
description = "bot断连时的通知插件"
authors = ["cypas <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit 33e3de8

Please sign in to comment.