Skip to main content

Simplified serverless Discord bot interactions

Project description

Utility library for serverless Discord apps

PyPI

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).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

discord-serverless-2023.1004a0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

discord_serverless-2023.1004a0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file discord-serverless-2023.1004a0.tar.gz.

File metadata

File hashes

Hashes for discord-serverless-2023.1004a0.tar.gz
Algorithm Hash digest
SHA256 3b452f93a32ce8f58b71435b6bbd46b1006721eae1b7c143fcfd201259b4b2f1
MD5 d5c321248eb2938bc74fc7a999f01b7a
BLAKE2b-256 c7d9509735087bc079bedcfb6574da617ec3c84a7330c0f99fae79e1e781eef2

See more details on using hashes here.

File details

Details for the file discord_serverless-2023.1004a0-py3-none-any.whl.

File metadata

File hashes

Hashes for discord_serverless-2023.1004a0-py3-none-any.whl
Algorithm Hash digest
SHA256 133dc6e202e6e7cc6f33be47059064ff6c52d181afe59f1a7aa9e1d04ce7979f
MD5 fbf8331c3cd6d30778165c0912ee2a54
BLAKE2b-256 c3f091bef752544e15001e52e205e16857fef1cb840bd00ce2dfd1b9dc5c857a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page