Skip to content

Discord.js ​

Compatibility ​

The package is compatible with Discord.js v14 or higher

Dependency ​

Let's install @discordanalytics/discordjs's package :

bash
npm install @discordanalytics/discordjs

Installation ​

javascript
// Import Discord.js's client and intents
const { Client, IntentsBitField } = require("discord.js")
// import discord-analytics
const { default: DiscordAnalytics } = require("@discordanalytics/discordjs")

// Create Discord client
const client = new Client({
  intents: [IntentsBitField.Flags.Guilds] // This intent is required
});

// Create Discord Analytics instance
// Don't forget to replace YOUR_API_TOKEN by your Discord Analytics token !
const analytics = new DiscordAnalytics({
  client: client,
  api_key: 'YOUR_API_TOKEN',
  sharded: false // Set it to true if your bot use shards
});

// When Discord client is ready
client.on('clientReady', async () => {
  await analytics.init();
  analytics.trackEvents();
  console.log("Bot is ready!");
});

// Login to Discord
// Don't forget to replace token by your Discord bot token !
client.login('token');
For self-hosted instances

By default, the stats are sent to discordanalytics.xyz. You can change this using the api_url property:

javascript
const analytics = new DiscordAnalytics({
  // ...
  api_url: "https://discordanalytics.example.com"
});

In order to start your bot with Discord Analytics, you need to add NODE_ENV to you environment variables and set it to production:

shell
export NODE_ENV=production

You can also add it to your .env file if you have one or add it to your start script in your package.json:

json
{
  // ...
  "scripts": {
    "start": "NODE_ENV=production node index.js"
  }
}

This feature allows you to don't send stats to Discord Analytics while you're in a development environment by just removing the variable.

Advanced usage ​

Using ESM (JavaScript)

Optimize events (NodeJS)

Complete installation ​

To complete installation, please restart your bot. And wait on the dashboard until you get this page:

ImageImage