Py-cord SDK
pip install mochi-analytics mochi-analytics-pycordRequires Python 3.10+.
Py-cord and discord.py both install themselves under the discord import name,
so they cannot share an environment. Install mochi-analytics-pycord or
mochi-analytics-discordpy, never both.
Quick start
import os
import discord
from mochi_analytics import MochiClient
from mochi_analytics_pycord import attach_mochi
mochi = MochiClient(
url="https://mochi.example.com", # your Mochi instance
api_key=os.environ["MOCHI_API_KEY"], # from the bot's settings page
)
bot = discord.Bot(intents=discord.Intents.default())
attach_mochi(bot, mochi)
bot.run(os.environ["DISCORD_TOKEN"])That’s it. Mochi now records slash/context-menu command usage, guild joins and leaves, and an hourly server-count snapshot.
attach_mochi works with discord.Client, discord.AutoShardedClient, and
discord.Bot. An AutoShardedClient sends one snapshot per shard, each
carrying that shard’s own guild count.
Options
attach_mochi(
bot,
mochi,
include_guild_names=True, # put guild names in join/leave metadata
ignore_commands=["ping"], # skip noisy commands
snapshot_interval=30 * 60, # seconds; default 1 hour
auto_track_commands=False, # see "accurate timings" below
)Accurate duration & success
Auto-tracking records commands the moment the interaction arrives — it can’t see whether your handler succeeded or how long it took. For that, disable auto-tracking and wrap your handlers:
from mochi_analytics_pycord import wrap_command
attach_mochi(bot, mochi, auto_track_commands=False)
@bot.slash_command()
@wrap_command(mochi)
async def play(ctx: discord.ApplicationContext):
... # duration and raised exceptions are recordedwrap_command accepts either an Interaction or the ApplicationContext
Py-cord passes to slash commands.
Custom events
from mochi_analytics import MochiEvent
mochi.track(MochiEvent(
type="custom",
name="premium_purchased",
user_id=str(ctx.author.id),
guild_id=str(ctx.guild_id) if ctx.guild_id else None,
meta={"tier": "gold"},
))Design guarantees
- Events are batched (flushed every 5 s or 100 events) and sent in the
background —
track()never blocks or raises. - Transient failures retry with backoff; the queue is bounded (oldest dropped first), so a dead Mochi instance can never leak memory or crash the bot.
- Raw user ids are hashed server-side with a per-bot salt and never stored.
Everything above is a thin wrapper over two HTTP endpoints — see the Ingest API to integrate from any other framework.