add .token command
This commit is contained in:
		
							parent
							
								
									29d8ff41c5
								
							
						
					
					
						commit
						75c8727097
					
				
					 1 changed files with 17 additions and 4 deletions
				
			
		| 
						 | 
					@ -18,6 +18,8 @@ class Commander:
 | 
				
			||||||
    T_START_E = frozenset(['cmd_start_enabled'])
 | 
					    T_START_E = frozenset(['cmd_start_enabled'])
 | 
				
			||||||
    T_STOP = frozenset(['cmd_stop'])
 | 
					    T_STOP = frozenset(['cmd_stop'])
 | 
				
			||||||
    T_STOP_D = frozenset(['cmd_stop_disabled'])
 | 
					    T_STOP_D = frozenset(['cmd_stop_disabled'])
 | 
				
			||||||
 | 
					    NOPE = "а ты что ещё за хуй с горы?"
 | 
				
			||||||
 | 
					    YEP = "да, господин!"
 | 
				
			||||||
    DOC = """
 | 
					    DOC = """
 | 
				
			||||||
    Команды:
 | 
					    Команды:
 | 
				
			||||||
        __.start__
 | 
					        __.start__
 | 
				
			||||||
| 
						 | 
					@ -26,6 +28,8 @@ class Commander:
 | 
				
			||||||
            остановить бота
 | 
					            остановить бота
 | 
				
			||||||
        __.list__
 | 
					        __.list__
 | 
				
			||||||
            перечислить доступные токены
 | 
					            перечислить доступные токены
 | 
				
			||||||
 | 
					        __.regex token__
 | 
				
			||||||
 | 
					            перечислить регулярные выражения, относящиеся к токену
 | 
				
			||||||
        __.chat__
 | 
					        __.chat__
 | 
				
			||||||
            получить id текущего чата
 | 
					            получить id текущего чата
 | 
				
			||||||
        __.users__
 | 
					        __.users__
 | 
				
			||||||
| 
						 | 
					@ -55,7 +59,7 @@ class Commander:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __add_response(self, caller: int, command: str) -> bool:
 | 
					    def __add_response(self, caller: int, command: str) -> bool:
 | 
				
			||||||
        if caller not in self.admins:
 | 
					        if caller not in self.admins:
 | 
				
			||||||
            return 'а ты что ещё за хуй с горы?'
 | 
					            return self.NOPE
 | 
				
			||||||
        input_tokens, phrase = command.strip().split(' ', 1)
 | 
					        input_tokens, phrase = command.strip().split(' ', 1)
 | 
				
			||||||
        input_tokenset = frozenset(input_tokens.split(','))
 | 
					        input_tokenset = frozenset(input_tokens.split(','))
 | 
				
			||||||
        for token in input_tokenset:
 | 
					        for token in input_tokenset:
 | 
				
			||||||
| 
						 | 
					@ -64,17 +68,17 @@ class Commander:
 | 
				
			||||||
        query = 'INSERT INTO responses (tokens, response) values (%s,%s)'
 | 
					        query = 'INSERT INTO responses (tokens, response) values (%s,%s)'
 | 
				
			||||||
        values = (','.join(sorted(input_tokenset)), phrase.strip())
 | 
					        values = (','.join(sorted(input_tokenset)), phrase.strip())
 | 
				
			||||||
        self.db_conn.update(query, values)
 | 
					        self.db_conn.update(query, values)
 | 
				
			||||||
        return 'да, господин!'
 | 
					        return self.YEP
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __add_user(self, caller: int, userspec: str) -> bool:
 | 
					    def __add_user(self, caller: int, userspec: str) -> bool:
 | 
				
			||||||
        if caller not in self.admins:
 | 
					        if caller not in self.admins:
 | 
				
			||||||
            return 'а ты что ещё за хуй с горы?'
 | 
					            return self.NOPE
 | 
				
			||||||
        user_id, names = userspec.strip().split(' ', 1)
 | 
					        user_id, names = userspec.strip().split(' ', 1)
 | 
				
			||||||
        for name in names.strip().split(','):
 | 
					        for name in names.strip().split(','):
 | 
				
			||||||
            query = 'INSERT INTO names (tg_id, name) values(%s,%s)'
 | 
					            query = 'INSERT INTO names (tg_id, name) values(%s,%s)'
 | 
				
			||||||
            values = (user_id, name)
 | 
					            values = (user_id, name)
 | 
				
			||||||
            self.db_conn.update(query, values)
 | 
					            self.db_conn.update(query, values)
 | 
				
			||||||
        return 'да, господин!'
 | 
					        return self.YEP
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __start_response(self) -> str:
 | 
					    def __start_response(self) -> str:
 | 
				
			||||||
| 
						 | 
					@ -92,6 +96,13 @@ class Commander:
 | 
				
			||||||
                    for user in users]
 | 
					                    for user in users]
 | 
				
			||||||
        return '\n'.join(userlist)
 | 
					        return '\n'.join(userlist)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def __list_regex(self, token: str) -> str:
 | 
				
			||||||
 | 
					        for t, r in self.responder.tokens:
 | 
				
			||||||
 | 
					            if token == t:
 | 
				
			||||||
 | 
					                regexlist = [i.pattern for i in r]
 | 
				
			||||||
 | 
					                return '\n'.join(regexlist)
 | 
				
			||||||
 | 
					        return 'not found!'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async def action(self,
 | 
					    async def action(self,
 | 
				
			||||||
                     event: telethon.events.common.EventBuilder) -> None:
 | 
					                     event: telethon.events.common.EventBuilder) -> None:
 | 
				
			||||||
        chat_id = telethon.utils.get_peer_id(event.message.peer_id)
 | 
					        chat_id = telethon.utils.get_peer_id(event.message.peer_id)
 | 
				
			||||||
| 
						 | 
					@ -114,6 +125,8 @@ class Commander:
 | 
				
			||||||
            response = str(chat_id)
 | 
					            response = str(chat_id)
 | 
				
			||||||
        elif command == '.list':
 | 
					        elif command == '.list':
 | 
				
			||||||
            response = ', '.join(self.available_tokens)
 | 
					            response = ', '.join(self.available_tokens)
 | 
				
			||||||
 | 
					        elif command.startswith('.regex '):
 | 
				
			||||||
 | 
					            response = self.__list_regex(command[7:].strip())
 | 
				
			||||||
        elif command == '.users':
 | 
					        elif command == '.users':
 | 
				
			||||||
            users = await self.client.get_participants(
 | 
					            users = await self.client.get_participants(
 | 
				
			||||||
                    entity=event.message.peer_id)
 | 
					                    entity=event.message.peer_id)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue