Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Utility library for serverless Discord apps

This is a (currently very simple) library that eases development of serverless Discord app and bots.

Currently, it targets AWS Lambda only, and provides a decorator and some utility functions that make discord signature verification and ping handling automatic.

I will grow it with things I end up needing in my work, but if you have different needs and want to extend the library to support them, then PRs would be gratefully received!

MVP Example

This will accept any slash command you've configured for your bot, and just greet the user who calls it, tagging them in the reply.

from discord_serverless import discord_command_lambda, discord_command_response

@discord_command_lambda(discord_key=YOUR_PUBLIC_KEY)
def command_handler(payload):
    return discord_command_response(4, f'Hi there <@{payload["member"]["user"]["id"]}>')

General usage

AWS (Tested)

Note: If using with API Gateway, you'll need to set up proxy integration. Failing to do this will likely lead to none of the requests from Discord passing signature verification, which is based on the raw original payload.

This shouldn't be a problem if you're using function URLs (but I still don't recommend that for reasons that are way beyond the scope of this README 😅).

For AWS, simply create a handler function that takes a single payload argument, and decorate it with discord_command_lambda decorator. You need to pass in the Discord public key for your application, e.g:

@discord_command_lambda(discord_key=YOUR_PUBLIC_KEY)
def command_handler(payload):
    # ... do stuff ...

This function should return a regular AWS response (compatible with your chosen integration) with the payload format outlined in the Discord interaction webhook docs.

The payload that gets passed in is the standard Discord interaction payload.

To check the actual slash command that was called (in case you have a single bot doing multiple commands), look in payload["data"]["name"].

If your command supports options, you'll find them in a list of dicts under payload["data"]["options"] with entries like:

{
 "name": "the option name",
 "value": "the value, appropriately typed" 
}

See Example Interaction in the Discord docs for full layout.

There are a couple of helper functions for responses:

  • discord_command_response - Create a regular success response. Response type 4 will send the content you supply as the reply message to the user.
  • discord_unknown_command_response - Create an error response. This will get sent as a HTTP 400, and will cause discord to display "The interaction failed" to the the user (but only the user - not the rest of the channel).

For help with the interaction response types, see Responding to an interaction in the Discord docs.

Other Cloud Providers

Things will be a bit more manual here (but I'll happily add comfort wrappers if there's enough call and a PR that contributes them 😉).

The easiest thing to do is use the discord_command_webhook decorator, which is similar to the lambda one above but needs you pass in a couple of Callables the code will use to get access to the data in a provider-specific way.

You could also go full manual mode and directly call discord_verify_signature and handle_discord_command, passing things in as appropriate - this is how the decorator works under the hood and gives you full flexibility if that's your thing.

Take a look at the code in discord_serverless.py for examples to get you started.

Developing

Don't forget to set up pre-commit if you're developing things, especially if you plan to push a PR.

Legal Mumbo-Jumbo

Copyright (c)2023 Ross Bamford (and contributors)

License: MIT (see LICENSE.md for details).

Metadata

Release files for discord-serverless 2023.1001a0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for discord-serverless 2023.1001a0
File Size Uploaded Provenance
discord-serverless-2023.1001a0.tar.gz 5.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for discord-serverless 2023.1001a0
File Interpreter ABI Platform
discord_serverless-2023.1001a0-py3-none-any.whl Python 3 none any Details

Total release size: 12.5 kB

Release files / discord-serverless-2023.1001a0.tar.gz

Download URL discord-serverless-2023.1001a0.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0fd845e0b9bcdf3cbbff40f7e6d7411cecf540f0ece02ad2ad3237a14e986c20
BLAKE2b-256 checksum
How to use checksums
6189383674efd7d3d4befecd220ffab0533dc55983e54ae1b2cc79955b34a4fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.11

Release files / discord_serverless-2023.1001a0-py3-none-any.whl

Download URL discord_serverless-2023.1001a0-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
04d6acfcf5e2bc70289285d026038f6fd2828a6ca60c773bcfc62c6950e23d6e
BLAKE2b-256 checksum
How to use checksums
6d4f332a816212e7fb0e52e33c3f0d8988d070f486d79fb0033c26e112d7591e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.11

Security for discord-serverless 2023.1001a0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page