The Discord side of the adapter, for what the core has no op for.
bot_user_idcomes fromGET /users/@me, read once. A failed read is tried again next time.dispatchtakes one gateway dispatch,tandd, and hands its events to theChatHandlerone at a time. When aMESSAGE_CREATEcarries nochannel_type, it looks the channel up, cached and with a five second timeout. A failed lookup is logged and fails withNotAccepted; one that would fail the same way again, such as a deleted channel, is logged and the event dropped.interactiontakes oneINTERACTION_CREATEpayload from the gateway and answers through the interaction callback endpoint, which has to land inside Discord's 3 seconds:deadlineis the handler's share, 2 seconds by default. It ignores an interaction it can't express: a message or user command, an autocomplete request, a slash command with anything but at most one string option.- A click is answered with callback type 6, or type 9 for
OpenModal. A submit'scloseis type 6. Anerrorsreply is type 4 with the ephemeral flag, listing each message as plain text, and the modal closes: Discord can't show an error inside a modal. - A slash command goes to
Handler.command.respondis callback type 4,defertype 5 andOpenModaltype 9, each with the ephemeral flag when private. AnAcksends nothing, so a command another library owns is left to it.with_post_optionsdoes not reach a command's answer. - A modal that can't be built, a reply to a command that fails the checks
of a
post, or a callback that fails, goes toHandler.refused. A callback that comes back 404Unknown interactionwas late:NotFoundwith detailinteraction_expired. guildreads the channel, cached;Nonefor a direct.
Attributes#
| Attribute | Type | Description |
|---|---|---|
bot_user_id | Effect[str, ChatError] | The bot's user id, from |
adapter | DiscordAdapter | The registered adapter. |
Functions#
__init__#
__init__(*, bot_user_id, adapter) -> None| Parameter | Type | Description |
|---|---|---|
bot_user_id | Effect[str, ChatError] | |
adapter | DiscordAdapter |
def __init__(
self,
*,
bot_user_id: Effect[str, ChatError],
adapter: DiscordAdapter,
) -> None:
self._bot_user_id = bot_user_id
self._adapter = adapterdispatch#
dispatch(payload) -> Effect[None, NotAccepted, ChatHandler]Hands one gateway dispatch's events to the ChatHandler; see the class.
| Parameter | Type | Description |
|---|---|---|
payload | GatewayDispatch |
Returns Effect[None, NotAccepted, ChatHandler]
def dispatch(self, payload: GatewayDispatch) -> Effect[None, NotAccepted, ChatHandler]:
"""Hands one gateway dispatch's events to the ``ChatHandler``; see the class."""
t, d = payload.t, payload.d
if t not in _normalize.HANDLED:
return fx.void
adapter = self._adapter
def refuse(message: str, error: ChatError) -> Effect[Never, NotAccepted]:
return fx.log_warning(message, error).and_then(fx.fail(NotAccepted()))
@fx.gen
def run() -> Generator[Effect[Any, NotAccepted, Any], Any]:
handler = yield from ChatHandler()
bot_user_id = yield from self._bot_user_id.catch_all(
lambda error: refuse("discord dispatch: bot user lookup failed", error)
)
result = _normalize.normalize(t, d, bot_user_id=bot_user_id)
if isinstance(result, _normalize.MissingChannelType):
def failed(error: ChatError) -> Effect[float | None, NotAccepted]:
if error.retry == "denied":
return fx.log_warning(
"discord dispatch: channel lookup denied, event dropped", error
).as_(None)
return refuse("discord dispatch: channel lookup failed", error)
found = (
yield from adapter.channel_type(result.channel_id)
.timeout_or_else(
_LOOKUP_TIMEOUT,
lambda: fx.fail(
_errors.chat_error(
"dispatch", _errors.transport(False, "lookup_timeout")
)
),
)
.catch_all(failed)
)
if found is None:
return
result = _normalize.normalize(t, d, bot_user_id=bot_user_id, channel_type=found)
if isinstance(result, _normalize.MissingChannelType):
return
for event in result.events:
yield from handler.handle(event)
return runinteraction#
interaction(payload, *, deadline=DEFAULT_DEADLINE) -> Effect[None, Never, Handler]Handles one INTERACTION_CREATE payload from the gateway; see the class.
| Parameter | Type | Description |
|---|---|---|
payload | object | |
deadline | DurationInput = DEFAULT_DEADLINE |
Returns Effect[None, Never, Handler]
def interaction(
self, payload: object, *, deadline: DurationInput = DEFAULT_DEADLINE
) -> Effect[None, Never, Handler]:
"""Handles one ``INTERACTION_CREATE`` payload from the gateway; see the class."""
return self._adapter.interaction(
payload, from_input_unsafe(deadline), self._adapter.send_callback
)guild#
guild(thread) -> Effect[str | None, ChatError]The guild a channel or thread is in; None for a direct.
| Parameter | Type | Description |
|---|---|---|
thread | ChatThread |
Returns Effect[str | None, ChatError]
def guild(self, thread: ChatThread) -> Effect[str | None, ChatError]:
"""The guild a channel or thread is in; ``None`` for a direct."""
return self._adapter.guild(thread)