Discord Bots

SunLicense Integration Guide for Discord Bots

Prerequisites

  • Node.js environment (for Discord.js)

  • Your Discord bot project

  • SunLicense API credentials

Integration Steps

1. Install Required Packages

npm install axios

2. Create License Validator

const axios = require('axios');

class LicenseValidator {
    constructor(licenseKey, productId) {
        this.licenseKey = licenseKey;
        this.productId = productId;
        this.apiUrl = 'YOUR_API_URL/api/v1/validate';
    }

    async validateLicense() {
        try {
            const response = await axios.post(this.apiUrl, {
                licenseKey: this.licenseKey,
                productId: this.productId,
                hwid: 'YOUR-HWID', // Optional
                productVersion: '1.0.0'
            });

            return response.status === 200;
        } catch (error) {
            console.error('License validation failed:', error.message);
            return false;
        }
    }
}

3. Implement in Bot

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

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

const licenseValidator = new LicenseValidator('YOUR-LICENSE-KEY', YOUR_PRODUCT_ID);

client.once('ready', async () => {
    const isValid = await licenseValidator.validateLicense();
    if (!isValid) {
        console.error('Invalid license - Bot shutting down');
        client.destroy();
        process.exit(1);
    }
    console.log('Bot is ready!');
});

// Add periodic validation
setInterval(async () => {
    const isValid = await licenseValidator.validateLicense();
    if (!isValid) {
        console.error('License validation failed - Bot shutting down');
        client.destroy();
        process.exit(1);
    }
}, 24 * 60 * 60 * 1000); // Check every 24 hours

client.login('YOUR_BOT_TOKEN');

Best Practices

  1. Startup Validation

    • Validate before bot initialization

    • Implement graceful shutdown

    • Clear error logging

  2. Periodic Validation

    • Regular license checks

    • Handle network issues

    • Graceful error handling

  3. Security

    • Secure storage of license key

    • Environment variable usage

    • Proper error handling

Common Issues

  1. Validation Failures

    • Check license key validity

    • Verify network connectivity

    • Confirm product ID

  2. Runtime Issues

    • Monitor bot logs

    • Check for API rate limits

    • Verify license status

Last updated