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.

101 lines
3.5KB

  1. import discord
  2. from discord.ext import commands
  3. import load_config
  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. self.embed_colour = load_config.embedcolour
  11. @commands.command(pass_context=True)
  12. async def listroles(self, ctx):
  13. """
  14. List's all roles that can be self-assigned on this server.
  15. Usage:
  16. {command_prefix}listroles
  17. """
  18. if not self.servers[str(ctx.guild.id)]["self_assign"]["enabled"]:
  19. embed = discord.Embed(colour=discord.Colour(self.embed_colour),
  20. description="SelfAssignable roles are not enabled on this server")
  21. return await ctx.send(embed=embed)
  22. roles = []
  23. for role in self.servers[str(ctx.guild.id)]["self_assign"]["roles"]:
  24. for serverrole in ctx.guild.roles:
  25. if role == serverrole.id:
  26. roles.append("**"+serverrole.name+"**")
  27. roles = '\n'.join(roles)
  28. embed = discord.Embed(colour=self.embed_colour,
  29. description="The self-assignable roles for this server are: \n"+roles)
  30. return await ctx.send(embed=embed)
  31. @commands.command(pass_context=True)
  32. async def iam(self, ctx, *, role: discord.Role = None):
  33. """
  34. Self-assign yourself a role. Only one role at a time.
  35. Usage:
  36. {command_prefix}iam [role]
  37. Example:
  38. .iam OverwatchPing
  39. """
  40. if not self.servers[str(ctx.guild.id)]["self_assign"]["enabled"]:
  41. embed = discord.Embed(colour=discord.Colour(self.embed_colour),
  42. description="SelfAssignable roles are not enabled on this server")
  43. return await ctx.send(embed=embed)
  44. user = ctx.author
  45. server = ctx.guild
  46. if role not in server.roles:
  47. return await ctx.send("That role doesn't exist. Roles are case sensitive. ")
  48. if role in user.roles:
  49. return await ctx.send("You already have that role.")
  50. if role.id in self.servers[str(ctx.guild.id)]["self_assign"]["roles"]:
  51. await user.add_roles(role, reason="'iam' command triggered.")
  52. return await ctx.send("Yay {}! You now have the {} role!".format(user.mention, role.name))
  53. else:
  54. return await ctx.send("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. if not self.servers[str(ctx.guild.id)]["self_assign"]["enabled"]:
  65. embed = discord.Embed(colour=discord.Colour(self.embed_colour),
  66. description="SelfAssignable roles are not enabled on this server")
  67. return await ctx.send(embed=embed)
  68. user = ctx.author
  69. server = ctx.guild
  70. if role in user.roles and role.id in self.servers[str(ctx.guild.id)]["self_assign"]["roles"]:
  71. await user.remove_roles(role, reason="'iamn' command triggered.")
  72. return await ctx.send("{} has been successfully removed.".format(role.name))
  73. elif role not in user.roles and role.id in self.servers[str(ctx.guild.id)]["self_assign"]["roles"]:
  74. return await ctx.send("You do not have {}.".format(role.name))
  75. else:
  76. return await ctx.send("That role is not self-assignable.")
  77. @iam.error
  78. async def iam_err(self, ctx, error):
  79. if isinstance(error, commands.BadArgument):
  80. return await ctx.send("This role doesn't exist. Reminder, roles are case-sensitive.")
  81. @iamn.error
  82. async def iamn_err(self, ctx, error):
  83. if isinstance(error, commands.BadArgument):
  84. return await ctx.send("This role doesn't exist. Reminder, roles are case-sensitive.")
  85. def setup(Bot):
  86. Bot.add_cog(SelfAssign(Bot))