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.

184 lines
7.1KB

  1. # -*- coding: utf-8 -*-
  2. # MIT License
  3. #
  4. # Copyright (c) 2017-2018 Roxanne Gibson
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in all
  14. # copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. import discord
  24. from discord.ext import commands
  25. import roxbot
  26. from roxbot.db import *
  27. class SelfAssignRoles(db.Entity):
  28. role_id = Required(int, unique=True, size=64)
  29. guild_id = Required(int, size=64)
  30. class SelfAssignSingle(db.Entity):
  31. enabled = Required(bool, default=False)
  32. guild_id = Required(int, unique=True, size=64)
  33. class SelfAssign(commands.Cog):
  34. """The SelfAssign cog allows guild's to mark roles as 'self assignable'. This allows users to give themselves these roles and to see all the roles marked as 'self assignable'."""
  35. def __init__(self, Bot):
  36. self.bot = Bot
  37. self.autogen_db = SelfAssignSingle
  38. @commands.Cog.listener()
  39. async def on_guild_role_delete(self, role):
  40. """Cleans up settings on removal of stored IDs."""
  41. with db_session:
  42. query = SelfAssignRoles.get(role_id=role.id)
  43. if query:
  44. query.delete()
  45. @commands.guild_only()
  46. @commands.has_permissions(manage_roles=True)
  47. @commands.command(aliases=["sa"])
  48. async def selfassign(self, ctx, setting, *, role: discord.Role = None):
  49. """Edits settings for self assign cog. Requires Manage Roles permission.
  50. Options:
  51. enable/disable: Enable/disables the cog.
  52. add/remove: adds or removes a role that can be self assigned in the server.
  53. Example:
  54. Turn on self assigned roles and add a role called "ROLE"
  55. `;sa enable`
  56. `;sa add ROLE`
  57. """
  58. with db_session:
  59. self_assign = SelfAssignSingle.get(guild_id=ctx.guild.id)
  60. if setting == "enable":
  61. self_assign.enabled = True
  62. await ctx.send("'self_assign' was enabled!")
  63. elif setting == "disable":
  64. self_assign.enabled = False
  65. await ctx.send("'self_assign' was disabled :cry:")
  66. elif setting == "add":
  67. try:
  68. SelfAssignRoles(role_id=role.id, guild_id=ctx.guild.id)
  69. await ctx.send('Role "{}" added'.format(str(role)))
  70. except AttributeError:
  71. raise commands.BadArgument("Could not find that role.")
  72. except TransactionIntegrityError:
  73. raise commands.BadArgument("{} is already a self-assignable role.".format(role.name))
  74. elif setting == "remove":
  75. try:
  76. query = SelfAssignRoles.get(role_id=role.id)
  77. if query:
  78. query.delete()
  79. return await ctx.send('"{}" has been removed from the self-assignable roles.'.format(str(role)))
  80. else:
  81. return await ctx.send("That role was not in the list.")
  82. except AttributeError:
  83. raise commands.BadArgument("Could not find that role.")
  84. else:
  85. return await ctx.send("No valid option given.")
  86. @commands.guild_only()
  87. @commands.command(pass_context=True)
  88. async def listroles(self, ctx):
  89. """
  90. List's all roles that can be self-assigned on this server.
  91. """
  92. with db_session:
  93. self_assign = SelfAssignSingle.get(guild_id=ctx.guild.id)
  94. roles = select(r for r in SelfAssignRoles if r.guild_id == ctx.guild.id)[:]
  95. if not self_assign.enabled:
  96. embed = discord.Embed(colour=roxbot.EmbedColours.pink, description="SelfAssignable roles are not enabled on this server")
  97. return await ctx.send(embed=embed)
  98. paginator = commands.Paginator(prefix="`", suffix="`")
  99. paginator.add_line("The self-assignable roles for this server are: \n")
  100. for role in roles:
  101. r = ctx.guild.get_role(role.role_id)
  102. if r:
  103. paginator.add_line("- {}".format(r.name))
  104. for page in paginator.pages:
  105. await ctx.send(page)
  106. @commands.guild_only()
  107. @commands.command(pass_context=True)
  108. async def iam(self, ctx, *, role: discord.Role):
  109. """
  110. Self-assign yourself a role. Can only be done one role at a time.
  111. Example:
  112. .iam OverwatchPing
  113. """
  114. with db_session:
  115. self_assign = SelfAssignSingle.get(guild_id=ctx.guild.id)
  116. query = SelfAssignRoles.get(role_id=role.id)
  117. if not self_assign.enabled:
  118. embed = discord.Embed(colour=roxbot.EmbedColours.pink, description="SelfAssignable roles are not enabled on this server")
  119. return await ctx.send(embed=embed)
  120. member = ctx.author
  121. if role in member.roles:
  122. return await ctx.send("You already have that role.")
  123. if query:
  124. await member.add_roles(role, reason="'iam' command triggered.")
  125. return await ctx.send("Yay {}! You now have the {} role!".format(member.mention, role.name))
  126. else:
  127. return await ctx.send("That role is not self-assignable.")
  128. @commands.guild_only()
  129. @commands.command(pass_context=True)
  130. async def iamn(self, ctx, *, role: discord.Role):
  131. """
  132. Remove a self-assigned role. Can only be done one role at a time.
  133. Example:
  134. .iamn OverwatchPing
  135. """
  136. with db_session:
  137. self_assign = SelfAssignSingle.get(guild_id=ctx.guild.id)
  138. query = SelfAssignRoles.get(role_id=role.id)
  139. if not self_assign.enabled:
  140. embed = discord.Embed(colour=roxbot.EmbedColours.pink, description="SelfAssignable roles are not enabled on this server")
  141. return await ctx.send(embed=embed)
  142. member = ctx.author
  143. if role in member.roles and query:
  144. await member.remove_roles(role, reason="'iamn' command triggered.")
  145. return await ctx.send("{} has been successfully removed.".format(role.name))
  146. elif role not in member.roles and query:
  147. return await ctx.send("You do not have {}.".format(role.name))
  148. else:
  149. return await ctx.send("That role is not self-assignable.")
  150. def setup(Bot):
  151. Bot.add_cog(SelfAssign(Bot))