import discord from discord.ext import commands from discord.commands import Option from discord.commands import slash_command import configparser import os from dotenv import load_dotenv class Matesearch(commands.Cog): def __init__(self, bot: discord.Bot): self.bot = bot #Modal form class Form(discord.ui.Modal): def __init__(self, bot: discord.Bot, *args, **kwargs): super().__init__(*args, **kwargs) self.bot = bot #Modal form layout self.add_item(discord.ui.InputText(label="Date", placeholder="Insert the estimated date.", required=False, )) self.add_item(discord.ui.InputText(label="Time", placeholder="Insert the estimated time.")) #funktions to load the config and get the channels def _load_config(self): config = configparser.ConfigParser() configFilePath = r'config.cfg' config.read(configFilePath) return config def _get_guild(self): load_dotenv() debug_guilds_up = [] server_token = os.getenv("SERVER").split(",") for i in range(len(server_token)): debug_guilds_up.append(int(server_token[i])) def _get_channel(self): config = self._load_config() search_channel_id = int(config["Matesearch"]["search_channel_id"]) search_channel = self.bot.get_channel(search_channel_id) if search_channel is None: print(f"Channel with ID {search_channel_id} not found.") return None return search_channel def _get_role(self, interaction: discord.Interaction): config = self._load_config() search_role_id = int(config["Matesearch"]["search_role_id"]) search_role = interaction.guild.get_role(search_role_id) if interaction.guild else None if search_role is None: print(f"Role with ID {search_role_id} not found.") return None return search_role #Output of the Modal form async def callback(self, interaction: discord.Interaction): #Public embed with less information embed = discord.Embed(title="Mitspieler gesucht!") embed.add_field(name="Ersteller", value=interaction.user.mention, inline=False) embed.add_field(name="Datum", value=self.children[0].value, inline=False) embed.add_field(name="Zeit", value=self.children[1].value, inline=False) embed.add_field(value=self._get_role, inline=False) self.embed = embed #sending the embed await interaction.response.send_message("Your search for players has been submitted.", ephemeral=True) search_channel = self._get_channel() await search_channel.send(embeds=[embed]) #Slash command to trigger the Modal form @slash_command() async def mitspielergesucht(self, ctx: discord.ApplicationContext): """Start a search for players.""" ## Only be able to send if last request is min 20h old config = self.Form._load_config(self) enable_search = config.getboolean("Matesearch","enable_search_mates") if not enable_search: return modal = self.Form(self.bot, title="Fill the form to search for players.") # type: ignore await ctx.send_modal(modal) def setup(bot: discord.Bot): bot.add_cog(Matesearch(bot))