A library around the Discord Interactions API
Project description
A wrapper for the Discord Interactions API that does not rely on websockets and can therefore be used in a stateless webhook environment.
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
Use with Flask
This library is specifically designed to work seamlessly with the Flask microframework.
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(
response_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.
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
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
Built Distribution
Hashes for discord-interactions.py-0.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | bddd93eac61fe11b7f9b874862af4569c5e6b013813a01f192d976febcdb802f |
|
MD5 | 769faec8803e950fe74b39c10030b3eb |
|
BLAKE2b-256 | 34a530d890454d12292f6c60ac568d9694af8b9df6e9faeeb3e2d2bf79a2c734 |
Hashes for discord_interactions.py-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 646a38836815cf94d168818393c56a9746ec2f00059cff882a4cfbf80411184c |
|
MD5 | 5d0f282321b471df19d2d3d71ee038c0 |
|
BLAKE2b-256 | d7627f4c25b917b30cba6f76e4bfe2453186bf8f12200d31eb92832b5394bd9c |