Skip to main content

Discord SLS

A set of tools for building a discord bot with a serverless architecture in mind. If you are looking for a more complete discord sdk, check out discord.py.

PyPI

Install with pip: pip install discord_sls

Example/Template Repo

Usage

The library provides a decorator @bot_handler which can be used to decorate a lambda handler to respond to discord api requests. It will handle the ping-pong handshake, and will parse the request body into a python object for you to use. The decorator expects you to return Ineraction Callback Data.

Discord expects a quick response to the initial request, if your bot needs longer to handle an interaction you can use the send_command_to_queue function to send the command to a queue for processing in another lambda decorated with @deferred_response_handler. The queue is determined by the LONG_RESPONSE_QUEUE environment variable.

import json
import logging
from discord_sls import Interaction, bot_handler, deferred_response_handler

@bot_handler
def discord_bot(command_body, send_command_to_queue):
    command_name = command_body.get("data", {}).get("name")
    if command_name == "hello":
        return {"content": "Hello Moto"}
    elif command_name == "helloasync":
        send_command_to_queue(command_body)
        return {"content": "Hello..."}
    else:
        logging.warn(f"unhandled command: {command_name}")
        return {"content": "Unknown Command"}


@deferred_response_handler
def long_response_handler(interaction: Interaction):
  interaction.follow_up({"content": "Hello...async"})

Keeping the bot warm

With most serverless architectures you will need to keep your lambdas warm to avoid cold start times. A popular mechanism for doing that is using cloudwatch event rules, the @bot_handler decorator will automatically handle these requests for you.T his is what a SAM template could look like for the rule:

BotKeepWarm:
  Type: AWS::Events::Rule
  Properties:
    Description: Keeps the bot lambda warm
    Name: !Sub 'keep-warm-${Stage}'
    ScheduleExpression: rate(5 minutes)
    Targets:
      - Id: KeepWarmDiscordBot
        Arn: !GetAtt DiscordBotFunction.Arn

Metadata

Release files for discord-sls 0.4.0

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

Built distribution (wheel)

Table of built distributions (wheels) for discord-sls 0.4.0
File Interpreter ABI Platform Actions
discord_sls-0.4.0-py3-none-any.whl Python 3 none any Details for discord_sls-0.4.0-py3-none-any.whl

Release files / discord_sls-0.4.0-py3-none-any.whl

Download URL discord_sls-0.4.0-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e0875ccec9303a89c6db16abba423dc5487169a4cf495162a1163c35092462c4
BLAKE2b-256 checksum
How to use checksums
523565ba0a688c3ce4ecf2e18f1f6f0a771c406b4855b095d97375d1ddcee7a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.9.2 readme-renderer/37.3 requests/2.28.1 requests-toolbelt/0.10.1 urllib3/1.26.13 tqdm/4.64.1 importlib-metadata/5.2.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.9.16

Security for discord-sls 0.4.0

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