From 162895edf36566be6923cf23c53272affb2f5642 Mon Sep 17 00:00:00 2001 From: Soenke Date: Sun, 24 May 2026 17:28:05 +0200 Subject: [PATCH] Mettup is now a form --- commands/create_meet.py | 204 +++++++++++++++++++++++----------------- 1 file changed, 118 insertions(+), 86 deletions(-) diff --git a/commands/create_meet.py b/commands/create_meet.py index 0888003..8679c68 100644 --- a/commands/create_meet.py +++ b/commands/create_meet.py @@ -1,111 +1,143 @@ +import os import discord -from discord.ext import commands +from discord.ext import commands, tasks from discord import app_commands from datetime import datetime +from dotenv import load_dotenv -class InteractiveMenu(discord.ui.View): +load_dotenv() +CREATE_CHANNEL_ID = os.getenv('CREATE_CHANNEL_ID') + +class ConfirmInteractiveMenu(discord.ui.View): def __init__(self): super().__init__(timeout=60.0) - @discord.ui.button(label="Register Event!", style=discord.ButtonStyle.success) + @discord.ui.button(label="Register Meetup!", style=discord.ButtonStyle.success) async def primary_button_callback(self, interaction: discord.Interaction, button: discord.ui.Button): for child in self.children: child.disabled = True await interaction.response.edit_message(view=self) - await interaction.followup.send(f"## Event Registered Successfully!") + await interaction.followup.send(f"## Meetup Registered Successfully!", ephemeral=True) +class CreateMeetupForm(discord.ui.Modal, title="Create a local meetup"): + def __init__(self, chosen_country: str): + super().__init__() + self.chosen_country = chosen_country + print("1") + + form_postal_code = discord.ui.TextInput( + label="Enter the local postal code", + style=discord.TextStyle.short, + placeholder="", + required=True, + max_length=10 + ) -class EventLookup(commands.Cog): + form_date = discord.ui.TextInput( + label="Enter the date (Format: YYYY-MM-DD)", + style=discord.TextStyle.short, + placeholder="YYYY-MM-DD", + required=True, + max_length=10 + ) + + form_start_time = discord.ui.TextInput( + label="Enter the start time (Format: HH:MM [UTC]", + style=discord.TextStyle.short, + placeholder="HH:MM", + required=True, + max_length=5 + ) + + form_end_time = discord.ui.TextInput( + label="Enter the end time (Format: HH:MM [UTC])", + style=discord.TextStyle.short, + placeholder="HH:MM", + required=False, + max_length=5 + ) + + form_location = discord.ui.TextInput( + label="Enter the location or specific", + style=discord.TextStyle.paragraph, + placeholder="", + required=True, + max_length=200 + ) + + async def on_submit(self, interaction: discord.Interaction): + + await interaction.response.send_message( + content=( + "## Event Register Preview\n" + f"**Country:** {self.chosen_country}\n" + f"**Postal Code:** {self.form_postal_code.value}\n" + f"**Date:** {self.form_date.value}\n" + f"**Time:** {self.form_start_time.value} - {self.form_end_time.value}\n" + f"**Location:** {self.form_location.value}\n" + ), + ephemeral=True, view=ConfirmInteractiveMenu() + ) + +class CountrySelect(discord.ui.Select): + def __init__(self): + options = [ + discord.SelectOption(label="Deutschland", value="de", emoji="🇩🇪"), + discord.SelectOption(label="Österreich", value="at", emoji="🇦🇹"), + discord.SelectOption(label="Switzerland", value="ch", emoji="🇨🇭"), + discord.SelectOption(label="United Kingdom", value="uk", emoji="🇬🇧"), + discord.SelectOption(label="United States", value="us", emoji="🇺🇸"), + ] + super().__init__(placeholder="Select your country...", min_values=1, max_values=1, options=options) + + async def callback(self, interaction: discord.Interaction): + await interaction.response.send_modal(CreateMeetupForm(chosen_country=self.values[0])) + + await interaction.delete_original_response() + +class CountryDropdownView(discord.ui.View): + def __init__(self): + super().__init__(timeout=60) + self.add_item(CountrySelect()) + +class MainInteractiveMenu(discord.ui.View): + def __init__(self): + super().__init__() + + @discord.ui.button(label="Register a Meetup", style=discord.ButtonStyle.success) + async def primary_button_callback(self, interaction: discord.Interaction, button: discord.ui.Button): + await interaction.response.send_message( + content="Please select your country below to continue:", + view=CountryDropdownView(), + ephemeral=True + ) + +class CreateMeetup(commands.Cog): def __init__(self, bot: commands.Bot): self.bot = bot - @app_commands.command(name="create_meet", description="Create a local meetup") - @app_commands.choices(country=[ - app_commands.Choice(name="Deutschland", value="de"), - app_commands.Choice(name="United Kingdom", value="uk"), - app_commands.Choice(name="Österreich", value="at"), - app_commands.Choice(name="Switzerland", value="ch") - ]) - @app_commands.describe( - country="Select your country from the list", - postal_code="Enter the local postal code", + async def cog_load(self): + self.startup.start() - date="Enter the date (Format: YYYY-MM-DD)", - start_time="Enter start time (Format: HH:MM [UTC+00:00], e.g., 14:30)", - end_time="Enter end time (Format: HH:MM [UTC+00:00], e.g., 17:00)", - location="Enter the location or specific address" - ) - async def create_meet(self, - interaction: discord.Interaction, - country: app_commands.Choice[str], - date: str, - start_time: str, - end_time: str, - postal_code: str, - location: str - ): + def cog_unload(self): + self.startup.cancel() + @tasks.loop(count=1) + async def startup(self): + await self.bot.wait_until_ready() - try: - valid_date = datetime.strptime(date, "%Y-%m-%d").date() - formatted_date = valid_date.strftime("%B %d, %Y") - except ValueError: - await interaction.response.send_message( - f"❌ Invalid date format: `{date}`. Please use **YYYY-MM-DD**.", - ephemeral=True - ) - return + channel = await self.bot.fetch_channel(CREATE_CHANNEL_ID) + await channel.purge(limit=None, bulk=True) - if datetime.now().date() > valid_date: - await interaction.response.send_message( - "❌ The date cannot be in the past!", - ephemeral=True - ) - return + await channel.send(""" +# Create a local meetup +bla bla bla +bla bla bla +-# bla bla bla + """, view=MainInteractiveMenu()) - try: - valid_start = datetime.strptime(start_time, "%H:%M").time() - valid_end = datetime.strptime(end_time, "%H:%M").time() - - formatted_start = valid_start.strftime("%H:%M") - formatted_end = valid_end.strftime("%H:%M") - except ValueError: - await interaction.response.send_message( - f"❌ Invalid time format. Please use **HH:MM** in 24-hour format (e.g., `14:30`).", - ephemeral=True - ) - return - - - if valid_start >= valid_end: - await interaction.response.send_message( - "❌ The start time must be earlier than the end time!", - ephemeral=True - ) - return - - - country_labels = { - "de": "Deutschland", - "uk": "United Kingdom", - "at": "Österreich", - "ch": "Switzerland" - } - chosen_country = country_labels.get(country.value, country.name) - - response_text = ( - f"## Event Register Preview\n" - f"- **Country:** {chosen_country}\n" - f"- **Postal Code:** {postal_code}\n" - f"- **Date:** {formatted_date}\n" - f"- **Time:** {formatted_start} - {formatted_end}\n" - f"- **Location:** {location}\n" - ) - - await interaction.response.send_message(response_text, view=InteractiveMenu()) - async def setup(bot: commands.Bot): - await bot.add_cog(EventLookup(bot)) \ No newline at end of file + await bot.add_cog(CreateMeetup(bot)) \ No newline at end of file