86 lines
2.9 KiB
Python
86 lines
2.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Discord User Data Collector
|
|
Main application entry point for collecting Discord user data for research purposes.
|
|
"""
|
|
|
|
import asyncio
|
|
import logging
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Check if we're in the right directory
|
|
if not Path("src").exists():
|
|
print("❌ Error: 'src' directory not found. Please run from the project root directory.")
|
|
sys.exit(1)
|
|
|
|
# Add src to Python path
|
|
sys.path.insert(0, str(Path(__file__).parent))
|
|
|
|
try:
|
|
from src.client import DiscordDataClient
|
|
from src.config import Config
|
|
from src.database import create_database
|
|
from src.logger import setup_logger
|
|
except ImportError as e:
|
|
print(f"❌ Import error: {e}")
|
|
print("\n🔧 To fix this, try:")
|
|
print("1. Run: python setup.py")
|
|
print("2. Or run: python test_imports.py")
|
|
print("3. Or install dependencies: pip install discord.py-self python-dotenv toml colorlog")
|
|
sys.exit(1)
|
|
|
|
|
|
async def main():
|
|
"""Main application entry point."""
|
|
try:
|
|
# Setup configuration
|
|
config = Config()
|
|
|
|
# Setup logging
|
|
logger = setup_logger(config.log_level, config.log_file)
|
|
logger.info("Starting Discord Data Collector")
|
|
|
|
# Initialize database with MariaDB->JSON fallback
|
|
mariadb_config = config.get_mariadb_config()
|
|
if mariadb_config:
|
|
logger.info(f"Found MariaDB config for {mariadb_config['host']}:{mariadb_config['port']}")
|
|
else:
|
|
logger.info("No MariaDB credentials found in .env, will use JSON fallback")
|
|
|
|
database = await create_database(mariadb_config=mariadb_config)
|
|
|
|
# Test Discord connectivity first
|
|
logger.info("Testing Discord connectivity...")
|
|
try:
|
|
import aiohttp
|
|
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=5)) as session:
|
|
async with session.get("https://discord.com/api/v10/gateway") as response:
|
|
if response.status != 200:
|
|
raise Exception(f"Discord API returned {response.status}")
|
|
logger.info("✅ Discord connectivity confirmed")
|
|
except Exception as e:
|
|
logger.error(f"❌ Discord connectivity failed: {e}")
|
|
logger.error("Please check network settings, firewall, or try a different network")
|
|
sys.exit(1)
|
|
|
|
# Initialize Discord client
|
|
client = DiscordDataClient(config, database)
|
|
|
|
# Start the client
|
|
logger.info("Starting Discord client...")
|
|
await client.start(config.discord_token)
|
|
|
|
except KeyboardInterrupt:
|
|
logger.info("Received keyboard interrupt, shutting down...")
|
|
except Exception as e:
|
|
logger.error(f"Fatal error: {e}", exc_info=True)
|
|
sys.exit(1)
|
|
finally:
|
|
if 'client' in locals():
|
|
await client.close()
|
|
logger.info("Application shutdown complete")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main()) |