discordbots-sdk 1.0.6__tar.gz
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_sdk-1.0.6/.gitignore +14 -0
- discordbots_sdk-1.0.6/LICENSE +21 -0
- discordbots_sdk-1.0.6/PKG-INFO +291 -0
- discordbots_sdk-1.0.6/README.md +245 -0
- discordbots_sdk-1.0.6/pyproject.toml +99 -0
- discordbots_sdk-1.0.6/src/discordbots/__init__.py +126 -0
- discordbots_sdk-1.0.6/src/discordbots/client.py +485 -0
- discordbots_sdk-1.0.6/src/discordbots/discord_py.py +293 -0
- discordbots_sdk-1.0.6/src/discordbots/errors.py +72 -0
- discordbots_sdk-1.0.6/src/discordbots/http.py +425 -0
- discordbots_sdk-1.0.6/src/discordbots/public.py +305 -0
- discordbots_sdk-1.0.6/src/discordbots/py.typed +0 -0
- discordbots_sdk-1.0.6/src/discordbots/types.py +672 -0
- discordbots_sdk-1.0.6/src/discordbots/webhooks/__init__.py +23 -0
- discordbots_sdk-1.0.6/src/discordbots/webhooks/aiohttp.py +28 -0
- discordbots_sdk-1.0.6/src/discordbots/webhooks/fastapi.py +48 -0
- discordbots_sdk-1.0.6/src/discordbots/webhooks/flask.py +28 -0
- discordbots_sdk-1.0.6/src/discordbots/webhooks/handler.py +494 -0
- discordbots_sdk-1.0.6/tests/test_client.py +214 -0
- discordbots_sdk-1.0.6/tests/test_discord_py.py +112 -0
- discordbots_sdk-1.0.6/tests/test_public.py +81 -0
- discordbots_sdk-1.0.6/tests/test_webhooks.py +158 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 DiscordBots.in
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: discordbots-sdk
|
|
3
|
+
Version: 1.0.6
|
|
4
|
+
Summary: Official DiscordBots.in SDK for Bot API, public directory, discord.py integration, and webhooks
|
|
5
|
+
Project-URL: Homepage, https://discordbots.in/
|
|
6
|
+
Project-URL: Repository, https://github.com/harshcodezzz/py-sdk-discordbots
|
|
7
|
+
Project-URL: Issues, https://github.com/harshcodezzz/py-sdk-discordbots/issues
|
|
8
|
+
Author: DiscordBots
|
|
9
|
+
License: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: bot-list,discord,discord.py,discordbots,discordbots.in,sdk,webhook
|
|
12
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.9
|
|
25
|
+
Requires-Dist: httpx>=0.24.0
|
|
26
|
+
Provides-Extra: aiohttp
|
|
27
|
+
Requires-Dist: aiohttp>=3.8.0; extra == 'aiohttp'
|
|
28
|
+
Provides-Extra: all
|
|
29
|
+
Requires-Dist: aiohttp>=3.8.0; extra == 'all'
|
|
30
|
+
Requires-Dist: discord-py>=2.0.0; extra == 'all'
|
|
31
|
+
Requires-Dist: fastapi>=0.100.0; extra == 'all'
|
|
32
|
+
Requires-Dist: flask>=2.2.0; extra == 'all'
|
|
33
|
+
Provides-Extra: dev
|
|
34
|
+
Requires-Dist: build>=1.2.0; extra == 'dev'
|
|
35
|
+
Requires-Dist: mypy>=1.10.0; extra == 'dev'
|
|
36
|
+
Requires-Dist: pytest-asyncio>=0.23.0; extra == 'dev'
|
|
37
|
+
Requires-Dist: pytest>=8.0.0; extra == 'dev'
|
|
38
|
+
Requires-Dist: ruff>=0.4.0; extra == 'dev'
|
|
39
|
+
Provides-Extra: discord
|
|
40
|
+
Requires-Dist: discord-py>=2.0.0; extra == 'discord'
|
|
41
|
+
Provides-Extra: fastapi
|
|
42
|
+
Requires-Dist: fastapi>=0.100.0; extra == 'fastapi'
|
|
43
|
+
Provides-Extra: flask
|
|
44
|
+
Requires-Dist: flask>=2.2.0; extra == 'flask'
|
|
45
|
+
Description-Content-Type: text/markdown
|
|
46
|
+
|
|
47
|
+
# discordbots-sdk
|
|
48
|
+
|
|
49
|
+
Official Python SDK for [DiscordBots.in](https://discordbots.in) — Bot API (sync & async), Public Directory, `discord.py` integration, and Webhook verification.
|
|
50
|
+
|
|
51
|
+
## Installation
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
pip install discordbots-sdk
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
With optional integrations:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
pip install "discordbots-sdk[discord]" # discord.py integration
|
|
61
|
+
pip install "discordbots-sdk[fastapi]" # FastAPI webhook handler
|
|
62
|
+
pip install "discordbots-sdk[flask]" # Flask webhook view
|
|
63
|
+
pip install "discordbots-sdk[aiohttp]" # aiohttp webhook handler
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Requires **Python 3.9+**.
|
|
67
|
+
|
|
68
|
+
## Modules
|
|
69
|
+
|
|
70
|
+
| Import | Purpose |
|
|
71
|
+
| --- | --- |
|
|
72
|
+
| `discordbots` | `DiscordBots`, `AsyncDiscordBots`, `PublicDirectoryClient`, `AsyncPublicDirectoryClient`, errors, and types |
|
|
73
|
+
| `discordbots.webhooks` | Standalone `WebhookHandler` (sync, async, and raw ASGI) |
|
|
74
|
+
| `discordbots.webhooks.fastapi` | FastAPI / Starlette webhook handler and router helper |
|
|
75
|
+
| `discordbots.webhooks.flask` | Flask webhook view |
|
|
76
|
+
| `discordbots.webhooks.aiohttp` | `aiohttp.web` webhook handler |
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## Bot API
|
|
81
|
+
|
|
82
|
+
### Asynchronous (`AsyncDiscordBots`)
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
import asyncio
|
|
86
|
+
import os
|
|
87
|
+
from discordbots import AsyncDiscordBots, BotCommand
|
|
88
|
+
|
|
89
|
+
async def main() -> None:
|
|
90
|
+
async with AsyncDiscordBots(api_key=os.environ["DISCORDBOTS_API_KEY"]) as sdk:
|
|
91
|
+
# Get bot profile, vote totals, and registered commands
|
|
92
|
+
profile = await sdk.get_bot()
|
|
93
|
+
print(profile.bot.name, profile.votes.total)
|
|
94
|
+
|
|
95
|
+
# Update server and shard count
|
|
96
|
+
await sdk.post_stats(server_count=1520, shard_count=2)
|
|
97
|
+
|
|
98
|
+
# Sync slash commands to your public listing
|
|
99
|
+
await sdk.sync_commands([
|
|
100
|
+
BotCommand(
|
|
101
|
+
name="play",
|
|
102
|
+
description="Plays a track",
|
|
103
|
+
category="Music",
|
|
104
|
+
usage="/play <song>",
|
|
105
|
+
),
|
|
106
|
+
BotCommand(name="skip", description="Skips the current track", category="Music"),
|
|
107
|
+
])
|
|
108
|
+
|
|
109
|
+
# Check if a user has an active vote (12-hour cooldown)
|
|
110
|
+
voted = await sdk.has_voted("123456789012345678")
|
|
111
|
+
vote_details = await sdk.check_vote("123456789012345678")
|
|
112
|
+
print(voted, vote_details.remaining_ms)
|
|
113
|
+
|
|
114
|
+
asyncio.run(main())
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Synchronous (`DiscordBots`)
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
import os
|
|
121
|
+
from discordbots import DiscordBots
|
|
122
|
+
|
|
123
|
+
with DiscordBots(api_key=os.environ["DISCORDBOTS_API_KEY"]) as sdk:
|
|
124
|
+
profile = sdk.get_bot()
|
|
125
|
+
sdk.post_stats(1520, shard_count=2)
|
|
126
|
+
voted = sdk.has_voted("123456789012345678")
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
## `discord.py` Integration
|
|
132
|
+
|
|
133
|
+
Pass your `discord.py` `Client` or `commands.Bot` directly to `AsyncDiscordBots` or to `post_stats`, `sync_commands`, and `auto_post`:
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
import os
|
|
137
|
+
import discord
|
|
138
|
+
from discord.ext import commands
|
|
139
|
+
from discordbots import AsyncDiscordBots
|
|
140
|
+
|
|
141
|
+
intents = discord.Intents.default()
|
|
142
|
+
bot = commands.Bot(command_prefix="!", intents=intents)
|
|
143
|
+
|
|
144
|
+
sdk = AsyncDiscordBots(
|
|
145
|
+
api_key=os.environ["DISCORDBOTS_API_KEY"],
|
|
146
|
+
client=bot,
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
@bot.event
|
|
150
|
+
async def on_ready() -> None:
|
|
151
|
+
# Checks every 60s and only posts when server_count or shard_count changes.
|
|
152
|
+
# Posts immediately on initial run once the client is ready.
|
|
153
|
+
sdk.auto_post(
|
|
154
|
+
on_posted=lambda result, stats: print(f"Posted {result.server_count} servers"),
|
|
155
|
+
on_error=lambda err: print(f"Failed to post stats: {err}"),
|
|
156
|
+
)
|
|
157
|
+
await sdk.sync_commands(bot)
|
|
158
|
+
|
|
159
|
+
bot.run(os.environ["DISCORD_TOKEN"])
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
You can also manually post or stop the auto-poster at any time:
|
|
163
|
+
|
|
164
|
+
```python
|
|
165
|
+
await sdk.post_stats(bot)
|
|
166
|
+
poster.stop()
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
## Public Directory
|
|
172
|
+
|
|
173
|
+
Browse public bots, servers, and categories without an API key:
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
from discordbots import PublicDirectoryClient
|
|
177
|
+
|
|
178
|
+
with PublicDirectoryClient() as directory:
|
|
179
|
+
bots = directory.get_bots(search="music", page=1, limit=10)
|
|
180
|
+
bot = directory.get_bot("932627636079976489")
|
|
181
|
+
servers = directory.get_servers(sort="votes")
|
|
182
|
+
categories = directory.get_categories()
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
*(Also available on an authenticated instance via `sdk.directory`, and asynchronously via `AsyncPublicDirectoryClient`.)*
|
|
186
|
+
|
|
187
|
+
---
|
|
188
|
+
|
|
189
|
+
## Webhooks
|
|
190
|
+
|
|
191
|
+
All webhook handlers verify `X-Webhook-Signature-256` (HMAC-SHA256 over the raw body), validate `X-Webhook-Timestamp` (5-minute replay window), and support `Authorization` header fallback.
|
|
192
|
+
|
|
193
|
+
### Standalone (`discordbots.webhooks`)
|
|
194
|
+
|
|
195
|
+
```python
|
|
196
|
+
import os
|
|
197
|
+
from discordbots.webhooks import WebhookHandler
|
|
198
|
+
|
|
199
|
+
webhook = WebhookHandler(secret=os.environ["DISCORDBOTS_WEBHOOK_SECRET"])
|
|
200
|
+
|
|
201
|
+
@webhook.on("botVote")
|
|
202
|
+
async def handle_bot_vote(payload, context):
|
|
203
|
+
print(f"{payload.user.name} ({payload.user.discord_id}) voted! Weight: {payload.vote.weight}")
|
|
204
|
+
|
|
205
|
+
@webhook.on("serverVote")
|
|
206
|
+
async def handle_server_vote(payload, context):
|
|
207
|
+
print(f"Server {payload.server.name} received a vote from {payload.user.discord_id}")
|
|
208
|
+
|
|
209
|
+
@webhook.on("test")
|
|
210
|
+
async def handle_test(payload, context):
|
|
211
|
+
print("Test webhook received:", payload.message)
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### FastAPI (`discordbots.webhooks.fastapi`)
|
|
215
|
+
|
|
216
|
+
```python
|
|
217
|
+
import os
|
|
218
|
+
from fastapi import FastAPI
|
|
219
|
+
from discordbots.webhooks import WebhookHandler
|
|
220
|
+
from discordbots.webhooks.fastapi import mount_fastapi_webhook
|
|
221
|
+
|
|
222
|
+
app = FastAPI()
|
|
223
|
+
webhook = WebhookHandler(secret=os.environ["DISCORDBOTS_WEBHOOK_SECRET"])
|
|
224
|
+
|
|
225
|
+
@webhook.on("botVote")
|
|
226
|
+
async def on_vote(payload, context):
|
|
227
|
+
print(f"Vote from {payload.user.discord_id}")
|
|
228
|
+
|
|
229
|
+
mount_fastapi_webhook(app, path="/webhook/discordbots", handler=webhook)
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### Flask (`discordbots.webhooks.flask`)
|
|
233
|
+
|
|
234
|
+
```python
|
|
235
|
+
import os
|
|
236
|
+
from flask import Flask
|
|
237
|
+
from discordbots.webhooks.flask import create_flask_view
|
|
238
|
+
|
|
239
|
+
app = Flask(__name__)
|
|
240
|
+
|
|
241
|
+
app.add_url_rule(
|
|
242
|
+
"/webhook/discordbots",
|
|
243
|
+
view_func=create_flask_view(
|
|
244
|
+
os.environ["DISCORDBOTS_WEBHOOK_SECRET"],
|
|
245
|
+
on_bot_vote=lambda payload, ctx: print(f"Vote from {payload.user.discord_id}"),
|
|
246
|
+
),
|
|
247
|
+
methods=["POST"],
|
|
248
|
+
)
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### aiohttp (`discordbots.webhooks.aiohttp`)
|
|
252
|
+
|
|
253
|
+
```python
|
|
254
|
+
import os
|
|
255
|
+
from aiohttp import web
|
|
256
|
+
from discordbots.webhooks.aiohttp import create_aiohttp_handler
|
|
257
|
+
|
|
258
|
+
app = web.Application()
|
|
259
|
+
app.router.add_post(
|
|
260
|
+
"/webhook/discordbots",
|
|
261
|
+
create_aiohttp_handler(
|
|
262
|
+
os.environ["DISCORDBOTS_WEBHOOK_SECRET"],
|
|
263
|
+
on_vote=lambda payload, ctx: print(f"Vote from {payload.user.discord_id}"),
|
|
264
|
+
),
|
|
265
|
+
)
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
---
|
|
269
|
+
|
|
270
|
+
## Error Handling
|
|
271
|
+
|
|
272
|
+
```python
|
|
273
|
+
from discordbots import (
|
|
274
|
+
DiscordBots,
|
|
275
|
+
DiscordBotsApiError,
|
|
276
|
+
DiscordBotsError,
|
|
277
|
+
RateLimitError,
|
|
278
|
+
WebhookVerificationError,
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
sdk = DiscordBots(api_key="...")
|
|
282
|
+
|
|
283
|
+
try:
|
|
284
|
+
sdk.post_stats(100)
|
|
285
|
+
except RateLimitError as err:
|
|
286
|
+
print(f"Rate limited! Retry after {err.retry_after}s (limit: {err.limit})")
|
|
287
|
+
except DiscordBotsApiError as err:
|
|
288
|
+
print(f"API error {err.status}: {err.message}")
|
|
289
|
+
except DiscordBotsError as err:
|
|
290
|
+
print(f"SDK error [{err.code}]: {err.message}")
|
|
291
|
+
```
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
# discordbots-sdk
|
|
2
|
+
|
|
3
|
+
Official Python SDK for [DiscordBots.in](https://discordbots.in) — Bot API (sync & async), Public Directory, `discord.py` integration, and Webhook verification.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install discordbots-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
With optional integrations:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pip install "discordbots-sdk[discord]" # discord.py integration
|
|
15
|
+
pip install "discordbots-sdk[fastapi]" # FastAPI webhook handler
|
|
16
|
+
pip install "discordbots-sdk[flask]" # Flask webhook view
|
|
17
|
+
pip install "discordbots-sdk[aiohttp]" # aiohttp webhook handler
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Requires **Python 3.9+**.
|
|
21
|
+
|
|
22
|
+
## Modules
|
|
23
|
+
|
|
24
|
+
| Import | Purpose |
|
|
25
|
+
| --- | --- |
|
|
26
|
+
| `discordbots` | `DiscordBots`, `AsyncDiscordBots`, `PublicDirectoryClient`, `AsyncPublicDirectoryClient`, errors, and types |
|
|
27
|
+
| `discordbots.webhooks` | Standalone `WebhookHandler` (sync, async, and raw ASGI) |
|
|
28
|
+
| `discordbots.webhooks.fastapi` | FastAPI / Starlette webhook handler and router helper |
|
|
29
|
+
| `discordbots.webhooks.flask` | Flask webhook view |
|
|
30
|
+
| `discordbots.webhooks.aiohttp` | `aiohttp.web` webhook handler |
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## Bot API
|
|
35
|
+
|
|
36
|
+
### Asynchronous (`AsyncDiscordBots`)
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
import asyncio
|
|
40
|
+
import os
|
|
41
|
+
from discordbots import AsyncDiscordBots, BotCommand
|
|
42
|
+
|
|
43
|
+
async def main() -> None:
|
|
44
|
+
async with AsyncDiscordBots(api_key=os.environ["DISCORDBOTS_API_KEY"]) as sdk:
|
|
45
|
+
# Get bot profile, vote totals, and registered commands
|
|
46
|
+
profile = await sdk.get_bot()
|
|
47
|
+
print(profile.bot.name, profile.votes.total)
|
|
48
|
+
|
|
49
|
+
# Update server and shard count
|
|
50
|
+
await sdk.post_stats(server_count=1520, shard_count=2)
|
|
51
|
+
|
|
52
|
+
# Sync slash commands to your public listing
|
|
53
|
+
await sdk.sync_commands([
|
|
54
|
+
BotCommand(
|
|
55
|
+
name="play",
|
|
56
|
+
description="Plays a track",
|
|
57
|
+
category="Music",
|
|
58
|
+
usage="/play <song>",
|
|
59
|
+
),
|
|
60
|
+
BotCommand(name="skip", description="Skips the current track", category="Music"),
|
|
61
|
+
])
|
|
62
|
+
|
|
63
|
+
# Check if a user has an active vote (12-hour cooldown)
|
|
64
|
+
voted = await sdk.has_voted("123456789012345678")
|
|
65
|
+
vote_details = await sdk.check_vote("123456789012345678")
|
|
66
|
+
print(voted, vote_details.remaining_ms)
|
|
67
|
+
|
|
68
|
+
asyncio.run(main())
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Synchronous (`DiscordBots`)
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
import os
|
|
75
|
+
from discordbots import DiscordBots
|
|
76
|
+
|
|
77
|
+
with DiscordBots(api_key=os.environ["DISCORDBOTS_API_KEY"]) as sdk:
|
|
78
|
+
profile = sdk.get_bot()
|
|
79
|
+
sdk.post_stats(1520, shard_count=2)
|
|
80
|
+
voted = sdk.has_voted("123456789012345678")
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
## `discord.py` Integration
|
|
86
|
+
|
|
87
|
+
Pass your `discord.py` `Client` or `commands.Bot` directly to `AsyncDiscordBots` or to `post_stats`, `sync_commands`, and `auto_post`:
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
import os
|
|
91
|
+
import discord
|
|
92
|
+
from discord.ext import commands
|
|
93
|
+
from discordbots import AsyncDiscordBots
|
|
94
|
+
|
|
95
|
+
intents = discord.Intents.default()
|
|
96
|
+
bot = commands.Bot(command_prefix="!", intents=intents)
|
|
97
|
+
|
|
98
|
+
sdk = AsyncDiscordBots(
|
|
99
|
+
api_key=os.environ["DISCORDBOTS_API_KEY"],
|
|
100
|
+
client=bot,
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
@bot.event
|
|
104
|
+
async def on_ready() -> None:
|
|
105
|
+
# Checks every 60s and only posts when server_count or shard_count changes.
|
|
106
|
+
# Posts immediately on initial run once the client is ready.
|
|
107
|
+
sdk.auto_post(
|
|
108
|
+
on_posted=lambda result, stats: print(f"Posted {result.server_count} servers"),
|
|
109
|
+
on_error=lambda err: print(f"Failed to post stats: {err}"),
|
|
110
|
+
)
|
|
111
|
+
await sdk.sync_commands(bot)
|
|
112
|
+
|
|
113
|
+
bot.run(os.environ["DISCORD_TOKEN"])
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
You can also manually post or stop the auto-poster at any time:
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
await sdk.post_stats(bot)
|
|
120
|
+
poster.stop()
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
## Public Directory
|
|
126
|
+
|
|
127
|
+
Browse public bots, servers, and categories without an API key:
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
from discordbots import PublicDirectoryClient
|
|
131
|
+
|
|
132
|
+
with PublicDirectoryClient() as directory:
|
|
133
|
+
bots = directory.get_bots(search="music", page=1, limit=10)
|
|
134
|
+
bot = directory.get_bot("932627636079976489")
|
|
135
|
+
servers = directory.get_servers(sort="votes")
|
|
136
|
+
categories = directory.get_categories()
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
*(Also available on an authenticated instance via `sdk.directory`, and asynchronously via `AsyncPublicDirectoryClient`.)*
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
## Webhooks
|
|
144
|
+
|
|
145
|
+
All webhook handlers verify `X-Webhook-Signature-256` (HMAC-SHA256 over the raw body), validate `X-Webhook-Timestamp` (5-minute replay window), and support `Authorization` header fallback.
|
|
146
|
+
|
|
147
|
+
### Standalone (`discordbots.webhooks`)
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
import os
|
|
151
|
+
from discordbots.webhooks import WebhookHandler
|
|
152
|
+
|
|
153
|
+
webhook = WebhookHandler(secret=os.environ["DISCORDBOTS_WEBHOOK_SECRET"])
|
|
154
|
+
|
|
155
|
+
@webhook.on("botVote")
|
|
156
|
+
async def handle_bot_vote(payload, context):
|
|
157
|
+
print(f"{payload.user.name} ({payload.user.discord_id}) voted! Weight: {payload.vote.weight}")
|
|
158
|
+
|
|
159
|
+
@webhook.on("serverVote")
|
|
160
|
+
async def handle_server_vote(payload, context):
|
|
161
|
+
print(f"Server {payload.server.name} received a vote from {payload.user.discord_id}")
|
|
162
|
+
|
|
163
|
+
@webhook.on("test")
|
|
164
|
+
async def handle_test(payload, context):
|
|
165
|
+
print("Test webhook received:", payload.message)
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### FastAPI (`discordbots.webhooks.fastapi`)
|
|
169
|
+
|
|
170
|
+
```python
|
|
171
|
+
import os
|
|
172
|
+
from fastapi import FastAPI
|
|
173
|
+
from discordbots.webhooks import WebhookHandler
|
|
174
|
+
from discordbots.webhooks.fastapi import mount_fastapi_webhook
|
|
175
|
+
|
|
176
|
+
app = FastAPI()
|
|
177
|
+
webhook = WebhookHandler(secret=os.environ["DISCORDBOTS_WEBHOOK_SECRET"])
|
|
178
|
+
|
|
179
|
+
@webhook.on("botVote")
|
|
180
|
+
async def on_vote(payload, context):
|
|
181
|
+
print(f"Vote from {payload.user.discord_id}")
|
|
182
|
+
|
|
183
|
+
mount_fastapi_webhook(app, path="/webhook/discordbots", handler=webhook)
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Flask (`discordbots.webhooks.flask`)
|
|
187
|
+
|
|
188
|
+
```python
|
|
189
|
+
import os
|
|
190
|
+
from flask import Flask
|
|
191
|
+
from discordbots.webhooks.flask import create_flask_view
|
|
192
|
+
|
|
193
|
+
app = Flask(__name__)
|
|
194
|
+
|
|
195
|
+
app.add_url_rule(
|
|
196
|
+
"/webhook/discordbots",
|
|
197
|
+
view_func=create_flask_view(
|
|
198
|
+
os.environ["DISCORDBOTS_WEBHOOK_SECRET"],
|
|
199
|
+
on_bot_vote=lambda payload, ctx: print(f"Vote from {payload.user.discord_id}"),
|
|
200
|
+
),
|
|
201
|
+
methods=["POST"],
|
|
202
|
+
)
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### aiohttp (`discordbots.webhooks.aiohttp`)
|
|
206
|
+
|
|
207
|
+
```python
|
|
208
|
+
import os
|
|
209
|
+
from aiohttp import web
|
|
210
|
+
from discordbots.webhooks.aiohttp import create_aiohttp_handler
|
|
211
|
+
|
|
212
|
+
app = web.Application()
|
|
213
|
+
app.router.add_post(
|
|
214
|
+
"/webhook/discordbots",
|
|
215
|
+
create_aiohttp_handler(
|
|
216
|
+
os.environ["DISCORDBOTS_WEBHOOK_SECRET"],
|
|
217
|
+
on_vote=lambda payload, ctx: print(f"Vote from {payload.user.discord_id}"),
|
|
218
|
+
),
|
|
219
|
+
)
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
---
|
|
223
|
+
|
|
224
|
+
## Error Handling
|
|
225
|
+
|
|
226
|
+
```python
|
|
227
|
+
from discordbots import (
|
|
228
|
+
DiscordBots,
|
|
229
|
+
DiscordBotsApiError,
|
|
230
|
+
DiscordBotsError,
|
|
231
|
+
RateLimitError,
|
|
232
|
+
WebhookVerificationError,
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
sdk = DiscordBots(api_key="...")
|
|
236
|
+
|
|
237
|
+
try:
|
|
238
|
+
sdk.post_stats(100)
|
|
239
|
+
except RateLimitError as err:
|
|
240
|
+
print(f"Rate limited! Retry after {err.retry_after}s (limit: {err.limit})")
|
|
241
|
+
except DiscordBotsApiError as err:
|
|
242
|
+
print(f"API error {err.status}: {err.message}")
|
|
243
|
+
except DiscordBotsError as err:
|
|
244
|
+
print(f"SDK error [{err.code}]: {err.message}")
|
|
245
|
+
```
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.24.0"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "discordbots-sdk"
|
|
7
|
+
version = "1.0.6"
|
|
8
|
+
description = "Official DiscordBots.in SDK for Bot API, public directory, discord.py integration, and webhooks"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
requires-python = ">=3.9"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "DiscordBots" },
|
|
14
|
+
]
|
|
15
|
+
keywords = [
|
|
16
|
+
"discordbots",
|
|
17
|
+
"discordbots.in",
|
|
18
|
+
"discord",
|
|
19
|
+
"discord.py",
|
|
20
|
+
"bot-list",
|
|
21
|
+
"webhook",
|
|
22
|
+
"sdk",
|
|
23
|
+
]
|
|
24
|
+
classifiers = [
|
|
25
|
+
"Development Status :: 5 - Production/Stable",
|
|
26
|
+
"Intended Audience :: Developers",
|
|
27
|
+
"License :: OSI Approved :: MIT License",
|
|
28
|
+
"Operating System :: OS Independent",
|
|
29
|
+
"Programming Language :: Python :: 3",
|
|
30
|
+
"Programming Language :: Python :: 3.9",
|
|
31
|
+
"Programming Language :: Python :: 3.10",
|
|
32
|
+
"Programming Language :: Python :: 3.11",
|
|
33
|
+
"Programming Language :: Python :: 3.12",
|
|
34
|
+
"Programming Language :: Python :: 3.13",
|
|
35
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
36
|
+
"Typing :: Typed",
|
|
37
|
+
]
|
|
38
|
+
dependencies = [
|
|
39
|
+
"httpx>=0.24.0",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
[project.optional-dependencies]
|
|
43
|
+
discord = ["discord.py>=2.0.0"]
|
|
44
|
+
fastapi = ["fastapi>=0.100.0"]
|
|
45
|
+
flask = ["flask>=2.2.0"]
|
|
46
|
+
aiohttp = ["aiohttp>=3.8.0"]
|
|
47
|
+
all = [
|
|
48
|
+
"discord.py>=2.0.0",
|
|
49
|
+
"fastapi>=0.100.0",
|
|
50
|
+
"flask>=2.2.0",
|
|
51
|
+
"aiohttp>=3.8.0",
|
|
52
|
+
]
|
|
53
|
+
dev = [
|
|
54
|
+
"pytest>=8.0.0",
|
|
55
|
+
"pytest-asyncio>=0.23.0",
|
|
56
|
+
"ruff>=0.4.0",
|
|
57
|
+
"mypy>=1.10.0",
|
|
58
|
+
"build>=1.2.0",
|
|
59
|
+
]
|
|
60
|
+
|
|
61
|
+
[project.urls]
|
|
62
|
+
Homepage = "https://discordbots.in/"
|
|
63
|
+
Repository = "https://github.com/harshcodezzz/py-sdk-discordbots"
|
|
64
|
+
Issues = "https://github.com/harshcodezzz/py-sdk-discordbots/issues"
|
|
65
|
+
|
|
66
|
+
[tool.hatch.build.targets.wheel]
|
|
67
|
+
packages = ["src/discordbots"]
|
|
68
|
+
|
|
69
|
+
[tool.hatch.build.targets.sdist]
|
|
70
|
+
include = [
|
|
71
|
+
"src/discordbots",
|
|
72
|
+
"tests",
|
|
73
|
+
"README.md",
|
|
74
|
+
"LICENSE",
|
|
75
|
+
"pyproject.toml",
|
|
76
|
+
]
|
|
77
|
+
|
|
78
|
+
[tool.pytest.ini_options]
|
|
79
|
+
testpaths = ["tests"]
|
|
80
|
+
asyncio_mode = "auto"
|
|
81
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
82
|
+
|
|
83
|
+
[tool.ruff]
|
|
84
|
+
target-version = "py39"
|
|
85
|
+
line-length = 100
|
|
86
|
+
src = ["src", "tests"]
|
|
87
|
+
|
|
88
|
+
[tool.ruff.lint]
|
|
89
|
+
select = ["E", "F", "I", "UP", "B", "SIM"]
|
|
90
|
+
|
|
91
|
+
[tool.mypy]
|
|
92
|
+
python_version = "3.10"
|
|
93
|
+
strict = true
|
|
94
|
+
mypy_path = "src"
|
|
95
|
+
packages = ["discordbots"]
|
|
96
|
+
|
|
97
|
+
[[tool.mypy.overrides]]
|
|
98
|
+
module = ["fastapi.*", "flask.*", "aiohttp.*", "discord.*"]
|
|
99
|
+
ignore_missing_imports = true
|