matesearch.py 4.2 KB

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