This Integration allows full access to the Discord API for cloud-hosted instances from your application. Fusebit's multi-tenant approach makes it easy for you to manage multiple Discord connections on behalf of your app's users. Our Node.js-based programming model and lightweight deployment story make it easy to customize the Integration with any necessary business logic.

Getting Started

  1. Use the Fusebit Management Portal to create an account and log in.
  2. Create a new Integration using one of our Discord templates
30643064
  1. You will then be able to run the Integration, download it's code, modify it, and deploy your changes. Detailed instructions on how to get started with an Integration are available here.

Invoking the Discord API

The Integration template comes pre-configured with some example calls that invoke Discord's API, for example:

const discordClient = await integration.tenant.getSdkByTenant(ctx, connectorName, ctx.params.tenantId);
await superagent.post(discordClient.fusebit.credentials.webhook.url).send({
      content: ctx.req.body.message || 'Hello world from Fusebit!',
    });
ctx.body = 'Message posted successfully';

As discord.js currently requires Node.js V16 and AWS does not currently support it, we wrote our own sdk. The discordClient object provided by Fusebit is essentially a thin client that sits on top of the Discord API.

We ensure the above SDK object is already bootstrapped with the Discord credentials of the user invoking the integration. You do not need to worry about obtaining an access token or refresh token -- Fusebit does that for you automatically.

Creating your own Discord App

Out of the box, Fusebit's Discord Integration uses our own demonstration application. This is to make it easier to get started, however you will likely need to register your own Discord app for use in production.

To create your own Discord App and configure it successfully with Fusebit, you will need the following items:

  • OAuth Redirect URL (from Fusebit)
  • Discord App Client ID
  • Discord App Client Secret
  • Discord App OAuth Scopes
  • Discord Public Key
  1. In the Discord Developer Portal, click on Applications and click on New Application.
960960
  1. In the Fusebit Management Portal, find the Integration you would like to connect to the new Discord App you just created and select the Connector tied to that integration.

    Once there, Select Enable Production Credentials in the Connector Configuration and note the OAuth Redirect URL under the 'Discord Configuration' section. You will need this information to successfully configure your app in Discord.

960960

📘

If more than one Integration is linked to that same Connector, they will all use the new Discord App you are about to configure.

When switching a Connector to a different Discord App, all Identity objects associated with that Connector will be invalidated. Your users may need to re-authenticate against Discord after you make this change to avoid any issues. You can easily trigger this by deleting the associated identities through the Fusebit Management Portal.

  1. In the Discord Developer Portal, click on OAuth2 Apps and navigate to Redirects and click on Add Redirect. Paste the OAuth Redirect URL you just copied from the Connector.

    While you are here, copy the Client ID and Client Secret, paste them in the corresponding fields back in the Connector.

960960
  1. While in the Connector, ensure you have the correct OAuth scopes. You will see identify and webhook.incoming already pre-populated, however you may want to add more depending on your needs. See full list and descriptions here.

    Finally, retrieve the Discord Public Key from General Information and paste this back in the Connector.

960960

Setting up your Discord App Bot

Now, you likely also want to create a bot so that it can interact with users on behalf of your newly created application. For this, you will need the following items:

  • Discord Bot Token
  • OAuth2 Code Grant Enabled
  • Discord Bot Permissions Integer
  1. In the Discord Developer Portal, select the application you want to create a bot for, click on Bot and select Add Bot.
960960
  1. Copy the Bot Token, scroll down and enable the toggle for REQUIRES OAUTH2 CODE GRANT. In the Connector, paste the copied Bot Token into the *Discord Application Bot Token field.
960960
  1. Scroll to the bottom of the Bot page and select the permissions that you want to enable. Based on the permissions you enable, it will provide you with a unique integer. Copy that integer and paste it in into the 'Bot Permissions' field in your Connector.
960960

👍

Good Job!

Any time a new user installs your Integration, you should now see your Discord App information displayed during the authorization step along with the Bot authorization information as well!

Also, now that you've configured your own app, you can also leverage our Webhooks SDK to receive events from Discord!

Receiving events from Discord

Now that you have your Discord App & Bot configured, you will likely also want to have your application respond to events from Discord (on behalf of your users). We can achieve that easily by leveraging Fusebit's built in support for Webhooks.

📘

Interactions vs. Webhooks

When a user interacts with your Discord app, your app will receive an Interaction. This is received via a webhooks endpoint that is also referred to as an Interactions endpoint. You can learn more about interactions here.

To set this up, you will need the following items:

  • Webhook URL (from Fusebit)
  • Integration Name to handle incoming Events
  1. In your Connector, copy the Webhook URL and paste it into the Interactions Endpoint URL for your Discord Application in the Discord Developers Portal.

    Lastly, type in the name of the integration which will be used to handle all incoming events that are not tied to a specific tenant ID.

960960

In order for your app to work with webhooks, it must have the webhooks.incoming scope configured.

  1. If it doesn't already exist, add a listener to your integration file. In this example, we are listening for events from your installation and responding with a simple message saying 'It works!'.
integration.event.on('/:componentName/webhook/:eventType', async (ctx) => {
  const {
    data: { data: event },
  } = ctx.req.body;
  console.log('received event', event);
  const {
    data: { application_id, token },
  } = ctx.req.body;
  
  await superagent.post(`https://discord.com/api/v8/webhooks/${application_id}/${token}`).send({
    content: 'It works!',
  });
});

Helpful References

Want to learn more about how to work with Discord Apps and Discord Bots? Here's some helpful references:


Did this page help you?