more validate stuff and OSM intigration

This commit is contained in:
Soenke committed 2026-05-24 19:41:51 +02:00
1 parent a184ca6a57
commit 9cd6f160f9
2 files changed
+107 -4

No files matched your search

+1
View File
@@ -1,2 +1,3 @@
DISCORD_TOKEN="" DISCORD_TOKEN=""
CREATE_CHANNEL_ID= CREATE_CHANNEL_ID=
+106 -4
View File
@@ -1,5 +1,10 @@
import os import os
import re
import discord import discord
import traceback
import time
from geopy.geocoders import Nominatim
from geopy.exc import GeocoderTimedOut
from discord.ext import commands, tasks from discord.ext import commands, tasks
from discord import app_commands from discord import app_commands
from datetime import datetime from datetime import datetime
@@ -20,11 +25,54 @@ class ConfirmInteractiveMenu(discord.ui.View):
await interaction.followup.send(f"## Meetup Registered Successfully!", ephemeral=True) 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"): class CreateMeetupForm(discord.ui.Modal, title="Create a local meetup"):
def __init__(self, chosen_country: str): def __init__(self, chosen_country: str):
super().__init__() super().__init__()
self.chosen_country = chosen_country self.chosen_country = chosen_country
print("1")
form_postal_code = discord.ui.TextInput( form_postal_code = discord.ui.TextInput(
label="Enter the local postal code", 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])", label="Enter the end time (Format: HH:MM [UTC])",
style=discord.TextStyle.short, style=discord.TextStyle.short,
placeholder="HH:MM", placeholder="HH:MM",
required=False, required=True,
max_length=5 max_length=5
) )
@@ -67,19 +115,73 @@ class CreateMeetupForm(discord.ui.Modal, title="Create a local meetup"):
) )
async def on_submit(self, interaction: discord.Interaction): 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
)
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( await interaction.response.send_message(
content=( content=(
"## Event Register Preview\n" "## Event Register Preview\n"
f"**Country:** {self.chosen_country}\n" f"**Country:** {self.chosen_country}\n"
f"**Postal Code:** {self.form_postal_code.value}\n" f"**Postal Code:** {self.form_postal_code.value}\n"
f"**Date:** {self.form_date.value}\n" f"**State:** {self.state}\n"
f"**Time:** {self.form_start_time.value} - {self.form_end_time.value}\n" f"**Date:** {valid_date}\n"
f"**Time:** {formatted_start} - {formatted_end}\n"
f"**Location:** {self.form_location.value}\n" f"**Location:** {self.form_location.value}\n"
), ),
ephemeral=True, view=ConfirmInteractiveMenu() 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): class CountrySelect(discord.ui.Select):
def __init__(self): def __init__(self):
options = [ options = [