Skip to content

Commit

Permalink
✨ 网页控制台可设置同用户相同消息触发CD
Browse files Browse the repository at this point in the history
  • Loading branch information
KimigaiiWuyi committed Dec 22, 2024
1 parent ba6b54d commit f585326
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
2 changes: 1 addition & 1 deletion gsuid_core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async def send_msg(self):
intent = await self._input()
content = Message(type='text', data=intent)
group_id = random.choice(['8888', '88888'])
user_id = random.choice(['99999', '74152'])
user_id = random.choice(['99999'])
msg = MessageReceive(
bot_id='console',
# bot_id='qqgroup',
Expand Down
24 changes: 21 additions & 3 deletions gsuid_core/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@
from gsuid_core.trigger import Trigger
from gsuid_core.config import core_config
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.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')
enable_empty = core_config.get_config('enable_empty_start')
config_masters = core_config.get_config('masters')
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

_command_start: List[str]
Expand All @@ -29,6 +34,14 @@


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)
event = await msg_process(msg)
Expand All @@ -38,10 +51,15 @@ async def handle_event(ws: _Bot, msg: MessageReceive, is_http: bool = False):
local_val['receive'] += 1

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:
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:
for shield_id in shield_list:
Expand Down
18 changes: 18 additions & 0 deletions gsuid_core/utils/cooldown.py
Original file line number Diff line number Diff line change
@@ -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()
21 changes: 13 additions & 8 deletions gsuid_core/utils/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ class Subscribe(BaseModel, table=True):
async def send(
self,
reply: Optional[
Union[Message, List[Message], List[str], str, bytes]
Union[
Message,
List[Message],
List[str],
str,
bytes,
]
] = None,
option_list: Optional[ButtonType] = None,
unsuported_platform: bool = False,
Expand Down Expand Up @@ -157,8 +163,10 @@ async def insert_user(
user_id: str,
group_id: Optional[str],
) -> int:
data: Optional[Type["CoreUser"]] = await cls.base_select_data(
bot_id=bot_id, user_id=user_id, group_id=group_id
data: Optional["CoreUser"] = await cls.base_select_data(
bot_id=bot_id,
user_id=user_id,
group_id=group_id,
)
if not data:
await cls.full_insert_data(
Expand Down Expand Up @@ -423,11 +431,8 @@ async def update_data(
game_name: Optional[str] = None,
**data,
):
sql = (
update(cls)
.where(cls.main_uid == uid)
.where(cls.game_name == game_name)
)
sql = update(cls).where(cls.main_uid == uid)
sql = sql.where(cls.game_name == game_name)
if data is not None:
query = sql.values(**data)
query.execution_options(synchronize_session='fetch')
Expand Down
7 changes: 7 additions & 0 deletions gsuid_core/utils/plugins_config/sp_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@
'消息最前',
['消息最前', '消息最后'],
),
'SameUserEventCD': GsIntConfig(
'启用同个人触发命令CD(0为不启用)',
'启用同个人触发命令CD(0为不启用)',
0,
3600,
[0, 1, 2, 3, 5, 10, 15, 30],
),
}

0 comments on commit f585326

Please sign in to comment.