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.

132 lines
6.1KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # MIT License
  4. #
  5. # Copyright (c) 2017-2018 Roxanne Gibson
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in all
  15. # copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. # SOFTWARE.
  24. import os
  25. import json
  26. import hashlib
  27. import datetime
  28. from roxbot.db import *
  29. from roxbot.cogs.customcommands import CCCommands
  30. @db_session
  31. def admin_convert(guild_id, settings):
  32. warning_limit = settings.get("warning_limit", None)
  33. if warning_limit is not None:
  34. db.execute("UPDATE AdminSingle SET `warning_limit` = {} WHERE `guild_id` = {}".format(warning_limit, guild_id))
  35. db.commit()
  36. warnings = settings.get("warnings", None)
  37. if warnings is None:
  38. return
  39. for user, warns in warnings.items():
  40. user_id = int(user)
  41. for warn in warns:
  42. date = datetime.datetime.fromtimestamp(warn["date"])
  43. try:
  44. db.insert("AdminWarnings", user_id=user_id, guild_id=guild_id, date=date, warning=warn["warning"], warned_by=warn["warned-by"])
  45. db.commit()
  46. except (TransactionIntegrityError, IntegrityError):
  47. pass
  48. @db_session
  49. def custom_commands_convert(guild_id, settings):
  50. for com_type, commands in settings.items():
  51. if com_type != "convert":
  52. for name, command in commands.items():
  53. com_hash = hashlib.md5(name.encode() + str(guild_id).encode() + str(com_type).encode()).hexdigest()
  54. if isinstance(command, str):
  55. com = [command]
  56. else:
  57. com = command
  58. try:
  59. CCCommands(name=name, hash=com_hash, output=com, type=int(com_type), guild_id=guild_id)
  60. db.commit()
  61. except (TransactionIntegrityError, CacheIndexError, IntegrityError):
  62. pass
  63. @db_session
  64. def joinleave_convert(guild_id, settings):
  65. greet = settings["greets"]
  66. goodbye = settings["goodbyes"]
  67. db.execute("UPDATE `JoinLeaveSingle` SET `greets_enabled` = {} WHERE `guild_id` = {}".format(greet["enabled"], guild_id))
  68. db.execute("UPDATE `JoinLeaveSingle` SET `goodbyes_enabled` = {} WHERE `guild_id` = {}".format(goodbye["enabled"], guild_id))
  69. db.execute("UPDATE `JoinLeaveSingle` SET `greets_channel_id` = {} WHERE `guild_id` = {}".format(greet["welcome-channel"], guild_id))
  70. db.execute("UPDATE `JoinLeaveSingle` SET `goodbyes_channel_id` = {} WHERE `guild_id` = {}".format(goodbye["goodbye-channel"], guild_id))
  71. db.execute("UPDATE `JoinLeaveSingle` SET `greets_custom_message` = '{}' WHERE `guild_id` = {}".format(greet["custom-message"],guild_id))
  72. @db_session
  73. def nsfw_convert(guild_id, settings):
  74. db.execute("UPDATE `NSFWSingle` SET `enabled` = {} WHERE `guild_id` = {}".format(settings["enabled"], guild_id))
  75. db.execute('UPDATE `NSFWSingle` SET `blacklisted_tags` = "{}" WHERE `guild_id` = {}'.format(settings["blacklist"], guild_id))
  76. @db_session
  77. def logging_convert(guild_id, settings):
  78. db.execute("UPDATE `LoggingSingle` SET `enabled` = {} WHERE `guild_id` = {}".format(settings["enabled"], guild_id))
  79. db.execute('UPDATE `LoggingSingle` SET `logging_channel_id` = "{}" WHERE `guild_id` = {}'.format(settings["channel"], guild_id))
  80. @db_session
  81. def voice_convert(guild_id, settings):
  82. db.execute("UPDATE `VoiceSingle` SET `need_perms` = {} WHERE `guild_id` = {}".format(settings["need_perms"], guild_id))
  83. db.execute("UPDATE `VoiceSingle` SET `skip_voting` = {} WHERE `guild_id` = {}".format(settings["skip_voting"], guild_id))
  84. db.execute("UPDATE `VoiceSingle` SET `skip_ratio` = {} WHERE `guild_id` = {}".format(settings["skip_ratio"], guild_id))
  85. db.execute("UPDATE `VoiceSingle` SET `max_length` = {} WHERE `guild_id` = {}".format(settings["max_length"], guild_id))
  86. @db_session
  87. def selfassign_convert(guild_id, settings):
  88. db.execute("UPDATE `SelfAssignSingle` SET `enabled` = {} WHERE `guild_id` = {}".format(settings["enabled"], guild_id))
  89. for role in settings["roles"]:
  90. try:
  91. db.insert("SelfAssignRoles", role_id=role, guild_id=guild_id)
  92. except IntegrityError:
  93. pass
  94. def check_convert(guilds):
  95. if os.path.isdir(os.getcwd() + "/roxbot/settings/servers"):
  96. for guild in guilds:
  97. settings = {}
  98. for cog in ("Admin.json", "Core.json", "CustomCommands.json", "JoinLeave.json", "NFSW.json", "SelfAssign.json", "Voice.json"):
  99. with open('roxbot/settings/servers/{}/{}'.format(guild.id, cog), 'r') as config_file:
  100. settings = {**settings, **json.load(config_file)}
  101. admin_convert(guild.id, settings["admin"])
  102. custom_commands_convert(guild.id, settings["custom_commands"])
  103. joinleave = {}
  104. joinleave["greets"] = settings["greets"]
  105. joinleave["goodbyes"] = settings["goodbyes"]
  106. joinleave_convert(guild.id, joinleave)
  107. nsfw_convert(guild.id, settings["nsfw"])
  108. logging_convert(guild.id, settings["logging"])
  109. voice_convert(guild.id, settings["voice"])
  110. selfassign_convert(guild.id, settings["self_assign"])
  111. os.rename(os.getcwd() + "/roxbot/settings/servers", os.getcwd() + "/roxbot/settings/servers.old")