Module sanic_discord.interaction

Expand source code
"""
.. include:: ./README.md
"""
from .client import InteractionClient
from .errors import InvaildSignatureError


__all__ = (
    "InteractionClient",
    "InvaildSignatureError"
)

Sub-modules

sanic_discord.interaction.client
sanic_discord.interaction.errors
sanic_discord.interaction.http
sanic_discord.interaction.interactions

Classes

class InteractionClient (app: sanic.app.Sanic, token: str, public_key: str)

interaction client

Args

token : str
The bot token.
public_key : str
The public key.

Attributes

http : HttpClient
The http client.
verify_key : nacl.signing.VerifyKey
The public key.
Expand source code
class InteractionClient:
    """
    interaction client
    
    Args:
        token (str): The bot token.
        public_key (str): The public key.
        
    Attributes:
        http (HttpClient): The http client.
        verify_key (nacl.signing.VerifyKey): The public key."""
    def __init__(self, app: Sanic, token: str, public_key: str):
        self.app = app
        self.http = HttpClient(token, public_key)
        self.verify_key = VerifyKey(bytes.fromhex(public_key))
        self.interaction_event: callable = None

    async def close(self) -> None:
        """
        Closes the client.
        """
        await self.http.close()

    def on_interaction(self, func: callable) -> callable:
        """
        Decorator for handling interaction requests.
        Interaction event cacher."""
        self.interaction_event = func
        return func

    async def handle_interaction(self, request: Request) -> HTTPResponse:
        """
        Handles an interaction request.
        """
        signature = request.headers["X-Signature-Ed25519"]
        timestamp = request.headers["X-Signature-Timestamp"]
        body = request.body.decode("utf-8")
        try:
            self.verify_key.verify(f'{timestamp}{body}'.encode(), bytes.fromhex(signature))
        except BadSignatureError:
            raise InvaildSignatureError("Invalid signature")
        else:
            return await self.interaction_event(request.json)

Methods

async def close(self) ‑> None

Closes the client.

Expand source code
async def close(self) -> None:
    """
    Closes the client.
    """
    await self.http.close()
async def handle_interaction(self, request: sanic.request.Request) ‑> sanic.response.HTTPResponse

Handles an interaction request.

Expand source code
async def handle_interaction(self, request: Request) -> HTTPResponse:
    """
    Handles an interaction request.
    """
    signature = request.headers["X-Signature-Ed25519"]
    timestamp = request.headers["X-Signature-Timestamp"]
    body = request.body.decode("utf-8")
    try:
        self.verify_key.verify(f'{timestamp}{body}'.encode(), bytes.fromhex(signature))
    except BadSignatureError:
        raise InvaildSignatureError("Invalid signature")
    else:
        return await self.interaction_event(request.json)
def on_interaction(self, func: ) ‑> 

Decorator for handling interaction requests. Interaction event cacher.

Expand source code
def on_interaction(self, func: callable) -> callable:
    """
    Decorator for handling interaction requests.
    Interaction event cacher."""
    self.interaction_event = func
    return func
class InvaildSignatureError (message: Union[str, bytes, ForwardRef(None)] = None, status_code: Optional[int] = None, quiet: Optional[bool] = None, context: Optional[Dict[str, Any]] = None, extra: Optional[Dict[str, Any]] = None)

If the signature is invalid.

Expand source code
class InvaildSignatureError(InteractionException):
    """
    If the signature is invalid.
    """
    status_code = 401

Ancestors

Class variables

var status_code