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
- Customer links their Discord account during checkout
- When purchase completes, Pixlpay assigns configured roles
- For subscriptions, roles are removed on expiry/cancellation
Setting Up Discord Roles
In Your Pixlpay Dashboard
- Go to Settings > Integrations > Discord
- Connect your Discord server
- 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
- Handle failures gracefully - Discord API may rate limit or fail
- Queue role changes - Process asynchronously to avoid delays
- Log all changes - Keep audit trail of role assignments
- Handle edge cases - User may leave server, role may be deleted