funcs.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import os
  2. import pickle
  3. import importlib.util
  4. from settings import *
  5. import time
  6. def check_module(module_name):
  7. module_spec = importlib.util.find_spec(module_name)
  8. if module_spec is None:
  9. print('Module: {} not found'.format(module_name))
  10. return None
  11. else:
  12. return module_spec
  13. def import_module_from_spec(module_spec):
  14. module = importlib.util.module_from_spec(module_spec)
  15. module_spec.loader.exec_module(module)
  16. return module
  17. def import_module(module):
  18. module_spec = check_module(module)
  19. if module_spec is not None:
  20. return import_module_from_spec(module_spec)
  21. def get_path(*args):
  22. if len(args) == 0:
  23. raise AttributeError
  24. if len(args) == 1:
  25. if isinstance(args[0], str):
  26. return args[0]
  27. args = args[0]
  28. if len(args) == 1:
  29. return args[0]
  30. path = args[0]
  31. for i in args[1:]:
  32. path = os.path.join(path, i)
  33. return path
  34. def mkdir(*args):
  35. path = get_path(args)
  36. if not os.path.isdir(path):
  37. os.mkdir(path)
  38. def mkfile(*args):
  39. path = get_path(args)
  40. if not os.path.isfile(path):
  41. f = open(path, 'w')
  42. f.close()
  43. def save_cfg():
  44. with open(get_path([BASE_FOLDER, 'config']), 'wb') as f:
  45. pickle.dump(CONFIG, f)
  46. pjoin = os.path.join
  47. def drp_thread(DRP):
  48. global DRP_CONFIG
  49. while True:
  50. time.sleep(DRP_UPDATE_TIME)
  51. if not DRP_IS_RUNNING:
  52. continue
  53. DRP.update(**DRP_CONFIG)
  54. DRP.shutdown()
  55. def drp_addon_thread(func):
  56. global DRP_CONFIG
  57. while True:
  58. time.sleep(DRP_UPDATE_TIME)
  59. if not DRP_IS_RUNNING:
  60. continue
  61. DRP_CONFIG = func()
  62. def get_user_avatar(user):
  63. return f'https://cdn.discordapp.com/avatars/{user["id"]}/{user["avatar"]}.png?size=128'