mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-09-21 05:12:34 +00:00
fix some bug
This commit is contained in:
parent
87a848d8c3
commit
a895089a8b
@ -148,19 +148,17 @@ class Driver(BaseDriver):
|
|||||||
access_token: Optional[str] = Depends(get_auth_bearer)):
|
access_token: Optional[str] = Depends(get_auth_bearer)):
|
||||||
secret = self.config.secret
|
secret = self.config.secret
|
||||||
if secret is not None and secret != access_token:
|
if secret is not None and secret != access_token:
|
||||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
|
await websocket.close(code=status.WS_1008_POLICY_VIOLATION)
|
||||||
detail="Not authenticated",
|
|
||||||
headers={"WWW-Authenticate": "Bearer"})
|
|
||||||
|
|
||||||
websocket = WebSocket(websocket)
|
websocket = WebSocket(websocket)
|
||||||
|
|
||||||
if not x_self_id:
|
if not x_self_id:
|
||||||
logger.error(f"Error Connection Unkown: self_id {x_self_id}")
|
logger.error(f"Error Connection Unkown: self_id {x_self_id}")
|
||||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
await websocket.close(code=status.WS_1008_POLICY_VIOLATION)
|
||||||
|
|
||||||
if x_self_id in self._clients:
|
if x_self_id in self._clients:
|
||||||
logger.error(f"Error Connection Conflict: self_id {x_self_id}")
|
logger.error(f"Error Connection Conflict: self_id {x_self_id}")
|
||||||
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
|
await websocket.close(code=status.WS_1008_POLICY_VIOLATION)
|
||||||
|
|
||||||
# Create Bot Object
|
# Create Bot Object
|
||||||
if adapter in self._adapters:
|
if adapter in self._adapters:
|
||||||
|
@ -147,8 +147,6 @@ class Matcher:
|
|||||||
else:
|
else:
|
||||||
state[state["_current_key"]] = str(event.message)
|
state[state["_current_key"]] = str(event.message)
|
||||||
|
|
||||||
if cls.handlers:
|
|
||||||
# 已有前置handlers则接受一条新的消息,否则视为接收初始消息
|
|
||||||
cls.handlers.append(_key_getter)
|
cls.handlers.append(_key_getter)
|
||||||
cls.handlers.append(_key_parser)
|
cls.handlers.append(_key_parser)
|
||||||
|
|
||||||
|
@ -65,6 +65,7 @@ async def handle_event(bot: Bot, event: Event):
|
|||||||
return
|
return
|
||||||
|
|
||||||
# Trie Match
|
# Trie Match
|
||||||
|
if event.type == "message":
|
||||||
_, _ = TrieRule.get_value(bot, event, state)
|
_, _ = TrieRule.get_value(bot, event, state)
|
||||||
|
|
||||||
break_flag = False
|
break_flag = False
|
||||||
|
@ -142,10 +142,12 @@ def on_endswith(msg: str,
|
|||||||
startswith(msg), permission, **kwargs)
|
startswith(msg), permission, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def on_command(cmd: Tuple[str],
|
def on_command(cmd: Union[str, Tuple[str]],
|
||||||
rule: Optional[Union[Rule, RuleChecker]] = None,
|
rule: Optional[Union[Rule, RuleChecker]] = None,
|
||||||
permission: Permission = Permission(),
|
permission: Permission = Permission(),
|
||||||
**kwargs) -> Type[Matcher]:
|
**kwargs) -> Type[Matcher]:
|
||||||
|
if isinstance(cmd, str):
|
||||||
|
cmd = (cmd,)
|
||||||
return on_message(command(cmd) &
|
return on_message(command(cmd) &
|
||||||
rule, permission, **kwargs) if rule else on_message(
|
rule, permission, **kwargs) if rule else on_message(
|
||||||
command(cmd), permission, **kwargs)
|
command(cmd), permission, **kwargs)
|
||||||
|
@ -125,3 +125,11 @@ def regex(regex: str, flags: Union[int, re.RegexFlag] = 0) -> Rule:
|
|||||||
return bool(pattern.search(str(event.message)))
|
return bool(pattern.search(str(event.message)))
|
||||||
|
|
||||||
return Rule(_regex)
|
return Rule(_regex)
|
||||||
|
|
||||||
|
|
||||||
|
def to_me() -> Rule:
|
||||||
|
|
||||||
|
async def _to_me(bot: Bot, event: Event, state: dict) -> bool:
|
||||||
|
return bool(event.to_me)
|
||||||
|
|
||||||
|
return Rule(_to_me)
|
||||||
|
@ -10,13 +10,12 @@ test_message = on_message(state={"default": 1})
|
|||||||
|
|
||||||
@test_message.handle()
|
@test_message.handle()
|
||||||
async def test_handler(bot: Bot, event: Event, state: dict):
|
async def test_handler(bot: Bot, event: Event, state: dict):
|
||||||
print("Test Matcher Received:", event)
|
print("[*] Test Matcher Received:", event)
|
||||||
print("Current State:", state)
|
|
||||||
state["event"] = event
|
state["event"] = event
|
||||||
await bot.send_private_msg(message="Received", user_id=event.user_id)
|
await bot.send_private_msg(message="Received", user_id=event.user_id)
|
||||||
|
|
||||||
|
|
||||||
@test_message.receive()
|
@test_message.receive()
|
||||||
async def test_receive(bot: Bot, event: Event, state: dict):
|
async def test_receive(bot: Bot, event: Event, state: dict):
|
||||||
print("Test Matcher Received next time:", event)
|
print("[*] Test Matcher Received next time:", event)
|
||||||
print("Current State:", state)
|
print("[*] Current State:", state)
|
||||||
|
@ -12,6 +12,6 @@ async def heartbeat(bot: Bot, event: Event, state: dict) -> bool:
|
|||||||
test_matcher = on_metaevent(heartbeat)
|
test_matcher = on_metaevent(heartbeat)
|
||||||
|
|
||||||
|
|
||||||
@test_matcher.handle()
|
@test_matcher.receive()
|
||||||
async def handle_heartbeat(bot: Bot, event: Event, state: dict):
|
async def handle_heartbeat(bot: Bot, event: Event, state: dict):
|
||||||
print("[i] Heartbeat")
|
print("[i] Heartbeat")
|
||||||
|
@ -1,14 +1,29 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from nonebot.rule import Rule
|
from nonebot.rule import to_me
|
||||||
from nonebot.typing import Event
|
from nonebot.typing import Event
|
||||||
from nonebot.plugin import on_command
|
from nonebot.plugin import on_command
|
||||||
from nonebot.adapters.cqhttp import Bot
|
from nonebot.adapters.cqhttp import Bot
|
||||||
|
|
||||||
test_command = on_command(("帮助",))
|
test_command = on_command("帮助", to_me())
|
||||||
|
|
||||||
|
|
||||||
@test_command.handle()
|
@test_command.handle()
|
||||||
async def test_handler(bot: Bot, event: Event, state: dict):
|
async def test_handler(bot: Bot, event: Event, state: dict):
|
||||||
print(state["_prefix"])
|
print("[!] Command:", state["_prefix"])
|
||||||
|
args = str(event.message)[len(state["_prefix"]):].strip()
|
||||||
|
if args:
|
||||||
|
state["help"] = args
|
||||||
|
else:
|
||||||
|
await bot.send_private_msg(message="命令:\n1. test1\n2. test2",
|
||||||
|
user_id=event.user_id)
|
||||||
|
|
||||||
|
|
||||||
|
@test_command.got("help", prompt="你要帮助的命令是?")
|
||||||
|
async def test_handler(bot: Bot, event: Event, state: dict):
|
||||||
|
print("[!] Command 帮助:", state["help"])
|
||||||
|
if state["help"] not in ["test1", "test2"]:
|
||||||
|
await bot.send_private_msg(message=f"{state['help']} 不支持,请重新输入!")
|
||||||
|
test_command.reject()
|
||||||
|
await bot.send_private_msg(message=f"{state['help']} 帮助:\n...")
|
||||||
|
Loading…
Reference in New Issue
Block a user