You can use any Node.js library for Discord, but this example uses discord.js

Prerequisites

npm install discord.js node-fetch

Project Structure

For this example, we only have two files, index.js and whop.js.

index.js is where we will be setting up our bot and whop.js is where we will be adding our Whop API code.

Your project structure does not matter, this is just our example.
.
├── index.js
├── whop.js
└── package.json

Setting up your bot

After you’ve got your Discord Bot Token and installed the necessary packages, you can start setting up your bot.

It’s recommended that you keep your bot token as an environment variable and not hardcode it into your code.

index.js
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Messages, GatewayIntentBits.Guilds] });
    client.once('ready', () => {
    console.log('Bot is online!');
});

client.login('YOUR_BOT_TOKEN');


Adding the Whop API

For this step, you’ll need a Whop API key. More details on obtaining one can be found here.

In your whop.js file, add the following code to check if there are any memberships attached to that user’s Discord ID.

For more information on this endpoint, see here.

whop.js
const fetch = require('node-fetch');

class WhopUtilities {
  static async checkUserAccess(discord_id) {
    const url = `https://api.whop.com/api/v2/memberships?page=1&per=10&discord_id=${discord_id}&hide_metadata=false`;

    const headers = {
        "Accept": "application/json",
        "Authorization": `Bearer ${process.env.LICENSE_KEY}`
    };

    try {
        const response = await fetch(url, { method: 'GET', headers });

        if (response.status === 200) {
            return true;
        } else {
            throw new Error('Invalid response status');
        }
    } catch (error) {
        return false; // Couldn't find a membership
    }
  }
}

module.exports = WhopUtilities;

Adding the Whop API to your Bot Command

Now, let’s integrate the Whop API check into a bot command:

The import will work only if your whop.js file is in the same directory as your index.js file. Adjust if your file structure is different.

const { Client, GatewayIntentBits, MessageActionRow, MessageButton } = require('discord.js');
const WhopUtilities = require('./whop');
const client = new Client({ intents: [GatewayIntentBits.Messages, GatewayIntentBits.Guilds] });

client.on('messageCreate', async message => {
if (message.content.toLowerCase() === '!my_first_command') {
    if (await WhopUtilities.checkUserAccess(message.author.id)) {
        message.reply('Hi there!');
    } else {
        message.reply("You don't have access to this command!");
    }
}
});

client.once('ready', () => {
    console.log('Bot is online!');
});

client.login(process.env.BOT_TOKEN);

Remember to add a .env file for your bot token and license key (or any other method you’d prefer) to avoid hardcoding sensitive data.