mirror of
				https://github.com/nonebot/nonebot2.git
				synced 2025-10-31 06:56:39 +00:00 
			
		
		
		
	✨ Feature: 新增事件类型过滤 rule (#1183)
This commit is contained in:
		
							
								
								
									
										1
									
								
								tests/plugins/plugin/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								tests/plugins/plugin/__init__.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| from . import matchers | ||||
							
								
								
									
										243
									
								
								tests/plugins/plugin/matchers.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										243
									
								
								tests/plugins/plugin/matchers.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,243 @@ | ||||
| from datetime import datetime, timezone | ||||
|  | ||||
| from nonebot.adapters import Event | ||||
| from nonebot import ( | ||||
|     CommandGroup, | ||||
|     MatcherGroup, | ||||
|     on, | ||||
|     on_type, | ||||
|     on_regex, | ||||
|     on_notice, | ||||
|     on_command, | ||||
|     on_keyword, | ||||
|     on_message, | ||||
|     on_request, | ||||
|     on_endswith, | ||||
|     on_fullmatch, | ||||
|     on_metaevent, | ||||
|     on_startswith, | ||||
|     on_shell_command, | ||||
| ) | ||||
|  | ||||
|  | ||||
| async def rule() -> bool: | ||||
|     return True | ||||
|  | ||||
|  | ||||
| async def permission() -> bool: | ||||
|     return True | ||||
|  | ||||
|  | ||||
| async def handler(): | ||||
|     return | ||||
|  | ||||
|  | ||||
| expire_time = datetime.now(timezone.utc) | ||||
| priority = 100 | ||||
| state = {"test": "test"} | ||||
|  | ||||
|  | ||||
| matcher_on = on( | ||||
|     "test", | ||||
|     rule=rule, | ||||
|     permission=permission, | ||||
|     handlers=[handler], | ||||
|     temp=True, | ||||
|     expire_time=expire_time, | ||||
|     priority=priority, | ||||
|     block=True, | ||||
|     state=state, | ||||
| ) | ||||
|  | ||||
|  | ||||
| matcher_on_metaevent = on_metaevent( | ||||
|     rule=rule, | ||||
|     handlers=[handler], | ||||
|     temp=True, | ||||
|     expire_time=expire_time, | ||||
|     priority=priority, | ||||
|     block=True, | ||||
|     state=state, | ||||
| ) | ||||
|  | ||||
|  | ||||
| matcher_on_message = on_message( | ||||
|     rule=rule, | ||||
|     permission=permission, | ||||
|     handlers=[handler], | ||||
|     temp=True, | ||||
|     expire_time=expire_time, | ||||
|     priority=priority, | ||||
|     block=True, | ||||
|     state=state, | ||||
| ) | ||||
|  | ||||
|  | ||||
| matcher_on_notice = on_notice( | ||||
|     rule=rule, | ||||
|     handlers=[handler], | ||||
|     temp=True, | ||||
|     expire_time=expire_time, | ||||
|     priority=priority, | ||||
|     block=True, | ||||
|     state=state, | ||||
| ) | ||||
|  | ||||
|  | ||||
| matcher_on_request = on_request( | ||||
|     rule=rule, | ||||
|     handlers=[handler], | ||||
|     temp=True, | ||||
|     expire_time=expire_time, | ||||
|     priority=priority, | ||||
|     block=True, | ||||
|     state=state, | ||||
| ) | ||||
|  | ||||
|  | ||||
| matcher_on_startswith = on_startswith( | ||||
|     "test", | ||||
|     rule=rule, | ||||
|     permission=permission, | ||||
|     handlers=[handler], | ||||
|     temp=True, | ||||
|     expire_time=expire_time, | ||||
|     priority=priority, | ||||
|     block=True, | ||||
|     state=state, | ||||
| ) | ||||
|  | ||||
|  | ||||
| matcher_on_endswith = on_endswith( | ||||
|     "test", | ||||
|     rule=rule, | ||||
|     permission=permission, | ||||
|     handlers=[handler], | ||||
|     temp=True, | ||||
|     expire_time=expire_time, | ||||
|     priority=priority, | ||||
|     block=True, | ||||
|     state=state, | ||||
| ) | ||||
|  | ||||
|  | ||||
| matcher_on_fullmatch = on_fullmatch( | ||||
|     "test", | ||||
|     rule=rule, | ||||
|     permission=permission, | ||||
|     handlers=[handler], | ||||
|     temp=True, | ||||
|     expire_time=expire_time, | ||||
|     priority=priority, | ||||
|     block=True, | ||||
|     state=state, | ||||
| ) | ||||
|  | ||||
|  | ||||
| matcher_on_keyword = on_keyword( | ||||
|     {"test"}, | ||||
|     rule=rule, | ||||
|     permission=permission, | ||||
|     handlers=[handler], | ||||
|     temp=True, | ||||
|     expire_time=expire_time, | ||||
|     priority=priority, | ||||
|     block=True, | ||||
|     state=state, | ||||
| ) | ||||
|  | ||||
|  | ||||
| matcher_on_command = on_command( | ||||
|     "test", | ||||
|     rule=rule, | ||||
|     permission=permission, | ||||
|     handlers=[handler], | ||||
|     temp=True, | ||||
|     expire_time=expire_time, | ||||
|     priority=priority, | ||||
|     block=True, | ||||
|     state=state, | ||||
| ) | ||||
|  | ||||
|  | ||||
| matcher_on_shell_command = on_shell_command( | ||||
|     "test", | ||||
|     rule=rule, | ||||
|     permission=permission, | ||||
|     handlers=[handler], | ||||
|     temp=True, | ||||
|     expire_time=expire_time, | ||||
|     priority=priority, | ||||
|     block=True, | ||||
|     state=state, | ||||
| ) | ||||
|  | ||||
|  | ||||
| matcher_on_regex = on_regex( | ||||
|     "test", | ||||
|     rule=rule, | ||||
|     permission=permission, | ||||
|     handlers=[handler], | ||||
|     temp=True, | ||||
|     expire_time=expire_time, | ||||
|     priority=priority, | ||||
|     block=True, | ||||
|     state=state, | ||||
| ) | ||||
|  | ||||
|  | ||||
| class TestEvent(Event): | ||||
|     ... | ||||
|  | ||||
|  | ||||
| matcher_on_type = on_type( | ||||
|     TestEvent, | ||||
|     rule=rule, | ||||
|     permission=permission, | ||||
|     handlers=[handler], | ||||
|     temp=True, | ||||
|     expire_time=expire_time, | ||||
|     priority=priority, | ||||
|     block=True, | ||||
|     state=state, | ||||
| ) | ||||
|  | ||||
|  | ||||
| cmd_group = CommandGroup( | ||||
|     "test", | ||||
|     rule=rule, | ||||
|     permission=permission, | ||||
|     handlers=[handler], | ||||
|     temp=True, | ||||
|     expire_time=expire_time, | ||||
|     priority=priority, | ||||
|     block=True, | ||||
|     state=state, | ||||
| ) | ||||
| matcher_sub_cmd = cmd_group.command("sub") | ||||
| matcher_sub_shell_cmd = cmd_group.shell_command("sub") | ||||
|  | ||||
|  | ||||
| matcher_group = MatcherGroup( | ||||
|     rule=rule, | ||||
|     permission=permission, | ||||
|     handlers=[handler], | ||||
|     temp=True, | ||||
|     expire_time=expire_time, | ||||
|     priority=priority, | ||||
|     block=True, | ||||
|     state=state, | ||||
| ) | ||||
| matcher_group_on = matcher_group.on(type="test") | ||||
| matcher_group_on_metaevent = matcher_group.on_metaevent() | ||||
| matcher_group_on_message = matcher_group.on_message() | ||||
| matcher_group_on_notice = matcher_group.on_notice() | ||||
| matcher_group_on_request = matcher_group.on_request() | ||||
| matcher_group_on_startswith = matcher_group.on_startswith("test") | ||||
| matcher_group_on_endswith = matcher_group.on_endswith("test") | ||||
| matcher_group_on_fullmatch = matcher_group.on_fullmatch("test") | ||||
| matcher_group_on_keyword = matcher_group.on_keyword({"test"}) | ||||
| matcher_group_on_command = matcher_group.on_command("test") | ||||
| matcher_group_on_shell_command = matcher_group.on_shell_command("test") | ||||
| matcher_group_on_regex = matcher_group.on_regex("test") | ||||
| matcher_group_on_type = matcher_group.on_type(TestEvent) | ||||
		Reference in New Issue
	
	Block a user