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.

173 lines
4.8KB

  1. import json
  2. guild_template = {
  3. "example": {
  4. "greets": {
  5. "enabled": 0,
  6. "welcome-channel": "",
  7. "member-role": "",
  8. "custom-message": "",
  9. "default-message": "Be sure to read the rules."
  10. },
  11. "goodbyes": {
  12. "enabled": 0,
  13. "goodbye-channel": "",
  14. },
  15. "self_assign": {
  16. "enabled": 0,
  17. "roles": []
  18. },
  19. "twitch": {
  20. "enabled": 0,
  21. "channel": "",
  22. "whitelist": {
  23. "enabled": 0,
  24. "list": []
  25. }
  26. },
  27. "nsfw": {
  28. "enabled": 0,
  29. "channels": [],
  30. "blacklist": []
  31. },
  32. "perm_roles": {
  33. "admin": [],
  34. "mod": []
  35. },
  36. "custom_commands": {
  37. "0": {},
  38. "1": {}
  39. },
  40. "gss": {
  41. "log_channel": "",
  42. "required_days": "",
  43. "required_score": "",
  44. },
  45. "warnings": {},
  46. "is_anal": {"y/n": 0}
  47. }
  48. }
  49. def _open_config():
  50. """
  51. Opens the guild settings file
  52. :return settings file: :type dict:
  53. """
  54. with open('Roxbot/settings/servers.json', 'r') as config_file:
  55. return json.load(config_file)
  56. def _write_changes(config):
  57. """
  58. Writes given config to disk.
  59. :param config: :type dict:
  60. :return:
  61. """
  62. with open('Roxbot/settings/servers.json', 'w') as conf_file:
  63. json.dump(config, conf_file)
  64. def remove_guild(guild):
  65. settings = _open_config()
  66. settings.pop(str(guild.id))
  67. _write_changes(settings)
  68. def error_check(servers):
  69. settings = _open_config()
  70. for server in servers:
  71. # Server ID needs to be made a string for this statement because keys have to be strings in JSON. Which is annoying now we use int for ids.
  72. server_id = str(server.id)
  73. if str(server_id) not in settings:
  74. settings[server_id] = guild_template["example"]
  75. _write_changes(settings)
  76. print(
  77. "WARNING: The settings file for {} was not found. A template has been loaded and saved. All cogs are turned off by default.".format(
  78. server.name.upper()))
  79. else:
  80. for cog_setting in guild_template["example"]:
  81. if cog_setting not in settings[server_id]:
  82. settings[server_id][cog_setting] = guild_template["example"][
  83. cog_setting]
  84. _write_changes(settings)
  85. print(
  86. "WARNING: The settings file for {} was missing the {} cog. This has been fixed with the template version. It is disabled by default.".format(
  87. server.name.upper(), cog_setting.upper()))
  88. for setting in guild_template["example"][cog_setting]:
  89. if setting not in settings[server_id][cog_setting]:
  90. settings[server_id][cog_setting][setting] = guild_template["example"][
  91. cog_setting][setting]
  92. _write_changes(settings)
  93. print(
  94. "WARNING: The settings file for {} was missing the {} setting in the {} cog. This has been fixed with the template version. It is disabled by default.".format(
  95. server.name.upper(), setting.upper(), cog_setting.upper()))
  96. def get_all(guilds):
  97. """
  98. Returns a list of GuildSettings for all guilds the bot can see.
  99. :param guilds:
  100. :return list of GuildSettings: :type list:
  101. """
  102. error_check(guilds)
  103. guild_list = []
  104. for guild in guilds:
  105. guild = GuildSettings(guild)
  106. guild_list.append(guild)
  107. return guild_list
  108. def get(guild):
  109. """
  110. Gets a single GuildSettings Object representing the settings of that guild
  111. :param guild:
  112. :return Single GuildSettings Object: :type GuildSettings:
  113. """
  114. return GuildSettings(guild)
  115. def get_guild(guilds, wanted_guild):
  116. for guild in guilds:
  117. if guild.id == wanted_guild.id:
  118. return guild
  119. return None
  120. class GuildSettings(object):
  121. """
  122. An Object to store all settings for one guild.
  123. The goal is to make editing settings a lot easier and make it so you don't have to handle things like ID's which caused a lot of issues when moving over to discord.py 1.0
  124. """
  125. __slots__ = ["settings", "id", "name", "nsfw", "self_assign", "greets", "goodbyes", "twitch", "perm_roles", "custom_commands", "warnings", "is_anal", "gss"]
  126. def __init__(self, guild):
  127. self.id = guild.id
  128. self.name = str(guild)
  129. self.settings = self.refresh()
  130. self.get_settings()
  131. def __str__(self):
  132. return self.name
  133. def refresh(self):
  134. return _open_config()[str(self.id)]
  135. def get_settings(self):
  136. self.nsfw = self.settings["nsfw"]
  137. self.self_assign = self.settings["self_assign"]
  138. self.greets = self.settings["greets"]
  139. self.goodbyes = self.settings["goodbyes"]
  140. self.twitch = self.settings["twitch"]
  141. self.perm_roles = self.settings["perm_roles"]
  142. self.custom_commands = self.settings["custom_commands"]
  143. self.warnings = self.settings["warnings"]
  144. self.is_anal = self.settings["is_anal"]
  145. # Add custom cog settings loading here
  146. self.gss = self.settings["gss"]
  147. def update(self, changed_dict, setting = None):
  148. self.settings = self.refresh()
  149. self.get_settings()
  150. if setting is not None:
  151. self.settings[setting] = changed_dict
  152. else:
  153. self.settings = changed_dict
  154. json = _open_config()
  155. json[str(self.id)] = self.settings
  156. _write_changes(json)
  157. self.get_settings()