matesearch.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. import os
  7. from dotenv import load_dotenv
  8. class Matesearch(commands.Cog):
  9. def __init__(self, bot: discord.Bot):
  10. self.bot = bot
  11. #Modal form
  12. class Form(discord.ui.Modal):
  13. def __init__(self, bot: discord.Bot, *args, **kwargs):
  14. super().__init__(*args, **kwargs)
  15. self.bot = bot
  16. #Modal form layout
  17. """self.add_item(Select(placeholder="Select a day.",
  18. options=[
  19. discord.SelectOption(label="Montag", value="Montag"),
  20. discord.SelectOption(label="Dienstag", value="Dienstag"),
  21. discord.SelectOption(label="Mittwoch", value="Mittwoch"),
  22. discord.SelectOption(label="Donnerstag", value="Donnerstag"),
  23. discord.SelectOption(label="Freitag", value="Freitag"),
  24. discord.SelectOption(label="Samstag", value="Samstag"),
  25. discord.SelectOption(label="Sonntag", value="Sontag"),
  26. ],
  27. min_values=1,
  28. max_values=1))"""
  29. self.add_item(discord.ui.InputText(label="Tag", placeholder="Montag, Dienstag, Mittwoch, Donnerstag, Freitag", required=False, ))
  30. self.add_item(discord.ui.InputText(label="Zeit", placeholder="19 Uhr"))
  31. #funktions to load the config and get the channels
  32. def _load_config(self):
  33. config = configparser.ConfigParser()
  34. configFilePath = r'config.cfg'
  35. config.read(configFilePath)
  36. return config
  37. def _get_guild(self):
  38. load_dotenv()
  39. debug_guilds_up = []
  40. server_token = os.getenv("SERVER").split(",")
  41. for i in range(len(server_token)):
  42. debug_guilds_up.append(int(server_token[i]))
  43. def _get_channel(self):
  44. config = self._load_config()
  45. search_channel_id = int(config["Matesearch"]["search_channel_id"])
  46. search_channel = self.bot.get_channel(search_channel_id)
  47. if search_channel is None:
  48. print(f"Channel with ID {search_channel_id} not found.")
  49. return None
  50. return search_channel
  51. def _get_role(self, interaction: discord.Interaction):
  52. config = self._load_config()
  53. search_role_id = int(config["Matesearch"]["search_role_id"])
  54. search_role = interaction.guild.get_role(search_role_id) if interaction.guild else None
  55. if search_role is None:
  56. print(f"Role with ID {search_role_id} not found.")
  57. return None
  58. return search_role_id
  59. #Output of the Modal form
  60. async def callback(self, interaction: discord.Interaction):
  61. #Public embed with less information
  62. role = self._get_role(interaction)
  63. #ping_value = role.mention if role is not None else "Role not found"
  64. embed = discord.Embed(title="Mitspieler gesucht!")
  65. embed.add_field(name="Ersteller", value=interaction.user.mention, inline=False)
  66. embed.add_field(name="Tag", value=self.children[0].value, inline=False)
  67. embed.add_field(name="Zeit", value=self.children[1].value, inline=False)
  68. self.embed = embed
  69. #sending the embed
  70. search_channel = self._get_channel()
  71. msg = await search_channel.send(embeds=[embed])
  72. await interaction.response.send_message("Your search for players has been submitted.", ephemeral=True)
  73. try:
  74. await msg.add_reaction("✅")
  75. await msg.add_reaction("❌")
  76. except Exception as e:
  77. await interaction.respond(f"Failed to add reaction: {e}", ephemeral=True)
  78. await search_channel.send(f"<@&{role}>")
  79. #Slash command to trigger the Modal form
  80. @slash_command()
  81. async def spielersuche(self, ctx: discord.ApplicationContext):
  82. """Start a search for players."""
  83. ## Only be able to send if last request is min 20h old
  84. config = self.Form._load_config(self)
  85. enable_search = config.getboolean("Matesearch","enable_search_mates")
  86. if not enable_search:
  87. return
  88. modal = self.Form(self.bot, title="Fill the form to search for players.") # type: ignore
  89. await ctx.send_modal(modal)
  90. def setup(bot: discord.Bot):
  91. bot.add_cog(Matesearch(bot))