You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
3.1KB

  1. import json
  2. class Config():
  3. def __init__(self, bot):
  4. # TODO: Move default message into settings.ini
  5. self.serverconfig_template = {
  6. "example": {
  7. "greets": {
  8. "enabled": 0,
  9. "welcome-channel": "",
  10. "member-role": "",
  11. "custom-message": "",
  12. "default-message": "Be sure to read the rules."
  13. },
  14. "goodbyes": {
  15. "enabled": 0,
  16. "goodbye-channel": "",
  17. },
  18. "selfAssign": {
  19. "enabled": 0,
  20. "roles": []
  21. },
  22. "twitch": {
  23. "enabled": 0,
  24. "twitch-channel": "",
  25. "whitelist": {
  26. "enabled": 0,
  27. "list": []
  28. }
  29. }
  30. }
  31. }
  32. self.serverconfig = self.load_config()
  33. self.bot = bot
  34. self.no_perms_reponse = ":no_entry_sign: You do not have permission to use this command."
  35. self.delete_after = 20
  36. async def on_server_join(self, server):
  37. self.serverconfig[server.id] = self.serverconfig_template["example"]
  38. self.updateconfig(self.serverconfig)
  39. async def on_server_remove(self, server):
  40. self.serverconfig.pop(server.id)
  41. self.updateconfig(self.serverconfig)
  42. def load_config(self):
  43. with open('config/config.json', 'r') as config_file:
  44. return json.load(config_file)
  45. def updateconfig(self, config):
  46. with open('config/config.json', 'w') as conf_file:
  47. json.dump(config, conf_file)
  48. def config_errorcheck(self):
  49. # TODO: Fix so that it checks for problems in children of module settings. i.e children of 'greets'
  50. # TODO: Fix issue where a setting can be enabled when it has no channel to post to.
  51. for server in self.bot.servers:
  52. if server.id not in self.serverconfig:
  53. self.serverconfig[server.id] = self.serverconfig_template["example"]
  54. self.updateconfig(self.serverconfig)
  55. print(
  56. "WARNING: The config file for {} was not found. A template has been loaded and saved. All modules are turned off by default.".format(
  57. server.name.upper()))
  58. else:
  59. for module_setting in self.serverconfig_template["example"]:
  60. if module_setting not in self.serverconfig[server.id]:
  61. self.serverconfig[server.id][module_setting] = self.serverconfig_template["example"][
  62. module_setting]
  63. self.updateconfig(self.serverconfig)
  64. print(
  65. "WARNING: The config file for {} was missing the {} module. This has been fixed with the template version. It is disabled by default.".format(
  66. server.name.upper(), module_setting.upper()))
  67. print("")
  68. def setup(bot):
  69. bot.add_cog(Config(bot))