From 52eea52def5b95e53c74e778c492e3a83b394999 Mon Sep 17 00:00:00 2001 From: SimolZimol <70102430+SimolZimol@users.noreply.github.com> Date: Thu, 8 Jan 2026 21:33:46 +0100 Subject: [PATCH] modified: bot.py --- bot.py | 78 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/bot.py b/bot.py index 4fe1604..70405b4 100644 --- a/bot.py +++ b/bot.py @@ -51,10 +51,39 @@ def init_db(): archived BOOLEAN DEFAULT FALSE ) ''') + cursor.execute(''' + CREATE TABLE IF NOT EXISTS server_settings ( + guild_id BIGINT PRIMARY KEY, + active_channel_id BIGINT, + archive_channel_id BIGINT + ) + ''') conn.commit() cursor.close() conn.close() +# Server settings DB functions + +def save_server_settings(guild_id, active_channel_id, archive_channel_id): + conn = get_db_connection() + cursor = conn.cursor() + cursor.execute(''' + REPLACE INTO server_settings (guild_id, active_channel_id, archive_channel_id) + VALUES (%s, %s, %s) + ''', (guild_id, active_channel_id, archive_channel_id)) + conn.commit() + cursor.close() + conn.close() + +def load_server_settings(guild_id): + conn = get_db_connection() + cursor = conn.cursor(dictionary=True) + cursor.execute('SELECT * FROM server_settings WHERE guild_id = %s', (guild_id,)) + result = cursor.fetchone() + cursor.close() + conn.close() + return result + # Ticket storage (in production, use a database) tickets = {} ticket_counter = 1 @@ -66,29 +95,9 @@ class TicketStatus: COMPLETED = "✅ Completed" CANCELLED = "❌ Cancelled" -# Channel IDs (configure these in config.json) -config = {} - -def load_config(): - global config - try: - with open('config.json', 'r') as f: - config = json.load(f) - except FileNotFoundError: - config = { - "active_channel_id": None, - "archive_channel_id": None - } - save_config() - -def save_config(): - with open('config.json', 'w') as f: - json.dump(config, f, indent=4) - @bot.event async def on_ready(): print(f'{bot.user} has connected to Discord!') - load_config() init_db() try: synced = await bot.tree.sync() @@ -106,11 +115,11 @@ async def setup( active_channel: discord.TextChannel, archive_channel: discord.TextChannel ): - """Setup the channels for ticket management""" - config["active_channel_id"] = active_channel.id - config["archive_channel_id"] = archive_channel.id - save_config() - + guild_id = interaction.guild.id if interaction.guild else None + if not guild_id: + await interaction.response.send_message("❌ This command must be used in a server.", ephemeral=True) + return + save_server_settings(guild_id, active_channel.id, archive_channel.id) await interaction.response.send_message( f"✅ Configuration saved!\n" f"Active tickets: {active_channel.mention}\n" @@ -130,19 +139,15 @@ async def create_ticket( project_name: str, title: str ): - """Create a new ticket for project updates""" - global ticket_counter - - # Check if channels are configured - if not config.get("active_channel_id"): + guild_id = interaction.guild.id if interaction.guild else None + settings = load_server_settings(guild_id) + if not settings or not settings.get("active_channel_id"): await interaction.response.send_message( "❌ Bot not configured! Please use `/setup` first.", ephemeral=True ) return - - # Get the active channel - active_channel = bot.get_channel(config["active_channel_id"]) + active_channel = bot.get_channel(settings["active_channel_id"]) if not active_channel: await interaction.response.send_message( "❌ Active channel not found! Please reconfigure with `/setup`.", @@ -298,16 +303,15 @@ async def update_status( ) async def archive_ticket(interaction, ticket_id, message, current_channel): - """Archive a completed ticket""" - - archive_channel_id = config.get("archive_channel_id") + guild_id = interaction.guild.id if interaction.guild else None + settings = load_server_settings(guild_id) + archive_channel_id = settings.get("archive_channel_id") if settings else None if not archive_channel_id: await interaction.response.send_message( "⚠️ Status updated, but no archive channel configured. Use `/setup` to configure.", ephemeral=True ) return - archive_channel = bot.get_channel(archive_channel_id) if not archive_channel: await interaction.response.send_message(