Skip to content

Commit b876ce7

Browse files
committed
feat: mark platform-specific sv function by decorator
1 parent 5ff3fbf commit b876ce7

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

kokkoro/modules/pcrclanbattle/clanbattle/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# 公主连接Re:Dive会战管理插件
22
# clan == クラン == 戰隊(直译为氏族)(CLANNAD的CLAN(笑))
3+
from functools import wraps
34

45
from .argparse import ArgParser
56
from .exception import *
@@ -23,6 +24,7 @@ def cb_cmd(prefixes, parser:ArgParser) -> Callable:
2324
raise ValueError('`name` of cb_cmd must be `str` or `Iterable[str]`')
2425

2526
def deco(func):
27+
@wraps(func)
2628
async def wrapper(bot: KokkoroBot, ev: EventInterface):
2729
try:
2830
args = parser.parse(ev.get_param().args, ev)

kokkoro/platform_patch.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import kokkoro
12
from kokkoro import config
23

34
def process_mention_me(raw_msg:str):
@@ -25,6 +26,47 @@ def preprocess_message(ev) -> bool:
2526
ev.set_content(processed_msg)
2627
return to_me
2728

29+
PLATFORM_FIELD = '__kkr_platform__'
30+
31+
def discord(func):
32+
func.__setattr__(PLATFORM_FIELD, ['discord'])
33+
return func
34+
35+
def tomon(func):
36+
func.__setattr__(PLATFORM_FIELD, ['tomon'])
37+
return func
38+
39+
def telegram(func):
40+
func.__setattr__(PLATFORM_FIELD, ['telegram'])
41+
return func
42+
43+
def platform(pl):
44+
if type(pl) == str:
45+
pl = [pl]
46+
def deco(func):
47+
func.__setattr__(PLATFORM_FIELD, pl)
48+
return func
49+
return deco
50+
51+
def is_supported(func):
52+
try:
53+
supported_platforms = func.__getattribute__(PLATFORM_FIELD)
54+
except AttributeError as e:
55+
#supported_platforms = "*"
56+
return True
57+
platform = config.BOT_TYPE
58+
if platform in supported_platforms:
59+
return True
60+
return False
61+
62+
def check_platform(on_deco):
63+
def wrapper(sv_func):
64+
if is_supported(sv_func):
65+
return on_deco(sv_func)
66+
else:
67+
kokkoro.logger.warning(f'Function {sv_func.__name__} isn\'t supported in current platform')
68+
return sv_func
69+
return wrapper
2870

2971

3072

kokkoro/service.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
from collections import defaultdict
77

88
import kokkoro
9-
from kokkoro.typing import *
109
from kokkoro import logger
1110
from kokkoro import priv, log, typing, trigger
11+
from kokkoro.typing import *
1212
from kokkoro.common_interface import *
1313
from kokkoro.bot import get_scheduler, get_bot
1414
from kokkoro.util import join_iterable
15+
from kokkoro.platform_patch import check_platform
1516

1617
# service management
1718
_loaded_services: Dict[str, "Service"] = {} # {name: service}
@@ -190,6 +191,7 @@ def get_enable_groups(self) -> dict:
190191
def on_prefix(self, prefix, only_to_me=False) -> Callable:
191192
if isinstance(prefix, str):
192193
prefix = (prefix, )
194+
@check_platform
193195
def deco(func) -> Callable:
194196
sf = ServiceFunc(self, func, only_to_me)
195197
for p in prefix:
@@ -200,6 +202,7 @@ def deco(func) -> Callable:
200202
def on_fullmatch(self, word, only_to_me=False) -> Callable:
201203
if isinstance(word, str):
202204
word = (word, )
205+
@check_platform
203206
def deco(func) -> Callable:
204207
@wraps(func)
205208
async def wrapper(bot: KokkoroBot, ev: EventInterface):
@@ -226,6 +229,7 @@ async def wrapper(bot: KokkoroBot, ev: EventInterface):
226229
def on_suffix(self, suffix, only_to_me=False) -> Callable:
227230
if isinstance(suffix, str):
228231
suffix = (suffix, )
232+
@check_platform
229233
def deco(func) -> Callable:
230234
sf = ServiceFunc(self, func, only_to_me)
231235
for s in suffix:
@@ -236,6 +240,7 @@ def deco(func) -> Callable:
236240
def on_keyword(self, keywords, only_to_me=False) -> Callable:
237241
if isinstance(keywords, str):
238242
keywords = (keywords, )
243+
@check_platform
239244
def deco(func) -> Callable:
240245
sf = ServiceFunc(self, func, only_to_me)
241246
for kw in keywords:
@@ -246,6 +251,7 @@ def deco(func) -> Callable:
246251
def on_rex(self, rex: Union[str, re.Pattern], only_to_me=False) -> Callable:
247252
if isinstance(rex, str):
248253
rex = re.compile(rex)
254+
@check_platform
249255
def deco(func) -> Callable:
250256
sf = ServiceFunc(self, func, only_to_me)
251257
trigger.rex.add(rex, sf)
@@ -261,6 +267,7 @@ def scheduled_job(self, *args, **kwargs) -> Callable:
261267
else:
262268
kwargs.setdefault('misfire_grace_time', 60)
263269

270+
@check_platform
264271
def deco(func: Callable) -> Callable:
265272
kokkoro.logger.debug(f'{func.__name__} registered to scheduler')
266273
@wraps(func)

0 commit comments

Comments
 (0)