Skip to content

Commit

Permalink
🎨 v0.3.2a2: improved code format
Browse files Browse the repository at this point in the history
  • Loading branch information
KafCoppelia committed Dec 27, 2022
1 parent 4124ebf commit ab526a5
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 208 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ _🌈 おはよう! 🌈_
<img src="https://img.shields.io/badge/nonebot2-2.0.0rc1+-green">
</a>

<a href="https://github.com/MinatoAquaCrews/nonebot_plugin_morning/releases/tag/v0.3.2a1">
<a href="https://github.com/MinatoAquaCrews/nonebot_plugin_morning/releases/tag/v0.3.2a2">
<img src="https://img.shields.io/github/v/release/MinatoAquaCrews/nonebot_plugin_morning?color=orange">
</a>

Expand All @@ -34,11 +34,11 @@ _🌈 おはよう! 🌈_

## 版本

v0.3.2a1
v0.3.2a2

⚠ 适配nonebot2-2.0.0rc1+

[更新日志](https://github.com/MinatoAquaCrews/nonebot_plugin_morning/releases/tag/v0.3.2a1)
[更新日志](https://github.com/MinatoAquaCrews/nonebot_plugin_morning/releases/tag/v0.3.2a2)

## 安装

Expand Down
99 changes: 59 additions & 40 deletions nonebot_plugin_morning/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .config import driver
from .data_source import morning_manager

__morning_version__ = "v0.3.2a1"
__morning_version__ = "v0.3.2a2"
__morning_usages__ = f'''
[早安] 早安/哦哈哟/おはよう
[晚安] 晚安/哦呀斯密/おやすみ
Expand Down Expand Up @@ -45,16 +45,17 @@
morning_setting = on_regex(pattern=r"^早安(开启|关闭|设置)( (时限|多重起床|超级亢奋)(( \d{1,2}){1,2})?)?$", permission=SUPERUSER | GROUP_OWNER | GROUP_ADMIN, priority=10, block=True)
night_setting = on_regex(pattern=r"^晚安(开启|关闭|设置)( (时限|优质睡眠|深度睡眠)(( \d{1,2}){1,2})?)?$", permission=SUPERUSER | GROUP_OWNER | GROUP_ADMIN, priority=10, block=True)


@morning.handle()
async def good_morning(bot: Bot, matcher: Matcher, event: GroupMessageEvent, args: Message = CommandArg()):
arg: str = args.extract_plain_text()
if arg == "帮助":
await matcher.finish(__morning_usages__)

uid = event.user_id
gid = event.group_id
mem_info = await bot.call_api("get_group_member_info", group_id=gid, user_id=uid)

sex = mem_info["sex"]
if sex == "male":
sex_str = "少年"
Expand All @@ -66,16 +67,17 @@ async def good_morning(bot: Bot, matcher: Matcher, event: GroupMessageEvent, arg
msg = morning_manager.get_morning_msg(str(gid), str(uid), sex_str)
await matcher.finish(message=msg, at_sender=True)


@night.handle()
async def good_night(bot: Bot, matcher: Matcher, event: GroupMessageEvent, args: Message = CommandArg()):
arg: str = args.extract_plain_text()
if arg == "帮助":
await matcher.finish(__morning_usages__)

uid: int = event.user_id
gid: int = event.group_id
mem_info = await bot.call_api("get_group_member_info", group_id=gid, user_id=uid)

sex = mem_info["sex"]
if sex == "male":
sex_str = "少年"
Expand All @@ -87,77 +89,82 @@ async def good_night(bot: Bot, matcher: Matcher, event: GroupMessageEvent, args:
msg = morning_manager.get_night_msg(str(gid), str(uid), sex_str)
await matcher.finish(message=msg, at_sender=True)


@my_routine.handle()
async def _(matcher: Matcher, event: GroupMessageEvent):
gid = str(event.group_id)
uid = str(event.user_id)

msg = morning_manager.get_my_routine(gid, uid)
await matcher.finish(message=msg, at_sender=True)


@group_routine.handle()
async def _(bot: Bot, matcher: Matcher, event: GroupMessageEvent):
gid = event.group_id
morning_count, night_count, uid = morning_manager.get_group_routine(str(gid))
msg: str = f"今天已经有{morning_count}位群友早安了,{night_count}位群友晚安了~"

if uid:
mem_info = await bot.call_api("get_group_member_info", group_id=gid, user_id=int(uid))
nickname: str = mem_info["card"] if mem_info["card"] else mem_info["nickname"]
msg += f"\n上周睡觉大王是群友:{nickname},再接再厉~"

await matcher.finish(MessageSegment.text(msg))


@configure.handle()
async def _(matcher: Matcher, event: GroupMessageEvent):
gid = str(event.group_id)
msg = morning_manager.get_group_config(gid)
msg = morning_manager.get_group_config()
await matcher.finish(msg)


def parse_item(_key: str):
'''
Parser setting item
'''
async def _item_parser(matcher: Matcher, arg: str = ArgStr("item")) -> None:
if arg == "取消":
await matcher.finish("操作已取消")

if arg == "时限" or arg == "多重起床" or arg == "超级亢奋" or \
arg == "优质睡眠" or arg == "深度睡眠":
arg == "优质睡眠" or arg == "深度睡眠":
matcher.set_arg("item", Message(arg))
else:
if _key == "morning":
await matcher.reject_arg("item", "输入配置不合法,可选时限/多重起床/超级亢奋")
else:
await matcher.reject_arg("item", "输入配置不合法,可选时限/优质睡眠/深度睡眠")

return _item_parser


def parse_params():
'''
Parser extra params
'''
async def _params_parser(matcher: Matcher, input_args: str = ArgStr("param1")) -> None:
args: List[str] = input_args.split()

logger.info(f"check in _params_parser: {args}")

if args[0] == "取消":
await matcher.finish("操作已取消")

item = matcher.get_arg("item", None)
if not item:
await matcher.finish("配置出错,操作已取消")

if item != "时限":
if len(args) > 1:
await matcher.send("输入参数过多,仅取第一个参数")

try:
_ = int(args[0])
except ValueError:
await matcher.reject_arg("param1", "输入参数必须是纯数字")

matcher.set_arg("param1", Message(args[0]))
else:
if len(args) == 1:
Expand All @@ -168,28 +175,29 @@ async def _params_parser(matcher: Matcher, input_args: str = ArgStr("param1")) -
_ = int(args[1])
except ValueError:
await matcher.send("输入参数必须是纯数字,请重新输入")

matcher.set_arg("param1", Message(args[0]))
matcher.set_arg("param2", Message(args[1]))

return _params_parser


@morning_setting.handle()
async def _(matcher: Matcher, matched: str = RegexMatched()):
args: List[str] = matched.split()
arg_len: int = len(args)

if args[0][-2:] == "开启" or args[0][-2:] == "关闭" or args[0][-2:] == "设置":
matcher.set_arg("op_type", Message(args[0][-2:]))
else:
await matcher.finish("输入指令不合法,可选:开启/关闭/设置")

if arg_len > 1:
if args[1] == "时限" or args[1] == "多重起床" or args[1] == "超级亢奋":
matcher.set_arg("item", Message(args[1]))
else:
await matcher.finish("输入配置不合法,可选:时限/多重起床/超级亢奋")

# Params are numbers, but store in state in string
if arg_len > 2:
if args[1] != "时限":
Expand All @@ -212,10 +220,11 @@ async def _(matcher: Matcher, matched: str = RegexMatched()):
_ = int(args[3])
except ValueError:
await matcher.send("输入参数必须是纯数字,请重新输入")

matcher.set_arg("param1", Message(args[2]))
matcher.set_arg("param2", Message(args[3]))


@morning_setting.got(
"item",
prompt="请选择配置项,可选:时限/多重起床/超级亢奋,输入取消以取消操作",
Expand All @@ -224,6 +233,7 @@ async def _(matcher: Matcher, matched: str = RegexMatched()):
async def _(matcher: Matcher):
matcher.skip()


@morning_setting.got(
"param1",
prompt="请输入设置参数,时限配置项请输入允许的最早/晚的睡觉时间(空格间隔),其余配置项请输入一个时间,输入取消以取消操作",
Expand All @@ -234,51 +244,52 @@ async def _(event: GroupMessageEvent, matcher: Matcher):
_op_type: str = ""
if __op_type:
_op_type = __op_type.extract_plain_text()

__item = matcher.get_arg("item", None)
_item: str = ""
if __item:
_item = __item.extract_plain_text()

__param1 = matcher.get_arg("param1", None)
if __param1:
_param1 = int(__param1.extract_plain_text())
else:
_param1 = 0

__param2 = matcher.get_arg("param2", None)
if __param2:
_param2 = int(__param2.extract_plain_text())
else:
_param2 = 0

if _op_type == "设置":
msg = morning_manager.morning_config(_item, _param1, _param2)
elif _op_type == "开启":
msg = morning_manager.morning_switch(_item, True)
else:
msg = morning_manager.morning_switch(_item, False)

await morning_setting.finish(msg)



@night_setting.handle()
async def _(matcher: Matcher, matched: str = RegexMatched()):
args: List[str] = matched.split()
arg_len: int = len(args)

logger.info(f"check in handle: {args}")

if args[0][-2:] == "开启" or args[0][-2:] == "关闭" or args[0][-2:] == "设置":
matcher.set_arg("op_type", Message(args[0][-2:]))
else:
await matcher.finish("输入指令不合法,可选:开启/关闭/设置")

if arg_len > 1:
if args[1] == "时限" or args[1] == "优质睡眠" or args[1] == "深度睡眠":
matcher.set_arg("item", Message(args[1]))
else:
await matcher.finish("输入配置不合法,可选:时限/优质睡眠/深度睡眠")

# Params are numbers, but store in state in string
if arg_len > 2:
if args[1] != "时限":
Expand All @@ -301,10 +312,11 @@ async def _(matcher: Matcher, matched: str = RegexMatched()):
_ = int(args[3])
except ValueError:
await matcher.send("输入参数必须是纯数字,请重新输入")

matcher.set_arg("param1", Message(args[2]))
matcher.set_arg("param2", Message(args[3]))



@night_setting.got(
"item",
prompt="请选择配置项,可选:时限/优质睡眠/深度睡眠,输入取消以取消操作",
Expand All @@ -313,6 +325,7 @@ async def _(matcher: Matcher, matched: str = RegexMatched()):
async def _(matcher: Matcher):
matcher.skip()


@night_setting.got(
"param1",
prompt="请输入设置参数,时限配置项请输入允许的最早/晚的睡觉时间(空格间隔),其余配置项请输入一个时间,输入取消以取消操作",
Expand All @@ -323,47 +336,53 @@ async def _(event: GroupMessageEvent, matcher: Matcher):
_op_type: str = ""
if __op_type:
_op_type = __op_type.extract_plain_text()

__item = matcher.get_arg("item", None)
_item: str = ""
if __item:
_item = __item.extract_plain_text()

__param1 = matcher.get_arg("param1", None)
if __param1:
_param1 = int(__param1.extract_plain_text())
else:
_param1 = 0

__param2 = matcher.get_arg("param2", None)
if __param2:
_param2 = int(__param2.extract_plain_text())
else:
_param2 = 0

if _op_type == "设置":
msg = morning_manager.night_config(_item, _param1, _param2)
elif _op_type == "开启":
msg = morning_manager.night_switch(_item, True)
else:
msg = morning_manager.night_switch(_item, False)

await night_setting.finish(msg)

# 每日最早晚安时间,重置昨日早晚安计数


@driver.on_startup
async def daily_refresh():
morning_manager.daily_scheduler()
logger.info("每日早晚安定时刷新任务已启动!")

# 每周一最晚晚安时间统计部分周数据


@driver.on_startup
async def monday_weekly_night_refresh():
morning_manager.weekly_night_scheduler()
logger.info("每周晚安定时刷新任务已启动!")

# 每周一最晚早安时间,统计上周睡眠时间、早安并重置


@driver.on_startup
async def weekly_refresh():
morning_manager.weekly_sleep_time_scheduler()
logger.info("每周睡眠时间定时刷新任务已启动!")
logger.info("每周睡眠时间定时刷新任务已启动!")
Loading

0 comments on commit ab526a5

Please sign in to comment.