The client, with the bot token. Each method's failure is a
ChatError whose op is the method's name.
Attributes#
| Attribute | Type | Description |
|---|---|---|
base_url | str | The API's base URL, which |
Functions#
__init__#
__init__(http, bot_token, base_url) -> None| Parameter | Type | Description |
|---|---|---|
http | HttpClientService[HttpClientError, Never] | |
bot_token | Redacted[str] | |
base_url | str |
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_urlrequest#
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.
| Parameter | Type | Description |
|---|---|---|
method | Method | |
route | str | |
op | str = 'request' | |
body | object = None | |
files | Sequence[File] = () | |
query | Mapping[str, str] | None = None | |
write | bool = 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.
| Parameter | Type | Description |
|---|---|---|
user_id | str |
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.
| Parameter | Type | Description |
|---|---|---|
channel_id | str |
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.
| Parameter | Type | Description |
|---|---|---|
channel_id | str | |
message_id | str |
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.
| Parameter | Type | Description |
|---|---|---|
channel_id | str | |
before | str | None = None | |
after | str | None = None | |
limit | int = 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.
| Parameter | Type | Description |
|---|---|---|
channel_id | str | |
body | Mapping[str, object] | |
files | Sequence[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.
| Parameter | Type | Description |
|---|---|---|
channel_id | str | |
message_id | str | |
body | Mapping[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.
| Parameter | Type | Description |
|---|---|---|
channel_id | str | |
message_id | str |
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.
| Parameter | Type | Description |
|---|---|---|
channel_id | str | |
message_id | str | |
body | Mapping[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.
| Parameter | Type | Description |
|---|---|---|
guild_id | str | |
user_id | str |
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.
| Parameter | Type | Description |
|---|---|---|
interaction_id | str | |
token | str | |
body | Mapping[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.
| Parameter | Type | Description |
|---|---|---|
application_id | str | |
commands | Sequence[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,
)