discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.4k stars 3.97k forks source link

Support for interaction endpoints? #7231

Open Romejanic opened 2 years ago

Romejanic commented 2 years ago

Which package is the feature request for?

discord.js

Feature

Discord now also supports interaction endpoints, where Discord will send a webhook to a specified endpoint when it receives one of your app's interactions (such as a slash command) and allows us to respond to them without even needing a bot user.

As far as I can tell, discord.js does not support this. It only supports gateway clients (i.e. bot users), and the WebhookClient class is only useful for sending webhooks to Discord itself.

Since interaction endpoints require a web server to work correctly, a dedicated solution within discord.js would be required.

Ideal solution or implementation

The best solution would be a new class (i.e. EndpointClient, InteractionClient, etc), which runs a HTTP/HTTPS server, performs the required signature verification and emits events upon receiving an interaction, exactly how the normal Client class does.

This then allows full access to discord.js' API for things like embeds, attachments, mentions, message components, etc. This would also simplify things like follow-up messages which require separate requests to be made to Discord using the credentials provided by the interactions.

Alternative solutions or implementations

The alternative could be a separate package which makes use of discord.js' API but handles the actual web server. Or an entirely new package which implements these things from the ground up.

I was considering making a new package which works as Express middleware, and reimplementing many of discord.js' features myself. Or possibly integrating it into my existing slasher package.

Other context

No response

meister03 commented 2 years ago

This could be a nice to have to the new module structure of discord.js considering that non-djs users can use it too.

louy commented 2 years ago

I think this is easily doable if you manually call the discordjs client.actions.INTERACTION_CREATE with the correct object, but i haven't had time to do this yet

jaw0r3k commented 1 year ago

or just add @discordjs/http-interactions package To receive interactionCreate event you need websocket which it tries to prevent

mitchheddles commented 1 year ago

I came across this issue recently and was able to get most things working like this:

export function handler(req, res) {
  // It might not be required for everybody, but I found I had to defer the reply here before doing anything
  // You can make a fetch request or use the discord api to do this using the req.body

  const client = new Client({
    // setup options
  });

  client.on('interactionCreate', async (interaction) => {
    if (interaction.isChatInputCommand()) {
      await interaction.reply('Hello');
      // If you're like me and had to defer the reply you will need to mark the interaction as deferred
      // interaction.deferred = true;
      // await interaction.followUp('Hello');
    }
  })

  // If you're using TS then it won't be happy about accessing client.actions
  client.actions.InteractionCreate.handle(req.body);
}