diff --git a/bot.py b/bot.py index 2f21218..ea42767 100644 --- a/bot.py +++ b/bot.py @@ -86,9 +86,59 @@ def load_server_settings(guild_id): conn.close() return result -# Ticket storage (in production, use a database) -tickets = {} -ticket_counter = 1 +def get_next_ticket_id(): + """Generate the next ticket ID based on existing tickets in database""" + conn = get_db_connection() + cursor = conn.cursor() + cursor.execute('SELECT COUNT(*) as count FROM tickets') + result = cursor.fetchone() + cursor.close() + conn.close() + count = result[0] if result else 0 + return f"TICKET-{count + 1:04d}" + +def save_ticket(ticket_id, ticket): + conn = get_db_connection() + cursor = conn.cursor() + cursor.execute(''' + REPLACE INTO tickets (ticket_id, message_id, channel_id, title, project, status, creator, created_at, reference_message_id, archived) + VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) + ''', ( + ticket_id, + ticket['message_id'], + ticket['channel_id'], + ticket['title'], + ticket['project'], + ticket['status'], + ticket['creator'], + ticket['created_at'], + ticket.get('reference_message_id'), + ticket.get('archived', False) + )) + conn.commit() + cursor.close() + conn.close() + +def load_ticket(ticket_id): + conn = get_db_connection() + cursor = conn.cursor(dictionary=True) + cursor.execute('SELECT * FROM tickets WHERE ticket_id = %s', (ticket_id,)) + result = cursor.fetchone() + cursor.close() + conn.close() + return result + +def load_all_tickets(archived=None): + conn = get_db_connection() + cursor = conn.cursor(dictionary=True) + if archived is None: + cursor.execute('SELECT * FROM tickets') + else: + cursor.execute('SELECT * FROM tickets WHERE archived = %s', (archived,)) + results = cursor.fetchall() + cursor.close() + conn.close() + return results # Project cache for autocomplete project_cache = [] @@ -201,9 +251,8 @@ async def create_ticket( ) return - # Create ticket ID - ticket_id = f"TICKET-{ticket_counter:04d}" - ticket_counter += 1 + # Create ticket ID from database + ticket_id = get_next_ticket_id() # Create embed for the ticket embed = discord.Embed(