mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-16 11:00:54 +00:00
add permission and command
This commit is contained in:
@ -5,6 +5,7 @@ import asyncio
|
||||
from datetime import datetime
|
||||
|
||||
from nonebot.log import logger
|
||||
from nonebot.rule import TrieRule
|
||||
from nonebot.matcher import matchers
|
||||
from nonebot.exception import IgnoredException
|
||||
from nonebot.typing import Bot, Set, Event, PreProcessor
|
||||
@ -19,8 +20,9 @@ def event_preprocessor(func: PreProcessor) -> PreProcessor:
|
||||
|
||||
async def handle_event(bot: Bot, event: Event):
|
||||
coros = []
|
||||
state = {}
|
||||
for preprocessor in _event_preprocessors:
|
||||
coros.append(preprocessor(bot, event))
|
||||
coros.append(preprocessor(bot, event, state))
|
||||
if coros:
|
||||
try:
|
||||
await asyncio.gather(*coros)
|
||||
@ -28,6 +30,9 @@ async def handle_event(bot: Bot, event: Event):
|
||||
logger.info(f"Event {event} is ignored")
|
||||
return
|
||||
|
||||
# Trie Match
|
||||
_, _ = TrieRule.get_value(bot, event, state)
|
||||
|
||||
for priority in sorted(matchers.keys()):
|
||||
index = 0
|
||||
while index <= len(matchers[priority]):
|
||||
@ -40,7 +45,9 @@ async def handle_event(bot: Bot, event: Event):
|
||||
|
||||
# Check rule
|
||||
try:
|
||||
if not await Matcher.check_rule(bot, event):
|
||||
if not await Matcher.check_perm(
|
||||
bot, event) or not await Matcher.check_rule(
|
||||
bot, event, state):
|
||||
index += 1
|
||||
continue
|
||||
except Exception as e:
|
||||
@ -55,7 +62,7 @@ async def handle_event(bot: Bot, event: Event):
|
||||
del matchers[priority][index]
|
||||
|
||||
try:
|
||||
await matcher.run(bot, event)
|
||||
await matcher.run(bot, event, state)
|
||||
except Exception as e:
|
||||
logger.error(f"Running matcher {matcher} failed.")
|
||||
logger.exception(e)
|
||||
|
Reference in New Issue
Block a user