Is it possible to use this API inside the cloudflare workers framework/engine? #8428
-
I came across this amazing API and wondered if it was compatible with the cloudflare worker model. I've seen an example from discord setting up a worker to respond as your bot and the basic ones work fine, was wondering how easy it would be to adapt this engine to the same. https://discord.com/developers/docs/tutorials/hosting-on-cloudflare-workers I was following this tutorial but the main issue is how the handle requests coming in: export default {
/**
* Every request to a worker will start in the `fetch` method.
* Verify the signature with the request, and dispatch to the router.
* @param {*} request A Fetch Request object
* @param {*} env A map of key/value pairs with env vars and secrets from the cloudflare env.
* @returns
*/
async fetch(request, env) {
if (request.method === 'POST') {
// Using the incoming headers, verify this request actually came from discord.
const signature = request.headers.get('x-signature-ed25519');
const timestamp = request.headers.get('x-signature-timestamp');
console.log(signature, timestamp, env.DISCORD_PUBLIC_KEY);
const body = await request.clone().arrayBuffer();
const isValidRequest = verifyKey(
body,
signature,
timestamp,
env.DISCORD_PUBLIC_KEY
);
if (!isValidRequest) {
console.error('Invalid Request');
return new Response('Bad request signature.', { status: 401 });
}
}
// Dispatch the request to the appropriate route
return router.handle(request, env);
},
}; and in their example they are using an The fetch method will get the requests in and if they are legit it passes to a handler and uses the discord-interactions code right now which is good but a bit verbose. Any ideas would be appreciated, thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 15 replies
-
discord.js is designed for gateway connections, generally running as a Node application. It's not intended to be used for http interactions. However, the discord.js library is composed of several packages that can be used independently - you may be interested in There's also a standalone structures package being built for class structures to represent Discord objects (User, Message etc) |
Beta Was this translation helpful? Give feedback.
discord.js is designed for gateway connections, generally running as a Node application. It's not intended to be used for http interactions.
However, the discord.js library is composed of several packages that can be used independently - you may be interested in
@discordjs/rest
for making API calls and@discordjs/builders
for building components, embeds etc in responses.There's also a standalone structures package being built for class structures to represent Discord objects (User, Message etc)