test 1 de la récupération de pp discord sur site web
This commit is contained in:
parent
9d0bb77225
commit
ee31269209
9 changed files with 591 additions and 0 deletions
9
src/api/__init__.py
Normal file
9
src/api/__init__.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
"""
|
||||
API module for Kuby Discord bot
|
||||
Provides REST API endpoints for bot functionality
|
||||
"""
|
||||
|
||||
from .app import APIManager
|
||||
from .config import APIConfig, PARTNER_IDS
|
||||
|
||||
__all__ = ['APIManager', 'APIConfig', 'PARTNER_IDS']
|
||||
56
src/api/app.py
Normal file
56
src/api/app.py
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
"""
|
||||
Flask application for Kuby Discord bot API
|
||||
"""
|
||||
|
||||
from flask import Flask
|
||||
from flask_cors import CORS
|
||||
import threading
|
||||
from .routes import setup_routes
|
||||
from .config import APIConfig
|
||||
from src.logger import kuby_logger
|
||||
|
||||
|
||||
class APIManager:
|
||||
"""Manages the Flask API server for the bot"""
|
||||
|
||||
def __init__(self, bot_instance):
|
||||
"""Initialize the API manager
|
||||
|
||||
Args:
|
||||
bot_instance: The Discord bot instance
|
||||
"""
|
||||
self.bot = bot_instance
|
||||
self.app = Flask(__name__)
|
||||
CORS(self.app)
|
||||
self.config = APIConfig()
|
||||
|
||||
# Configuration Flask
|
||||
self.app.config['SECRET_KEY'] = self.config.SECRET_KEY
|
||||
self.app.config['JSON_SORT_KEYS'] = False
|
||||
|
||||
# Setup routes
|
||||
setup_routes(self.app, self.bot)
|
||||
|
||||
kuby_logger.info("✅ API Manager initialized")
|
||||
|
||||
def run_async(self):
|
||||
"""Start the API server in a separate thread"""
|
||||
try:
|
||||
kuby_logger.info(f"🌐 Starting API server on {self.config.HOST}:{self.config.PORT}")
|
||||
threading.Thread(
|
||||
target=lambda: self.app.run(
|
||||
host=self.config.HOST,
|
||||
port=self.config.PORT,
|
||||
debug=self.config.DEBUG,
|
||||
use_reloader=False # Important: disable reloader in thread
|
||||
),
|
||||
daemon=True
|
||||
).start()
|
||||
kuby_logger.info(f"✅ API server started successfully")
|
||||
except Exception as e:
|
||||
kuby_logger.error(f"❌ Failed to start API server: {e}")
|
||||
raise
|
||||
|
||||
def get_app(self):
|
||||
"""Get the Flask app instance (for testing purposes)"""
|
||||
return self.app
|
||||
22
src/api/config.py
Normal file
22
src/api/config.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
"""
|
||||
Configuration for the API module
|
||||
"""
|
||||
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
class APIConfig:
|
||||
"""API configuration class"""
|
||||
HOST = os.getenv("API_HOST", "127.0.0.1")
|
||||
PORT = int(os.getenv("API_PORT", 5000))
|
||||
DEBUG = os.getenv("API_DEBUG", "False").lower() == "true"
|
||||
SECRET_KEY = os.getenv("API_SECRET_KEY", "votre-cle-secrete-api")
|
||||
|
||||
# Partner server IDs for the API
|
||||
PARTNER_IDS = [
|
||||
111111111111111111, # ID serveur partenaire 1
|
||||
222222222222222222, # ID serveur partenaire 2
|
||||
333333333333333333, # ID serveur partenaire 3
|
||||
]
|
||||
62
src/api/routes.py
Normal file
62
src/api/routes.py
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
"""
|
||||
API routes for Kuby Discord bot
|
||||
"""
|
||||
|
||||
from flask import jsonify
|
||||
from .config import PARTNER_IDS
|
||||
|
||||
|
||||
def setup_routes(app, bot):
|
||||
"""Setup all API routes"""
|
||||
|
||||
@app.route("/api/user/<int:user_id>")
|
||||
def get_user(user_id):
|
||||
"""Get user information by ID"""
|
||||
user = bot.get_user(user_id)
|
||||
if not user:
|
||||
return jsonify({
|
||||
"username": "Utilisateur inconnu",
|
||||
"avatar_url": "https://cdn.discordapp.com/embed/avatars/0.png",
|
||||
})
|
||||
return jsonify({
|
||||
"username": user.display_name,
|
||||
"avatar_url": str(user.display_avatar.url) if user.display_avatar else "https://cdn.discordapp.com/embed/avatars/0.png",
|
||||
})
|
||||
|
||||
@app.route("/api/partners")
|
||||
def get_partners():
|
||||
"""Get partner server information"""
|
||||
partners = []
|
||||
for guild_id in PARTNER_IDS:
|
||||
guild = bot.get_guild(guild_id)
|
||||
if guild:
|
||||
partners.append({
|
||||
"name": guild.name,
|
||||
"icon_url": guild.icon.url if guild.icon else None,
|
||||
"member_count": guild.member_count
|
||||
})
|
||||
return jsonify(partners)
|
||||
|
||||
@app.route("/api/status")
|
||||
def get_status():
|
||||
"""Get bot status and basic information"""
|
||||
return jsonify({
|
||||
"status": "online" if bot.is_ready() else "connecting",
|
||||
"username": bot.user.name if bot.user else "Unknown",
|
||||
"guild_count": len(bot.guilds),
|
||||
"total_members": sum(guild.member_count for guild in bot.guilds) if bot.guilds else 0
|
||||
})
|
||||
|
||||
@app.route("/api/guilds")
|
||||
def get_guilds():
|
||||
"""Get basic information about all guilds the bot is in"""
|
||||
guilds_info = []
|
||||
for guild in bot.guilds:
|
||||
guilds_info.append({
|
||||
"id": guild.id,
|
||||
"name": guild.name,
|
||||
"icon_url": guild.icon.url if guild.icon else None,
|
||||
"member_count": guild.member_count,
|
||||
"owner_id": guild.owner_id
|
||||
})
|
||||
return jsonify(guilds_info)
|
||||
Loading…
Add table
Add a link
Reference in a new issue