discordbots-sdk 1.0.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- discordbots/__init__.py +126 -0
- discordbots/client.py +485 -0
- discordbots/discord_py.py +293 -0
- discordbots/errors.py +72 -0
- discordbots/http.py +425 -0
- discordbots/public.py +305 -0
- discordbots/py.typed +0 -0
- discordbots/types.py +672 -0
- discordbots/webhooks/__init__.py +23 -0
- discordbots/webhooks/aiohttp.py +28 -0
- discordbots/webhooks/fastapi.py +48 -0
- discordbots/webhooks/flask.py +28 -0
- discordbots/webhooks/handler.py +494 -0
- discordbots_sdk-1.0.6.dist-info/METADATA +291 -0
- discordbots_sdk-1.0.6.dist-info/RECORD +17 -0
- discordbots_sdk-1.0.6.dist-info/WHEEL +4 -0
- discordbots_sdk-1.0.6.dist-info/licenses/LICENSE +21 -0
discordbots/__init__.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
from .client import (
|
|
2
|
+
AsyncDiscordBots,
|
|
3
|
+
AsyncDiscordBotsClient,
|
|
4
|
+
DiscordBots,
|
|
5
|
+
DiscordBotsClient,
|
|
6
|
+
)
|
|
7
|
+
from .discord_py import (
|
|
8
|
+
AsyncAutoPosterHandle,
|
|
9
|
+
SyncAutoPosterHandle,
|
|
10
|
+
extract_dpy_commands,
|
|
11
|
+
extract_dpy_stats,
|
|
12
|
+
fetch_dpy_commands,
|
|
13
|
+
is_discord_py_client,
|
|
14
|
+
)
|
|
15
|
+
from .errors import (
|
|
16
|
+
DiscordBotsApiError,
|
|
17
|
+
DiscordBotsError,
|
|
18
|
+
RateLimitError,
|
|
19
|
+
WebhookVerificationError,
|
|
20
|
+
)
|
|
21
|
+
from .http import (
|
|
22
|
+
DEFAULT_API_BASE_URL,
|
|
23
|
+
DEFAULT_TIMEOUT_S,
|
|
24
|
+
AsyncHttpClient,
|
|
25
|
+
HttpClient,
|
|
26
|
+
parse_rate_limit_headers,
|
|
27
|
+
)
|
|
28
|
+
from .public import (
|
|
29
|
+
DEFAULT_PUBLIC_BASE_URL,
|
|
30
|
+
AsyncPublicDirectoryClient,
|
|
31
|
+
PublicDirectoryClient,
|
|
32
|
+
)
|
|
33
|
+
from .types import (
|
|
34
|
+
BotCategory,
|
|
35
|
+
BotCommand,
|
|
36
|
+
BotOwner,
|
|
37
|
+
BotProfile,
|
|
38
|
+
BotProfileData,
|
|
39
|
+
BotVoteStats,
|
|
40
|
+
BotVoteWebhookPayload,
|
|
41
|
+
DirectoryListOptions,
|
|
42
|
+
DiscordBotsWebhookPayload,
|
|
43
|
+
PaginatedDirectoryResponse,
|
|
44
|
+
PublicBot,
|
|
45
|
+
PublicServer,
|
|
46
|
+
RateLimitInfo,
|
|
47
|
+
ServerVoteWebhookPayload,
|
|
48
|
+
SyncCommandsResult,
|
|
49
|
+
TestWebhookPayload,
|
|
50
|
+
TestWebhookTarget,
|
|
51
|
+
UpdateStatsInput,
|
|
52
|
+
UpdateStatsResult,
|
|
53
|
+
VoteCheckResult,
|
|
54
|
+
WebhookBotTarget,
|
|
55
|
+
WebhookDeliveryContext,
|
|
56
|
+
WebhookHandleResult,
|
|
57
|
+
WebhookServerTarget,
|
|
58
|
+
WebhookVoteInfo,
|
|
59
|
+
WebhookVoter,
|
|
60
|
+
)
|
|
61
|
+
from .webhooks import (
|
|
62
|
+
WebhookHandler,
|
|
63
|
+
compute_hmac_sha256_hex,
|
|
64
|
+
create_webhook_handler,
|
|
65
|
+
verify_webhook_signature,
|
|
66
|
+
verify_webhook_timestamp,
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
__version__ = "1.0.6"
|
|
70
|
+
|
|
71
|
+
__all__ = [
|
|
72
|
+
"__version__",
|
|
73
|
+
"DEFAULT_API_BASE_URL",
|
|
74
|
+
"DEFAULT_PUBLIC_BASE_URL",
|
|
75
|
+
"DEFAULT_TIMEOUT_S",
|
|
76
|
+
"AsyncAutoPosterHandle",
|
|
77
|
+
"AsyncDiscordBots",
|
|
78
|
+
"AsyncDiscordBotsClient",
|
|
79
|
+
"AsyncHttpClient",
|
|
80
|
+
"AsyncPublicDirectoryClient",
|
|
81
|
+
"BotCategory",
|
|
82
|
+
"BotCommand",
|
|
83
|
+
"BotOwner",
|
|
84
|
+
"BotProfile",
|
|
85
|
+
"BotProfileData",
|
|
86
|
+
"BotVoteStats",
|
|
87
|
+
"BotVoteWebhookPayload",
|
|
88
|
+
"DirectoryListOptions",
|
|
89
|
+
"DiscordBots",
|
|
90
|
+
"DiscordBotsApiError",
|
|
91
|
+
"DiscordBotsClient",
|
|
92
|
+
"DiscordBotsError",
|
|
93
|
+
"DiscordBotsWebhookPayload",
|
|
94
|
+
"HttpClient",
|
|
95
|
+
"PaginatedDirectoryResponse",
|
|
96
|
+
"PublicBot",
|
|
97
|
+
"PublicDirectoryClient",
|
|
98
|
+
"PublicServer",
|
|
99
|
+
"RateLimitError",
|
|
100
|
+
"RateLimitInfo",
|
|
101
|
+
"ServerVoteWebhookPayload",
|
|
102
|
+
"SyncAutoPosterHandle",
|
|
103
|
+
"SyncCommandsResult",
|
|
104
|
+
"TestWebhookPayload",
|
|
105
|
+
"TestWebhookTarget",
|
|
106
|
+
"UpdateStatsInput",
|
|
107
|
+
"UpdateStatsResult",
|
|
108
|
+
"VoteCheckResult",
|
|
109
|
+
"WebhookBotTarget",
|
|
110
|
+
"WebhookDeliveryContext",
|
|
111
|
+
"WebhookHandleResult",
|
|
112
|
+
"WebhookHandler",
|
|
113
|
+
"WebhookServerTarget",
|
|
114
|
+
"WebhookVerificationError",
|
|
115
|
+
"WebhookVoteInfo",
|
|
116
|
+
"WebhookVoter",
|
|
117
|
+
"compute_hmac_sha256_hex",
|
|
118
|
+
"create_webhook_handler",
|
|
119
|
+
"extract_dpy_commands",
|
|
120
|
+
"extract_dpy_stats",
|
|
121
|
+
"fetch_dpy_commands",
|
|
122
|
+
"is_discord_py_client",
|
|
123
|
+
"parse_rate_limit_headers",
|
|
124
|
+
"verify_webhook_signature",
|
|
125
|
+
"verify_webhook_timestamp",
|
|
126
|
+
]
|
discordbots/client.py
ADDED
|
@@ -0,0 +1,485 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from collections.abc import Callable, Mapping, Sequence
|
|
5
|
+
from typing import Any
|
|
6
|
+
from urllib.parse import quote
|
|
7
|
+
|
|
8
|
+
import httpx
|
|
9
|
+
|
|
10
|
+
from .discord_py import (
|
|
11
|
+
DEFAULT_AUTO_POST_INTERVAL_S,
|
|
12
|
+
AsyncAutoPosterHandle,
|
|
13
|
+
SyncAutoPosterHandle,
|
|
14
|
+
extract_dpy_commands,
|
|
15
|
+
extract_dpy_stats,
|
|
16
|
+
fetch_dpy_commands,
|
|
17
|
+
get_client_bot_id,
|
|
18
|
+
is_discord_py_client,
|
|
19
|
+
)
|
|
20
|
+
from .errors import DiscordBotsError
|
|
21
|
+
from .http import DEFAULT_API_BASE_URL, DEFAULT_TIMEOUT_S, AsyncHttpClient, HttpClient
|
|
22
|
+
from .public import AsyncPublicDirectoryClient, PublicDirectoryClient
|
|
23
|
+
from .types import (
|
|
24
|
+
AuthHeaderType,
|
|
25
|
+
BotCommand,
|
|
26
|
+
BotProfileData,
|
|
27
|
+
SyncCommandsResult,
|
|
28
|
+
UpdateStatsInput,
|
|
29
|
+
UpdateStatsResult,
|
|
30
|
+
VoteCheckResult,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
_SNOWFLAKE_RE = re.compile(r"^\d{17,20}$")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _validate_snowflake(discord_id: str) -> None:
|
|
37
|
+
if not discord_id or not _SNOWFLAKE_RE.match(discord_id):
|
|
38
|
+
raise DiscordBotsError(
|
|
39
|
+
"A valid Discord user snowflake ID (17-20 digits) is required.",
|
|
40
|
+
"INVALID_DISCORD_ID",
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _normalize_commands(
|
|
45
|
+
commands: Sequence[BotCommand | Mapping[str, Any]],
|
|
46
|
+
) -> list[dict[str, str]]:
|
|
47
|
+
normalized: list[dict[str, str]] = []
|
|
48
|
+
for cmd in commands:
|
|
49
|
+
if isinstance(cmd, BotCommand):
|
|
50
|
+
normalized.append(cmd.to_dict())
|
|
51
|
+
elif isinstance(cmd, Mapping):
|
|
52
|
+
normalized.append(BotCommand.from_dict(cmd).to_dict())
|
|
53
|
+
else:
|
|
54
|
+
raise DiscordBotsError(
|
|
55
|
+
"Commands must be BotCommand instances or dicts.",
|
|
56
|
+
"INVALID_ARGUMENT",
|
|
57
|
+
)
|
|
58
|
+
return normalized
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _validate_stats_payload(payload: UpdateStatsInput) -> None:
|
|
62
|
+
if (
|
|
63
|
+
not isinstance(payload.server_count, int)
|
|
64
|
+
or isinstance(payload.server_count, bool)
|
|
65
|
+
or payload.server_count < 0
|
|
66
|
+
):
|
|
67
|
+
raise DiscordBotsError(
|
|
68
|
+
"server_count must be a non-negative integer.",
|
|
69
|
+
"INVALID_STATS",
|
|
70
|
+
)
|
|
71
|
+
if payload.shard_count is not None and (
|
|
72
|
+
not isinstance(payload.shard_count, int)
|
|
73
|
+
or isinstance(payload.shard_count, bool)
|
|
74
|
+
or payload.shard_count < 0
|
|
75
|
+
):
|
|
76
|
+
raise DiscordBotsError(
|
|
77
|
+
"shard_count must be a non-negative integer.",
|
|
78
|
+
"INVALID_STATS",
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _coerce_stats_input(
|
|
83
|
+
stats_or_client_or_count: (
|
|
84
|
+
UpdateStatsInput | Mapping[str, Any] | int | Any | None
|
|
85
|
+
),
|
|
86
|
+
shard_count: int | None,
|
|
87
|
+
default_client: Any,
|
|
88
|
+
) -> tuple[UpdateStatsInput, str | None]:
|
|
89
|
+
inferred_bot_id: str | None = None
|
|
90
|
+
|
|
91
|
+
if stats_or_client_or_count is None:
|
|
92
|
+
if default_client is None:
|
|
93
|
+
raise DiscordBotsError(
|
|
94
|
+
"post_stats() requires stats, a discord.py Client, "
|
|
95
|
+
"or a `client` configured on DiscordBots.",
|
|
96
|
+
"INVALID_ARGUMENT",
|
|
97
|
+
)
|
|
98
|
+
payload = extract_dpy_stats(default_client)
|
|
99
|
+
inferred_bot_id = get_client_bot_id(default_client)
|
|
100
|
+
elif isinstance(stats_or_client_or_count, int) and not isinstance(
|
|
101
|
+
stats_or_client_or_count, bool
|
|
102
|
+
):
|
|
103
|
+
payload = UpdateStatsInput(
|
|
104
|
+
server_count=stats_or_client_or_count,
|
|
105
|
+
shard_count=shard_count,
|
|
106
|
+
)
|
|
107
|
+
elif isinstance(stats_or_client_or_count, UpdateStatsInput):
|
|
108
|
+
payload = stats_or_client_or_count
|
|
109
|
+
elif isinstance(stats_or_client_or_count, Mapping):
|
|
110
|
+
raw_server = (
|
|
111
|
+
stats_or_client_or_count.get("serverCount")
|
|
112
|
+
if "serverCount" in stats_or_client_or_count
|
|
113
|
+
else stats_or_client_or_count.get("server_count")
|
|
114
|
+
)
|
|
115
|
+
raw_shard = (
|
|
116
|
+
stats_or_client_or_count.get("shardCount")
|
|
117
|
+
if "shardCount" in stats_or_client_or_count
|
|
118
|
+
else stats_or_client_or_count.get("shard_count")
|
|
119
|
+
)
|
|
120
|
+
payload = UpdateStatsInput(
|
|
121
|
+
server_count=raw_server if isinstance(raw_server, int) else -1,
|
|
122
|
+
shard_count=raw_shard if isinstance(raw_shard, int) else shard_count,
|
|
123
|
+
)
|
|
124
|
+
elif is_discord_py_client(stats_or_client_or_count):
|
|
125
|
+
payload = extract_dpy_stats(stats_or_client_or_count)
|
|
126
|
+
inferred_bot_id = get_client_bot_id(stats_or_client_or_count)
|
|
127
|
+
else:
|
|
128
|
+
raise DiscordBotsError(
|
|
129
|
+
"Invalid stats argument for post_stats().",
|
|
130
|
+
"INVALID_ARGUMENT",
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
_validate_stats_payload(payload)
|
|
134
|
+
return payload, inferred_bot_id
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
class _BaseDiscordBots:
|
|
138
|
+
def __init__(
|
|
139
|
+
self,
|
|
140
|
+
api_key: str,
|
|
141
|
+
*,
|
|
142
|
+
bot_id: str | None = None,
|
|
143
|
+
client: Any = None,
|
|
144
|
+
) -> None:
|
|
145
|
+
if not api_key or not isinstance(api_key, str):
|
|
146
|
+
raise DiscordBotsError(
|
|
147
|
+
"A valid DiscordBots.in Bot API key is required.",
|
|
148
|
+
"MISSING_API_KEY",
|
|
149
|
+
)
|
|
150
|
+
self._default_bot_id = bot_id
|
|
151
|
+
self._dpy_client = client
|
|
152
|
+
|
|
153
|
+
def _resolve_bot_id(self, bot_id: str | None = None) -> str | None:
|
|
154
|
+
if bot_id:
|
|
155
|
+
return bot_id
|
|
156
|
+
if self._default_bot_id:
|
|
157
|
+
return self._default_bot_id
|
|
158
|
+
if self._dpy_client is not None:
|
|
159
|
+
return get_client_bot_id(self._dpy_client)
|
|
160
|
+
return None
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
class DiscordBots(_BaseDiscordBots):
|
|
164
|
+
"""Synchronous DiscordBots.in Bot API and public directory client."""
|
|
165
|
+
|
|
166
|
+
def __init__(
|
|
167
|
+
self,
|
|
168
|
+
api_key: str,
|
|
169
|
+
*,
|
|
170
|
+
bot_id: str | None = None,
|
|
171
|
+
client: Any = None,
|
|
172
|
+
base_url: str | None = None,
|
|
173
|
+
public_base_url: str | None = None,
|
|
174
|
+
auth_header: AuthHeaderType = "bearer",
|
|
175
|
+
timeout: float = DEFAULT_TIMEOUT_S,
|
|
176
|
+
user_agent: str | None = None,
|
|
177
|
+
http_client: httpx.Client | None = None,
|
|
178
|
+
public_http_client: httpx.Client | None = None,
|
|
179
|
+
) -> None:
|
|
180
|
+
super().__init__(api_key, bot_id=bot_id, client=client)
|
|
181
|
+
self._http = HttpClient(
|
|
182
|
+
base_url=base_url or DEFAULT_API_BASE_URL,
|
|
183
|
+
api_key=api_key,
|
|
184
|
+
auth_header=auth_header,
|
|
185
|
+
timeout=timeout,
|
|
186
|
+
user_agent=user_agent,
|
|
187
|
+
client=http_client,
|
|
188
|
+
)
|
|
189
|
+
self.directory = PublicDirectoryClient(
|
|
190
|
+
base_url=public_base_url,
|
|
191
|
+
timeout=timeout,
|
|
192
|
+
http_client=public_http_client,
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
@property
|
|
196
|
+
def public(self) -> PublicDirectoryClient:
|
|
197
|
+
return self.directory
|
|
198
|
+
|
|
199
|
+
def close(self) -> None:
|
|
200
|
+
self._http.close()
|
|
201
|
+
self.directory.close()
|
|
202
|
+
|
|
203
|
+
def __enter__(self) -> DiscordBots:
|
|
204
|
+
return self
|
|
205
|
+
|
|
206
|
+
def __exit__(self, *args: object) -> None:
|
|
207
|
+
self.close()
|
|
208
|
+
|
|
209
|
+
def get_bot(
|
|
210
|
+
self, bot_id: str | None = None, *, timeout: float | None = None
|
|
211
|
+
) -> BotProfileData:
|
|
212
|
+
target_id = self._resolve_bot_id(bot_id)
|
|
213
|
+
path = f"/bots/{quote(target_id, safe='')}" if target_id else "/bots"
|
|
214
|
+
data = self._http.get(path, require_auth=True, timeout=timeout)
|
|
215
|
+
return BotProfileData.from_dict(data if isinstance(data, Mapping) else {})
|
|
216
|
+
|
|
217
|
+
def get_profile(
|
|
218
|
+
self, bot_id: str | None = None, *, timeout: float | None = None
|
|
219
|
+
) -> BotProfileData:
|
|
220
|
+
return self.get_bot(bot_id, timeout=timeout)
|
|
221
|
+
|
|
222
|
+
def sync_commands(
|
|
223
|
+
self,
|
|
224
|
+
commands_or_client: Sequence[BotCommand | Mapping[str, Any]] | Any,
|
|
225
|
+
bot_id: str | None = None,
|
|
226
|
+
*,
|
|
227
|
+
timeout: float | None = None,
|
|
228
|
+
) -> SyncCommandsResult:
|
|
229
|
+
inferred_bot_id = bot_id
|
|
230
|
+
if is_discord_py_client(commands_or_client):
|
|
231
|
+
commands_payload = [c.to_dict() for c in extract_dpy_commands(commands_or_client)]
|
|
232
|
+
if inferred_bot_id is None:
|
|
233
|
+
inferred_bot_id = get_client_bot_id(commands_or_client)
|
|
234
|
+
elif isinstance(commands_or_client, Sequence) and not isinstance(
|
|
235
|
+
commands_or_client, (str, bytes)
|
|
236
|
+
):
|
|
237
|
+
commands_payload = _normalize_commands(commands_or_client)
|
|
238
|
+
else:
|
|
239
|
+
raise DiscordBotsError(
|
|
240
|
+
"sync_commands requires a sequence of BotCommand/dict objects "
|
|
241
|
+
"or a discord.py Client.",
|
|
242
|
+
"INVALID_ARGUMENT",
|
|
243
|
+
)
|
|
244
|
+
|
|
245
|
+
target_id = self._resolve_bot_id(inferred_bot_id)
|
|
246
|
+
path = f"/bots/{quote(target_id, safe='')}/commands" if target_id else "/bots/commands"
|
|
247
|
+
data = self._http.put(path, commands_payload, require_auth=True, timeout=timeout)
|
|
248
|
+
return SyncCommandsResult.from_dict(data if isinstance(data, Mapping) else {})
|
|
249
|
+
|
|
250
|
+
def check_vote(
|
|
251
|
+
self,
|
|
252
|
+
discord_id: str,
|
|
253
|
+
bot_id: str | None = None,
|
|
254
|
+
*,
|
|
255
|
+
timeout: float | None = None,
|
|
256
|
+
) -> VoteCheckResult:
|
|
257
|
+
_validate_snowflake(discord_id)
|
|
258
|
+
target_id = self._resolve_bot_id(bot_id)
|
|
259
|
+
safe_user = quote(discord_id, safe="")
|
|
260
|
+
path = (
|
|
261
|
+
f"/bots/{quote(target_id, safe='')}/votes/{safe_user}"
|
|
262
|
+
if target_id
|
|
263
|
+
else f"/bots/votes/{safe_user}"
|
|
264
|
+
)
|
|
265
|
+
data = self._http.get(path, require_auth=True, timeout=timeout)
|
|
266
|
+
return VoteCheckResult.from_dict(data if isinstance(data, Mapping) else {})
|
|
267
|
+
|
|
268
|
+
def has_voted(
|
|
269
|
+
self,
|
|
270
|
+
discord_id: str,
|
|
271
|
+
bot_id: str | None = None,
|
|
272
|
+
*,
|
|
273
|
+
timeout: float | None = None,
|
|
274
|
+
) -> bool:
|
|
275
|
+
return self.check_vote(discord_id, bot_id, timeout=timeout).has_voted
|
|
276
|
+
|
|
277
|
+
def post_stats(
|
|
278
|
+
self,
|
|
279
|
+
stats_or_client_or_count: UpdateStatsInput | Mapping[str, Any] | int | Any | None = None,
|
|
280
|
+
shard_count: int | None = None,
|
|
281
|
+
*,
|
|
282
|
+
bot_id: str | None = None,
|
|
283
|
+
timeout: float | None = None,
|
|
284
|
+
) -> UpdateStatsResult:
|
|
285
|
+
payload, inferred_id = _coerce_stats_input(
|
|
286
|
+
stats_or_client_or_count, shard_count, self._dpy_client
|
|
287
|
+
)
|
|
288
|
+
resolved_id = self._resolve_bot_id(bot_id or inferred_id)
|
|
289
|
+
path = f"/bots/{quote(resolved_id, safe='')}/stats" if resolved_id else "/bots/stats"
|
|
290
|
+
data = self._http.post(path, payload.to_dict(), require_auth=True, timeout=timeout)
|
|
291
|
+
return UpdateStatsResult.from_dict(data if isinstance(data, Mapping) else {})
|
|
292
|
+
|
|
293
|
+
def auto_post(
|
|
294
|
+
self,
|
|
295
|
+
client: Any = None,
|
|
296
|
+
*,
|
|
297
|
+
interval_seconds: float = DEFAULT_AUTO_POST_INTERVAL_S,
|
|
298
|
+
bot_id: str | None = None,
|
|
299
|
+
post_on_start: bool = True,
|
|
300
|
+
on_posted: Callable[[UpdateStatsResult, UpdateStatsInput], Any] | None = None,
|
|
301
|
+
on_error: Callable[[Exception], Any] | None = None,
|
|
302
|
+
) -> SyncAutoPosterHandle:
|
|
303
|
+
target_client = client if client is not None else self._dpy_client
|
|
304
|
+
if not is_discord_py_client(target_client):
|
|
305
|
+
raise DiscordBotsError(
|
|
306
|
+
"auto_post requires a discord.py Client either as an argument or on DiscordBots.",
|
|
307
|
+
"INVALID_DPY_CLIENT",
|
|
308
|
+
)
|
|
309
|
+
return SyncAutoPosterHandle(
|
|
310
|
+
lambda stats, b_id: self.post_stats(stats, bot_id=b_id),
|
|
311
|
+
target_client,
|
|
312
|
+
interval_seconds=interval_seconds,
|
|
313
|
+
bot_id=bot_id,
|
|
314
|
+
post_on_start=post_on_start,
|
|
315
|
+
on_posted=on_posted,
|
|
316
|
+
on_error=on_error,
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
class AsyncDiscordBots(_BaseDiscordBots):
|
|
321
|
+
"""Asynchronous DiscordBots.in Bot API and public directory client."""
|
|
322
|
+
|
|
323
|
+
def __init__(
|
|
324
|
+
self,
|
|
325
|
+
api_key: str,
|
|
326
|
+
*,
|
|
327
|
+
bot_id: str | None = None,
|
|
328
|
+
client: Any = None,
|
|
329
|
+
base_url: str | None = None,
|
|
330
|
+
public_base_url: str | None = None,
|
|
331
|
+
auth_header: AuthHeaderType = "bearer",
|
|
332
|
+
timeout: float = DEFAULT_TIMEOUT_S,
|
|
333
|
+
user_agent: str | None = None,
|
|
334
|
+
http_client: httpx.AsyncClient | None = None,
|
|
335
|
+
public_http_client: httpx.AsyncClient | None = None,
|
|
336
|
+
) -> None:
|
|
337
|
+
super().__init__(api_key, bot_id=bot_id, client=client)
|
|
338
|
+
self._http = AsyncHttpClient(
|
|
339
|
+
base_url=base_url or DEFAULT_API_BASE_URL,
|
|
340
|
+
api_key=api_key,
|
|
341
|
+
auth_header=auth_header,
|
|
342
|
+
timeout=timeout,
|
|
343
|
+
user_agent=user_agent,
|
|
344
|
+
client=http_client,
|
|
345
|
+
)
|
|
346
|
+
self.directory = AsyncPublicDirectoryClient(
|
|
347
|
+
base_url=public_base_url,
|
|
348
|
+
timeout=timeout,
|
|
349
|
+
http_client=public_http_client,
|
|
350
|
+
)
|
|
351
|
+
|
|
352
|
+
@property
|
|
353
|
+
def public(self) -> AsyncPublicDirectoryClient:
|
|
354
|
+
return self.directory
|
|
355
|
+
|
|
356
|
+
async def aclose(self) -> None:
|
|
357
|
+
await self._http.aclose()
|
|
358
|
+
await self.directory.aclose()
|
|
359
|
+
|
|
360
|
+
async def __aenter__(self) -> AsyncDiscordBots:
|
|
361
|
+
return self
|
|
362
|
+
|
|
363
|
+
async def __aexit__(self, *args: object) -> None:
|
|
364
|
+
await self.aclose()
|
|
365
|
+
|
|
366
|
+
async def get_bot(
|
|
367
|
+
self, bot_id: str | None = None, *, timeout: float | None = None
|
|
368
|
+
) -> BotProfileData:
|
|
369
|
+
target_id = self._resolve_bot_id(bot_id)
|
|
370
|
+
path = f"/bots/{quote(target_id, safe='')}" if target_id else "/bots"
|
|
371
|
+
data = await self._http.get(path, require_auth=True, timeout=timeout)
|
|
372
|
+
return BotProfileData.from_dict(data if isinstance(data, Mapping) else {})
|
|
373
|
+
|
|
374
|
+
async def get_profile(
|
|
375
|
+
self, bot_id: str | None = None, *, timeout: float | None = None
|
|
376
|
+
) -> BotProfileData:
|
|
377
|
+
return await self.get_bot(bot_id, timeout=timeout)
|
|
378
|
+
|
|
379
|
+
async def sync_commands(
|
|
380
|
+
self,
|
|
381
|
+
commands_or_client: Sequence[BotCommand | Mapping[str, Any]] | Any,
|
|
382
|
+
bot_id: str | None = None,
|
|
383
|
+
*,
|
|
384
|
+
timeout: float | None = None,
|
|
385
|
+
) -> SyncCommandsResult:
|
|
386
|
+
inferred_bot_id = bot_id
|
|
387
|
+
if is_discord_py_client(commands_or_client):
|
|
388
|
+
dpy_cmds = await fetch_dpy_commands(commands_or_client)
|
|
389
|
+
commands_payload = [c.to_dict() for c in dpy_cmds]
|
|
390
|
+
if inferred_bot_id is None:
|
|
391
|
+
inferred_bot_id = get_client_bot_id(commands_or_client)
|
|
392
|
+
elif isinstance(commands_or_client, Sequence) and not isinstance(
|
|
393
|
+
commands_or_client, (str, bytes)
|
|
394
|
+
):
|
|
395
|
+
commands_payload = _normalize_commands(commands_or_client)
|
|
396
|
+
else:
|
|
397
|
+
raise DiscordBotsError(
|
|
398
|
+
"sync_commands requires a sequence of BotCommand/dict objects "
|
|
399
|
+
"or a discord.py Client.",
|
|
400
|
+
"INVALID_ARGUMENT",
|
|
401
|
+
)
|
|
402
|
+
|
|
403
|
+
target_id = self._resolve_bot_id(inferred_bot_id)
|
|
404
|
+
path = f"/bots/{quote(target_id, safe='')}/commands" if target_id else "/bots/commands"
|
|
405
|
+
data = await self._http.put(path, commands_payload, require_auth=True, timeout=timeout)
|
|
406
|
+
return SyncCommandsResult.from_dict(data if isinstance(data, Mapping) else {})
|
|
407
|
+
|
|
408
|
+
async def check_vote(
|
|
409
|
+
self,
|
|
410
|
+
discord_id: str,
|
|
411
|
+
bot_id: str | None = None,
|
|
412
|
+
*,
|
|
413
|
+
timeout: float | None = None,
|
|
414
|
+
) -> VoteCheckResult:
|
|
415
|
+
_validate_snowflake(discord_id)
|
|
416
|
+
target_id = self._resolve_bot_id(bot_id)
|
|
417
|
+
safe_user = quote(discord_id, safe="")
|
|
418
|
+
path = (
|
|
419
|
+
f"/bots/{quote(target_id, safe='')}/votes/{safe_user}"
|
|
420
|
+
if target_id
|
|
421
|
+
else f"/bots/votes/{safe_user}"
|
|
422
|
+
)
|
|
423
|
+
data = await self._http.get(path, require_auth=True, timeout=timeout)
|
|
424
|
+
return VoteCheckResult.from_dict(data if isinstance(data, Mapping) else {})
|
|
425
|
+
|
|
426
|
+
async def has_voted(
|
|
427
|
+
self,
|
|
428
|
+
discord_id: str,
|
|
429
|
+
bot_id: str | None = None,
|
|
430
|
+
*,
|
|
431
|
+
timeout: float | None = None,
|
|
432
|
+
) -> bool:
|
|
433
|
+
result = await self.check_vote(discord_id, bot_id, timeout=timeout)
|
|
434
|
+
return result.has_voted
|
|
435
|
+
|
|
436
|
+
async def post_stats(
|
|
437
|
+
self,
|
|
438
|
+
stats_or_client_or_count: UpdateStatsInput | Mapping[str, Any] | int | Any | None = None,
|
|
439
|
+
shard_count: int | None = None,
|
|
440
|
+
*,
|
|
441
|
+
bot_id: str | None = None,
|
|
442
|
+
timeout: float | None = None,
|
|
443
|
+
) -> UpdateStatsResult:
|
|
444
|
+
payload, inferred_id = _coerce_stats_input(
|
|
445
|
+
stats_or_client_or_count, shard_count, self._dpy_client
|
|
446
|
+
)
|
|
447
|
+
resolved_id = self._resolve_bot_id(bot_id or inferred_id)
|
|
448
|
+
path = f"/bots/{quote(resolved_id, safe='')}/stats" if resolved_id else "/bots/stats"
|
|
449
|
+
data = await self._http.post(path, payload.to_dict(), require_auth=True, timeout=timeout)
|
|
450
|
+
return UpdateStatsResult.from_dict(data if isinstance(data, Mapping) else {})
|
|
451
|
+
|
|
452
|
+
def auto_post(
|
|
453
|
+
self,
|
|
454
|
+
client: Any = None,
|
|
455
|
+
*,
|
|
456
|
+
interval_seconds: float = DEFAULT_AUTO_POST_INTERVAL_S,
|
|
457
|
+
bot_id: str | None = None,
|
|
458
|
+
post_on_start: bool = True,
|
|
459
|
+
on_posted: Callable[[UpdateStatsResult, UpdateStatsInput], Any] | None = None,
|
|
460
|
+
on_error: Callable[[Exception], Any] | None = None,
|
|
461
|
+
) -> AsyncAutoPosterHandle:
|
|
462
|
+
target_client = client if client is not None else self._dpy_client
|
|
463
|
+
if not is_discord_py_client(target_client):
|
|
464
|
+
raise DiscordBotsError(
|
|
465
|
+
"auto_post requires a discord.py Client either as an argument "
|
|
466
|
+
"or on AsyncDiscordBots.",
|
|
467
|
+
"INVALID_DPY_CLIENT",
|
|
468
|
+
)
|
|
469
|
+
|
|
470
|
+
async def _post(stats: UpdateStatsInput, b_id: str | None) -> UpdateStatsResult:
|
|
471
|
+
return await self.post_stats(stats, bot_id=b_id)
|
|
472
|
+
|
|
473
|
+
return AsyncAutoPosterHandle(
|
|
474
|
+
_post,
|
|
475
|
+
target_client,
|
|
476
|
+
interval_seconds=interval_seconds,
|
|
477
|
+
bot_id=bot_id,
|
|
478
|
+
post_on_start=post_on_start,
|
|
479
|
+
on_posted=on_posted,
|
|
480
|
+
on_error=on_error,
|
|
481
|
+
)
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
DiscordBotsClient = DiscordBots
|
|
485
|
+
AsyncDiscordBotsClient = AsyncDiscordBots
|