necropy 0.1.0__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.
- necropy-0.1.0/LICENSE +21 -0
- necropy-0.1.0/PKG-INFO +314 -0
- necropy-0.1.0/README.md +287 -0
- necropy-0.1.0/pyproject.toml +45 -0
- necropy-0.1.0/setup.cfg +4 -0
- necropy-0.1.0/src/necropy/__init__.py +31 -0
- necropy-0.1.0/src/necropy/bot.py +345 -0
- necropy-0.1.0/src/necropy/components.py +190 -0
- necropy-0.1.0/src/necropy/exceptions.py +47 -0
- necropy-0.1.0/src/necropy/http.py +309 -0
- necropy-0.1.0/src/necropy/models.py +288 -0
- necropy-0.1.0/src/necropy/utils.py +36 -0
- necropy-0.1.0/src/necropy/webhook.py +81 -0
- necropy-0.1.0/src/necropy.egg-info/PKG-INFO +314 -0
- necropy-0.1.0/src/necropy.egg-info/SOURCES.txt +19 -0
- necropy-0.1.0/src/necropy.egg-info/dependency_links.txt +1 -0
- necropy-0.1.0/src/necropy.egg-info/requires.txt +5 -0
- necropy-0.1.0/src/necropy.egg-info/top_level.txt +1 -0
- necropy-0.1.0/tests/test_bot.py +72 -0
- necropy-0.1.0/tests/test_components.py +42 -0
- necropy-0.1.0/tests/test_http.py +87 -0
necropy-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 necropy contributors
|
|
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.
|
necropy-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: necropy
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: کتابخانه بات سازی در پیامرسان نکرو
|
|
5
|
+
Author: necropy contributors
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://necro.ir/developers
|
|
8
|
+
Project-URL: Documentation, https://necro.ir/developers
|
|
9
|
+
Keywords: necro,necropy,bot,messaging,sdk
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: requests<3.0.0,>=2.31.0
|
|
23
|
+
Requires-Dist: python-socketio[client]<6.0.0,>=5.11.0
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
# necropy
|
|
29
|
+
|
|
30
|
+
Persian documentation is included in `README.fa.md` in the source archive.
|
|
31
|
+
|
|
32
|
+
**necropy** is a Python SDK for building bots on [Necro](https://necro.ir). It wraps Necro's Bot REST API and Socket.IO event gateway, and includes helpers for messages, slash commands, interactions, embeds, buttons, select menus, file uploads, and channel webhooks.
|
|
33
|
+
|
|
34
|
+
> This is the first development release (`0.1.0`). The SDK is based on Necro's published developer documentation. It has been tested locally with mocked HTTP responses; connecting to the live service requires your own bot token.
|
|
35
|
+
|
|
36
|
+
## Requirements
|
|
37
|
+
|
|
38
|
+
- Python 3.10+
|
|
39
|
+
- A bot created in Necro → Settings → My Bots
|
|
40
|
+
- The bot token (keep it secret)
|
|
41
|
+
|
|
42
|
+
## Install
|
|
43
|
+
|
|
44
|
+
From the extracted source directory, install the package and its runtime dependencies with:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
python -m pip install .
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
When the project is published to the Python Package Index, it will also be installable with `pip install necropy`.
|
|
51
|
+
|
|
52
|
+
## Quick start
|
|
53
|
+
|
|
54
|
+
Set the token as an environment variable instead of putting it in your source code:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
# Linux / macOS
|
|
58
|
+
export NECRO_BOT_TOKEN="your-token"
|
|
59
|
+
|
|
60
|
+
# PowerShell
|
|
61
|
+
$env:NECRO_BOT_TOKEN="your-token"
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Create `bot.py`:
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
import os
|
|
68
|
+
from necropy import Bot
|
|
69
|
+
|
|
70
|
+
bot = Bot(os.environ["NECRO_BOT_TOKEN"])
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@bot.event
|
|
74
|
+
def on_ready(data):
|
|
75
|
+
user = data["user"]
|
|
76
|
+
print(f"Connected as {user['name']} in {len(data['servers'])} server(s)")
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
@bot.event
|
|
80
|
+
def on_message(message):
|
|
81
|
+
# Necro sends the bot's own messages back to it too.
|
|
82
|
+
if message.author.is_bot:
|
|
83
|
+
return
|
|
84
|
+
|
|
85
|
+
if message.content.strip() == "!ping":
|
|
86
|
+
message.reply("pong 🏓")
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
bot.run()
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Run it:
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
python bot.py
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
The bot needs the **View Channels** and **Send Messages** permissions to read and reply in a channel. Only channels visible to the bot's role are delivered to the gateway.
|
|
99
|
+
|
|
100
|
+
## Slash commands
|
|
101
|
+
|
|
102
|
+
Decorate a callback with `@bot.command`, then publish the registered definitions with `sync_commands()`. Command callbacks receive an `Interaction` object.
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
from necropy import Bot
|
|
106
|
+
import os
|
|
107
|
+
|
|
108
|
+
bot = Bot(os.environ["NECRO_BOT_TOKEN"])
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
@bot.command(name="hello", description="Say hello")
|
|
112
|
+
def hello(interaction):
|
|
113
|
+
interaction.respond(f"سلام {interaction.user.name}!")
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
@bot.command(
|
|
117
|
+
name="roll",
|
|
118
|
+
description="Roll a die",
|
|
119
|
+
options=[
|
|
120
|
+
{
|
|
121
|
+
"type": "integer",
|
|
122
|
+
"name": "sides",
|
|
123
|
+
"description": "Number of sides",
|
|
124
|
+
"required": False,
|
|
125
|
+
"minValue": 2,
|
|
126
|
+
"maxValue": 1000,
|
|
127
|
+
}
|
|
128
|
+
],
|
|
129
|
+
)
|
|
130
|
+
def roll(interaction):
|
|
131
|
+
import random
|
|
132
|
+
|
|
133
|
+
sides = interaction.options.get("sides", 6)
|
|
134
|
+
interaction.respond(f"🎲 {random.randint(1, sides)} (1–{sides})")
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
# Register globally (or pass server_id=123 for server-scoped commands).
|
|
138
|
+
bot.sync_commands()
|
|
139
|
+
bot.run()
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
`sync_commands()` replaces the commands in that scope. To add/replace each definition individually instead, call `sync_commands(replace=False)`. Necro supports a limited subset of Discord-style slash-command options; see its documentation for accepted option types and limits.
|
|
143
|
+
|
|
144
|
+
## Embeds, buttons, and select menus
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
from necropy import ActionRow, Bot, Button, ButtonStyle, Embed, SelectMenu, SelectOption
|
|
148
|
+
|
|
149
|
+
# A rich message
|
|
150
|
+
card = (
|
|
151
|
+
Embed(title="Build completed", description="The deployment is ready.", color=0x40D490)
|
|
152
|
+
.add_field("Status", "Success", inline=True)
|
|
153
|
+
.add_field("Environment", "Production", inline=True)
|
|
154
|
+
.set_footer("CI pipeline")
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
row = ActionRow(
|
|
158
|
+
Button("Approve", custom_id="approve", style=ButtonStyle.SUCCESS),
|
|
159
|
+
Button("Open docs", style=ButtonStyle.LINK, url="https://necro.ir/developers"),
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
bot.send_message(12345, "Deployment report", embeds=[card], components=[row])
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
@bot.component("approve")
|
|
166
|
+
def approve(interaction):
|
|
167
|
+
interaction.update_message(content="✅ Approved", components=[])
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
menu = ActionRow(
|
|
171
|
+
SelectMenu(
|
|
172
|
+
"choose_colour",
|
|
173
|
+
[
|
|
174
|
+
SelectOption("Red", "red"),
|
|
175
|
+
SelectOption("Green", "green"),
|
|
176
|
+
SelectOption("Blue", "blue"),
|
|
177
|
+
],
|
|
178
|
+
placeholder="Choose a colour",
|
|
179
|
+
)
|
|
180
|
+
)
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
A component callback can inspect `interaction.custom_id`, `interaction.values`, `interaction.user`, and `interaction.message`. Answer it promptly (Necro expects the initial acknowledgement within five seconds):
|
|
184
|
+
|
|
185
|
+
```python
|
|
186
|
+
@bot.component("choose_colour")
|
|
187
|
+
def choose_colour(interaction):
|
|
188
|
+
selected = ", ".join(interaction.values)
|
|
189
|
+
interaction.respond(f"You selected: {selected}", ephemeral=True)
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## Deferred interaction responses
|
|
193
|
+
|
|
194
|
+
For work that takes longer, acknowledge the interaction first:
|
|
195
|
+
|
|
196
|
+
```python
|
|
197
|
+
@bot.command(name="report", description="Build a report")
|
|
198
|
+
def report(interaction):
|
|
199
|
+
interaction.defer(ephemeral=True)
|
|
200
|
+
result = build_report()
|
|
201
|
+
interaction.edit_original(f"Report ready: {result}")
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
Other interaction helpers:
|
|
205
|
+
|
|
206
|
+
- `interaction.respond(content, ephemeral=False, embeds=None, components=None)` — initial response.
|
|
207
|
+
- `interaction.defer(ephemeral=False, update=False)` — acknowledge and continue later.
|
|
208
|
+
- `interaction.edit_original(..., files=None)` — edit the deferred response or original reply (supports file uploads).
|
|
209
|
+
- `interaction.update_message(...)` — acknowledge a component click by editing its message.
|
|
210
|
+
- `interaction.followup(content, ephemeral=False, embeds=None, files=None)` — send a follow-up message (supports file uploads).
|
|
211
|
+
- `interaction.delete_original()` — delete the original response.
|
|
212
|
+
|
|
213
|
+
## File uploads
|
|
214
|
+
|
|
215
|
+
The REST API accepts up to four files per message, each up to 8 MB:
|
|
216
|
+
|
|
217
|
+
```python
|
|
218
|
+
bot.send_message(
|
|
219
|
+
12345,
|
|
220
|
+
"Here is the report",
|
|
221
|
+
files=["report.pdf", "chart.png"],
|
|
222
|
+
)
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
You can also pass open binary file objects or `(filename, file_object, content_type)` tuples. The library opens and closes file paths itself; file objects you pass remain owned by your code.
|
|
226
|
+
|
|
227
|
+
## Server, channel, member, and role methods
|
|
228
|
+
|
|
229
|
+
The high-level `Bot` provides:
|
|
230
|
+
|
|
231
|
+
```python
|
|
232
|
+
bot.get_me()
|
|
233
|
+
bot.get_gateway()
|
|
234
|
+
bot.list_servers(after=None, limit=100)
|
|
235
|
+
bot.get_server(server_id)
|
|
236
|
+
bot.leave_server(server_id)
|
|
237
|
+
bot.list_channels(server_id)
|
|
238
|
+
bot.list_roles(server_id)
|
|
239
|
+
bot.list_members(server_id, after=None, limit=100)
|
|
240
|
+
bot.get_member(server_id, user_id)
|
|
241
|
+
bot.add_role(server_id, user_id, role_id)
|
|
242
|
+
bot.remove_role(server_id, user_id, role_id)
|
|
243
|
+
bot.get_channel(channel_id)
|
|
244
|
+
bot.get_messages(channel_id, limit=50, before=None, after=None)
|
|
245
|
+
bot.get_message(channel_id, message_id)
|
|
246
|
+
bot.send_message(channel_id, content, ...)
|
|
247
|
+
bot.edit_message(channel_id, message_id, content, ...)
|
|
248
|
+
bot.delete_message(channel_id, message_id)
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
`get_messages()` returns `Message` objects. Each message has `reply()`, `edit()`, and `delete()` methods, as well as `author`, `content`, `attachments`, `embeds`, `components`, and ID properties.
|
|
252
|
+
|
|
253
|
+
For less common endpoints or full control, use `bot.rest` (a public `RESTClient` instance).
|
|
254
|
+
|
|
255
|
+
## Channel webhooks
|
|
256
|
+
|
|
257
|
+
A webhook is separate from a bot connection and can only post into its configured channel:
|
|
258
|
+
|
|
259
|
+
```python
|
|
260
|
+
from necropy import Webhook
|
|
261
|
+
|
|
262
|
+
hook = Webhook("https://necro.ir/api/webhooks/ID/TOKEN")
|
|
263
|
+
hook.send("Nightly backup finished", username="Backup", wait=True)
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
Webhook URLs are credentials. Do not commit them to source control. Necro webhooks do not support file uploads or message components.
|
|
267
|
+
|
|
268
|
+
## Events
|
|
269
|
+
|
|
270
|
+
Register event handlers with `@bot.event`:
|
|
271
|
+
|
|
272
|
+
| Decorator function | Necro gateway event | Callback argument |
|
|
273
|
+
|---|---|---|
|
|
274
|
+
| `on_ready` | `ready` | raw event dictionary |
|
|
275
|
+
| `on_server_create` | `serverCreate` | raw event dictionary |
|
|
276
|
+
| `on_server_delete` | `serverDelete` | raw event dictionary |
|
|
277
|
+
| `on_message` | `messageCreate` | `Message` |
|
|
278
|
+
| `on_message_update` | `messageUpdate` | `Message` |
|
|
279
|
+
| `on_message_delete` | `messageDelete` | raw event dictionary |
|
|
280
|
+
| `on_member_join` | `memberJoin` | raw event dictionary |
|
|
281
|
+
| `on_member_leave` | `memberLeave` | raw event dictionary |
|
|
282
|
+
| `on_interaction` | `interactionCreate` | `Interaction` |
|
|
283
|
+
|
|
284
|
+
You can subscribe to another gateway event with `@bot.listen("eventName")`. Async handlers (`async def`) are accepted, but the REST methods are synchronous and will block while their HTTP request is in progress.
|
|
285
|
+
|
|
286
|
+
## Errors
|
|
287
|
+
|
|
288
|
+
API errors raise `HTTPException`, with these useful attributes:
|
|
289
|
+
|
|
290
|
+
- `status` / `status_code`: HTTP status code.
|
|
291
|
+
- `code`: Necro's machine-readable error code (for example `MISSING_PERMISSIONS` or `RATE_LIMITED`).
|
|
292
|
+
- `message`: human-readable message.
|
|
293
|
+
- `retry_after`: rate-limit delay in seconds, when supplied by Necro.
|
|
294
|
+
- `response_data`: decoded error body.
|
|
295
|
+
|
|
296
|
+
The client does not silently retry rate-limited requests. Handle `HTTPException` and respect `retry_after` when retrying.
|
|
297
|
+
|
|
298
|
+
## Important platform limitations
|
|
299
|
+
|
|
300
|
+
Necro's bot API does not expose private conversations, group chats, voice calls, online status, phone numbers, or email addresses. Bots only receive messages from channels they can see. Do not make bots reply to other bots; the gateway also echoes your own bot's messages.
|
|
301
|
+
|
|
302
|
+
## Development and tests
|
|
303
|
+
|
|
304
|
+
```bash
|
|
305
|
+
python -m pip install -e ".[dev]"
|
|
306
|
+
python -m pytest -q
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
The first release is intentionally synchronous and focused on the documented Bot API. Run the tests with `python -m pytest -q`. The project is not affiliated with or endorsed by Necro.
|
|
310
|
+
|
|
311
|
+
## References
|
|
312
|
+
|
|
313
|
+
- [Necro developer documentation](https://necro.ir/developers)
|
|
314
|
+
- [Socket.IO Python client](https://python-socketio.readthedocs.io/)
|
necropy-0.1.0/README.md
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
# necropy
|
|
2
|
+
|
|
3
|
+
Persian documentation is included in `README.fa.md` in the source archive.
|
|
4
|
+
|
|
5
|
+
**necropy** is a Python SDK for building bots on [Necro](https://necro.ir). It wraps Necro's Bot REST API and Socket.IO event gateway, and includes helpers for messages, slash commands, interactions, embeds, buttons, select menus, file uploads, and channel webhooks.
|
|
6
|
+
|
|
7
|
+
> This is the first development release (`0.1.0`). The SDK is based on Necro's published developer documentation. It has been tested locally with mocked HTTP responses; connecting to the live service requires your own bot token.
|
|
8
|
+
|
|
9
|
+
## Requirements
|
|
10
|
+
|
|
11
|
+
- Python 3.10+
|
|
12
|
+
- A bot created in Necro → Settings → My Bots
|
|
13
|
+
- The bot token (keep it secret)
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
From the extracted source directory, install the package and its runtime dependencies with:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
python -m pip install .
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
When the project is published to the Python Package Index, it will also be installable with `pip install necropy`.
|
|
24
|
+
|
|
25
|
+
## Quick start
|
|
26
|
+
|
|
27
|
+
Set the token as an environment variable instead of putting it in your source code:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
# Linux / macOS
|
|
31
|
+
export NECRO_BOT_TOKEN="your-token"
|
|
32
|
+
|
|
33
|
+
# PowerShell
|
|
34
|
+
$env:NECRO_BOT_TOKEN="your-token"
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Create `bot.py`:
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
import os
|
|
41
|
+
from necropy import Bot
|
|
42
|
+
|
|
43
|
+
bot = Bot(os.environ["NECRO_BOT_TOKEN"])
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@bot.event
|
|
47
|
+
def on_ready(data):
|
|
48
|
+
user = data["user"]
|
|
49
|
+
print(f"Connected as {user['name']} in {len(data['servers'])} server(s)")
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@bot.event
|
|
53
|
+
def on_message(message):
|
|
54
|
+
# Necro sends the bot's own messages back to it too.
|
|
55
|
+
if message.author.is_bot:
|
|
56
|
+
return
|
|
57
|
+
|
|
58
|
+
if message.content.strip() == "!ping":
|
|
59
|
+
message.reply("pong 🏓")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
bot.run()
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Run it:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
python bot.py
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
The bot needs the **View Channels** and **Send Messages** permissions to read and reply in a channel. Only channels visible to the bot's role are delivered to the gateway.
|
|
72
|
+
|
|
73
|
+
## Slash commands
|
|
74
|
+
|
|
75
|
+
Decorate a callback with `@bot.command`, then publish the registered definitions with `sync_commands()`. Command callbacks receive an `Interaction` object.
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
from necropy import Bot
|
|
79
|
+
import os
|
|
80
|
+
|
|
81
|
+
bot = Bot(os.environ["NECRO_BOT_TOKEN"])
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
@bot.command(name="hello", description="Say hello")
|
|
85
|
+
def hello(interaction):
|
|
86
|
+
interaction.respond(f"سلام {interaction.user.name}!")
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@bot.command(
|
|
90
|
+
name="roll",
|
|
91
|
+
description="Roll a die",
|
|
92
|
+
options=[
|
|
93
|
+
{
|
|
94
|
+
"type": "integer",
|
|
95
|
+
"name": "sides",
|
|
96
|
+
"description": "Number of sides",
|
|
97
|
+
"required": False,
|
|
98
|
+
"minValue": 2,
|
|
99
|
+
"maxValue": 1000,
|
|
100
|
+
}
|
|
101
|
+
],
|
|
102
|
+
)
|
|
103
|
+
def roll(interaction):
|
|
104
|
+
import random
|
|
105
|
+
|
|
106
|
+
sides = interaction.options.get("sides", 6)
|
|
107
|
+
interaction.respond(f"🎲 {random.randint(1, sides)} (1–{sides})")
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
# Register globally (or pass server_id=123 for server-scoped commands).
|
|
111
|
+
bot.sync_commands()
|
|
112
|
+
bot.run()
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
`sync_commands()` replaces the commands in that scope. To add/replace each definition individually instead, call `sync_commands(replace=False)`. Necro supports a limited subset of Discord-style slash-command options; see its documentation for accepted option types and limits.
|
|
116
|
+
|
|
117
|
+
## Embeds, buttons, and select menus
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
from necropy import ActionRow, Bot, Button, ButtonStyle, Embed, SelectMenu, SelectOption
|
|
121
|
+
|
|
122
|
+
# A rich message
|
|
123
|
+
card = (
|
|
124
|
+
Embed(title="Build completed", description="The deployment is ready.", color=0x40D490)
|
|
125
|
+
.add_field("Status", "Success", inline=True)
|
|
126
|
+
.add_field("Environment", "Production", inline=True)
|
|
127
|
+
.set_footer("CI pipeline")
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
row = ActionRow(
|
|
131
|
+
Button("Approve", custom_id="approve", style=ButtonStyle.SUCCESS),
|
|
132
|
+
Button("Open docs", style=ButtonStyle.LINK, url="https://necro.ir/developers"),
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
bot.send_message(12345, "Deployment report", embeds=[card], components=[row])
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
@bot.component("approve")
|
|
139
|
+
def approve(interaction):
|
|
140
|
+
interaction.update_message(content="✅ Approved", components=[])
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
menu = ActionRow(
|
|
144
|
+
SelectMenu(
|
|
145
|
+
"choose_colour",
|
|
146
|
+
[
|
|
147
|
+
SelectOption("Red", "red"),
|
|
148
|
+
SelectOption("Green", "green"),
|
|
149
|
+
SelectOption("Blue", "blue"),
|
|
150
|
+
],
|
|
151
|
+
placeholder="Choose a colour",
|
|
152
|
+
)
|
|
153
|
+
)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
A component callback can inspect `interaction.custom_id`, `interaction.values`, `interaction.user`, and `interaction.message`. Answer it promptly (Necro expects the initial acknowledgement within five seconds):
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
@bot.component("choose_colour")
|
|
160
|
+
def choose_colour(interaction):
|
|
161
|
+
selected = ", ".join(interaction.values)
|
|
162
|
+
interaction.respond(f"You selected: {selected}", ephemeral=True)
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Deferred interaction responses
|
|
166
|
+
|
|
167
|
+
For work that takes longer, acknowledge the interaction first:
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
@bot.command(name="report", description="Build a report")
|
|
171
|
+
def report(interaction):
|
|
172
|
+
interaction.defer(ephemeral=True)
|
|
173
|
+
result = build_report()
|
|
174
|
+
interaction.edit_original(f"Report ready: {result}")
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
Other interaction helpers:
|
|
178
|
+
|
|
179
|
+
- `interaction.respond(content, ephemeral=False, embeds=None, components=None)` — initial response.
|
|
180
|
+
- `interaction.defer(ephemeral=False, update=False)` — acknowledge and continue later.
|
|
181
|
+
- `interaction.edit_original(..., files=None)` — edit the deferred response or original reply (supports file uploads).
|
|
182
|
+
- `interaction.update_message(...)` — acknowledge a component click by editing its message.
|
|
183
|
+
- `interaction.followup(content, ephemeral=False, embeds=None, files=None)` — send a follow-up message (supports file uploads).
|
|
184
|
+
- `interaction.delete_original()` — delete the original response.
|
|
185
|
+
|
|
186
|
+
## File uploads
|
|
187
|
+
|
|
188
|
+
The REST API accepts up to four files per message, each up to 8 MB:
|
|
189
|
+
|
|
190
|
+
```python
|
|
191
|
+
bot.send_message(
|
|
192
|
+
12345,
|
|
193
|
+
"Here is the report",
|
|
194
|
+
files=["report.pdf", "chart.png"],
|
|
195
|
+
)
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
You can also pass open binary file objects or `(filename, file_object, content_type)` tuples. The library opens and closes file paths itself; file objects you pass remain owned by your code.
|
|
199
|
+
|
|
200
|
+
## Server, channel, member, and role methods
|
|
201
|
+
|
|
202
|
+
The high-level `Bot` provides:
|
|
203
|
+
|
|
204
|
+
```python
|
|
205
|
+
bot.get_me()
|
|
206
|
+
bot.get_gateway()
|
|
207
|
+
bot.list_servers(after=None, limit=100)
|
|
208
|
+
bot.get_server(server_id)
|
|
209
|
+
bot.leave_server(server_id)
|
|
210
|
+
bot.list_channels(server_id)
|
|
211
|
+
bot.list_roles(server_id)
|
|
212
|
+
bot.list_members(server_id, after=None, limit=100)
|
|
213
|
+
bot.get_member(server_id, user_id)
|
|
214
|
+
bot.add_role(server_id, user_id, role_id)
|
|
215
|
+
bot.remove_role(server_id, user_id, role_id)
|
|
216
|
+
bot.get_channel(channel_id)
|
|
217
|
+
bot.get_messages(channel_id, limit=50, before=None, after=None)
|
|
218
|
+
bot.get_message(channel_id, message_id)
|
|
219
|
+
bot.send_message(channel_id, content, ...)
|
|
220
|
+
bot.edit_message(channel_id, message_id, content, ...)
|
|
221
|
+
bot.delete_message(channel_id, message_id)
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
`get_messages()` returns `Message` objects. Each message has `reply()`, `edit()`, and `delete()` methods, as well as `author`, `content`, `attachments`, `embeds`, `components`, and ID properties.
|
|
225
|
+
|
|
226
|
+
For less common endpoints or full control, use `bot.rest` (a public `RESTClient` instance).
|
|
227
|
+
|
|
228
|
+
## Channel webhooks
|
|
229
|
+
|
|
230
|
+
A webhook is separate from a bot connection and can only post into its configured channel:
|
|
231
|
+
|
|
232
|
+
```python
|
|
233
|
+
from necropy import Webhook
|
|
234
|
+
|
|
235
|
+
hook = Webhook("https://necro.ir/api/webhooks/ID/TOKEN")
|
|
236
|
+
hook.send("Nightly backup finished", username="Backup", wait=True)
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
Webhook URLs are credentials. Do not commit them to source control. Necro webhooks do not support file uploads or message components.
|
|
240
|
+
|
|
241
|
+
## Events
|
|
242
|
+
|
|
243
|
+
Register event handlers with `@bot.event`:
|
|
244
|
+
|
|
245
|
+
| Decorator function | Necro gateway event | Callback argument |
|
|
246
|
+
|---|---|---|
|
|
247
|
+
| `on_ready` | `ready` | raw event dictionary |
|
|
248
|
+
| `on_server_create` | `serverCreate` | raw event dictionary |
|
|
249
|
+
| `on_server_delete` | `serverDelete` | raw event dictionary |
|
|
250
|
+
| `on_message` | `messageCreate` | `Message` |
|
|
251
|
+
| `on_message_update` | `messageUpdate` | `Message` |
|
|
252
|
+
| `on_message_delete` | `messageDelete` | raw event dictionary |
|
|
253
|
+
| `on_member_join` | `memberJoin` | raw event dictionary |
|
|
254
|
+
| `on_member_leave` | `memberLeave` | raw event dictionary |
|
|
255
|
+
| `on_interaction` | `interactionCreate` | `Interaction` |
|
|
256
|
+
|
|
257
|
+
You can subscribe to another gateway event with `@bot.listen("eventName")`. Async handlers (`async def`) are accepted, but the REST methods are synchronous and will block while their HTTP request is in progress.
|
|
258
|
+
|
|
259
|
+
## Errors
|
|
260
|
+
|
|
261
|
+
API errors raise `HTTPException`, with these useful attributes:
|
|
262
|
+
|
|
263
|
+
- `status` / `status_code`: HTTP status code.
|
|
264
|
+
- `code`: Necro's machine-readable error code (for example `MISSING_PERMISSIONS` or `RATE_LIMITED`).
|
|
265
|
+
- `message`: human-readable message.
|
|
266
|
+
- `retry_after`: rate-limit delay in seconds, when supplied by Necro.
|
|
267
|
+
- `response_data`: decoded error body.
|
|
268
|
+
|
|
269
|
+
The client does not silently retry rate-limited requests. Handle `HTTPException` and respect `retry_after` when retrying.
|
|
270
|
+
|
|
271
|
+
## Important platform limitations
|
|
272
|
+
|
|
273
|
+
Necro's bot API does not expose private conversations, group chats, voice calls, online status, phone numbers, or email addresses. Bots only receive messages from channels they can see. Do not make bots reply to other bots; the gateway also echoes your own bot's messages.
|
|
274
|
+
|
|
275
|
+
## Development and tests
|
|
276
|
+
|
|
277
|
+
```bash
|
|
278
|
+
python -m pip install -e ".[dev]"
|
|
279
|
+
python -m pytest -q
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
The first release is intentionally synchronous and focused on the documented Bot API. Run the tests with `python -m pytest -q`. The project is not affiliated with or endorsed by Necro.
|
|
283
|
+
|
|
284
|
+
## References
|
|
285
|
+
|
|
286
|
+
- [Necro developer documentation](https://necro.ir/developers)
|
|
287
|
+
- [Socket.IO Python client](https://python-socketio.readthedocs.io/)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "necropy"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "کتابخانه بات سازی در پیامرسان نکرو"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = {text = "MIT"}
|
|
12
|
+
authors = [{name = "necropy contributors"}]
|
|
13
|
+
keywords = ["necro", "necropy", "bot", "messaging", "sdk"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: 3.13",
|
|
24
|
+
]
|
|
25
|
+
dependencies = [
|
|
26
|
+
"requests>=2.31.0,<3.0.0",
|
|
27
|
+
"python-socketio[client]>=5.11.0,<6.0.0",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Homepage = "https://necro.ir/developers"
|
|
32
|
+
Documentation = "https://necro.ir/developers"
|
|
33
|
+
|
|
34
|
+
[project.optional-dependencies]
|
|
35
|
+
dev = ["pytest>=8.0"]
|
|
36
|
+
|
|
37
|
+
[tool.setuptools]
|
|
38
|
+
package-dir = {"" = "src"}
|
|
39
|
+
|
|
40
|
+
[tool.setuptools.packages.find]
|
|
41
|
+
where = ["src"]
|
|
42
|
+
|
|
43
|
+
[tool.pytest.ini_options]
|
|
44
|
+
pythonpath = ["src"]
|
|
45
|
+
testpaths = ["tests"]
|
necropy-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""necropy: Python tools for building bots on Necro."""
|
|
2
|
+
|
|
3
|
+
from .bot import Bot
|
|
4
|
+
from .components import ActionRow, Button, ButtonStyle, Embed, SelectMenu, SelectOption
|
|
5
|
+
from .exceptions import ClientClosedError, GatewayException, HTTPException, NecroError
|
|
6
|
+
from .http import API_PREFIX, API_URL, RESTClient
|
|
7
|
+
from .models import Interaction, Message, User
|
|
8
|
+
from .webhook import Webhook
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"API_PREFIX",
|
|
12
|
+
"API_URL",
|
|
13
|
+
"ActionRow",
|
|
14
|
+
"Bot",
|
|
15
|
+
"Button",
|
|
16
|
+
"ButtonStyle",
|
|
17
|
+
"ClientClosedError",
|
|
18
|
+
"Embed",
|
|
19
|
+
"GatewayException",
|
|
20
|
+
"HTTPException",
|
|
21
|
+
"Interaction",
|
|
22
|
+
"Message",
|
|
23
|
+
"NecroError",
|
|
24
|
+
"RESTClient",
|
|
25
|
+
"SelectMenu",
|
|
26
|
+
"SelectOption",
|
|
27
|
+
"User",
|
|
28
|
+
"Webhook",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
__version__ = "0.1.0"
|