Git Product home page Git Product logo

discord-interactions-js's Introduction

discord-interactions

version Build Status

Types and helper functions that may come in handy when you implement a Discord Interactions webhook.

Installation

npm install discord-interactions

Usage

Use the InteractionType and InteractionResponseType enums to figure out how to respond to a webhook.

Use verifyKey to check a request signature:

 const signature = req.get('X-Signature-Ed25519');
 const timestamp = req.get('X-Signature-Timestamp');
 const isValidRequest = verifyKey(req.rawBody, signature, timestamp, 'MY_CLIENT_PUBLIC_KEY');
 if (!isValidRequest) {
   return res.status(401).end('Bad request signature');
 }

Note that req.rawBody must be populated by a middleware (it is also set by some cloud function providers).

If you're using an express-like API, you can simplify things by using the verifyKeyMiddleware. For example:

app.post('/interactions', verifyKeyMiddleware('MY_CLIENT_PUBLIC_KEY'), (req, res) => {
  const message = req.body;
  if (message.type === InteractionType.APPLICATION_COMMAND) {
    res.send({
      type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
      data: {
        content: 'Hello world',
      },
    });
  }
});

Make sure that you do not use other middlewares like body-parser, which tamper with the request body, for interaction routes.

Exports

This module exports the following:

InteractionType

An enum of interaction types that can be POSTed to your webhook endpoint.

InteractionResponseType

An enum of response types you may provide in reply to Discord's webhook.

InteractionResponseFlags

An enum of flags you can set on your response data.

verifyKey(rawBody: Buffer, signature: string, timestamp: string, clientPublicKey: string): Promise

Verify a signed payload POSTed to your webhook endpoint.

verifyKeyMiddleware(clientPublicKey: string)

Express-style middleware that will verify request signatures (make sure you include this before any other middleware that modifies the request body).

discord-interactions-js's People

Contributors

brightskyz avatar j-human avatar lewisakura avatar ponjimon avatar typpo avatar zyhou 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.