spearkit 0.1.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 +92 -0
- package/README.md +228 -0
- package/dist/index.cjs +1242 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +849 -0
- package/dist/index.d.ts +849 -0
- package/dist/index.js +1190 -0
- package/dist/index.js.map +1 -0
- package/package.json +64 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
Copyright (c) 2026 byigitt
|
|
2
|
+
|
|
3
|
+
spearkit is licensed under the PolyForm Noncommercial License 1.0.0.
|
|
4
|
+
|
|
5
|
+
In plain terms: you may use, copy, modify, and distribute spearkit FREE OF
|
|
6
|
+
CHARGE for any noncommercial purpose — personal projects, learning, research,
|
|
7
|
+
experimentation, and use by nonprofit/educational/government organizations.
|
|
8
|
+
|
|
9
|
+
You may NOT use spearkit for a commercial purpose. In particular, you may not
|
|
10
|
+
use it in software, products, or projects that you sell, that are built for
|
|
11
|
+
commercial advantage, or that are intended to generate monetary compensation,
|
|
12
|
+
without obtaining a separate commercial license from the author.
|
|
13
|
+
|
|
14
|
+
For a commercial license, contact the author via https://github.com/byigitt.
|
|
15
|
+
|
|
16
|
+
Required Notice: Copyright (c) 2026 byigitt (https://github.com/byigitt)
|
|
17
|
+
|
|
18
|
+
--------------------------------------------------------------------------------
|
|
19
|
+
|
|
20
|
+
# PolyForm Noncommercial License 1.0.0
|
|
21
|
+
|
|
22
|
+
<https://polyformproject.org/licenses/noncommercial/1.0.0>
|
|
23
|
+
|
|
24
|
+
## Acceptance
|
|
25
|
+
|
|
26
|
+
In order to get any license under these terms, you must agree to them as both strict obligations and conditions to all your licenses.
|
|
27
|
+
|
|
28
|
+
## Copyright License
|
|
29
|
+
|
|
30
|
+
The licensor grants you a copyright license for the software to do everything you might do with the software that would otherwise infringe the licensor's copyright in it for any permitted purpose. However, you may only distribute the software according to [Distribution License](#distribution-license) and make changes or new works based on the software according to [Changes and New Works License](#changes-and-new-works-license).
|
|
31
|
+
|
|
32
|
+
## Distribution License
|
|
33
|
+
|
|
34
|
+
The licensor grants you an additional copyright license to distribute copies of the software. Your license to distribute covers distributing the software with changes and new works permitted by [Changes and New Works License](#changes-and-new-works-license).
|
|
35
|
+
|
|
36
|
+
## Notices
|
|
37
|
+
|
|
38
|
+
You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms or the URL for them above, as well as copies of any plain-text lines beginning with `Required Notice:` that the licensor provided with the software. For example:
|
|
39
|
+
|
|
40
|
+
> Required Notice: Copyright Yoyodyne, Inc. (http://example.com)
|
|
41
|
+
|
|
42
|
+
## Changes and New Works License
|
|
43
|
+
|
|
44
|
+
The licensor grants you an additional copyright license to make changes and new works based on the software for any permitted purpose.
|
|
45
|
+
|
|
46
|
+
## Patent License
|
|
47
|
+
|
|
48
|
+
The licensor grants you a patent license for the software that covers patent claims the licensor can license, or becomes able to license, that you would infringe by using the software.
|
|
49
|
+
|
|
50
|
+
## Noncommercial Purposes
|
|
51
|
+
|
|
52
|
+
Any noncommercial purpose is a permitted purpose.
|
|
53
|
+
|
|
54
|
+
## Personal Uses
|
|
55
|
+
|
|
56
|
+
Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, is use for a permitted purpose.
|
|
57
|
+
|
|
58
|
+
## Noncommercial Organizations
|
|
59
|
+
|
|
60
|
+
Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution is use for a permitted purpose regardless of the source of funding or obligations resulting from the funding.
|
|
61
|
+
|
|
62
|
+
## Fair Use
|
|
63
|
+
|
|
64
|
+
You may have "fair use" rights for the software under the law. These terms do not limit them.
|
|
65
|
+
|
|
66
|
+
## No Other Rights
|
|
67
|
+
|
|
68
|
+
These terms do not allow you to sublicense or transfer any of your licenses to anyone else, or prevent the licensor from granting licenses to anyone else. These terms do not imply any other licenses.
|
|
69
|
+
|
|
70
|
+
## Patent Defense
|
|
71
|
+
|
|
72
|
+
If you make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
|
|
73
|
+
|
|
74
|
+
## Violations
|
|
75
|
+
|
|
76
|
+
The first time you are notified in writing that you have violated any of these terms, or done anything with the software not covered by your licenses, your licenses can nonetheless continue if you come into full compliance with these terms, and take practical steps to correct past violations, within 32 days of receiving notice. Otherwise, all your licenses end immediately.
|
|
77
|
+
|
|
78
|
+
## No Liability
|
|
79
|
+
|
|
80
|
+
***As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.***
|
|
81
|
+
|
|
82
|
+
## Definitions
|
|
83
|
+
|
|
84
|
+
The **licensor** is the individual or entity offering these terms, and the **software** is the software the licensor makes available under these terms.
|
|
85
|
+
|
|
86
|
+
**You** refers to the individual or entity agreeing to these terms.
|
|
87
|
+
|
|
88
|
+
**Your company** is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. **Control** means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
|
|
89
|
+
|
|
90
|
+
**Your licenses** are all the licenses granted to you for the software under these terms.
|
|
91
|
+
|
|
92
|
+
**Use** means anything you do with the software requiring one of your licenses.
|
package/README.md
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
# spearkit
|
|
2
|
+
|
|
3
|
+
**discord.js++** — a developer-experience-first layer over [discord.js](https://discord.js.org).
|
|
4
|
+
|
|
5
|
+
spearkit re-exports the entire discord.js surface (so it's a drop-in replacement) and
|
|
6
|
+
adds an ergonomic, **fully type-safe** API for the things that are tedious in raw
|
|
7
|
+
discord.js: setting up events, defining slash commands, and wiring interactive
|
|
8
|
+
components. No `any`, no `unknown` leaking into your handlers — option values,
|
|
9
|
+
custom-id params and modal fields are all inferred.
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install spearkit discord.js
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Documentation
|
|
16
|
+
|
|
17
|
+
- **Docs site** ([`website/`](./website)) — a [Fumadocs](https://fumadocs.dev) site themed like the discord.js docs. Run it with `cd website && pnpm install && pnpm dev`.
|
|
18
|
+
- **Guides & API reference** ([`docs/`](./docs)) — the Markdown the site is built from.
|
|
19
|
+
- **Examples** ([`examples/`](./examples)) — one folder per topic (commands, options, components, events, loading, …).
|
|
20
|
+
|
|
21
|
+
## Quick start
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { SpearClient, Intents, command, option, event } from "spearkit";
|
|
25
|
+
|
|
26
|
+
const client = new SpearClient({ intents: Intents.default });
|
|
27
|
+
|
|
28
|
+
const ping = command({
|
|
29
|
+
name: "ping",
|
|
30
|
+
description: "Check latency",
|
|
31
|
+
run: (ctx) => ctx.reply(`Pong! ${ctx.client.ws.ping}ms`),
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const ready = event("clientReady", (c) => console.log(`Online as ${c.user.tag}`));
|
|
35
|
+
|
|
36
|
+
client.register(ping, ready);
|
|
37
|
+
await client.start(process.env.DISCORD_TOKEN);
|
|
38
|
+
await client.deployCommands({ guildId: "YOUR_GUILD_ID" }); // instant in one guild
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Slash commands with inferred options
|
|
42
|
+
|
|
43
|
+
Option values are typed from your declaration. Required options are non-nullable;
|
|
44
|
+
optional ones are `T | undefined`; `choices` narrow to a literal union.
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { command, option } from "spearkit";
|
|
48
|
+
|
|
49
|
+
export default command({
|
|
50
|
+
name: "echo",
|
|
51
|
+
description: "Repeat a message",
|
|
52
|
+
options: {
|
|
53
|
+
text: option.string({ description: "What to say", required: true }),
|
|
54
|
+
times: option.integer({ description: "Repeat count", minValue: 1, maxValue: 5 }),
|
|
55
|
+
visibility: option.string({
|
|
56
|
+
description: "Who sees it",
|
|
57
|
+
choices: [
|
|
58
|
+
{ name: "Everyone", value: "public" },
|
|
59
|
+
{ name: "Just me", value: "private" },
|
|
60
|
+
],
|
|
61
|
+
}),
|
|
62
|
+
},
|
|
63
|
+
run: (ctx) => {
|
|
64
|
+
ctx.options.text; // string
|
|
65
|
+
ctx.options.times; // number | undefined
|
|
66
|
+
ctx.options.visibility; // "public" | "private" | undefined
|
|
67
|
+
return ctx.reply({
|
|
68
|
+
content: ctx.options.text.repeat(ctx.options.times ?? 1),
|
|
69
|
+
ephemeral: ctx.options.visibility === "private",
|
|
70
|
+
});
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Builders: `string`, `integer`, `number`, `boolean`, `user`, `channel`, `role`,
|
|
76
|
+
`mentionable`, `attachment`.
|
|
77
|
+
|
|
78
|
+
### Autocomplete
|
|
79
|
+
|
|
80
|
+
Co-locate the suggestion provider with the option:
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
option.string({
|
|
84
|
+
description: "Fruit",
|
|
85
|
+
required: true,
|
|
86
|
+
autocomplete: (ctx) =>
|
|
87
|
+
fruits.filter((f) => f.startsWith(ctx.value)).map((f) => ({ name: f, value: f })),
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Subcommands
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
import { commandGroup, subcommand, subcommandGroup, option } from "spearkit";
|
|
95
|
+
|
|
96
|
+
commandGroup({
|
|
97
|
+
name: "admin",
|
|
98
|
+
description: "Admin tools",
|
|
99
|
+
guildOnly: true,
|
|
100
|
+
subcommands: {
|
|
101
|
+
say: subcommand({
|
|
102
|
+
description: "Make the bot speak",
|
|
103
|
+
options: { message: option.string({ description: "Message", required: true }) },
|
|
104
|
+
run: (ctx) => ctx.reply(ctx.options.message),
|
|
105
|
+
}),
|
|
106
|
+
},
|
|
107
|
+
groups: {
|
|
108
|
+
users: subcommandGroup({
|
|
109
|
+
description: "Manage users",
|
|
110
|
+
subcommands: {
|
|
111
|
+
ban: subcommand({
|
|
112
|
+
description: "Ban a user",
|
|
113
|
+
options: { target: option.user({ description: "Who", required: true }) },
|
|
114
|
+
run: (ctx) => ctx.reply(`Banned ${ctx.options.target.tag}`),
|
|
115
|
+
}),
|
|
116
|
+
},
|
|
117
|
+
}),
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Interactive components
|
|
123
|
+
|
|
124
|
+
Define the component, its custom-id pattern and its handler in one place. Params
|
|
125
|
+
in the custom-id pattern (`{name}`) are typed everywhere — both in the handler's
|
|
126
|
+
`ctx.params` and in the `build()` call.
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
import { button, stringSelect, modal, textInput, row } from "spearkit";
|
|
130
|
+
|
|
131
|
+
const vote = button({
|
|
132
|
+
id: "vote:{choice}", // {choice} becomes a typed param
|
|
133
|
+
label: "Yes",
|
|
134
|
+
style: "Success",
|
|
135
|
+
run: (ctx) => ctx.update(`You chose ${ctx.params.choice}`), // ctx.params.choice: string
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const colour = stringSelect({
|
|
139
|
+
id: "colour",
|
|
140
|
+
placeholder: "Pick a colour",
|
|
141
|
+
options: [
|
|
142
|
+
{ label: "Red", value: "red" },
|
|
143
|
+
{ label: "Blue", value: "blue" },
|
|
144
|
+
],
|
|
145
|
+
run: (ctx) => ctx.reply({ content: ctx.values.join(", "), ephemeral: true }),
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const feedback = modal({
|
|
149
|
+
id: "feedback:{ticket}",
|
|
150
|
+
title: "Feedback",
|
|
151
|
+
fields: {
|
|
152
|
+
summary: textInput({ label: "Summary", required: true }),
|
|
153
|
+
detail: textInput({ label: "Details", style: "Paragraph" }),
|
|
154
|
+
},
|
|
155
|
+
run: (ctx) =>
|
|
156
|
+
// ctx.params.ticket: string, ctx.fields.summary / ctx.fields.detail: string
|
|
157
|
+
ctx.reply({ content: `#${ctx.params.ticket}: ${ctx.fields.summary}`, ephemeral: true }),
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
client.register(vote, colour, feedback);
|
|
161
|
+
|
|
162
|
+
// Use them in a message — build() requires exactly the params the pattern declares:
|
|
163
|
+
await channel.send({
|
|
164
|
+
content: "Choose:",
|
|
165
|
+
components: [row(vote.build({ choice: "yes" })), row(colour.build())],
|
|
166
|
+
});
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Component builders: `button`, `linkButton`, `stringSelect`, `userSelect`,
|
|
170
|
+
`roleSelect`, `channelSelect`, `mentionableSelect`, `modal` (+ `textInput`), `row`.
|
|
171
|
+
|
|
172
|
+
spearkit routes interactions automatically by the custom-id namespace and decodes
|
|
173
|
+
the params for you — no `interactionCreate` switch statements.
|
|
174
|
+
|
|
175
|
+
## File-based loading
|
|
176
|
+
|
|
177
|
+
Drop commands, events and components in a folder (default or named exports) and
|
|
178
|
+
load them all:
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
await client.load(new URL("./commands", import.meta.url).pathname);
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## Plugins
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
import { definePlugin } from "spearkit";
|
|
188
|
+
|
|
189
|
+
const moderation = definePlugin({
|
|
190
|
+
name: "moderation",
|
|
191
|
+
setup(client) {
|
|
192
|
+
client.register(/* commands, events, components */);
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
await client.use(moderation);
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Drop-in replacement
|
|
200
|
+
|
|
201
|
+
Everything discord.js exports is available from spearkit, so you can migrate
|
|
202
|
+
incrementally:
|
|
203
|
+
|
|
204
|
+
```ts
|
|
205
|
+
import { Client, EmbedBuilder, GatewayIntentBits } from "spearkit"; // all from discord.js
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## Error handling
|
|
209
|
+
|
|
210
|
+
Handler errors never crash the process. Customise the response:
|
|
211
|
+
|
|
212
|
+
```ts
|
|
213
|
+
client.commands.onError((error, interaction) => {
|
|
214
|
+
console.error(error);
|
|
215
|
+
return interaction.reply({ content: "Oops.", flags: 64 });
|
|
216
|
+
});
|
|
217
|
+
client.components.onError((error) => console.error(error));
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## License
|
|
221
|
+
|
|
222
|
+
[PolyForm Noncommercial License 1.0.0](./LICENSE).
|
|
223
|
+
|
|
224
|
+
spearkit is **free for noncommercial use** — personal projects, learning,
|
|
225
|
+
research, and use by nonprofit/educational/government organizations. You may
|
|
226
|
+
**not** use it in software or projects you sell or that are built for
|
|
227
|
+
commercial advantage without a separate commercial license. See [`LICENSE`](./LICENSE)
|
|
228
|
+
for the full terms, or open an issue to discuss a commercial license.
|