mirror of
				https://github.com/nonebot/nonebot2.git
				synced 2025-10-30 22:46:40 +00:00 
			
		
		
		
	Add argfilter type "controllers"
This commit is contained in:
		
							
								
								
									
										34
									
								
								nonebot/command/argfilter/controllers.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								nonebot/command/argfilter/controllers.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | ||||
| import re | ||||
|  | ||||
| from nonebot import CommandSession | ||||
| from nonebot.helpers import render_expression | ||||
|  | ||||
|  | ||||
| def handle_cancellation(session: CommandSession): | ||||
|     """ | ||||
|     If the input is a string of cancellation word, finish the command session. | ||||
|     """ | ||||
|  | ||||
|     def control(value): | ||||
|         if _is_cancellation(value): | ||||
|             session.finish(render_expression( | ||||
|                 session.bot.config.SESSION_CANCEL_EXPRESSION)) | ||||
|         return value | ||||
|  | ||||
|     return control | ||||
|  | ||||
|  | ||||
| async def _is_cancellation(sentence: str) -> bool: | ||||
|     for kw in ('算', '别', '不', '停', '取消'): | ||||
|         if kw in sentence: | ||||
|             # a keyword matches | ||||
|             break | ||||
|     else: | ||||
|         # no keyword matches | ||||
|         return False | ||||
|  | ||||
|     if re.match(r'^那?[算别不停]\w{0,3}了?吧?$', sentence) or \ | ||||
|             re.match(r'^那?(?:[给帮]我)?取消了?吧?$', sentence): | ||||
|         return True | ||||
|  | ||||
|     return False | ||||
| @@ -33,6 +33,7 @@ COMMAND_SEP: Iterable[Union[str, Pattern]] = {'/', '.'} | ||||
| SESSION_EXPIRE_TIMEOUT: Optional[timedelta] = timedelta(minutes=5) | ||||
| SESSION_RUN_TIMEOUT: Optional[timedelta] = None | ||||
| SESSION_RUNNING_EXPRESSION: Expression_T = '您有命令正在执行,请稍后再试' | ||||
| SESSION_CANCEL_EXPRESSION = '好的' | ||||
| SHORT_MESSAGE_MAX_LENGTH: int = 50 | ||||
| DEFAULT_VALIDATION_FAILURE_EXPRESSION: Expression_T = '您的输入不符合要求,请重新输入' | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user