matesearch.py 4.3 KB

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