civ_situ.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 civsitu(commands.Cog):
  7. def __init__(self, bot: discord.Bot):
  8. self.bot = bot
  9. #Modal form
  10. class Situ(discord.ui.Modal):
  11. def __init__(self, bot: discord.Bot, *args, **kwargs):
  12. super().__init__(*args, **kwargs)
  13. self.bot = bot
  14. #Modal form layout
  15. self.add_item(discord.ui.InputText(label="Teilnehmende Spieler", placeholder="Gib die Namen der teilnehmenden Spieler abgesehen von dir ein.", required=False, ))
  16. self.add_item(discord.ui.InputText(label="Benötigte Ausrüstung", placeholder="Gib die benötigte Ausrüstung an (Drogen, Waffen, etc.)."))
  17. self.add_item(discord.ui.InputText(label="Situationsbeschreibung", style=discord.InputTextStyle.long, placeholder="Beschreibe die Situation so detailliert wie möglich (Was? Wo? Wie?)."))
  18. self.add_item(discord.ui.InputText(label="Besonderheiten", placeholder="Langzeitsituationen, besondere Umstände, etc.", required=False))
  19. self.add_item(discord.ui.InputText(label="Geschätzte Dauer", placeholder="Gib die geschätzte Dauer der Situation an.", required=True))
  20. #funktions to load the config and get the channels
  21. def _load_config(self):
  22. config = configparser.ConfigParser()
  23. configFilePath = r'config.cfg'
  24. config.read(configFilePath)
  25. return config
  26. def _get_situ_channel(self):
  27. config = self._load_config()
  28. situ_channel_id = int(config["Civ"]["situ_channel_id"])
  29. situ_channel = self.bot.get_channel(situ_channel_id)
  30. if situ_channel is None:
  31. print(f"Log channel with ID {situ_channel_id} not found.")
  32. return None
  33. return situ_channel
  34. #This is for futur purposes
  35. """def _get_situ_team_channel(self):
  36. config = self._load_config()
  37. situ_team_channel_id = int(config["Civ"]["situ_team_channel_id"])
  38. situ_team_channel = self.bot.get_channel(situ_team_channel_id)
  39. if situ_team_channel is None:
  40. print(f"Log channel with ID {situ_team_channel_id} not found.")
  41. return None
  42. return situ_team_channel"""
  43. #Output of the Modal form
  44. async def callback(self, interaction: discord.Interaction):
  45. #Public embed with less information
  46. embed_pub = discord.Embed(title="Aktive Situation")
  47. embed_pub.add_field(name="Ersteller", value=interaction.user.mention, inline=False)
  48. embed_pub.add_field(name="Teilnehmer", value=self.children[0].value, inline=False)
  49. embed_pub.add_field(name="Geschätzte Dauer", value=self.children[4].value, inline=False)
  50. self.embed_pub = embed_pub
  51. #sending the embed
  52. await interaction.response.send_message("Your situation request has been created.", ephemeral=True)
  53. situ_channel = self._get_situ_channel()
  54. msg = await situ_channel.send(embeds=[embed_pub])
  55. name = f"situation-{interaction.user.name}"
  56. thread =await msg.create_thread(name=name, auto_archive_duration=1440)
  57. #embed with more information sent into a thread attached to the public embed
  58. embed_team = discord.Embed(title="Aktive Situation")
  59. embed_team.add_field(name="Ersteller", value=interaction.user.mention, inline=False)
  60. embed_team.add_field(name="Teilnehmer", value=self.children[0].value, inline=False)
  61. embed_team.add_field(name="Benötigte Ausrüstung", value=self.children[1].value, inline=False)
  62. embed_team.add_field(name="Situationsbeschreibung", value=self.children[2].value, inline=False)
  63. embed_team.add_field(name="Besonderheiten", value=self.children[3].value, inline=False)
  64. embed_team.add_field(name="Geschätzte Dauer", value=self.children[4].value, inline=False)
  65. #sending the embed with more information into the thread
  66. self.embed_team = embed_team
  67. await thread.send(embeds=[embed_team])
  68. await thread.send(f"{interaction.user.mention} Please wait with the start of your situation until a (civ-) team member approved your situation.")
  69. #Slash command to trigger the Modal form
  70. @slash_command()
  71. async def civsituation(self, ctx: discord.ApplicationContext):
  72. """Start a new civ-RP situation."""
  73. modal = self.Situ(self.bot, title="Create a new civ-rp situation") # type: ignore
  74. await ctx.send_modal(modal)
  75. def setup(bot: discord.Bot):
  76. bot.add_cog(civsitu(bot))