Skip to main content

A library around the Discord Interactions API

Project description

PyPI Documentation Status License https://img.shields.io/badge/code%20style-black-000000.svg https://github.com/LiBa001/discord-interactions.py/workflows/Python%20package/badge.svg

A wrapper for the Discord Interactions API that does not rely on websockets and can therefore be used in a stateless webhook environment.

Furthermore, it allows for strict separation between your commands’ structure and and the data that is received when triggering it.

Installation

Requires Python 3.8+

Latest release from PyPI using pip:

pip install discord-interactions.py

Latest commit from GitHub using pip and git:

pip install git+https://github.com/LiBa001/discord-interactions.py

If this doesn’t work, you might try:

python -m pip install ...

Or if you are on windows:

py -m pip install ...

Use with Flask

This library is specifically designed to work seamlessly with the Flask microframework.

Using API-like Data Classes

The most API-like example with the flask extension is this:

from discord_interactions.flask_ext import Interactions
from discord_interactions import (
    ApplicationCommand,
    ApplicationCommandOption,
    ApplicationCommandOptionType,
    Interaction,
    InteractionResponse,
    InteractionResponseType,
    InteractionApplicationCommandCallbackData,
)
from flask import Flask
import os

app = Flask(__name__)
interactions = Interactions(app, os.getenv("CLIENT_PUBLIC_KEY"))

echo_cmd = ApplicationCommand("echo", "what goes around comes around")
echo_cmd.add_option(
    ApplicationCommandOption(
        type=ApplicationCommandOptionType.STRING,
        name="message",
        description="This will be echoed.",
        required=True,
    )
)


@interactions.command(echo_cmd)
def _echo(interaction: Interaction):
    msg = interaction.data.options[0].value  # "message" option content

    return InteractionResponse(
        type=InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
        data=InteractionApplicationCommandCallbackData(content=msg),
    )

Here, we use the rudimentary ApplicationCommand, Interaction and InteractionResponse classes, which are in their structure basically exact counterparts of the original API models.

Let’s make it a bit simpler:

@interactions.command(echo_cmd)
def _echo(interaction: Interaction):
    # different way of getting an option
    msg = interaction.data.get_option("message").value

    return msg

Now, we don’t need to deal with InteractionResponse anymore, but instead just return the response content as a string. The response type then defaults to InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE. You could also just return None, if you don’t want to send a response. You can also simply return a boolean as a second value, indicating whether or not the command call should be displayed in Discord (i.e. the _WITH_SOURCE part of the response type). Also we get the option via the get_option helper method.

The Object-Command Mapper

This library provides another abstraction layer, though. Inspired by the concept of database ORMs, it has an Object-Command Mapper (OCM) that lets you define a class for each command which will then serve as both a generic structural description of the command (like ApplicationCommand) and a container for the actual data that is received when the command is called (like Interaction).

So, the simplest possible example looks like this:

from discord_interactions.flask_ext import Interactions
from discord_interactions.ocm import Command, Option
from flask import Flask
import os

app = Flask(__name__)
interactions = Interactions(app, os.getenv("CLIENT_PUBLIC_KEY"))


class _Echo(Command):
    """ what goes around comes around """

    message: str = Option("This will be echoed.", required=True)


@interactions.command
def _echo(cmd: _Echo):
    return cmd.message

Followup Messages

If you want to send messages after the initial response, you need to create followup messages. For this purpose you can use the after_command decorator, that registers a function to be called after the actual command function has returned. The function needs to take exactly one parameter, the AfterCommandContext, which contains the several things, like the Interaction and initial InteractionResponse.

interactions = Interactions(app, PUBLIC_KEY)

@interactions.command("delay")
def delay(_: Interaction):
    return "starting countdown", True  # this message is ephemeral


@delay.after_command
def after_delay(ctx: AfterCommandContext):
    delay_time = ctx.interaction.data.options[0].value
    time.sleep(delay_time)
    ctx.send(f"{delay_time} seconds have passed")

Message Components

You can also register callbacks for message components, such as buttons. Components are registered and identified by their custom_id.

@interactions.component("my_button")
def my_button_handler(ctx: ComponentContext):
    return f"{ctx.interaction.user.username} clicked the button"

More Examples

For more examples of the different features take a look at examples.

If you want to know how to make your Discord bot work with Slash Commands and how to set everything up, take a look at this example project. It hosts the program in a serverless environment via Google Cloud Run and also provides a demo bot, so you can try out Slash Commands in your Discord server. Check it out to learn more!

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-interactions.py-0.0.8.tar.gz (23.1 kB view details)

Uploaded Source

Built Distribution

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

discord_interactions.py-0.0.8-py3-none-any.whl (42.6 kB view details)

Uploaded Python 3

File details

Details for the file discord-interactions.py-0.0.8.tar.gz.

File metadata

  • Download URL: discord-interactions.py-0.0.8.tar.gz
  • Upload date:
  • Size: 23.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.8.0

File hashes

Hashes for discord-interactions.py-0.0.8.tar.gz
Algorithm Hash digest
SHA256 892f5c54a8cfa143ed11d52f41869242e54e387fc9041da9af526ea80af23682
MD5 60bea71b1b8b20b32c3ef8f11083645d
BLAKE2b-256 1fef59ea86a43c3dbd0669952f36eb33c7e38874086693ff186aa5c01e566333

See more details on using hashes here.

File details

Details for the file discord_interactions.py-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: discord_interactions.py-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 42.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.8.0

File hashes

Hashes for discord_interactions.py-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 7a503d8e5015577f58bf4573ca1a9ce2cea105d551328a93dd69463521810c66
MD5 fe5b6f6b2997995a3b6ee1f0b5f3ba81
BLAKE2b-256 42f0c74fd6f7ac7a31cf6d3efa34feb7950846d909c3389ff5e4b67bdff805e1

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