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.
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):
msg = interaction.data.options[0].value # "message" option content
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).
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
More Examples
For more examples take a look at examples.
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.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 194cdc698f7487bcf460864b68c68200a02dd84983fbc984f35d8dff245481cb |
|
MD5 | ec85c27fc2677f0ece167e63e5686fb8 |
|
BLAKE2b-256 | f83ebf3263ec69278d0f62c565ff419fd3482bbf65d9c21eee1c34baa00670b7 |
Hashes for discord_interactions.py-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 758ce2dc8c3f4cff00ba044ce147e5ffe088ba56543e70472ced9f42ac5b1a6a |
|
MD5 | b0407d909b66e592c2e44b5085bfc7aa |
|
BLAKE2b-256 | 093447e39ee72f4d19cbd49a907fa5b36fc800d9630b2608585fa40b446ba944 |