Skip to content

Discord Integration

Automatically manage Discord roles when customers make purchases.

Overview

Pixlpay can automatically:

  • Assign Discord roles when customers purchase products
  • Remove roles when subscriptions expire or are cancelled
  • Remove roles when orders are refunded

How It Works

  1. Customer links their Discord account during checkout
  2. When purchase completes, Pixlpay assigns configured roles
  3. For subscriptions, roles are removed on expiry/cancellation

Setting Up Discord Roles

In Your Pixlpay Dashboard

  1. Go to Settings > Integrations > Discord
  2. Connect your Discord server
  3. For each product, configure which roles to assign

Using Webhooks

You can also handle Discord role assignment yourself using webhooks:

javascript
// Webhook handler for order.paid
app.post('/webhooks/pixlpay', async (req, res) => {
  const event = req.body;

  if (event.event_type === 'order.paid') {
    const order = event.data;
    const discordId = order.metadata?.discord_id;

    if (discordId) {
      for (const item of order.items) {
        const roleId = await getProductRoleId(item.product_id);
        if (roleId) {
          await assignDiscordRole(discordId, roleId);
        }
      }
    }
  }

  res.status(200).json({ status: 'received' });
});

Webhook Events for Discord

order.paid

Assign roles when payment is received.

json
{
  "event_type": "order.paid",
  "data": {
    "order_id": 123,
    "metadata": {
      "discord_id": "123456789012345678"
    },
    "items": [
      {
        "product_id": 5,
        "product_name": "VIP Rank"
      }
    ]
  }
}

subscription.cancelled

Remove roles when subscription ends.

json
{
  "event_type": "subscription.cancelled",
  "data": {
    "subscription_id": 456,
    "customer_email": "player@example.com",
    "metadata": {
      "discord_id": "123456789012345678"
    }
  }
}

order.refunded

Remove roles when order is refunded.

json
{
  "event_type": "order.refunded",
  "data": {
    "order_id": 123,
    "metadata": {
      "discord_id": "123456789012345678"
    }
  }
}

Discord.js Example

javascript
const { Client, GatewayIntentBits } = require('discord.js');

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

const GUILD_ID = 'your_guild_id';

async function assignRole(discordId, roleId) {
  const guild = await client.guilds.fetch(GUILD_ID);
  const member = await guild.members.fetch(discordId);
  await member.roles.add(roleId);
  console.log(`Assigned role ${roleId} to ${member.user.tag}`);
}

async function removeRole(discordId, roleId) {
  const guild = await client.guilds.fetch(GUILD_ID);
  const member = await guild.members.fetch(discordId);
  await member.roles.remove(roleId);
  console.log(`Removed role ${roleId} from ${member.user.tag}`);
}

client.login(process.env.DISCORD_BOT_TOKEN);

Best Practices

  1. Handle failures gracefully - Discord API may rate limit or fail
  2. Queue role changes - Process asynchronously to avoid delays
  3. Log all changes - Keep audit trail of role assignments
  4. Handle edge cases - User may leave server, role may be deleted

Built for game developers, by game developers.