mirror of
https://github.com/Genshin-bots/gsuid_core.git
synced 2025-05-11 22:45:48 +08:00
✨ 网页控制台可设置同用户相同消息触发CD
This commit is contained in:
parent
ba6b54d379
commit
f58532656a
@ -48,7 +48,7 @@ class GsClient:
|
|||||||
intent = await self._input()
|
intent = await self._input()
|
||||||
content = Message(type='text', data=intent)
|
content = Message(type='text', data=intent)
|
||||||
group_id = random.choice(['8888', '88888'])
|
group_id = random.choice(['8888', '88888'])
|
||||||
user_id = random.choice(['99999', '74152'])
|
user_id = random.choice(['99999'])
|
||||||
msg = MessageReceive(
|
msg = MessageReceive(
|
||||||
bot_id='console',
|
bot_id='console',
|
||||||
# bot_id='qqgroup',
|
# bot_id='qqgroup',
|
||||||
|
@ -10,15 +10,20 @@ from gsuid_core.logger import logger
|
|||||||
from gsuid_core.trigger import Trigger
|
from gsuid_core.trigger import Trigger
|
||||||
from gsuid_core.config import core_config
|
from gsuid_core.config import core_config
|
||||||
from gsuid_core.global_val import get_global_val
|
from gsuid_core.global_val import get_global_val
|
||||||
|
from gsuid_core.utils.cooldown import cooldown_tracker
|
||||||
from gsuid_core.models import Event, Message, MessageReceive
|
from gsuid_core.models import Event, Message, MessageReceive
|
||||||
from gsuid_core.utils.database.models import CoreUser, CoreGroup
|
from gsuid_core.utils.database.models import CoreUser, CoreGroup
|
||||||
from gsuid_core.utils.plugins_config.gs_config import core_plugins_config
|
from gsuid_core.utils.plugins_config.gs_config import (
|
||||||
|
sp_config,
|
||||||
|
core_plugins_config,
|
||||||
|
)
|
||||||
|
|
||||||
command_start = core_config.get_config('command_start')
|
command_start = core_config.get_config('command_start')
|
||||||
enable_empty = core_config.get_config('enable_empty_start')
|
enable_empty = core_config.get_config('enable_empty_start')
|
||||||
config_masters = core_config.get_config('masters')
|
config_masters = core_config.get_config('masters')
|
||||||
config_superusers = core_config.get_config('superusers')
|
config_superusers = core_config.get_config('superusers')
|
||||||
|
|
||||||
|
same_user_cd: int = sp_config.get_config('SameUserEventCD').data
|
||||||
shield_list = core_plugins_config.get_config('ShieldQQBot').data
|
shield_list = core_plugins_config.get_config('ShieldQQBot').data
|
||||||
|
|
||||||
_command_start: List[str]
|
_command_start: List[str]
|
||||||
@ -29,6 +34,14 @@ else:
|
|||||||
|
|
||||||
|
|
||||||
async def handle_event(ws: _Bot, msg: MessageReceive, is_http: bool = False):
|
async def handle_event(ws: _Bot, msg: MessageReceive, is_http: bool = False):
|
||||||
|
# 是否启用相同消息CD
|
||||||
|
if same_user_cd != 0 and cooldown_tracker.is_on_cooldown(
|
||||||
|
msg.user_id,
|
||||||
|
same_user_cd,
|
||||||
|
):
|
||||||
|
logger.trace(f'[GsCore][触发相同消息CD] 忽略{msg.user_id}该消息!')
|
||||||
|
return
|
||||||
|
|
||||||
# 获取用户权限,越小越高
|
# 获取用户权限,越小越高
|
||||||
msg.user_pm = user_pm = await get_user_pml(msg)
|
msg.user_pm = user_pm = await get_user_pml(msg)
|
||||||
event = await msg_process(msg)
|
event = await msg_process(msg)
|
||||||
@ -38,10 +51,15 @@ async def handle_event(ws: _Bot, msg: MessageReceive, is_http: bool = False):
|
|||||||
local_val['receive'] += 1
|
local_val['receive'] += 1
|
||||||
|
|
||||||
await CoreUser.insert_user(
|
await CoreUser.insert_user(
|
||||||
event.real_bot_id, event.user_id, event.group_id
|
event.real_bot_id,
|
||||||
|
event.user_id,
|
||||||
|
event.group_id,
|
||||||
)
|
)
|
||||||
if event.group_id:
|
if event.group_id:
|
||||||
await CoreGroup.insert_group(event.real_bot_id, event.group_id)
|
await CoreGroup.insert_group(
|
||||||
|
event.real_bot_id,
|
||||||
|
event.group_id,
|
||||||
|
)
|
||||||
|
|
||||||
if event.at:
|
if event.at:
|
||||||
for shield_id in shield_list:
|
for shield_id in shield_list:
|
||||||
|
18
gsuid_core/utils/cooldown.py
Normal file
18
gsuid_core/utils/cooldown.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
class CooldownTracker:
|
||||||
|
def __init__(self):
|
||||||
|
self.timestamps = {}
|
||||||
|
|
||||||
|
def is_on_cooldown(self, user_id: str, cooldown: float):
|
||||||
|
now = time.time()
|
||||||
|
last_time = self.timestamps.get(user_id, 0)
|
||||||
|
print(self.timestamps)
|
||||||
|
if now - last_time >= cooldown:
|
||||||
|
self.timestamps[user_id] = now
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
cooldown_tracker = CooldownTracker()
|
@ -31,7 +31,13 @@ class Subscribe(BaseModel, table=True):
|
|||||||
async def send(
|
async def send(
|
||||||
self,
|
self,
|
||||||
reply: Optional[
|
reply: Optional[
|
||||||
Union[Message, List[Message], List[str], str, bytes]
|
Union[
|
||||||
|
Message,
|
||||||
|
List[Message],
|
||||||
|
List[str],
|
||||||
|
str,
|
||||||
|
bytes,
|
||||||
|
]
|
||||||
] = None,
|
] = None,
|
||||||
option_list: Optional[ButtonType] = None,
|
option_list: Optional[ButtonType] = None,
|
||||||
unsuported_platform: bool = False,
|
unsuported_platform: bool = False,
|
||||||
@ -157,8 +163,10 @@ class CoreUser(BaseBotIDModel, table=True):
|
|||||||
user_id: str,
|
user_id: str,
|
||||||
group_id: Optional[str],
|
group_id: Optional[str],
|
||||||
) -> int:
|
) -> int:
|
||||||
data: Optional[Type["CoreUser"]] = await cls.base_select_data(
|
data: Optional["CoreUser"] = await cls.base_select_data(
|
||||||
bot_id=bot_id, user_id=user_id, group_id=group_id
|
bot_id=bot_id,
|
||||||
|
user_id=user_id,
|
||||||
|
group_id=group_id,
|
||||||
)
|
)
|
||||||
if not data:
|
if not data:
|
||||||
await cls.full_insert_data(
|
await cls.full_insert_data(
|
||||||
@ -423,11 +431,8 @@ class GsUID(BaseIDModel, table=True):
|
|||||||
game_name: Optional[str] = None,
|
game_name: Optional[str] = None,
|
||||||
**data,
|
**data,
|
||||||
):
|
):
|
||||||
sql = (
|
sql = update(cls).where(cls.main_uid == uid)
|
||||||
update(cls)
|
sql = sql.where(cls.game_name == game_name)
|
||||||
.where(cls.main_uid == uid)
|
|
||||||
.where(cls.game_name == game_name)
|
|
||||||
)
|
|
||||||
if data is not None:
|
if data is not None:
|
||||||
query = sql.values(**data)
|
query = sql.values(**data)
|
||||||
query.execution_options(synchronize_session='fetch')
|
query.execution_options(synchronize_session='fetch')
|
||||||
|
@ -21,4 +21,11 @@ SP_CONIFG: Dict[str, GSC] = {
|
|||||||
'消息最前',
|
'消息最前',
|
||||||
['消息最前', '消息最后'],
|
['消息最前', '消息最后'],
|
||||||
),
|
),
|
||||||
|
'SameUserEventCD': GsIntConfig(
|
||||||
|
'启用同个人触发命令CD(0为不启用)',
|
||||||
|
'启用同个人触发命令CD(0为不启用)',
|
||||||
|
0,
|
||||||
|
3600,
|
||||||
|
[0, 1, 2, 3, 5, 10, 15, 30],
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user