matesearch.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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
  47. #Output of the Modal form
  48. async def callback(self, interaction: discord.Interaction):
  49. #Public embed with less information
  50. embed = discord.Embed(title="Mitspieler gesucht!")
  51. embed.add_field(name="Ersteller", value=interaction.user.mention, inline=False)
  52. embed.add_field(name="Datum", value=self.children[0].value, inline=False)
  53. embed.add_field(name="Zeit", value=self.children[1].value, inline=False)
  54. embed.add_field(name="Ping", value=self._get_role, inline=False)
  55. self.embed = embed
  56. #sending the embed
  57. await interaction.response.send_message("Your search for players has been submitted.", ephemeral=True)
  58. search_channel = self._get_channel()
  59. await search_channel.send(embeds=[embed])
  60. #Slash command to trigger the Modal form
  61. @slash_command()
  62. async def mitspielergesucht(self, ctx: discord.ApplicationContext):
  63. """Start a search for players."""
  64. ## Only be able to send if last request is min 20h old
  65. config = self.Form._load_config(self)
  66. enable_search = config.getboolean("Matesearch","enable_search_mates")
  67. if not enable_search:
  68. return
  69. modal = self.Form(self.bot, title="Fill the form to search for players.") # type: ignore
  70. await ctx.send_modal(modal)
  71. def setup(bot: discord.Bot):
  72. bot.add_cog(Matesearch(bot))