summaryrefslogtreecommitdiff
path: root/ATRI/rule.py
diff options
context:
space:
mode:
authorKyomotoi <[email protected]>2021-01-26 18:43:13 +0800
committerKyomotoi <[email protected]>2021-01-26 18:43:13 +0800
commit7640568a42493bc5a5e44bc82b1ecfa87e51c5f1 (patch)
tree60f29b756d58b085c95573515d3e4bd6f41426a8 /ATRI/rule.py
parentd0d31b2630697c97e848f00142f06b81f63b255a (diff)
downloadATRI-7640568a42493bc5a5e44bc82b1ecfa87e51c5f1.tar.gz
ATRI-7640568a42493bc5a5e44bc82b1ecfa87e51c5f1.tar.bz2
ATRI-7640568a42493bc5a5e44bc82b1ecfa87e51c5f1.zip
[Update]
Diffstat (limited to 'ATRI/rule.py')
-rw-r--r--ATRI/rule.py114
1 files changed, 87 insertions, 27 deletions
diff --git a/ATRI/rule.py b/ATRI/rule.py
index 1ed10d8..19930e5 100644
--- a/ATRI/rule.py
+++ b/ATRI/rule.py
@@ -1,46 +1,106 @@
-from re import T
+import datetime
+from random import choice
+
+from nonebot.adapters import Event
from nonebot.rule import Rule
-from nonebot.typing import Bot, Event
+from nonebot.adapters.cqhttp import GroupMessageEvent
-from ATRI.service import Service
+from .service.switch import Switch
+from .service.banlist import BanList
+from .service.dormant import Dormant
+from .apscheduler import scheduler, DateTrigger
+from .config import config
+from .utils import count_list, del_list_aim
-def isInService(service: str) -> Rule:
- async def _isInService(bot: Bot, event: Event, state: dict) -> bool:
- return Service().Switch().auth_service(service, event.group_id)
+warting_list = []
+fast_user_list = []
- return Rule(_isInService)
+def is_in_service(service: str) -> Rule:
+ async def _is_in_service(bot, event, state) -> bool:
+ if isinstance(event, GroupMessageEvent):
+ return await Switch.auth_service(service, event.group_id)
+ return await Switch.auth_service(service)
-def isInBanList() -> Rule:
- async def _isInBanList(bot: Bot, event: Event, state: dict) -> bool:
- return Service().BanList().is_in_list(event.user_id)
+ return Rule(_is_in_service)
- return Rule(_isInBanList)
+def is_in_ban_list() -> Rule:
+ async def _is_in_ban_list(bot, event, state) -> bool:
+ return BanList.is_in_list(event.get_user_id())
-def isInDormant() -> Rule:
- async def _isInDormant(bot: Bot, event: Event, state: dict) -> bool:
- return Service().Dormant().is_sleep()
+ return Rule(_is_in_ban_list)
- return Rule(_isInDormant)
+def is_in_dormant() -> Rule:
+ async def _is_in_dormant(bot, event, state) -> bool:
+ return Dormant.is_sleep()
-def toGroup() -> Rule:
- async def _toGroup(bot: Bot, event: Event, state: dict) -> bool:
- return bool(event.group_id)
+ return Rule(_is_in_dormant)
- return Rule(_toGroup)
+def is_max_times(max_times: int, time_day: int,
+ time_hour: int, time_min: int) -> Rule:
+ async def _is_max_times(bot, event: Event, state) -> bool:
+ global warting_list
+ user = event.get_user_id()
+ if user in warting_list:
+ return False
+
+
+
+
+ return True
+
+ return Rule(_is_max_times)
-def toPrivate() -> Rule:
- async def _toPrivate(bot: Bot, event: Event, state: dict) -> bool:
- return not bool(event.group_id)
+def is_too_fast(times: int, _type: str) -> Rule:
+ def remove_list(user: str) -> None:
+ global fast_user_list
+ fast_user_list = del_list_aim(fast_user_list, user)
+
+ async def _is_too_fast(bot, event: Event, state) -> bool:
+ global fast_user_list
+ user = event.get_user_id()
+
+ if user in fast_user_list:
+ await bot.send(
+ event, choice(config['bot']['session_waiting_repo']))
+ return False
+ else:
+ if count_list(fast_user_list, user) == times:
+ delta = datetime.timedelta(
+ seconds=config['bot']['session_waiting_time'])
+ trigger = DateTrigger(
+ run_date=datetime.datetime.now() + delta)
+
+ scheduler.add_job(
+ func=remove_list,
+ trigger=trigger,
+ args=(user,),
+ misfire_grace_time=1,
+ )
+
+ await bot.send(
+ event, choice(config['bot']['session_waiting_repo']))
+ return False
+ else:
+ fast_user_list.append(user)
+ return True
+
+ return Rule(_is_too_fast)
+
+
+def to_bot() -> Rule:
+ async def _to_bot(bot, event: Event, state) -> bool:
+ return event.is_tome()
+
+ return Rule(_to_bot)
- return Rule(_toPrivate)
-def toBot() -> Rule:
- async def _toBot(bot: Bot, event: Event, state: dict) -> bool:
- return bool(event.to_me)
+def poke() -> Rule:
+ async def _poke(bot, event, state) -> bool:
+ return True if event.is_tome() else False
- return Rule(_toBot)
+ return Rule(_poke)