diff --git a/.env.example b/.env.example index 0deeb8e..8036305 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,3 @@ DISCORD_TOKEN="" + CREATE_CHANNEL_ID= \ No newline at end of file diff --git a/commands/create_meet.py b/commands/create_meet.py index 8679c68..f0c3c4d 100644 --- a/commands/create_meet.py +++ b/commands/create_meet.py @@ -1,5 +1,10 @@ import os +import re import discord +import traceback +import time +from geopy.geocoders import Nominatim +from geopy.exc import GeocoderTimedOut from discord.ext import commands, tasks from discord import app_commands from datetime import datetime @@ -20,11 +25,54 @@ class ConfirmInteractiveMenu(discord.ui.View): await interaction.followup.send(f"## Meetup Registered Successfully!", ephemeral=True) +def validate_postal_code(country_code: str, postal_code: str) -> bool: + country_code = country_code.upper().strip() + postal_code = postal_code.strip() + + patterns = { + "DE": r"^\d{4,5}$", # Pure digits (4-5) + "AT": r"^\d{4,5}$", # Pure digits (4-5) + "CH": r"^\d{4,5}$", # Pure digits (4-5) + "US": r"^\d{5}(-\d{4})?$", # 5-digit or ZIP+4 + "UK": r"^[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}$" # Case-insensitive alphanumeric + } + + if country_code not in patterns: + return False + + if country_code == "UK": + postal_code = postal_code.upper() + + return bool(re.match(patterns[country_code], postal_code)) + +def get_state_from_postal_code(country_code: str, postal_code: str) -> str: + geolocator = Nominatim(user_agent="discord_bot_local_femboy_meetup") + + query = { + "postalcode": postal_code, + "country": country_code + } + + try: + location = geolocator.geocode(query, addressdetails=True, timeout=10) + + if location and "address" in location.raw: + address = location.raw["address"] + + state_keys = ["state", "province", "state_district", "region"] + for key in state_keys: + if key in address: + return address[key] + + return "Error: State not found :c" + + except GeocoderTimedOut: + return "Error: Request timed out :c" + 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", @@ -54,7 +102,7 @@ class CreateMeetupForm(discord.ui.Modal, title="Create a local meetup"): label="Enter the end time (Format: HH:MM [UTC])", style=discord.TextStyle.short, placeholder="HH:MM", - required=False, + required=True, max_length=5 ) @@ -67,27 +115,81 @@ class CreateMeetupForm(discord.ui.Modal, title="Create a local meetup"): ) async def on_submit(self, interaction: discord.Interaction): + try: + try: + valid_date = datetime.strptime(self.form_date.value, "%Y-%m-%d").date() + formatted_date = valid_date.strftime("%B %d, %Y") + except ValueError: + await interaction.response.send_message( + "❌ **Validation Error:** Invalid date format. Please use **YYYY-MM-DD**.", + ephemeral=True + ) - 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() - ) + return + + if datetime.now().date() > valid_date: + await interaction.response.send_message( + "❌ **Validation Error:** The date cannot be in the past!", + ephemeral=True + ) + return + + try: + valid_start = datetime.strptime(self.form_start_time.value, "%H:%M").time() + valid_end = datetime.strptime(self.form_end_time.value, "%H:%M").time() + + formatted_start = valid_start.strftime("%H:%M") + formatted_end = valid_end.strftime("%H:%M") + except ValueError: + await interaction.response.send_message( + "❌ **Validation Error:** Invalid time format. Please use **HH:MM** in 24-hour format.", + ephemeral=True + ) + return + + if valid_start >= valid_end: + await interaction.response.send_message( + "❌ **Validation Error:** The start time must be earlier than the end time!", + ephemeral=True + ) + return + + if not validate_postal_code(self.chosen_country, self.form_postal_code.value): + await interaction.response.send_message( + "❌ **Validation Error:** Please use a valid postal code for the local country", + ephemeral=True + ) + return + + self.state = get_state_from_postal_code(self.chosen_country, self.form_postal_code.value) + + 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"**State:** {self.state}\n" + f"**Date:** {valid_date}\n" + f"**Time:** {formatted_start} - {formatted_end}\n" + f"**Location:** {self.form_location.value}\n" + ), + ephemeral=True, view=ConfirmInteractiveMenu() + ) + + except Exception as e: + print("CRITICAL ERROR IN SUBMIT:") + traceback.print_exc() + + await interaction.response.send_message("An internal error occurred :c", ephemeral=True) 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="🇺🇸"), + 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)