matesearch.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. class Matesearch(commands.Cog):
  7. def __init__(self, bot: discord.Bot):
  8. self.bot = bot
  9. #Modal form
  10. class Form(discord.ui.Modal):
  11. def __init__(self, bot: discord.Bot, *args, **kwargs):
  12. super().__init__(*args, **kwargs)
  13. self.bot = bot
  14. #Modal form layout
  15. self.add_item(discord.ui.InputText(label="Date", placeholder="Insert the estimated date.", required=False, ))
  16. self.add_item(discord.ui.InputText(label="Time", placeholder="Insert the estimated time."))
  17. #funktions to load the config and get the channels
  18. def _load_config(self):
  19. config = configparser.ConfigParser()
  20. configFilePath = r'config.cfg'
  21. config.read(configFilePath)
  22. return config
  23. def _get_channel(self):
  24. config = self._load_config()
  25. search_channel_id = int(config["Matesearch"]["search_channel_id"])
  26. search_channel = self.bot.get_channel(search_channel_id)
  27. if search_channel is None:
  28. print(f"Log channel with ID {search_channel_id} not found.")
  29. return None
  30. return search_channel
  31. def _get_role(self):
  32. config = self._load_config()
  33. search_role_id = int(config["Matesearch"]["search_role_id"])
  34. search_role = self.bot.get_role(search_role_id)
  35. if search_role is None:
  36. print(f"Log channel with ID {search_role_id} not found.")
  37. return None
  38. return search_role
  39. #Output of the Modal form
  40. async def callback(self, interaction: discord.Interaction):
  41. #Public embed with less information
  42. embed = discord.Embed(title="Mitspieler gesucht!")
  43. embed.add_field(name="Ersteller", value=interaction.user.mention, inline=False)
  44. embed.add_field(name="Datum", value=self.children[0].value, inline=False)
  45. embed.add_field(name="Zeit", value=self.children[1].value, inline=False)
  46. embed.add_field(value=self.get_role(), inline=False)
  47. self.embed = embed
  48. #sending the embed
  49. await interaction.response.send_message("Your search for players has been submitted.", ephemeral=True)
  50. search_channel = self._get_channel()
  51. await search_channel.send(embeds=[embed])
  52. #Slash command to trigger the Modal form
  53. @slash_command()
  54. async def mitspielergesucht(self, ctx: discord.ApplicationContext):
  55. """Start a search for players."""
  56. ## Only be able to send if last request is min 20h old
  57. config = self.Form._load_config(self)
  58. enable_search = config.getboolean("Matesearch","enable_search_mates")
  59. if not enable_search:
  60. return
  61. modal = self.Form(self.bot, title="Fill the form to search for players.") # type: ignore
  62. await ctx.send_modal(modal)
  63. def setup(bot: discord.Bot):
  64. bot.add_cog(Matesearch(bot))