Coder Social home page Coder Social logo

discord-sls's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.