matesearch.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. member = discord.Member
  34. search_role_id = int(config["Matesearch"]["search_role_id"])
  35. search_role = member.guild.get_role(search_role_id)
  36. if search_role is None:
  37. print(f"Log channel with ID {search_role_id} not found.")
  38. return None
  39. return search_role
  40. #Output of the Modal form
  41. async def callback(self, interaction: discord.Interaction):
  42. #Public embed with less information
  43. embed = discord.Embed(title="Mitspieler gesucht!")
  44. embed.add_field(name="Ersteller", value=interaction.user.mention, inline=False)
  45. embed.add_field(name="Datum", value=self.children[0].value, inline=False)
  46. embed.add_field(name="Zeit", value=self.children[1].value, inline=False)
  47. embed.add_field(value=self._get_role(), inline=False)
  48. self.embed = embed
  49. #sending the embed
  50. await interaction.response.send_message("Your search for players has been submitted.", ephemeral=True)
  51. search_channel = self._get_channel()
  52. await search_channel.send(embeds=[embed])
  53. #Slash command to trigger the Modal form
  54. @slash_command()
  55. async def mitspielergesucht(self, ctx: discord.ApplicationContext):
  56. """Start a search for players."""
  57. ## Only be able to send if last request is min 20h old
  58. config = self.Form._load_config(self)
  59. enable_search = config.getboolean("Matesearch","enable_search_mates")
  60. if not enable_search:
  61. return
  62. modal = self.Form(self.bot, title="Fill the form to search for players.") # type: ignore
  63. await ctx.send_modal(modal)
  64. def setup(bot: discord.Bot):
  65. bot.add_cog(Matesearch(bot))