Skip to content

Commit

Permalink
优化命令冷却组件
Browse files Browse the repository at this point in the history
- 优化命令冷却组件, 可配置权限节点跳过冷却检查
  • Loading branch information
Ailitonia committed Mar 6, 2021
1 parent 4e26bfa commit bf756d3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions omega_miya/plugins/setu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

# 声明本插件可配置的权限节点
__plugin_auth_node__ = [
PluginCoolDown.skip_auth_node,
'setu',
'moepic'
]
Expand Down
21 changes: 18 additions & 3 deletions omega_miya/utils/Omega_CoolDown/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""
插件命令冷却系统
使用参考例:
plugins/setu
"""
from nonebot import get_plugin, get_driver, logger
from nonebot.adapters.cqhttp import MessageSegment, Message
from nonebot.exception import IgnoredException
Expand All @@ -8,8 +13,8 @@
from nonebot.adapters.cqhttp.event import Event
from omega_miya.utils.Omega_plugin_utils import \
check_and_set_global_cool_down, check_and_set_plugin_cool_down, \
check_and_set_group_cool_down, check_and_set_user_cool_down
from omega_miya.utils.Omega_Base import DBCoolDownEvent
check_and_set_group_cool_down, check_and_set_user_cool_down, PluginCoolDown
from omega_miya.utils.Omega_Base import DBCoolDownEvent, DBAuth


@run_preprocessor
Expand All @@ -35,10 +40,20 @@ async def handle_plugin_cooldown(matcher: Matcher, bot: Bot, event: Event, state
plugin = get_plugin(plugin_name)
plugin_cool_down_list = plugin.export.get('cool_down')

# 只有声明了__plugin_cool_down__的插件才检查冷却
# 只处理声明了__plugin_cool_down__的插件
if not plugin_cool_down_list:
return

# 检查用户或群组是否有skip_cd权限, 跳过冷却检查
skip_cd_auth_node = f'{plugin_name}.{PluginCoolDown.skip_auth_node}'
user_auth_res = DBAuth(auth_id=user_id, auth_type='user', auth_node=skip_cd_auth_node)
if user_auth_res.allow_tag().result == 1 and user_auth_res.deny_tag().result == 0:
return

group_auth_res = DBAuth(auth_id=group_id, auth_type='group', auth_node=skip_cd_auth_node)
if group_auth_res.allow_tag().result == 1 and group_auth_res.deny_tag().result == 0:
return

# 检查冷却情况
global_check = DBCoolDownEvent.check_global_cool_down_event()
plugin_check = DBCoolDownEvent.check_plugin_cool_down_event(plugin=plugin_name)
Expand Down
3 changes: 2 additions & 1 deletion omega_miya/utils/Omega_plugin_utils/cooldown.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import datetime
from omega_miya.utils.Omega_Base import DBCoolDownEvent, Result
from dataclasses import dataclass
from dataclasses import dataclass, field


@dataclass
class PluginCoolDown:
skip_auth_node: str = field(default='skip_cd', init=False)
plugin_name: str
type: str
cool_down_time: int
Expand Down

0 comments on commit bf756d3

Please sign in to comment.