matesearch.py 3.6 KB

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