unit.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import discord
  2. from discord.ext import commands
  3. from discord.commands import Option
  4. from discord.commands import slash_command
  5. import configparser
  6. class unit(commands.Cog):
  7. def __init__(self, bot: discord.Bot):
  8. self.bot = bot
  9. #Command initialization
  10. @slash_command(name="unit", description= "Add or remove a department member to/from a unit")
  11. async def unit(
  12. self,
  13. ctx,
  14. user: str = Option(discord.User, "Select User", required=True),
  15. unit: str = Option(str, "Select Unit", required=True)
  16. , choices=["Detective", "SWAT", "Canine", "Air Support"]
  17. ):
  18. if user not in ctx.guild.members:
  19. await ctx.respond("The selected user is not a member on this Server!", ephemeral=True)
  20. return
  21. elif user == self.bot.user:
  22. await ctx.respond(f"This is me - the {self.bot.user}", ephemeral=True)
  23. return
  24. elif user == ctx.author:
  25. await ctx.respond("You cannot promote yourself!", ephemeral=True)
  26. return
  27. server = ctx.guild
  28. config = configparser.RawConfigParser()
  29. configFilePath = r'config.cfg'
  30. config.read_file(open(configFilePath))
  31. #Role configuration
  32. department1_role_id = config.get('Einweisung', 'department1_role_id').split(', ')
  33. department1_role = ctx.guild.get_role(int(department1_role_id))
  34. #department2_role_id = config.get('Role Management', 'department2_role_id').split(', ')
  35. #department2_role = ctx.guild.get_role(int(department2_role_id))
  36. department1_units_id = config.get('Role Management', 'department1_units').split(', ')
  37. department1_units = [ctx.guild.get_role(int(role_id)) for role_id in department1_units_id]
  38. #department2_units_id = config.get('Role Management', 'department2_units').split(', ')
  39. #department2_units = [ctx.guild.get_role(int(role_id)) for role_id in
  40. if department1_role in user.roles:
  41. units = "department1"
  42. #elif department2_role in user.roles:
  43. #units = "department2"
  44. else:
  45. await ctx.respond("The selected user is not a member of any department!", ephemeral=True)
  46. return
  47. #Command implementation
  48. if unit == "Detective":
  49. unit_role = department1_units[0]
  50. elif unit == "SWAT":
  51. unit_role = department1_units[1]
  52. elif unit == "Canine":
  53. unit_role = department1_units[2]
  54. elif unit == "Air Support":
  55. unit_role = department1_units[3]
  56. else:
  57. await ctx.respond("The selected unit does not exist!", ephemeral=True)
  58. return
  59. def setup(bot: discord.Bot):
  60. bot.add_cog(unit(bot))