Giter Site home page Giter Site logo

linked-roles-sample-discord's Introduction

Linked Role example app

This repository contains the documentation and example for a linked role bot.

โ‡๏ธ A version of this code is also hosted on Glitch ๐ŸŽ

Project structure

All of the files for the project are on the left-hand side. Here's a quick glimpse at the structure:

โ”œโ”€โ”€ assets          -> Images used in this tutorial
โ”œโ”€โ”€ src
โ”‚   โ”œโ”€โ”€ config.js   -> Parsing of local configuration
โ”‚   โ”œโ”€โ”€ discord.js  -> Discord specific auth & API wrapper
โ”‚   โ”œโ”€โ”€ register.js -> Tool to register the metadata schema
โ”‚   โ”œโ”€โ”€ server.js   -> Main entry point for the application
โ”‚   โ”œโ”€โ”€ storage.js  -> Provider for storing OAuth2 tokens
โ”œโ”€โ”€ .env -> your credentials and IDs
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ package.json
โ””โ”€โ”€ README.md

Running app locally

Before you start, you'll need to create a Discord app with the bot scope

Configuring the app is covered in detail in the tutorial.

Setup project

First clone the project:

git clone https://github.com/discord/linked-roles-sample.git

Then navigate to its directory and install dependencies:

cd linked-roles-sample
npm install

Get app credentials

Fetch the credentials from your app's settings and add them to a .env file. You'll need your bot token (DISCORD_TOKEN), client ID (DISCORD_CLIENT_ID), client secret (DISCORD_CLIENT_SECRET). You'll also need a redirect URI (DISCORD_REDIRECT_URI) and a randomly generated UUID (COOKIE_SECRET), which are both explained below:

DISCORD_CLIENT_ID: <your OAuth2 client Id>
DISCORD_CLIENT_SECRET: <your OAuth2 client secret>
DISCORD_TOKEN: <your bot token>
DISCORD_REDIRECT_URI: https://<your-project-url>/discord-oauth-callback
COOKIE_SECRET: <random generated UUID>

For the UUID (COOKIE_SECRET), you can run the following commands:

$ node
crypto.randomUUID()

Copy and paste the value into your .env file.

Fetching credentials is covered in detail in the linked roles tutorial.

Running your app

After your credentials are added, you can run your app:

$ node server.js

And, just once, you need to register you connection metadata schema. In a new window, run:

$ node src/register.js

Set up interactivity

The project needs a public endpoint where Discord can send requests. To develop and test locally, you can use something like ngrok to tunnel HTTP traffic.

Install ngrok if you haven't already, then start listening on port 3000:

$ ngrok http 3000

You should see your connection open:

Tunnel Status                 online
Version                       2.0/2.0
Web Interface                 http://127.0.0.1:4040
Forwarding                    http://1234-someurl.ngrok.io -> localhost:3000
Forwarding                    https://1234-someurl.ngrok.io -> localhost:3000

Connections                  ttl     opn     rt1     rt5     p50     p90
                              0       0       0.00    0.00    0.00    0.00

Copy the forwarding address that starts with https, in this case https://1234-someurl.ngrok.io, then go to your app's settings.

On the General Information tab, there will be an Linked Roles Verification URL. Paste your ngrok address there, and append /linked-role (https://1234-someurl.ngrok.io/linked-role in the example).

You should also paste your ngrok address into the DISCORD_REDIRECT_URI variable in your .env file, with /discord-oauth-callback appended (https://1234-someurl.ngrok.io/discord-oauth-callback in the example). Then go to the General tab under OAuth2 in your app's settings, and add that same address to the list of Redirects.

Click Save Changes and restart your app.

Other resources

linked-roles-sample-discord's People

Contributors

justinbeckwith avatar dependabot[bot] avatar crivasr avatar mrhappyma avatar lulalaby avatar devtomatocake avatar wilsonthewolf avatar picapi avatar shaydewael avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.