GitHub

DiscordRestService

API reference for DiscordRestService.

The client, with the bot token. Each method's failure is a ChatError whose op is the method's name.

Attributes

AttributeTypeDescription
base_urlstr

The API's base URL, which follow_up sends to as well.

Functions

__init__

__init__(http, bot_token, base_url) -> None
ParameterTypeDescription
httpHttpClientService[HttpClientError, Never]
bot_tokenRedacted[str]
base_urlstr
def __init__(
    self,
    http: HttpClientService[HttpClientError, Never],
    bot_token: Redacted[str],
    base_url: str,
) -> None:
    self._http = http
    self._bot_token = bot_token
    self._base_url = base_url

request

request(method, route, *, op='request', body=None, files=(), query=None, write=False) -> Effect[Json, ChatError]

Sends one request to route (built with path) and succeeds with Discord's JSON answer. write marks a request that may land without an answer, so a failure is uncertain.

ParameterTypeDescription
methodMethod
routestr
opstr = 'request'
bodyobject = None
filesSequence[File] = ()
queryMapping[str, str] | None = None
writebool = False

Returns Effect[Json, ChatError]

def request(
    self,
    method: Method,
    route: str,
    *,
    op: str = "request",
    body: object = None,
    files: Sequence[File] = (),
    query: Mapping[str, str] | None = None,
    write: bool = False,
) -> Effect[Json, ChatError]:
    """Sends one request to ``route`` (built with ``path``) and succeeds
    with Discord's JSON answer. ``write`` marks a request that may land
    without an answer, so a failure is ``uncertain``."""
    request = build_request(self._base_url, method, route, body, files, query).set_header(
        "Authorization", "Bot " + redacted_value(self._bot_token)
    )
    return read_answer(op, write, self._http.execute(request))

get_my_user

get_my_user() -> Effect[Json, ChatError]

GET /users/@me: the bot's own user.

Returns Effect[Json, ChatError]

def get_my_user(self) -> Effect[Json, ChatError]:
    """``GET /users/@me``: the bot's own user."""
    return self.request("GET", path("users", "@me"), op="getMyUser")

get_user

get_user(user_id) -> Effect[Json, ChatError]

GET /users/{id}: a user.

ParameterTypeDescription
user_idstr

Returns Effect[Json, ChatError]

def get_user(self, user_id: str) -> Effect[Json, ChatError]:
    """``GET /users/{id}``: a user."""
    return self.request("GET", path("users", user_id), op="getUser")

get_channel

get_channel(channel_id) -> Effect[Json, ChatError]

GET /channels/{id}: a channel, thread or direct.

ParameterTypeDescription
channel_idstr

Returns Effect[Json, ChatError]

def get_channel(self, channel_id: str) -> Effect[Json, ChatError]:
    """``GET /channels/{id}``: a channel, thread or direct."""
    return self.request("GET", path("channels", channel_id), op="getChannel")

get_message

get_message(channel_id, message_id) -> Effect[Json, ChatError]

GET /channels/{id}/messages/{id}: one message.

ParameterTypeDescription
channel_idstr
message_idstr

Returns Effect[Json, ChatError]

def get_message(self, channel_id: str, message_id: str) -> Effect[Json, ChatError]:
    """``GET /channels/{id}/messages/{id}``: one message."""
    return self.request(
        "GET", path("channels", channel_id, "messages", message_id), op="getMessage"
    )

list_messages

list_messages(channel_id, *, before=None, after=None, limit=50) -> Effect[Json, ChatError]

GET /channels/{id}/messages: up to limit messages before or after an id, newest first.

ParameterTypeDescription
channel_idstr
beforestr | None = None
afterstr | None = None
limitint = 50

Returns Effect[Json, ChatError]

def list_messages(
    self,
    channel_id: str,
    *,
    before: str | None = None,
    after: str | None = None,
    limit: int = 50,
) -> Effect[Json, ChatError]:
    """``GET /channels/{id}/messages``: up to ``limit`` messages before or after an id,
    newest first."""
    query = {"limit": str(limit)}
    if before is not None:
        query["before"] = before
    if after is not None:
        query["after"] = after
    return self.request(
        "GET", path("channels", channel_id, "messages"), op="listMessages", query=query
    )

create_message

create_message(channel_id, body, files=()) -> Effect[Json, ChatError]

POST /channels/{id}/messages: a write, multipart when there are files.

ParameterTypeDescription
channel_idstr
bodyMapping[str, object]
filesSequence[File] = ()

