Skip to content

Responses

All response classes inherit from InteractionResponse and implement to_dict(), which produces the JSON payload Discord expects.

Usage

@router.command('username')
async def return_username(ctx):
    return MessageResponse(f'Hello {ctx.user.username}', ephemeral=True)

Reference

fastapi_interactions.responses.InteractionResponse

Bases: ABC

Source code in fastapi_interactions/responses.py
class InteractionResponse(ABC):

    @abstractmethod
    def to_dict(self):
        pass

    async def __call__(self) -> dict:
        return self.to_dict()

    def __await__(self):
        return self.__call__().__await__()

fastapi_interactions.responses.MessageResponse dataclass

Bases: InteractionResponse

Source code in fastapi_interactions/responses.py
@dataclass(slots=True)
class MessageResponse(InteractionResponse):
    callback_type = InteractionCallbackType.MESSAGE
    content: str
    ephemeral: bool = False

    def __init__(self, content: str, ephemeral: bool = False):
        self.content = content
        self.flags = MessageFlags.EPHEMERAL if ephemeral else MessageFlags(0)

    def to_payload(self):
        return {"content": self.content, "flags": int(self.flags)}

    def to_dict(self):
        return {"type": self.callback_type, "data": self.to_payload()}

fastapi_interactions.responses.DeferResponse dataclass

Bases: InteractionResponse

Source code in fastapi_interactions/responses.py
@dataclass(slots=True)
class DeferResponse(InteractionResponse):
    callback_type = InteractionCallbackType.DEFER

    def __init__(self, ephemeral: bool = False, finish: callable = None):
        self.flags = MessageFlags.EPHEMERAL if ephemeral else MessageFlags(0)
        self.finish = finish

    def to_dict(self):
        return {"type": self.callback_type, "data": {"flags": int(self.flags)}}

    async def __call__(self):
        if self.finish:
            asyncio.create_task(self.finish())
        return self.to_dict()

fastapi_interactions.responses.MessageFlags

Bases: IntFlag

Bit flags describing special message properties.

Source code in fastapi_interactions/models.py
class MessageFlags(IntFlag):
    """Bit flags describing special message properties."""

    CROSSPOSTED = 1 << 0
    IS_CROSSPOST = 1 << 1
    SUPPRESS_EMBEDS = 1 << 2
    SOURCE_MESSAGE_DELETED = 1 << 3
    URGENT = 1 << 4
    HAS_THREAD = 1 << 5
    EPHEMERAL = 1 << 6
    LOADING = 1 << 7
    FAILED_TO_MENTION_SOME_ROLES_IN_THREAD = 1 << 8

    SUPPRESS_NOTIFICATIONS = 1 << 12
    IS_VOICE_MESSAGE = 1 << 13
    HAS_SNAPSHOT = 1 << 14
    IS_COMPONENTS_V2 = 1 << 15