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.

108 lines
3.5KB

  1. import discord
  2. from discord.ext import commands
  3. from Roxbot import load_config
  4. from Roxbot.settings import guild_settings as gs
  5. class SelfAssign():
  6. def __init__(self, Bot):
  7. self.bot = Bot
  8. self.embed_colour = load_config.embedcolour
  9. @commands.command(pass_context=True)
  10. async def listroles(self, ctx):
  11. """
  12. List's all roles that can be self-assigned on this server.
  13. Usage:
  14. {command_prefix}listroles
  15. """
  16. settings = gs.get(ctx.guild)
  17. if not settings.self_assign["enabled"]:
  18. embed = discord.Embed(colour=discord.Colour(self.embed_colour),
  19. description="SelfAssignable roles are not enabled on this server")
  20. return await ctx.send(embed=embed)
  21. roles = []
  22. for role in settings.self_assign["roles"]:
  23. for serverrole in ctx.guild.roles:
  24. if role == serverrole.id:
  25. roles.append("**"+serverrole.name+"**")
  26. roles = '\n'.join(roles)
  27. embed = discord.Embed(colour=self.embed_colour,
  28. description="The self-assignable roles for this server are: \n"+roles)
  29. return await ctx.send(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. settings = gs.get(ctx.guild)
  40. if role is None:
  41. # Hacky way to get the error I want
  42. from inspect import Parameter
  43. raise commands.MissingRequiredArgument(Parameter("Role", False))
  44. if not settings.self_assign["enabled"]:
  45. embed = discord.Embed(colour=discord.Colour(self.embed_colour),
  46. description="SelfAssignable roles are not enabled on this server")
  47. return await ctx.send(embed=embed)
  48. member = ctx.author
  49. if role in member.roles:
  50. return await ctx.send("You already have that role.")
  51. if role.id in settings.self_assign["roles"]:
  52. await member.add_roles(role, reason="'iam' command triggered.")
  53. return await ctx.send("Yay {}! You now have the {} role!".format(member.mention, role.name))
  54. else:
  55. return await ctx.send("That role is not self-assignable.")
  56. @commands.command(pass_context=True)
  57. async def iamn(self, ctx, *, role: discord.Role = None):
  58. """
  59. Remove a self-assigned role
  60. Usage:
  61. {command_prefix}iamn [role]
  62. Example:
  63. .iamn OverwatchPing
  64. """
  65. settings = gs.get(ctx.guild)
  66. if role is None:
  67. from inspect import Parameter
  68. raise commands.MissingRequiredArgument(Parameter("role", False))
  69. if not settings.self_assign["enabled"]:
  70. embed = discord.Embed(colour=discord.Colour(self.embed_colour),
  71. description="SelfAssignable roles are not enabled on this server")
  72. return await ctx.send(embed=embed)
  73. member = ctx.author
  74. if role in member.roles and role.id in settings.self_assign["roles"]:
  75. await member.remove_roles(role, reason="'iamn' command triggered.")
  76. return await ctx.send("{} has been successfully removed.".format(role.name))
  77. elif role not in member.roles and role.id in settings.self_assign["roles"]:
  78. return await ctx.send("You do not have {}.".format(role.name))
  79. else:
  80. return await ctx.send("That role is not self-assignable.")
  81. @iam.error
  82. async def iam_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. @iamn.error
  86. async def iamn_err(self, ctx, error):
  87. if isinstance(error, commands.BadArgument):
  88. return await ctx.send("This role doesn't exist. Reminder, roles are case-sensitive.")
  89. def setup(Bot):
  90. Bot.add_cog(SelfAssign(Bot))