modified: bot.py
This commit is contained in:
38
bot.py
38
bot.py
@@ -6,6 +6,7 @@ from dotenv import load_dotenv
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import json
|
import json
|
||||||
import mysql.connector
|
import mysql.connector
|
||||||
|
import aiohttp
|
||||||
|
|
||||||
# Load environment variables
|
# Load environment variables
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
@@ -95,10 +96,34 @@ class TicketStatus:
|
|||||||
COMPLETED = "✅ Completed"
|
COMPLETED = "✅ Completed"
|
||||||
CANCELLED = "❌ Cancelled"
|
CANCELLED = "❌ Cancelled"
|
||||||
|
|
||||||
|
# Dynamically fetch project names from devanturas.net/versions
|
||||||
|
PROJECTS_URL = "https://devanturas.net/versions"
|
||||||
|
_project_cache = []
|
||||||
|
|
||||||
|
async def fetch_project_names():
|
||||||
|
global _project_cache
|
||||||
|
if _project_cache:
|
||||||
|
return _project_cache
|
||||||
|
try:
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.get(PROJECTS_URL) as resp:
|
||||||
|
data = await resp.json()
|
||||||
|
names = [v["name"] for v in data.values() if "name" in v]
|
||||||
|
_project_cache = names
|
||||||
|
return names
|
||||||
|
except Exception:
|
||||||
|
return []
|
||||||
|
|
||||||
@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!')
|
||||||
init_db()
|
init_db()
|
||||||
|
# Preload project names
|
||||||
|
try:
|
||||||
|
import asyncio
|
||||||
|
asyncio.create_task(fetch_project_names())
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
try:
|
try:
|
||||||
synced = await bot.tree.sync()
|
synced = await bot.tree.sync()
|
||||||
print(f"Synced {len(synced)} command(s)")
|
print(f"Synced {len(synced)} command(s)")
|
||||||
@@ -129,9 +154,9 @@ async def setup(
|
|||||||
|
|
||||||
@bot.tree.command(name="ticket", description="Create a new project update ticket")
|
@bot.tree.command(name="ticket", description="Create a new project update ticket")
|
||||||
@app_commands.describe(
|
@app_commands.describe(
|
||||||
message_id="Optional: Discord message ID to reference",
|
project_name="Select the project for this ticket",
|
||||||
project_name="The project name from devanturas.net/versions",
|
title="Brief title for this update",
|
||||||
title="Brief title for this update"
|
message_id="Optional: Discord message ID to reference"
|
||||||
)
|
)
|
||||||
async def create_ticket(
|
async def create_ticket(
|
||||||
interaction: discord.Interaction,
|
interaction: discord.Interaction,
|
||||||
@@ -140,6 +165,13 @@ async def create_ticket(
|
|||||||
message_id: str = None
|
message_id: str = None
|
||||||
):
|
):
|
||||||
global ticket_counter
|
global ticket_counter
|
||||||
|
valid_projects = await fetch_project_names()
|
||||||
|
if project_name not in valid_projects:
|
||||||
|
await interaction.response.send_message(
|
||||||
|
f"❌ Invalid project name. Please use one of: {', '.join(valid_projects)}",
|
||||||
|
ephemeral=True
|
||||||
|
)
|
||||||
|
return
|
||||||
guild_id = interaction.guild.id if interaction.guild else None
|
guild_id = interaction.guild.id if interaction.guild else None
|
||||||
settings = load_server_settings(guild_id)
|
settings = load_server_settings(guild_id)
|
||||||
if not settings or not settings.get("active_channel_id"):
|
if not settings or not settings.get("active_channel_id"):
|
||||||
|
|||||||
Reference in New Issue
Block a user