Returns Effect[Json, ChatError]

def create_message(
    self, channel_id: str, body: Mapping[str, object], files: Sequence[File] = ()
) -> Effect[Json, ChatError]:
    """``POST /channels/{id}/messages``: a write, multipart when there are files."""
    return self.request(
        "POST",
        path("channels", channel_id, "messages"),
        op="createMessage",
        body=body,
        files=files,
        write=True,
    )

update_message

update_message(channel_id, message_id, body) -> Effect[Json, ChatError]

PATCH /channels/{id}/messages/{id}: a write.

ParameterTypeDescription
channel_idstr
message_idstr
bodyMapping[str, object]

Returns Effect[Json, ChatError]

def update_message(
    self, channel_id: str, message_id: str, body: Mapping[str, object]
) -> Effect[Json, ChatError]:
    """``PATCH /channels/{id}/messages/{id}``: a write."""
    return self.request(
        "PATCH",
        path("channels", channel_id, "messages", message_id),
        op="updateMessage",
        body=body,
        write=True,
    )

delete_message

delete_message(channel_id, message_id) -> Effect[Json, ChatError]

DELETE /channels/{id}/messages/{id}: a write.

ParameterTypeDescription
channel_idstr
message_idstr

Returns Effect[Json, ChatError]

def delete_message(self, channel_id: str, message_id: str) -> Effect[Json, ChatError]:
    """``DELETE /channels/{id}/messages/{id}``: a write."""
    return self.request(
        "DELETE",
        path("channels", channel_id, "messages", message_id),
        op="deleteMessage",
        write=True,
    )

create_thread_from_message

create_thread_from_message(channel_id, message_id, body) -> Effect[Json, ChatError]

POST /channels/{id}/messages/{id}/threads: a write.

ParameterTypeDescription
channel_idstr
message_idstr
bodyMapping[str, object]

Returns Effect[Json, ChatError]

def create_thread_from_message(
    self, channel_id: str, message_id: str, body: Mapping[str, object]
) -> Effect[Json, ChatError]:
    """``POST /channels/{id}/messages/{id}/threads``: a write."""
    return self.request(
        "POST",
        path("channels", channel_id, "messages", message_id, "threads"),
        op="createThreadFromMessage",
        body=body,
        write=True,
    )

get_guild_member

get_guild_member(guild_id, user_id) -> Effect[Json, ChatError]

GET /guilds/{id}/members/{id}: a member, with their nick and roles.

ParameterTypeDescription
guild_idstr
user_idstr

Returns Effect[Json, ChatError]

def get_guild_member(self, guild_id: str, user_id: str) -> Effect[Json, ChatError]:
    """``GET /guilds/{id}/members/{id}``: a member, with their nick and roles."""
    return self.request(
        "GET", path("guilds", guild_id, "members", user_id), op="getGuildMember"
    )

create_interaction_response

create_interaction_response(interaction_id, token, body) -> Effect[Json, ChatError]

POST /interactions/{id}/{token}/callback: an interaction's answer.

ParameterTypeDescription
interaction_idstr
tokenstr
bodyMapping[str, object]

Returns Effect[Json, ChatError]

def create_interaction_response(
    self, interaction_id: str, token: str, body: Mapping[str, object]
) -> Effect[Json, ChatError]:
    """``POST /interactions/{id}/{token}/callback``: an interaction's answer."""
    return self.request(
        "POST",
        path("interactions", interaction_id, token, "callback"),
        op="createInteractionResponse",
        body=body,
        write=True,
    )

get_my_application

get_my_application() -> Effect[Json, ChatError]

GET /applications/@me: the bot's application.

Returns Effect[Json, ChatError]

def get_my_application(self) -> Effect[Json, ChatError]:
    """``GET /applications/@me``: the bot's application."""
    return self.request("GET", path("applications", "@me"), op="getMyApplication")

bulk_set_application_commands

bulk_set_application_commands(application_id, commands) -> Effect[Json, ChatError]

Replaces the application's whole list of global commands.

ParameterTypeDescription
application_idstr
commandsSequence[Mapping[str, object]]

Returns Effect[Json, ChatError]

def bulk_set_application_commands(
    self, application_id: str, commands: Sequence[Mapping[str, object]]
) -> Effect[Json, ChatError]:
    """Replaces the application's whole list of global commands."""
    return self.request(
        "PUT",
        path("applications", application_id, "commands"),
        op="bulkSetApplicationCommands",
        body=list(commands),
        write=True,
    )