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.

124 lines
4.5KB

  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. if not self.servers[ctx.message.server.id]["self_assign"]["enabled"]:
  18. embed = discord.Embed(colour=discord.Colour(0xDEADBF), # Make Embed colour a constant
  19. description="SelfAssignable roles are not enabled on this server")
  20. return await self.bot.say(embed=embed)
  21. roles = []
  22. for role in self.servers[ctx.message.server.id]["self_assign"]["roles"]:
  23. for serverrole in ctx.message.server.roles:
  24. if role == serverrole.id:
  25. roles.append("**"+serverrole.name+"**")
  26. roles = '\n'.join(roles)
  27. embed = discord.Embed(colour=discord.Colour(0xDEADBF), # Make Embed colour a constant
  28. description="The self-assignable roles for this server are: \n"+roles)
  29. return await self.bot.say(embed=embed)
  30. @commands.command(pass_context=True)
  31. async def iam(self, ctx, *, role: discord.Role = None):
  32. """
  33. Self-assign yourself a role. Only one role at a time.
  34. Usage:
  35. {command_prefix}iam [role]
  36. Example:
  37. .iam OverwatchPing
  38. """
  39. self.servers = self.con.load_config()
  40. if not self.servers[ctx.message.server.id]["self_assign"]["enabled"]:
  41. return
  42. user = ctx.message.author
  43. server = ctx.message.server
  44. if role not in server.roles:
  45. return await self.bot.say("That role doesn't exist. Roles are case sensitive. ")
  46. if role in user.roles:
  47. return await self.bot.say("You already have that role.")
  48. if role.id in self.servers[ctx.message.server.id]["self_assign"]["roles"]:
  49. await self.bot.add_roles(user, role)
  50. print("{} added {} to themselves in {} on {}".format(user.display_name, role.name, ctx.message.channel,
  51. ctx.message.server))
  52. return await self.bot.say("Yay {}! You now have the {} role!".format(user.mention, role.name))
  53. else:
  54. return await self.bot.say("That role is not self-assignable.")
  55. @commands.command(pass_context=True)
  56. async def iamn(self, ctx, *, role: discord.Role = None):
  57. """
  58. Remove a self-assigned role
  59. Usage:
  60. {command_prefix}iamn [role]
  61. Example:
  62. .iamn OverwatchPing
  63. """
  64. self.servers = self.con.load_config()
  65. if not self.servers[ctx.message.server.id]["self_assign"]["enabled"]:
  66. print("Self Assign is Disabled")
  67. return
  68. user = ctx.message.author
  69. server = ctx.message.server
  70. if role not in server.roles:
  71. return await self.bot.say("That role doesn't exist. Roles are case sensitive. ")
  72. elif role in user.roles and role.id in self.servers[ctx.message.server.id]["self_assign"]["roles"]:
  73. print("passed in server check")
  74. await self.bot.remove_roles(user, role)
  75. return await self.bot.reply("{} has been successfully removed.".format(role.name))
  76. elif role not in user.roles and role.id in self.servers[ctx.message.server.id]["self_assign"]["roles"]:
  77. return await self.bot.reply("You do not have {}.".format(role.name))
  78. else:
  79. return await self.bot.say("That role is not self-assignable.")
  80. @commands.command(pass_context=True, hidden=True)
  81. @checks.is_bot_owner()
  82. async def addrole(self, ctx, *, role: discord.Role = None):
  83. """
  84. ] Adds a role to the list of roles that can be self assigned for that server.
  85. """
  86. self.servers = self.con.load_config()
  87. if role.id in self.servers[ctx.message.server.id]["self_assign"]["roles"]:
  88. return await self.bot.say("{} is already a self-assignable role.".format(role.name), delete_after=self.con.delete_after)
  89. self.servers[ctx.message.server.id]["self_assign"]["roles"].append(role.id)
  90. self.con.update_config(self.servers)
  91. return await self.bot.say('Role "{}" added'.format(str(role)))
  92. @commands.command(pass_context=True, hidden=True)
  93. @checks.is_bot_owner()
  94. async def removerole(self, ctx, *, role: discord.Role = None):
  95. """
  96. Removes a role from the list of self assignable roles for that server.
  97. """
  98. self.servers = self.con.load_config()
  99. if role.id in self.servers[ctx.message.server.id]["self_assign"]["roles"]:
  100. self.servers[ctx.message.server.id]["self_assign"]["roles"].remove(role.id)
  101. self.con.update_config(self.servers)
  102. return await self.bot.say('"{}" has been removed from the self-assignable roles.'.format(str(role)))
  103. else:
  104. return await self.bot.say("That role was not in the list.")
  105. def setup(Bot):
  106. Bot.add_cog(SelfAssign(Bot))