utils.py 894 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from uuid import uuid4
  2. from telethon.utils import get_display_name
  3. def parse_command(text):
  4. text = text.strip()
  5. if not text.startswith('/'):
  6. raise ValueError
  7. text = text.split(' ')
  8. if len(text) < 1:
  9. raise ValueError
  10. command = text[0][1:].lower()
  11. if '@' in command:
  12. command = command.split('@')
  13. command = command[0]
  14. args = text[1:]
  15. argc = len(args)
  16. args_string = ' '.join(args)
  17. return (command, argc, args, args_string)
  18. def get_link_to_user(user):
  19. full_name = get_display_name(user)
  20. if not full_name:
  21. full_name = user.username
  22. if not full_name:
  23. full_name = '?'
  24. if user.username:
  25. return f'[{full_name}](@{user.username})'
  26. return full_name
  27. def is_valid_name(name):
  28. return name.isidentifier()
  29. def make_temporary_filename(ext):
  30. uid = uuid4().hex
  31. return f'tmp_{uid}.{ext}'