utils.py 1.0 KB

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