Easy Setup (Ready-to-use)

The easiest way to integrate Cordia is to use a global event listener. Instead of modifying every single command in your bot, you can simply drop in one file that listens for all command executions automatically.

💡Zero-Effort Integration
Using this method, any new commands you add to your bot in the future will automatically be tracked by Cordia without any extra code!

Python (discord.py)

If you use discord.py, you can create a dedicated Cog for analytics. Simply copy the code below into a new file named analytics.py inside your cogs folder.

cogs/analytics.py
python
import discord
from discord.ext import commands
from cordia import CordiaClient
import os

class Analytics(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        
        # Initialize Cordia SDK
        self.cordia = CordiaClient(
            api_key=os.getenv("CORDIA_API_KEY"),
            bot=bot,  # auto-detect shard_id and shard_count
            debug=True,
            batch_size=50,
            flush_interval=60000,
            auto_scale=True  # Automatically scales if rate limits are hit
        )

    @commands.Cog.listener()
    async def on_ready(self):
        # Start the background loops using the bot's active loop
        self.cordia.start(loop=self.bot.loop)
        print("📊 Cordia Analytics: Background Loops Started")
        
        # Post initial guild count
        await self.cordia.post_guild_count(len(self.bot.guilds))

    @commands.Cog.listener()
    async def on_command_completion(self, ctx):
        """Zero-Effort tracking for both Slash and Prefix commands"""
        if ctx.interaction:
            # Handle Slash Commands automatically
            await self.cordia.track_interaction(ctx.interaction)
        else:
            # Handle Prefix Commands automatically
            await self.cordia.track_message(ctx.message, ctx.command.name)

    def cog_unload(self):        """Emergency flush when the cog is unloaded or bot stops"""
        import asyncio
        asyncio.create_task(self.cordia.close())
        print("💾 Cordia Analytics: Final flush performed.")

async def setup(bot):
    await bot.add_cog(Analytics(bot))

JavaScript (discord.js)

If you use discord.js, you can add an interactionCreate event listener to your main file to track all slash commands instantly.

index.js / index.ts
typescript
import { Client, GatewayIntentBits } from 'discord.js';
import { CordiaClient } from 'cordia';
import 'dotenv/config';

const client = new Client({ intents: [GatewayIntentBits.Guilds] });

// Initialize Cordia SDK
const cordia = new CordiaClient({
  apiKey: process.env.CORDIA_API_KEY,
  discordClient: client, // auto-detect shardId + totalShards
  debug: true,
  batchSize: 50,
  flushInterval: 60000,
  autoScale: true // Automatically scales if rate limits are hit
});

client.once('ready', async () => {
  console.log(`Logged in as ${client.user.tag}!`);
  
  // Post initial guild count
  await cordia.postGuildCount(client.guilds.cache.size);
  console.log('📊 Cordia Analytics: Background Loops Started');
});

// Track all Slash Commands automatically (Zero-Effort)
client.on('interactionCreate', async interaction => {
  if (!interaction.isChatInputCommand()) return;

  // Automatically detects command name, user, guild name, and locale!
  cordia.trackInteraction(interaction);
  
  console.log(`✅ Tracked command: ${interaction.commandName} (Buffered in SDK)`);
});

// Track all prefix commands automatically (Zero-Effort)
client.on('messageCreate', async message => {
  if (message.author.bot || !message.content.startsWith('!')) return;

  const command = message.content.slice(1).split(' ')[0];
  
  // Automatically detects user and guild info!
  cordia.trackMessage(message, command);
});

client.login(process.env.DISCORD_TOKEN);