npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

discord-role-linker

v0.0.9

Published

A powerful library to easily manage your Discord linked roles using OAuth2

Downloads

96

Readme

Discord Role Linker

A powerful library to easily manage your Discord linked roles using OAuth2!

  • Supports full Discord OAuth2 authorization flow
  • Support for persistently storing the access tokens in the database of your choice
  • Built with modularity in mind, so you can easily extend it to your needs

NPM

Basic usage

Create your Discord application

  • Create a new application on the Discord Developer Portal
  • Go to the Bot tab, create a new bot and copy the bot token (click on Reset Token button)
  • Go to the General Information tab and set the following: http://localhost:3000/linked-role in the Linked roles verification URL field
  • Go to the OAuth2 tab and add the following URI in the Redirects section: http://localhost:3000/oauth-callback
  • On the same page, copy your Client Secret, above the Redirects section

Install Discord Role Linker:

npm install discord-role-linker
yarn add discord-role-linker
pnpm add discord-role-linker

Create your application

Instantiate the role linker by filling in your application parameters like so:

const { RoleLinker } = require('discord-role-linker');

const roleLinker = new RoleLinker({
    token: 'YOUR BOT TOKEN',
    clientId: 'YOUR CLIENT ID',
    clientSecret: 'YOUR CLIENT SECRET',
    redirectUri: 'YOUR REDIRECT URI',
});

In order for your application to work, you need to register your metadata first. Metadata is what will show in your Links tab in Roles settings, as well as user's Linked Roles modal when they link their accounts. You can register up to 5 metadata entries:

const { MetadataTypes } = require('discord-role-linker');

// ...your RoleLinker instance

roleLinker.metadata.register([
    {
        key: 'level',
        name: 'Level',
        description: 'Minimum user level',
        type: MetadataTypes.Integer.GreaterThanOrEqual,
    },
]);

Setting up your backend

In the example below, we will use Express and cookie-parser to set up our backend

npm install express cookie-parser
yarn add express cookie-parser
pnpm add express cookie-parser
const express = require('express');
const cookieParser = require('cookie-parser');
const crypto = require('node:crypto');

// ...your RoleLinker instance

const app = express();

app.use(cookieParser(crypto.randomUUID()));

// Set the cookie and redirect to the Discord OAuth2 page
app.get('/linked-role', roleLinker.auth.init.bind(roleLinker.auth));

// Endpoint Discord will redirect to after the user has authorized your application
app.get('/oauth-callback', async (req, res) => {
    try {
        // Verifies if the cookie matches the one given on the /linked-role route
        const code = roleLinker.auth.verifyCode(req);
        if (!code) return res.sendStatus(403);

        // Gets the user and stores the tokens
        const user = await roleLinker.getUserAndStoreToken(code);
        
        // Set user's metadata
        await roleLinker.metadata.setUserData(user.id, user.username, { level: 24 });

        res.send("Your account has been linked successfully!")
    } catch (error) {
        console.error(error);
        res.sendStatus(500);
    }
})

// Start the server on port 3000
app.listen(3000, () => {
  console.log(`Listening on port 3000`);
});

Getting the user's metadata

You can get the user's metadata by using the metadata.getUserData method

const metadata = await roleLinker.metadata.getUserData(userId);

Getting user's data

You can get the user's data by using the getUserData method

const user = await roleLinker.getUserData(userId);

Updating metadata of the users

  • From time to time, you might want to update your user's metadata
  • In the background, the library stores the user's metadata in a database, or in memory if you are not using a database provider (more info on that below)
const updateUser = async (userId) => {
    const user = await roleLinker.getUserData(userId);
    roleLinker.metadata.setUserData(user.id, user.username, { level: 123 })
}

const updateAllUsers = async () => {
    const users = await roleLinker.tokenStore.getAllUsers();

    // Make sure to handle your update calls accordingly to avoid hitting rate limits. You could either use setTimeout or a promise queue manager of your choice, such as p-queue
    users.forEach(async (user) => {
        await updateMetadata(user.id);
    })
}

You can also create an endpoint that will update the metadata of all the users in your database. This is useful if you want to update the metadata of your users from an external service.

app.post("/update-metadata", async (req, res) => {
  try {
    await updateUser(req.body.userId);
    res.sendStatus(204);
  } catch (e) {
    res.sendStatus(500);
  }
});

Persistent storage of the access tokens

  • By default, the library stores the access tokens in memory using Maps. This means that if you restart your application, all the access tokens will be lost. You can overcome this by using a database provider in your RoleLinker constructor:
const { RoleLinker } = require('discord-role-linker');
const { MongoDBProvider } = require('discord-role-linker');

const roleLinker = new RoleLinker({
    token: 'YOUR BOT TOKEN',
    clientId: 'YOUR CLIENT ID',
    clientSecret: 'YOUR CLIENT SECRET',
    redirectUri: 'YOUR REDIRECT URI',
    databaseProvider: new MongoDBProvider({
        uri: 'mongodb://localhost:27017/role-linker',
        schemaName: 'user_tokens', // Optional, defaults to "user_tokens"
    })
});

You can use one of the following built-in providers to store the access tokens in a database:

  • MongoDB
  • Redis
  • Your custom provider (coming soon)

Bugs, glitches and issues

If you encounter any problems feel free to open an issue or message me on Discord: Vedinsoh#0001