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.

109 lines
3.8KB

  1. import discord
  2. from discord.ext import commands
  3. import checks
  4. from config.server_config import ServerConfig
  5. class SelfAssign():
  6. def __init__(self, Bot):
  7. self.bot = Bot
  8. self.con = ServerConfig()
  9. self.servers = self.con.servers
  10. @commands.command(pass_context=True)
  11. async def listroles(self, ctx):
  12. """
  13. List's all roles that can be self-assigned on this server.
  14. Usage:
  15. {command_prefix}listroles
  16. """
  17. roles = " "
  18. for role in self.servers[ctx.message.server.id]["selfAssign"]["roles"]:
  19. for serverrole in ctx.message.server.roles:
  20. if role == serverrole.id:
  21. roles.join(serverrole.name)
  22. return await self.bot.say(roles)
  23. @commands.command(pass_context=True)
  24. async def iam(self, ctx, role: discord.Role = None):
  25. """
  26. Self-assign yourself a role. Only one role at a time. Doesn't work for roles with spaces.
  27. Usage:
  28. {command_prefix}iam [role]
  29. Example:
  30. .iam OverwatchPing
  31. """
  32. self.servers = self.con.load_config()
  33. if not self.servers[ctx.message.server.id]["selfAssign"]["enabled"]:
  34. return
  35. user = ctx.message.author
  36. server = ctx.message.server
  37. if role not in server.roles:
  38. return await self.bot.say("That role doesn't exist. Roles are case sensitive. ")
  39. if role in user.roles:
  40. return await self.bot.say("You already have that role.")
  41. if role.id in self.servers[ctx.message.server.id]["selfAssign"]["roles"]:
  42. await self.bot.add_roles(user, role)
  43. print("{} added {} to themselves in {} on {}".format(user.display_name, role.name, ctx.message.channel,
  44. ctx.message.server))
  45. return await self.bot.say("Yay {}! You now have the {} role!".format(user.mention, role.name))
  46. else:
  47. return await self.bot.say("That role is not self-assignable.")
  48. @commands.command(pass_context=True)
  49. async def iamn(self, ctx, role: discord.Role = None):
  50. """
  51. Remove a self-assigned role
  52. Usage:
  53. {command_prefix}iamn [role]
  54. Example:
  55. .iamn OverwatchPing
  56. """
  57. self.servers = self.con.load_config()
  58. if not self.servers[ctx.message.server.id]["selfAssign"]["enabled"]:
  59. return
  60. user = ctx.message.author
  61. server = ctx.message.server
  62. if role not in server.roles:
  63. return await self.bot.say("That role doesn't exist. Roles are case sensitive. ")
  64. elif role in user.roles and role.id in self.servers[ctx.message.server.id]["selfAssign"]["roles"]:
  65. await self.bot.remove_roles(user, role)
  66. return await self.bot.reply("{} has been successfully removed.".format(role.name))
  67. elif role not in user.roles and role.id in self.servers[ctx.message.server.id]["selfAssign"]["roles"]:
  68. return await self.bot.reply("You do not have {}.".format(role.name))
  69. else:
  70. return await self.bot.say("That role is not self-assignable.")
  71. @commands.command(pass_context=True, hidden=True)
  72. @checks.is_bot_owner()
  73. async def addrole(self, ctx, role: discord.Role = None):
  74. self.servers = self.con.load_config()
  75. if role.id in self.servers[ctx.message.server.id]["selfAssign"]["roles"]:
  76. return await self.bot.say("{} is already a self-assignable role.".format(role.name), delete_after=self.con.delete_after)
  77. self.servers[ctx.message.server.id]["selfAssign"]["roles"].append(role.id)
  78. self.con.update_config(self.servers)
  79. return await self.bot.say('Role "{}" added'.format(str(role)))
  80. @commands.command(pass_context=True, hidden=True)
  81. @checks.is_bot_owner()
  82. async def removerole(self, ctx, role: discord.Role = None):
  83. self.servers = self.con.load_config()
  84. if role.id in self.servers[ctx.message.server.id]["selfAssign"]["roles"]:
  85. self.servers[ctx.message.server.id]["selfAssign"]["roles"].remove(role.id)
  86. self.con.update_config(self.servers)
  87. return await self.bot.say('"{}" has been removed from the self-assignable roles.'.format(str(role)))
  88. else:
  89. return await self.bot.say("That role was not in the list.")
  90. def setup(Bot):
  91. Bot.add_cog(SelfAssign(Bot))