botflare 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Pongsakorn Thipayanate
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,139 @@
1
+ # botflare
2
+
3
+ [![NPM version](https://img.shields.io/npm/v/botflare?color=f38020&label=)](https://www.npmjs.com/package/botflare)
4
+
5
+ Build Discord bots that run on Cloudflare Workers, with types from the official Discord API spec.
6
+
7
+ [👉 Example bot](https://github.com/pyyupsk/botflare/tree/main/examples/basic) · [👉 Discord API spec](https://github.com/discord/discord-api-spec)
8
+
9
+ botflare is an unofficial library. It is not affiliated with, endorsed by, or sponsored by Discord Inc. or Cloudflare, Inc.
10
+
11
+ ## Features
12
+
13
+ - Slash commands, subcommands, context menu commands, buttons, select menus, modals and autocomplete
14
+ - Option values typed from the options you declare: a `choices` option gives only its values
15
+ - The Discord Gateway in a Durable Object, so a bot gets commands and events without a public URL
16
+ - `rest` for every Discord route, with path, query, body and result typed from the Discord API spec
17
+ - Cron jobs that post to channels, and `onError` for work that runs after Discord has its answer
18
+ - Messages ping nobody unless you allow it, even for `@everyone` in the text
19
+ - `botflare sync` sends your commands to Discord from the command line
20
+
21
+ ## Install
22
+
23
+ ```sh
24
+ npm install botflare
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ```ts
30
+ import { bot, command, option } from 'botflare';
31
+ import { gateway } from 'botflare/gateway';
32
+
33
+ const roll = command(
34
+ {
35
+ name: 'roll',
36
+ description: 'Roll a die',
37
+ options: { sides: option('integer', { description: 'Sides on the die', min: 2, max: 100 }) },
38
+ },
39
+ (c) => {
40
+ const sides = c.options.sides ?? 6; // number | undefined
41
+ const rolled = 1 + Math.floor(Math.random() * sides);
42
+
43
+ return c.reply(`You rolled ${rolled}`, {
44
+ buttons: [{ id: `again:${sides}`, label: 'Roll again', emoji: '🎲' }],
45
+ });
46
+ },
47
+ );
48
+
49
+ const app = bot({
50
+ commands: [roll],
51
+ buttons: {
52
+ again: (c) => c.update(`You rolled ${1 + Math.floor(Math.random() * Number(c.value))}`),
53
+ },
54
+ gateway: 'GATEWAY',
55
+ });
56
+
57
+ export const Gateway = gateway({ intents: ['Guilds'], bot: app, events: {} });
58
+
59
+ export default app;
60
+ ```
61
+
62
+ Bind the `Gateway` class in `wrangler.jsonc`:
63
+
64
+ ```jsonc
65
+ {
66
+ "durable_objects": { "bindings": [{ "name": "GATEWAY", "class_name": "Gateway" }] },
67
+ "migrations": [{ "tag": "v1", "new_sqlite_classes": ["Gateway"] }],
68
+ }
69
+ ```
70
+
71
+ Then run it:
72
+
73
+ ```sh
74
+ npx botflare sync src/index.ts # send the commands to Discord
75
+ npx wrangler dev # open the local URL once to start the gateway
76
+ ```
77
+
78
+ `botflare sync` reads `DISCORD_TOKEN`, `DISCORD_APPLICATION_ID` and, for one test server, `DISCORD_GUILD_ID`. The Worker reads `DISCORD_TOKEN`, and `DISCORD_PUBLIC_KEY` for interactions over HTTP.
79
+
80
+ ## API
81
+
82
+ | Export | Use |
83
+ | ------------------------------------- | ----------------------------------------------------------------------------------------- |
84
+ | `bot(config)` | The Worker handler. Checks signatures and answers commands, buttons, menus and modals |
85
+ | `command(config, run)` | A slash command, or a command with `subcommands` |
86
+ | `option(type, config)` | An option of a command, with `choices`, limits or `autocomplete` |
87
+ | `contextMenu('user' \| 'message', …)` | An item in the Apps menu of a user or a message |
88
+ | `select(type, config, run)` | A select menu of strings, users, roles, channels or mentionables |
89
+ | `modal(config, run)` | A form in a popup |
90
+ | `gateway(config)` | A Durable Object class that holds the Discord Gateway connection, from `botflare/gateway` |
91
+ | `rest` | Calls any Discord route as the bot and returns `[error, data]` |
92
+ | `mention`, `time` | Mentions and timestamps for message text |
93
+
94
+ A handler answers with one of these:
95
+
96
+ | Answer | What Discord shows |
97
+ | ------------------------------ | --------------------------------------------------------------------------------- |
98
+ | `c.reply(text, options)` | A new message |
99
+ | `c.defer(work, { ephemeral })` | "Thinking…", then the result of `work`, for work over 3 seconds |
100
+ | `c.update(text, options)` | The message of the button or menu, edited |
101
+ | `c.deferUpdate(work)` | The message of the button or menu as it is, then edited with the result of `work` |
102
+ | `c.modal(form, value)` | A form, with `value` back in `c.value` |
103
+
104
+ Message options are `buttons`, `selects`, `embeds`, `allowedMentions` and `ephemeral`. A command takes `permissions`, `contexts`, `installs` and `locales`. Every config has its description in the editor.
105
+
106
+ ## Gateway events
107
+
108
+ ```ts
109
+ export const Gateway = gateway({
110
+ intents: ['Guilds', 'GuildMessages', 'MessageContent'],
111
+ bot: app,
112
+ events: {
113
+ MESSAGE_CREATE: (message, c) => message.content === 'hi' && c.send(message.channel_id, 'hello'),
114
+ },
115
+ });
116
+ ```
117
+
118
+ Turn on privileged intents, such as `MessageContent`, in the Developer Portal first. If the connection is down, a Durable Object alarm opens it again within a minute.
119
+
120
+ ## REST
121
+
122
+ ```ts
123
+ import { rest } from 'botflare';
124
+
125
+ const [error, member] = await rest.get('/guilds/{guild_id}/members/{user_id}', {
126
+ path: { guild_id, user_id },
127
+ });
128
+ member?.joined_at; // typed from the Discord API spec
129
+ ```
130
+
131
+ On a 429, `rest` waits for `retry_after` and tries again, up to three tries in all.
132
+
133
+ ## Why?
134
+
135
+ discord.js and discord.py expect a process that stays on, with a WebSocket and a cache in memory. A Worker runs per request and then stops. botflare is built for that model from the start: it answers interactions over HTTP, keeps the gateway connection in a Durable Object, and takes its REST types from the spec that Discord publishes. When Discord adds a route, one script updates the types.
136
+
137
+ ## License
138
+
139
+ [MIT](https://github.com/pyyupsk/botflare/blob/main/LICENSE) License © 2026 [Pongsakorn Thipayanate](https://github.com/pyyupsk)