@noctaly/sdk 1.0.0

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Noctaly
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.
package/README.md ADDED
@@ -0,0 +1,401 @@
1
+ # @noctaly/sdk
2
+
3
+ Official Node.js SDK for the [Noctaly](https://noctaly.com) public API.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @noctaly/sdk
9
+ # or
10
+ pnpm add @noctaly/sdk
11
+ # or
12
+ yarn add @noctaly/sdk
13
+ ```
14
+
15
+ > **Requires Node.js ≥ 18**
16
+
17
+ ## Quick Start
18
+
19
+ ```ts
20
+ import { NoctalyClient } from "@noctaly/sdk";
21
+
22
+ const noctaly = new NoctalyClient({ apiKey: "your-api-key" });
23
+
24
+ const user = noctaly.guilds("GUILD_ID").users("USER_ID");
25
+
26
+ // Get leveling profile
27
+ const { data: profile } = await user.getLevelProfile();
28
+ console.log(`Level ${profile.level} - ${profile.totalXp} total XP`);
29
+
30
+ // Give 500 XP (with server multipliers applied)
31
+ const { data: xpResult } = await user.updateXP({ xp: 500, multiply: true });
32
+ console.log(`XP given: ${xpResult.xp}`);
33
+ ```
34
+
35
+ ---
36
+
37
+ ## API Reference
38
+
39
+ ### `new NoctalyClient(options)`
40
+
41
+ | Option | Type | Default | Description |
42
+ |--------------|-----------|----------------------------------|-----------------------------------------------------------------------|
43
+ | `apiKey` | `string` | - | Your Noctaly API key. |
44
+ | `baseURL` | `string` | `https://noctaly.com/api/v1` | Override the base URL. Useful for staging environments. |
45
+ | `retry` | `boolean` | `true` | Automatically retry requests that receive a `429` rate-limit response. |
46
+ | `maxRetries` | `number` | `3` | Maximum number of automatic retries before the error is thrown. |
47
+ | `timeout` | `number` | `30000` | Request timeout in milliseconds. Set to `0` to disable. |
48
+
49
+ When `retry` is `true`, the SDK waits the `retryAfter` duration from the API response before each retry, capped at 60 seconds.
50
+
51
+ ---
52
+
53
+ ### `client.guilds(guildID)` → `GuildResource`
54
+
55
+ Returns a `GuildResource` scoped to the given Discord guild.
56
+
57
+ #### `guild.users(userID)` → `UserResource`
58
+
59
+ Returns a `UserResource` scoped to the given guild member.
60
+
61
+ All methods return `Promise<NoctalyResponse<T>>`:
62
+
63
+ ```ts
64
+ interface NoctalyResponse<T> {
65
+ data: T;
66
+ rateLimit?: RateLimitInfo;
67
+ }
68
+ ```
69
+
70
+ ---
71
+
72
+ ### UserResource
73
+
74
+ #### `user.getLevelProfile()`
75
+
76
+ ```
77
+ GET /guilds/{guildID}/users/{userID}/level-profile
78
+ ```
79
+
80
+ ```ts
81
+ const { data } = await user.getLevelProfile();
82
+ // data: LevelProfile
83
+ ```
84
+
85
+ | Field | Type | Description |
86
+ |----------------|----------|-------------------------------------------|
87
+ | `level` | `number` | Current level. |
88
+ | `xp` | `number` | XP accumulated in the current level. |
89
+ | `neededXp` | `number` | Remaining XP needed to reach next level. |
90
+ | `totalXp` | `number` | Total XP across all levels. |
91
+ | `messages` | `number` | Total messages sent. |
92
+ | `voiceMinutes` | `number` | Total minutes spent in voice channels. |
93
+ | `reactions` | `number` | Total reactions added. |
94
+
95
+ ---
96
+
97
+ #### `user.getEcoProfile(options?)`
98
+
99
+ ```
100
+ GET /guilds/{guildID}/users/{userID}/eco-profile
101
+ ```
102
+
103
+ Only `money` is returned by default. Pass options to include optional relations.
104
+ The return type is narrowed automatically based on the options you pass.
105
+
106
+ ```ts
107
+ // Basic
108
+ const { data } = await user.getEcoProfile();
109
+ // data: { money: number }
110
+
111
+ // With all relations
112
+ const { data } = await user.getEcoProfile({ items: true, chests: true, dailyStreak: true });
113
+ // data: { money: number, items: UserItem[], chests: UserChest[], dailyStreak: DailyStreak | null }
114
+ ```
115
+
116
+ | Option | Type | Description |
117
+ |---------------|-----------|----------------------------------------|
118
+ | `items` | `boolean` | Include the member's item inventory. |
119
+ | `chests` | `boolean` | Include the member's chest inventory. |
120
+ | `dailyStreak` | `boolean` | Include the member's daily streak. |
121
+
122
+ ---
123
+
124
+ #### `user.updateXP(body)`
125
+
126
+ ```
127
+ POST /guilds/{guildID}/users/{userID}/xp
128
+ ```
129
+
130
+ ```ts
131
+ const { data } = await user.updateXP({ xp: 1000, multiply: true });
132
+ // data: { xp: number } ← actual XP given after multipliers
133
+ ```
134
+
135
+ | Field | Type | Required | Description |
136
+ |------------|-----------|----------|--------------------------------------------------------------|
137
+ | `xp` | `number` | ✓ | XP to give (positive) or remove (negative). Range: ±100 M. |
138
+ | `multiply` | `boolean` | | Apply server and booster-role multipliers. Default: `false`. |
139
+
140
+ ---
141
+
142
+ #### `user.updateLevel(body)`
143
+
144
+ ```
145
+ POST /guilds/{guildID}/users/{userID}/level
146
+ ```
147
+
148
+ ```ts
149
+ const { data } = await user.updateLevel({ level: 5 });
150
+ // data: { level: number }
151
+ ```
152
+
153
+ | Field | Type | Required | Description |
154
+ |---------|----------|----------|---------------------------------------------------------|
155
+ | `level` | `number` | ✓ | Levels to give (positive) or remove (negative). ±1 000. |
156
+
157
+ ---
158
+
159
+ #### `user.updateMoney(body)`
160
+
161
+ ```
162
+ POST /guilds/{guildID}/users/{userID}/money
163
+ ```
164
+
165
+ ```ts
166
+ const { data } = await user.updateMoney({ money: 5000, multiply: true });
167
+ // data: { money: number }
168
+ ```
169
+
170
+ | Field | Type | Required | Description |
171
+ |------------|-----------|----------|-----------------------------------------------------------------|
172
+ | `money` | `number` | ✓ | Money to give (positive) or remove (negative). Range: ±1 000 M. |
173
+ | `multiply` | `boolean` | | Apply server and booster-role multipliers. Default: `false`. |
174
+
175
+ ---
176
+
177
+ ### UserItemsResource - `user.items`
178
+
179
+ #### `user.items.add(body)`
180
+
181
+ ```
182
+ POST /guilds/{guildID}/users/{userID}/items
183
+ ```
184
+
185
+ ```ts
186
+ const { data } = await user.items.add({ itemID: "uuid", quantity: 3 });
187
+ // data: { quantity: number } ← new total quantity in inventory
188
+ ```
189
+
190
+ #### `user.items.remove(body)`
191
+
192
+ ```
193
+ DELETE /guilds/{guildID}/users/{userID}/items
194
+ ```
195
+
196
+ ```ts
197
+ const { data } = await user.items.remove({ itemID: "uuid", quantity: 1 });
198
+ // data: { quantity: number } ← remaining quantity (0 if fully removed)
199
+ ```
200
+
201
+ #### `user.items.set(body)`
202
+
203
+ ```
204
+ PUT /guilds/{guildID}/users/{userID}/items
205
+ ```
206
+
207
+ ```ts
208
+ const { data } = await user.items.set({ itemID: "uuid", quantity: 10 });
209
+ // Passing 0 removes the item entirely.
210
+ // data: { quantity: number }
211
+ ```
212
+
213
+ #### `user.items.use(itemID, body?)`
214
+
215
+ ```
216
+ POST /guilds/{guildID}/users/{userID}/items/{itemID}/use
217
+ ```
218
+
219
+ Triggers the item's configured actions. Only works for `CUSTOM` items with at least one action. The item is consumed unless it has the `KEEP_AFTER_USE` flag.
220
+
221
+ ```ts
222
+ const { data } = await user.items.use("item-uuid", { quantity: 2 });
223
+ // data: UseItemResult
224
+ ```
225
+
226
+ `UseItemResult` fields:
227
+
228
+ | Field | Type | Description |
229
+ |-----------------|--------------------------|--------------------------------------------------------------------|
230
+ | `xp` | `number` | Total XP awarded (negative if removed). |
231
+ | `money` | `number` | Total money awarded (negative if removed). |
232
+ | `addedRoles` | `string[]` | Role IDs added to the member. |
233
+ | `removedRoles` | `string[]` | Role IDs removed from the member. |
234
+ | `addedItems` | `Record<string, number>` | Map of item UUID → quantity added. |
235
+ | `itemsRemoved` | `Record<string, number>` | Map of item UUID → quantity removed. |
236
+ | `addedChests` | `Record<string, number>` | Map of chest UUID → quantity added. |
237
+ | `chestsRemoved` | `Record<string, number>` | Map of chest UUID → quantity removed. |
238
+ | `rolesDuration` | `Record<string, number>` | Map of role ID → Unix timestamp when the temporary role expires. |
239
+
240
+ ---
241
+
242
+ ### UserChestsResource - `user.chests`
243
+
244
+ #### `user.chests.add(body)`
245
+
246
+ ```
247
+ POST /guilds/{guildID}/users/{userID}/chests
248
+ ```
249
+
250
+ ```ts
251
+ const { data } = await user.chests.add({ chestID: "uuid", quantity: 2 });
252
+ // data: { quantity: number }
253
+ ```
254
+
255
+ #### `user.chests.remove(body)`
256
+
257
+ ```
258
+ DELETE /guilds/{guildID}/users/{userID}/chests
259
+ ```
260
+
261
+ ```ts
262
+ const { data } = await user.chests.remove({ chestID: "uuid", quantity: 1 });
263
+ // data: { quantity: number }
264
+ ```
265
+
266
+ #### `user.chests.set(body)`
267
+
268
+ ```
269
+ PUT /guilds/{guildID}/users/{userID}/chests
270
+ ```
271
+
272
+ ```ts
273
+ const { data } = await user.chests.set({ chestID: "uuid", quantity: 0 });
274
+ // Passing 0 removes the chest entirely.
275
+ // data: { quantity: number }
276
+ ```
277
+
278
+ ---
279
+
280
+ ### GuildItemsResource - `guild.items`
281
+
282
+ #### `guild.items.list()`
283
+
284
+ ```
285
+ GET /guilds/{guildID}/items
286
+ ```
287
+
288
+ ```ts
289
+ const { data } = await guild.items.list();
290
+ // data: { items: Item[] }
291
+ ```
292
+
293
+ `Item` fields:
294
+
295
+ | Field | Type | Description |
296
+ |-----------------|-------------------|------------------------------------------------|
297
+ | `id` | `string` | UUID of the item. |
298
+ | `name` | `string` | Display name. |
299
+ | `description` | `string \| null` | Optional description. |
300
+ | `type` | `string` | Item type (e.g. `"CUSTOM"`). |
301
+ | `flags` | `string[]` | Item flags (e.g. `["KEEP_AFTER_USE"]`). |
302
+ | `emoji` | `string \| null` | Emoji string. |
303
+ | `emojiType` | `"UNICODE" \| "CUSTOM" \| null` | Emoji type. |
304
+ | `iconURL` | `string \| null` | Custom icon URL. |
305
+ | `buyPrice` | `number` | Buy price. |
306
+ | `sellPrice` | `number` | Sell price. |
307
+ | `cooldown` | `number` | Cooldown between uses in seconds (`0` = none). |
308
+ | `durability` | `number \| null` | Full durability per item (`0` = no durability).|
309
+ | `quantity` | `number \| null` | Quantity in the shop (`null` = unlimited). |
310
+ | `quantityLimit` | `number \| null` | Per-member quantity limit (`null` = unlimited).|
311
+
312
+ ---
313
+
314
+ ### GuildChestsResource - `guild.chests`
315
+
316
+ #### `guild.chests.list()`
317
+
318
+ ```
319
+ GET /guilds/{guildID}/chests
320
+ ```
321
+
322
+ ```ts
323
+ const { data } = await guild.chests.list();
324
+ // data: { chests: Chest[] }
325
+ ```
326
+
327
+ `Chest` fields:
328
+
329
+ | Field | Type | Description |
330
+ |------------------|------------------|-------------------------------------------------|
331
+ | `id` | `string` | UUID of the chest. |
332
+ | `name` | `string` | Display name. |
333
+ | `primaryColor` | `string` | Primary hex color (without `#`). |
334
+ | `secondaryColor` | `string` | Secondary hex color (without `#`). |
335
+ | `iconURL` | `string \| null` | Custom icon URL. |
336
+ | `flags` | `string[]` | Chest flags (e.g. `["ANIMATED"]`). |
337
+ | `buyPrice` | `number` | Buy price. |
338
+ | `sellPrice` | `number` | Sell price. |
339
+ | `itemDrawCount` | `number` | Number of items drawn when the chest is opened. |
340
+ | `quantity` | `number \| null` | Quantity in the shop (`null` = unlimited). |
341
+ | `quantityLimit` | `number \| null` | Per-member quantity limit (`null` = unlimited). |
342
+
343
+ ---
344
+
345
+ ## Error Handling
346
+
347
+ All methods throw a `NoctalyError` on non-2xx responses.
348
+
349
+ ```ts
350
+ import { NoctalyError } from "@noctaly/sdk";
351
+
352
+ try {
353
+ await user.updateXP({ xp: 500 });
354
+ } catch (err) {
355
+ if (err instanceof NoctalyError) {
356
+ console.error(err.code); // "rate_limited" | "not_found" | …
357
+ console.error(err.status); // 429 | 404 | …
358
+ console.error(err.message); // Human-readable message
359
+ console.error(err.retryAfter); // seconds (rate_limited only)
360
+ console.error(err.global); // boolean (rate_limited only)
361
+ console.error(err.details); // ValidationIssue[] (validation_error only)
362
+ }
363
+ }
364
+ ```
365
+
366
+ | Code | Status | Description |
367
+ |--------------------|--------|--------------------------------------------------------|
368
+ | `unauthenticated` | 401 | No API key provided. |
369
+ | `unauthorized` | 403 | API key doesn't have access to this guild. |
370
+ | `rate_limited` | 429 | Rate limit exceeded. Check `retryAfter` and `global`. |
371
+ | `not_found` | 404 | The requested member or resource was not found. |
372
+ | `module_disabled` | 400 | The relevant module is disabled in this guild. |
373
+ | `validation_error` | 400 | Invalid request body. Check `details` for field errors.|
374
+ | `unexpected_error` | 500 | Server-side error. |
375
+
376
+ ---
377
+
378
+ ## Rate Limits
379
+
380
+ Every successful response exposes the endpoint's rate-limit info:
381
+
382
+ ```ts
383
+ const { data, rateLimit } = await user.getLevelProfile();
384
+
385
+ if (rateLimit) {
386
+ console.log(`${rateLimit.remaining}/${rateLimit.limit} remaining`);
387
+ console.log(`Resets at ${new Date(rateLimit.reset * 1000).toISOString()}`);
388
+ console.log(`Resets in ${rateLimit.resetAfter}s`);
389
+ }
390
+ ```
391
+
392
+ | Field | Type | Description |
393
+ |--------------|----------|--------------------------------------------------|
394
+ | `limit` | `number` | Maximum requests allowed in the window. |
395
+ | `remaining` | `number` | Requests remaining in the current window. |
396
+ | `reset` | `number` | Unix timestamp (seconds) when the window resets. |
397
+ | `resetAfter` | `number` | Seconds until the window resets. |
398
+
399
+ The global rate limit is **50 req/s** across all routes. Exceeding it results in a `NoctalyError` with `code: "rate_limited"` and `global: true`.
400
+
401
+ When `retry: true` (the default), the SDK automatically handles `429` responses by waiting `retryAfter` seconds and retrying up to `maxRetries` times before throwing.