slash-create icon indicating copy to clipboard operation
slash-create copied to clipboard

feat: Add Server Context Support

Open KitsuneDev opened this issue 1 year ago • 2 comments

This is a straightforward solution to #620, implementing a CommandContext.serverContext property that can be defined by the Server. CommandContext.serverContext is naturally a generic of the default type unknown, leaving the user responsible for determining its type and handling casts accordingly. This fits with the use case of CloudFlare Workers, as its structure changes depending on how bindings are defined.

Consequently, this PR enables using Durable Objects and D1 databases in bots. Through the use of Durable Objects, many things that could only previously be achieved in stateful workflows can now be achieved in serverless.

KitsuneDev avatar Jun 27 '24 22:06 KitsuneDev

By the way, I can also work on documenting this if you'd like. This is a super minimal implementation that I came up with in about an hour to allow me to use D1 in a bot of mine.

KitsuneDev avatar Jun 27 '24 22:06 KitsuneDev

I like the implementation, I'd also like to extend this implementation towards the other contexts, you'd probably be better to extend from BaseInteractionContext and move the context down the line.

Another thing I'd like to do as a whole cloudflare worker focused update is to automatically get the variables for the discord app ID, token, and public key while responding to a request, since the env can only be fetched on request. Using this will probably be easier to do that later.

Snazzah avatar Jun 28 '24 00:06 Snazzah

This does look better! I'll keep playing to see if I can find a way to make type inference work better in a subsequent PR

KitsuneDev avatar Jul 04 '24 16:07 KitsuneDev

Cool, the only thing to do now is to put server contexts in BaseInteractionContext so things like autocomplete can make use of it.

Snazzah avatar Jul 05 '24 23:07 Snazzah