def on_startswith(msg: str, rule: Optional[Union[Rule, RuleChecker]] = None, permission: Permission = Permission(), **kwargs) -> Type[Matcher]: return on_message(startswith(msg) & rule, permission, **kwargs) if rule else on_message( startswith(msg), permission, **kwargs)
def on_startswith( msg: Union[str, Tuple[str, ...]], rule: Optional[Optional[Union[Rule, T_RuleChecker]]] = None, ignorecase: bool = False, _depth: int = 0, **kwargs, ) -> Type[Matcher]: """ :说明: 注册一个消息事件响应器,并且当消息的**文本部分**以指定内容开头时响应。 :参数: * ``msg: Union[str, Tuple[str, ...]]``: 指定消息开头内容 * ``rule: Optional[Union[Rule, T_RuleChecker]]``: 事件响应规则 * ``ignorecase: bool``: 是否忽略大小写 * ``permission: Optional[Union[Permission, T_PermissionChecker]] =]]``: 事件响应权限 * ``handlers: Optional[List[Union[T_Handler, Dependent]]]``: 事件处理函数列表 * ``temp: bool``: 是否为临时事件响应器(仅执行一次) * ``priority: int``: 事件响应器优先级 * ``block: bool``: 是否阻止事件向更低优先级传递 * ``state: Optional[T_State]``: 默认 state :返回: - ``Type[Matcher]`` """ return on_message(startswith(msg, ignorecase) & rule, **kwargs, _depth=_depth + 1)
def on_startswith(msg: str, rule: Optional[Optional[Union[Rule, T_RuleChecker]]] = None, **kwargs) -> Type[Matcher]: """ :说明: 注册一个消息事件响应器,并且当消息的**文本部分**以指定内容开头时响应。 :参数: * ``msg: str``: 指定消息开头内容 * ``rule: Optional[Union[Rule, T_RuleChecker]]``: 事件响应规则 * ``permission: Optional[Permission]``: 事件响应权限 * ``handlers: Optional[List[T_Handler]]``: 事件处理函数列表 * ``temp: bool``: 是否为临时事件响应器(仅执行一次) * ``priority: int``: 事件响应器优先级 * ``block: bool``: 是否阻止事件向更低优先级传递 * ``state: Optional[T_State]``: 默认 state * ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数 :返回: - ``Type[Matcher]`` """ return on_message(startswith(msg) & rule, **kwargs)
async def test_startswith( app: App, msg: Union[str, Tuple[str, ...]], ignorecase: bool, type: str, text: str, expected: bool, ): from nonebot.rule import StartswithRule, startswith test_startswith = startswith(msg, ignorecase) dependent = list(test_startswith.checkers)[0] checker = dependent.call assert isinstance(checker, StartswithRule) assert checker.msg == (msg, ) if isinstance(msg, str) else msg assert checker.ignorecase == ignorecase message = make_fake_message()(text) event = make_fake_event(_type=type, _message=message)() assert await dependent(event=event) == expected
def on_startswith( msg: Union[str, Tuple[str, ...]], rule: Optional[Union[Rule, T_RuleChecker]] = None, ignorecase: bool = False, _depth: int = 0, **kwargs, ) -> Type[Matcher]: """ 注册一个消息事件响应器,并且当消息的**文本部分**以指定内容开头时响应。 参数: msg: 指定消息开头内容 rule: 事件响应规则 ignorecase: 是否忽略大小写 permission: 事件响应权限 handlers: 事件处理函数列表 temp: 是否为临时事件响应器(仅执行一次) priority: 事件响应器优先级 block: 是否阻止事件向更低优先级传递 state: 默认 state """ return on_message(startswith(msg, ignorecase) & rule, **kwargs, _depth=_depth + 1)
from .config import Config from nonebot import (on_command, on_message, logger) from nonebot.rule import to_me, startswith from nonebot.adapters import Bot from nonebot.adapters.cqhttp import MessageEvent, Message from nonebot.adapters.cqhttp.message import MessageSegment from .data_source import redis_client add = on_command("add", rule=startswith("add"), priority=5) @add.handle() async def _(bot: Bot, event: MessageEvent): stripped_arg = event.raw_message.split()[1:] logger.debug(f"{stripped_arg}, {len(stripped_arg)}") if len(list(stripped_arg)) < 2: await add.finish("输入错误") return elif stripped_arg[-1] == '帅': await add.finish('咕咕鸟从来不骗人, 不能说这句') return else: keyword, sentence = stripped_arg[0], ''.join( str(i) for i in stripped_arg[1:]) await redis_client.sadd(keyword, sentence) await add.finish(f"你说: {keyword}, 我说: {sentence}") del_event = on_command("del", rule=to_me(), priority=5)
from nonebot import on_command import nonebot from nonebot.rule import startswith from nonebot.adapters.cqhttp import Bot, Event import Pbot.cq as cq, json dg = on_command("点歌", rule=startswith("点歌")) parm = {"type": "1", "s": "十年", "limit": 1} @dg.handle() async def firsthandle(bot: Bot, event: Event, state: dict): msg = str(event.message).strip() if msg == "": dg.finish("参数为空!!!") parm["s"] = msg async with bot.config.session.get( "https://music.163.com/api/search/get/web", params=parm) as resp: if resp.status != 200: dg.finish("网络错误哦,咕噜灵波~(∠・ω< )⌒★") ShitJson = await resp.read() ShitJson = json.loads(ShitJson) ShitJson = ShitJson["result"]["songs"][0]["id"] await dg.send( cq.music("163", ShitJson), auto_escape=False, )
def on_command(name, *args, **kwargs): return _on_command(name, *args, rule=startswith("."), **kwargs)