modified: bot.py

This commit is contained in:
SimolZimol
2026-01-08 21:33:46 +01:00
parent 63e754bd96
commit 52eea52def

78
bot.py
View File

@@ -51,10 +51,39 @@ def init_db():
archived BOOLEAN DEFAULT FALSE 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() conn.commit()
cursor.close() cursor.close()
conn.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) # Ticket storage (in production, use a database)
tickets = {} tickets = {}
ticket_counter = 1 ticket_counter = 1
@@ -66,29 +95,9 @@ class TicketStatus:
COMPLETED = "✅ Completed" COMPLETED = "✅ Completed"
CANCELLED = "❌ Cancelled" 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 @bot.event
async def on_ready(): async def on_ready():
print(f'{bot.user} has connected to Discord!') print(f'{bot.user} has connected to Discord!')
load_config()
init_db() init_db()
try: try:
synced = await bot.tree.sync() synced = await bot.tree.sync()
@@ -106,11 +115,11 @@ async def setup(
active_channel: discord.TextChannel, active_channel: discord.TextChannel,
archive_channel: discord.TextChannel archive_channel: discord.TextChannel
): ):
"""Setup the channels for ticket management""" guild_id = interaction.guild.id if interaction.guild else None
config["active_channel_id"] = active_channel.id if not guild_id:
config["archive_channel_id"] = archive_channel.id await interaction.response.send_message("❌ This command must be used in a server.", ephemeral=True)
save_config() return
save_server_settings(guild_id, active_channel.id, archive_channel.id)
await interaction.response.send_message( await interaction.response.send_message(
f"✅ Configuration saved!\n" f"✅ Configuration saved!\n"
f"Active tickets: {active_channel.mention}\n" f"Active tickets: {active_channel.mention}\n"
@@ -130,19 +139,15 @@ async def create_ticket(
project_name: str, project_name: str,
title: str title: str
): ):
"""Create a new ticket for project updates""" guild_id = interaction.guild.id if interaction.guild else None
global ticket_counter settings = load_server_settings(guild_id)
if not settings or not settings.get("active_channel_id"):
# Check if channels are configured
if not config.get("active_channel_id"):
await interaction.response.send_message( await interaction.response.send_message(
"❌ Bot not configured! Please use `/setup` first.", "❌ Bot not configured! Please use `/setup` first.",
ephemeral=True ephemeral=True
) )
return return
active_channel = bot.get_channel(settings["active_channel_id"])
# Get the active channel
active_channel = bot.get_channel(config["active_channel_id"])
if not active_channel: if not active_channel:
await interaction.response.send_message( await interaction.response.send_message(
"❌ Active channel not found! Please reconfigure with `/setup`.", "❌ 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): async def archive_ticket(interaction, ticket_id, message, current_channel):
"""Archive a completed ticket""" guild_id = interaction.guild.id if interaction.guild else None
settings = load_server_settings(guild_id)
archive_channel_id = config.get("archive_channel_id") archive_channel_id = settings.get("archive_channel_id") if settings else None
if not archive_channel_id: if not archive_channel_id:
await interaction.response.send_message( await interaction.response.send_message(
"⚠️ Status updated, but no archive channel configured. Use `/setup` to configure.", "⚠️ Status updated, but no archive channel configured. Use `/setup` to configure.",
ephemeral=True ephemeral=True
) )
return return
archive_channel = bot.get_channel(archive_channel_id) archive_channel = bot.get_channel(archive_channel_id)
if not archive_channel: if not archive_channel:
await interaction.response.send_message( await interaction.response.send_message(