A discord interaction API wrapper for serverless applications. https://jnsougata.github.io/discohook/
Find a file
2026-09-26 22:53:32 +05:30
.github refactor: change default branch from main to rolling in publish configuration 2026-09-26 22:42:09 +05:30
discohook refactor: update version to 0.2.0 and enhance interaction response handling 2026-09-26 22:53:32 +05:30
docs docs: add detailed docstrings for components in API schema and update component documentation 2026-09-14 10:59:52 +05:30
.flake8 Add flake8 config 2023-05-12 16:23:20 -04:00
.gitignore docs: update .gitignore to exclude VitePress cache directory 2026-06-02 20:14:48 +05:30
CODE_OF_CONDUCT.md Create CODE_OF_CONDUCT.md 2023-05-26 12:13:04 +05:30
CONTRIBUTING.md Update CONTRIBUTING.md 2024-03-12 16:04:56 +05:30
docs.py docs: update documentation to reflect new features and modify action text 2026-06-04 15:20:20 +05:30
LICENSE improvements and new methods 2023-04-01 15:49:16 +05:30
MANIFEST.in Add MANIFEST.in 2025-02-28 07:56:29 +05:30
README.md refactor: update version to 0.2.0 and enhance interaction response handling 2026-09-26 22:53:32 +05:30
requirements.txt feat: add environment variable support for client initialization and improve webhook handling 2025-04-19 14:19:20 +05:30
setup.py refactor: improve package description for clarity and specificity 2025-11-19 01:05:45 +05:30

discohook

A discord interaction API wrapper for serverless applications.

Installation

pip install discohook

or

pip install git+https://github.com/jnsougata/discohook.git

Quickstart

import os

import discohook

DISCORD_TOKEN = os.environ["DISCORD_TOKEN"]
PUBLIC_KEY = os.environ["PUBLIC_KEY"]
APPLICATION_ID = os.environ["APPLICATION_ID"]
APPLICATION_PASSWORD = os.environ["APPLICATION_PASSWORD"]

app = discohook.Client(
    application_id=APPLICATION_ID,
    public_key=PUBLIC_KEY,
    token=DISCORD_TOKEN,
    password=APPLICATION_PASSWORD,  # Must be provided if you want to use the dashboard.
    default_help_command=True,  # This will enable your bot to use  default help command (/help).
)


# Adding an error handler for all interactions
@app.on_interaction_error()
async def handler(i: discohook.Interaction, err: Exception):
    user_response = "Some error occurred! Please contact the developer."
    if i.responded:
        await i.response.followup(user_response, ephemeral=True)
    else:
        await i.response.send(user_response, ephemeral=True)

    await app.send("12345678910", f"Error: {err}")  # send error to a channel in development server



# Adding an error handler for any serverside exception
@app.on_error()
async def handler(_request, err: Exception):
    # request: starlette.requests.Request
    # err is the error object
    await app.send("12345678910", f"Error: {err}")  # send error to a channel in development server
    # If you don't have reference to `app` object, you can use `request.app` to get the app object.

# Making slash command
@app.register
@discohook.command.slash()
async def ping(i: discohook.Interaction):
    """Ping the bot."""
    await i.response.send("Pong!")


# Making user command
@app.register
@discohook.command.user()
async def avatar(i: discohook.Interaction, user: discohook.User):
    container = discohook.Container(
        discohook.Section(
            discohook.TextDisplay(f"Avatar of {user.name}"),
            accessory=discohook.Thumbnail(user.avatar.url)
        )
    )
    await i.response.send(container)


# Making message command
@app.register
@discohook.command.message()
async def quote(i: discohook.Interaction, message: discohook.Message):
    container = discohook.Container(
        discohook.TextDisplay(f"Quote of {message.author.name}"),
        discohook.Separator(),
        discohook.TextDisplay(f"`{message.content}`"),
    )
    await i.response.send(container)

Deployment

Deploy the snippet above to your serverless function, and you're good to go to the next step. Remember to replace the variables with your own. Command will not be automatically registered. We provide a dashboard to register commands. We will talk about it later.

Developer Portal Setup

1. Head over to the Discord Developer Portal and create a new application. Once you've created the application, go to the "Bot" tab and create a new bot. Generate new token by using Reset Token and copy the bot token and paste it in the APPLICATION_TOKEN variable. image 2. Then go to the General Information tab and copy the Application ID and paste it in the APPLICATION_ID variable and copy the Public Key and paste it in the APPLICATION_PUBLIC_KEY variable.

image

3. Then take you serverless instance URL i.e. https://example.io and add /interactions to it (i.e. https://example.io/interactions). Then head over to the general information tab and paste it in the Interactions Endpoint URL field. If everything is correct, you should see a confirmation message. Make sure you deploy the above code to your serverless instance before doing this.

image

Registering Commands

You can sync commands by just visiting the dashboard. The dashboard will be available at https://example.io/api/dash .

image

Once you visit the dashboard, it will automatically register all the commands. You can also register commands manually by using the bash command below.

curl -d "{\"password\":  \"$(echo -n very_secure_password | sha256sum | awk '{ print $1 }')\"}" -X POST https://example.io/api/sync

🎉 Boom! You're done. Now you can test your bot by using /help command in your server.

Why we didn't add auto registration?

As this library is meant to be used in serverless functions, we didn't add auto registration as at each invocation the scripts will be reloaded and the commands will be re-registered again. This will cause rate limit issues and also due to command registration being a slow process, it will cause significant latency. So we decided to add a dashboard to register commands. Which is a lot faster and also doesn't cause any rate limit issues if used properly. Through the dashboard, you can also delete commands and sync the edited commands.