@minecraftbot-com/plugin 0.0.0-stage → 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/AGENTS.md +177 -0
- package/README.md +45 -2
- package/dist/api.d.ts +351 -0
- package/dist/api.js +2 -0
- package/dist/cli/api.d.ts +26 -0
- package/dist/cli/api.js +61 -0
- package/dist/cli/build.d.ts +23 -0
- package/dist/cli/build.js +65 -0
- package/dist/cli/check.d.ts +10 -0
- package/dist/cli/check.js +98 -0
- package/dist/cli/config.d.ts +11 -0
- package/dist/cli/config.js +38 -0
- package/dist/cli/create.d.ts +2 -0
- package/dist/cli/create.js +39 -0
- package/dist/cli/dev-mcp.d.ts +36 -0
- package/dist/cli/dev-mcp.js +101 -0
- package/dist/cli/dev.d.ts +15 -0
- package/dist/cli/dev.js +261 -0
- package/dist/cli/fetch-guard.d.ts +7 -0
- package/dist/cli/fetch-guard.js +66 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +112 -0
- package/dist/cli/mcp.d.ts +24 -0
- package/dist/cli/mcp.js +136 -0
- package/dist/cli/output.d.ts +11 -0
- package/dist/cli/output.js +25 -0
- package/dist/cli/test.d.ts +2 -0
- package/dist/cli/test.js +39 -0
- package/dist/errors.d.ts +22 -0
- package/dist/errors.js +35 -0
- package/dist/generated/block-states.d.ts +4087 -0
- package/dist/generated/block-states.js +2 -0
- package/dist/generated/blocks.d.ts +1289 -0
- package/dist/generated/blocks.js +1291 -0
- package/dist/generated/contracts.d.ts +304 -0
- package/dist/generated/contracts.js +327 -0
- package/dist/generated/effects.d.ts +43 -0
- package/dist/generated/effects.js +45 -0
- package/dist/generated/entities.d.ts +164 -0
- package/dist/generated/entities.js +166 -0
- package/dist/generated/items.d.ts +1661 -0
- package/dist/generated/items.js +1663 -0
- package/dist/generated/operations.d.ts +7 -0
- package/dist/generated/operations.js +65 -0
- package/dist/generated/permission-labels.d.ts +3 -0
- package/dist/generated/permission-labels.js +17 -0
- package/dist/generated/version.d.ts +2 -0
- package/dist/generated/version.js +3 -0
- package/dist/host/index.d.ts +7 -0
- package/dist/host/index.js +5 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +13 -0
- package/dist/permissions.d.ts +11 -0
- package/dist/permissions.js +18 -0
- package/dist/plugin.d.ts +86 -0
- package/dist/plugin.js +26 -0
- package/dist/protocol.d.ts +167 -0
- package/dist/protocol.js +4 -0
- package/dist/runtime/connection.d.ts +52 -0
- package/dist/runtime/connection.js +132 -0
- package/dist/runtime/handles.d.ts +95 -0
- package/dist/runtime/handles.js +289 -0
- package/dist/runtime/memory-transport.d.ts +6 -0
- package/dist/runtime/memory-transport.js +39 -0
- package/dist/runtime/runtime.d.ts +57 -0
- package/dist/runtime/runtime.js +265 -0
- package/dist/runtime/scope.d.ts +25 -0
- package/dist/runtime/scope.js +94 -0
- package/dist/runtime/storage-runtime.d.ts +10 -0
- package/dist/runtime/storage-runtime.js +131 -0
- package/dist/settings.d.ts +111 -0
- package/dist/settings.js +78 -0
- package/dist/storage.d.ts +78 -0
- package/dist/storage.js +24 -0
- package/dist/testing/fake-container.d.ts +175 -0
- package/dist/testing/fake-container.js +483 -0
- package/dist/testing/index.d.ts +37 -0
- package/dist/testing/index.js +54 -0
- package/dist/types.d.ts +65 -0
- package/dist/types.js +54 -0
- package/llms.txt +11 -0
- package/package.json +55 -4
- package/src/api.ts +334 -0
- package/src/cli/api.ts +79 -0
- package/src/cli/build.ts +76 -0
- package/src/cli/check.ts +103 -0
- package/src/cli/config.ts +48 -0
- package/src/cli/create.ts +38 -0
- package/src/cli/dev-mcp.ts +131 -0
- package/src/cli/dev.ts +272 -0
- package/src/cli/fetch-guard.ts +61 -0
- package/src/cli/main.ts +113 -0
- package/src/cli/mcp.ts +168 -0
- package/src/cli/output.ts +30 -0
- package/src/cli/test.ts +38 -0
- package/src/errors.ts +34 -0
- package/src/generated/block-states.ts +892 -0
- package/src/generated/blocks.ts +1291 -0
- package/src/generated/contracts.ts +329 -0
- package/src/generated/effects.ts +45 -0
- package/src/generated/entities.ts +166 -0
- package/src/generated/items.ts +1663 -0
- package/src/generated/operations.ts +69 -0
- package/src/generated/permission-labels.ts +19 -0
- package/src/generated/version.ts +3 -0
- package/src/host/index.ts +9 -0
- package/src/index.ts +34 -0
- package/src/permissions.ts +23 -0
- package/src/plugin.ts +105 -0
- package/src/protocol.ts +107 -0
- package/src/runtime/connection.ts +167 -0
- package/src/runtime/handles.ts +337 -0
- package/src/runtime/memory-transport.ts +41 -0
- package/src/runtime/runtime.ts +281 -0
- package/src/runtime/scope.ts +101 -0
- package/src/runtime/storage-runtime.ts +138 -0
- package/src/settings.ts +118 -0
- package/src/storage.ts +82 -0
- package/src/testing/fake-container.ts +571 -0
- package/src/testing/index.ts +70 -0
- package/src/types.ts +88 -0
- package/templates/basic/AGENTS.md +31 -0
- package/templates/basic/README.md +13 -0
- package/templates/basic/gitignore +3 -0
- package/templates/basic/package.json +18 -0
- package/templates/basic/src/index.ts +20 -0
- package/templates/basic/test/plugin.test.ts +14 -0
- package/templates/basic/tsconfig.json +14 -0
package/AGENTS.md
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
# @minecraftbot-com/plugin: guide for AI assistants (and people)
|
|
2
|
+
|
|
3
|
+
MineBot runs Minecraft bots for its users. A **plugin** is TypeScript that controls those bots. It runs in its
|
|
4
|
+
own process: on the developer's machine under `minebot dev`, or on MineBot's servers once uploaded. It talks to
|
|
5
|
+
the bots over a socket; it never runs inside the bot.
|
|
6
|
+
|
|
7
|
+
## Shape of a plugin
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { definePlugin, Block, BotEvent, ControlUnit, Permission, Setting, Storage, StorageScope } from "@minecraftbot-com/plugin";
|
|
11
|
+
|
|
12
|
+
export default definePlugin({
|
|
13
|
+
name: "Harvester",
|
|
14
|
+
// Everything the code does must be declared here. A call outside the list throws PermissionDeniedError,
|
|
15
|
+
// whose message names the permission to add.
|
|
16
|
+
permissions: [Permission.WorldRead, Permission.Move, Permission.Interact, Permission.ChatRead, Permission.ChatSend],
|
|
17
|
+
// Parts of the bot only this plugin drives while it runs.
|
|
18
|
+
exclusive: [ControlUnit.Movement, ControlUnit.Look, ControlUnit.Hand],
|
|
19
|
+
// Filled in per bot by the user on the dashboard (or with --settings under minebot dev).
|
|
20
|
+
settings: {
|
|
21
|
+
crop: Setting.block({ label: "Crop", options: [Block.Wheat, Block.Carrots], default: Block.Wheat }),
|
|
22
|
+
radius: Setting.radius({ label: "Search radius", default: 16, max: 48 }),
|
|
23
|
+
},
|
|
24
|
+
// Kept between runs. Belongs to the user who installed the plugin; 1 MB per scope, 64 KB per value, JSON only.
|
|
25
|
+
storage: {
|
|
26
|
+
harvested: Storage.value({ scope: StorageScope.Bot, default: 0 }),
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
// Once per bot the plugin is enabled on. Returning keeps listeners alive; the bot stopping ends everything.
|
|
30
|
+
async run(bot, { settings, storage, log, stats, warnings }) {
|
|
31
|
+
bot.on(BotEvent.Chat, async message => {
|
|
32
|
+
if (message.text === "!stats") await bot.say(`Harvested ${await storage.harvested.get()}`);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
await bot.loop(async () => {
|
|
36
|
+
const crops = await bot.world.findBlocks({ block: settings.crop, state: { age: 7 }, radius: settings.radius, limit: 8 });
|
|
37
|
+
if (crops.length === 0) return bot.wait({ seconds: 5 });
|
|
38
|
+
for (const crop of crops) {
|
|
39
|
+
const moved = await bot.goto(crop.position, { within: 3 });
|
|
40
|
+
if (!moved.ok) { log.warn(`Can't reach ${crop.position}: ${moved.reason}`); continue; }
|
|
41
|
+
if ((await bot.dig(crop.position)).ok) stats.set("Harvested", await storage.harvested.update(n => n + 1));
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
`runProfile(profile, context)` is the profile-wide counterpart of `run`: it runs once while the profile runs
|
|
49
|
+
(see "Profile" below).
|
|
50
|
+
|
|
51
|
+
## Rules
|
|
52
|
+
|
|
53
|
+
1. **One import.** Everything comes from `@minecraftbot-com/plugin`; tests use `@minecraftbot-com/plugin/testing`.
|
|
54
|
+
2. **Enums, not strings.** `Block.OakLog`, `Item.Bread`, `Entity.Zombie`, `Effect.Speed`, `BotEvent.Chat`,
|
|
55
|
+
`Permission.Move`, `BuiltInPlugin.AutoEater`. Their values are namespaced ids (`"minecraft:oak_log"`) and the
|
|
56
|
+
names come from the newest Minecraft version; on an older server a missing block finds nothing
|
|
57
|
+
(`await bot.supports(Block.X)` checks first).
|
|
58
|
+
3. **Expected failures are values.** `goto`, `dig`, `place`, `attack`, `equip`... return
|
|
59
|
+
`{ ok: true } | { ok: false, reason, message }`; `reason` is an enum (`MoveFailure.NoPath`,
|
|
60
|
+
`DigFailure.OutOfReach`). Exceptions (`PluginError`) mean a mistake in the code: a missing permission, bad
|
|
61
|
+
arguments, too many calls.
|
|
62
|
+
4. **Units are in the name.** `{ seconds: 5 }`, `{ ticks: 20 }`, `{ minutes: 1 }`. Distances and radii are blocks.
|
|
63
|
+
5. **Lifetimes are automatic.** When a bot stops (user stopped it, it disconnected), everything waiting in its
|
|
64
|
+
`run` ends with a `StoppedError` the SDK swallows. You don't need cancellation tokens or cleanup.
|
|
65
|
+
6. **Repeat with `bot.loop`.** It runs the body again and again with a floor between runs (one tick, or
|
|
66
|
+
`{ every }`) and backs off when it throws (the error is logged). Never write `while (true)` without awaiting
|
|
67
|
+
something: hosted plugins are cut off after 50 ms of continuous CPU.
|
|
68
|
+
7. **No tick hooks, no packets.** The API is intent-level (walk there, dig this). Movement, anti-cheat and world
|
|
69
|
+
data stay in the bot.
|
|
70
|
+
8. **Positions** the SDK returns are `Position` objects: they print as `x y z` in template strings and have
|
|
71
|
+
`offset`, `distanceTo`, `floored`, `equals`. Anything with `x`, `y`, `z` works as input; `vec3(x, y, z)` makes one.
|
|
72
|
+
|
|
73
|
+
## The bot (`run(bot)`)
|
|
74
|
+
|
|
75
|
+
| Call | Permission | Returns |
|
|
76
|
+
|---|---|---|
|
|
77
|
+
| `bot.name`, `bot.uuid`, `bot.id`, `bot.version` | none | the bot's Minecraft name, account uuid, profile id, server version |
|
|
78
|
+
| `bot.status()` | none | `{ health, food, saturation, alive, gameMode, dimension, experienceLevel }` |
|
|
79
|
+
| `bot.position()` | WorldRead | `Position` |
|
|
80
|
+
| `bot.goto(pos, { within?, timeout? })` | Move | `Result<MoveFailure>` |
|
|
81
|
+
| `bot.follow(entity, { distance?, timeout? })` | Move | `Result<MoveFailure>` when the target is lost or time runs out |
|
|
82
|
+
| `bot.stopMoving()`, `bot.lookAt(pos)`, `bot.jump()` | Move | |
|
|
83
|
+
| `bot.dig(pos)` | Interact | `Result<DigFailure>`; the block must be within reach (~4.5 blocks) |
|
|
84
|
+
| `bot.place(pos, item)` | Interact | `Result<PlaceFailure>` |
|
|
85
|
+
| `bot.activateBlock(pos)` | Interact | `Result<InteractFailure>`: doors, buttons, levers, chests |
|
|
86
|
+
| `bot.useItem()` | Interact | `Result<InteractFailure>`: the held item (eat, throw) |
|
|
87
|
+
| `bot.attack(entity)` | Attack | `Result<AttackFailure>`; within ~3 blocks |
|
|
88
|
+
| `bot.say(text)` | ChatSend | chat only; a leading `/` is refused |
|
|
89
|
+
| `bot.command("home")` | Commands | runs `/home` |
|
|
90
|
+
| `bot.world.block(pos)` | WorldRead | `{ position, block, state }` or null if not loaded |
|
|
91
|
+
| `bot.world.findBlocks({ block, state?, radius?, limit?, from? })` | WorldRead | closest first; `state` is typed per block |
|
|
92
|
+
| `bot.entities.nearby({ type?, radius?, limit? })` | EntitiesRead | `EntityInfo[]` closest first |
|
|
93
|
+
| `bot.entities.players()` | EntitiesRead | the server's player list |
|
|
94
|
+
| `bot.inventory.items()`, `.held()`, `.count(item)` | InventoryRead | |
|
|
95
|
+
| `bot.inventory.equip(item, slot?)`, `.drop(item, { count? })` | InventoryManage | `Result<InventoryFailure>` |
|
|
96
|
+
| `bot.craft(item, { count?, table? })` | InventoryManage | `Result<CraftFailure, { crafted }>`; 2x2 recipes in the inventory, bigger ones on a crafting table within reach |
|
|
97
|
+
| `bot.recipes(item)` | none | `Recipe[]`: `{ result, count, table, ingredients: [{ items, count }] }` on this server |
|
|
98
|
+
| `bot.containers.open(pos)` | InventoryManage | `Result<ContainerFailure, Container>`; chest, barrel, shulker box, hopper, furnace, ... within reach |
|
|
99
|
+
| `container.items()`, `.count(item)` | InventoryManage | what the open container holds |
|
|
100
|
+
| `container.deposit(item, { count? })`, `.withdraw(item, { count? })` | InventoryManage | `Result<ContainerFailure, { moved }>`: all of the item, or `count` |
|
|
101
|
+
| `container.close()` | InventoryManage | one container is open per bot; it closes on its own when another opens or the plugin stops |
|
|
102
|
+
| `bot.plugins.start(BuiltInPlugin.X)`, `.stop(...)` | ProfilePlugins | starts a plugin instance the profile already has |
|
|
103
|
+
| `bot.connect()`, `bot.disconnect()`, `bot.reconnect({ after? })` | ProfileBots | `Result<BotControlFailure>` |
|
|
104
|
+
| `bot.on(BotEvent.X, listener)` | see below | returns a function that stops listening |
|
|
105
|
+
| `bot.wait(duration)`, `bot.loop(body, { every? })` | none | |
|
|
106
|
+
|
|
107
|
+
Events: `Chat` (needs ChatRead) gives `{ text, sender, senderUuid, system }`; `PlayerJoined` and `PlayerLeft`
|
|
108
|
+
(EntitiesRead) give `{ player: { name, uuid } }`; `Died`, `Respawned`, `HealthChanged` (`{ health, food }`) need
|
|
109
|
+
nothing.
|
|
110
|
+
|
|
111
|
+
## Context (`run(bot, context)`)
|
|
112
|
+
|
|
113
|
+
- `settings`: typed from the `settings` declaration; missing values are the defaults.
|
|
114
|
+
- `storage.name.get()`, `.set(v)`, `.delete()`, `.update(fn)`; maps add `get(key)`, `set(key, v)`, `keys()`,
|
|
115
|
+
`entries()`. `update` is lossless when several bots write at once. Bot-scoped entries are bound to the bot in
|
|
116
|
+
`run`; in `runProfile` pick one with `storage.name.for(bot)`.
|
|
117
|
+
- `log.info/warn/error/debug(message)`: the plugin console on the dashboard and the terminal under `minebot dev`.
|
|
118
|
+
- `stats.set(name, n)`, `stats.add(name, n?)`: numbers on the plugin's card.
|
|
119
|
+
- `warnings.set(key, message)`, `warnings.clear(key)`: problems shown on the plugin's card until cleared.
|
|
120
|
+
|
|
121
|
+
## Profile (`runProfile(profile)`)
|
|
122
|
+
|
|
123
|
+
`profile.bots()` lists every bot of the profile (`ProfileBot`: `id`, `name`, `uuid`, `connected`, `enabled`,
|
|
124
|
+
`connect()`, `disconnect()`, `reconnect()`, `plugins`); `profile.bot(nameOrUuid)` finds one;
|
|
125
|
+
`profile.on(ProfileEvent.BotConnected | BotDisconnected, listener)` gives `{ bot }` / `{ bot, reason }`.
|
|
126
|
+
Connecting and disconnecting need `Permission.ProfileBots`.
|
|
127
|
+
|
|
128
|
+
## Network
|
|
129
|
+
|
|
130
|
+
`fetch` is the standard one, limited to what the plugin declares: `Permission.network("api.example.com")`,
|
|
131
|
+
`Permission.network("*.example.com")` for subdomains, or the exact origin a user typed into a `Setting.url`.
|
|
132
|
+
HTTPS only; redirects are followed only to allowed hosts. Hosted: 10 s timeout, 64 KB request and 256 KB
|
|
133
|
+
response bodies, about 60 requests a minute. Keys and webhooks belong in `Setting.secret()` or
|
|
134
|
+
`Setting.url({ secret: true })`: stored encrypted and never shown to the plugin's author.
|
|
135
|
+
|
|
136
|
+
## Testing
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
import { test } from "node:test";
|
|
140
|
+
import assert from "node:assert/strict";
|
|
141
|
+
import { Block } from "@minecraftbot-com/plugin";
|
|
142
|
+
import { testPlugin } from "@minecraftbot-com/plugin/testing";
|
|
143
|
+
import plugin from "../src/index.js";
|
|
144
|
+
|
|
145
|
+
test("digs ripe wheat", async () => {
|
|
146
|
+
const run = await testPlugin(plugin, {
|
|
147
|
+
bots: [{ id: "bot-1", name: "Farmer", position: { x: 0, y: 64, z: 0 } }],
|
|
148
|
+
blocks: [{ position: { x: 2, y: 64, z: 0 }, block: Block.Wheat, state: { age: 7 } }],
|
|
149
|
+
});
|
|
150
|
+
await run.until(() => run.container.blocks.size === 0);
|
|
151
|
+
await run.stop();
|
|
152
|
+
});
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
The fake bot (`run.container`) checks permissions like the real one and records `said`, `commands`, `calls`,
|
|
156
|
+
`logs`, `stats`, `warnings`, `storage` and `chests`. It crafts what `recipes: [{ result, ingredients: { [Item.Wheat]: 3 },
|
|
157
|
+
count?, table? }]` lists, and opens what `containers: [{ position, items?, size? }]` places; `chat(bot, text, sender)`, `emit(bot, event, data)`,
|
|
158
|
+
`disconnectBot(bot)`, `stopBot(bot)` drive it. `minebot test` runs `test/*.test.ts`.
|
|
159
|
+
|
|
160
|
+
## Live, under `minebot dev`
|
|
161
|
+
|
|
162
|
+
`minebot dev` serves MCP at `http://127.0.0.1:7788/mcp` (it prints the address; `--mcp-port`, `--no-mcp`), for
|
|
163
|
+
agents working on the plugin while it runs on the user's bots:
|
|
164
|
+
|
|
165
|
+
- `status`: build and connection state, the profile's bots and which the plugin runs on, granted permissions, settings.
|
|
166
|
+
- `logs`: what the plugin logged, plus build and connection lines; pass `since` (the last call's `next`) for new ones.
|
|
167
|
+
- `reload`: rebuild and restart, then report whether the build passed and the plugin connected.
|
|
168
|
+
- `query`: one read-only call as the plugin (`bot.position`, `world.findBlocks`, `entities.nearby`,
|
|
169
|
+
`inventory.items`, `storage.get`, ...). It needs the plugin's permissions and never moves or changes anything.
|
|
170
|
+
|
|
171
|
+
Saving a file restarts the plugin on its own; `reload` is for waiting on the outcome.
|
|
172
|
+
|
|
173
|
+
## Limits when hosted
|
|
174
|
+
|
|
175
|
+
About 1.5 MB of memory after start (bundled libraries are the usual cost; import only what you use), 50 ms of
|
|
176
|
+
CPU at a time, roughly 50 calls per second per bot, 256 queued events (the oldest are dropped first). No Node
|
|
177
|
+
built-ins (`fs`, `child_process`, ...), no `Intl`. `minebot check` reports the bundle size and the permissions.
|
package/README.md
CHANGED
|
@@ -1,3 +1,46 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @minecraftbot-com/plugin
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Write [MineBot](https://minecraftbot.com) plugins in TypeScript: a typed API for your bots, enums for every
|
|
4
|
+
block, item, entity and event, and the `minebot` CLI to run them.
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
npx @minecraftbot-com/plugin create my-plugin
|
|
8
|
+
cd my-plugin && npm install
|
|
9
|
+
npx minebot login # approve the sign-in on the dashboard
|
|
10
|
+
npm run dev # runs on your running profile, restarting on every save
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { definePlugin, BotEvent, Permission } from "@minecraftbot-com/plugin";
|
|
15
|
+
|
|
16
|
+
export default definePlugin({
|
|
17
|
+
name: "Greeter",
|
|
18
|
+
permissions: [Permission.ChatRead, Permission.ChatSend],
|
|
19
|
+
async run(bot) {
|
|
20
|
+
bot.on(BotEvent.Chat, async message => {
|
|
21
|
+
if (message.text === "!hello") await bot.say(`Hello, ${message.sender}!`);
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
- **Permissions you can read.** A plugin declares what it does; users approve the list; calls outside it fail
|
|
28
|
+
with a message naming the permission to add.
|
|
29
|
+
- **Failures are values.** `const moved = await bot.goto(pos); if (!moved.ok) log.warn(moved.reason);`
|
|
30
|
+
- **Lifetimes are handled.** Everything a bot's `run` started ends when the bot stops.
|
|
31
|
+
- **Test without a server.** `@minecraftbot-com/plugin/testing` runs your plugin against a fake bot.
|
|
32
|
+
|
|
33
|
+
[AGENTS.md](./AGENTS.md) documents the whole API (written for AI assistants, readable by people).
|
|
34
|
+
|
|
35
|
+
## Commands
|
|
36
|
+
|
|
37
|
+
| | |
|
|
38
|
+
|---|---|
|
|
39
|
+
| `minebot create <folder>` | new plugin project |
|
|
40
|
+
| `minebot login` / `logout` | sign in with your MineBot account |
|
|
41
|
+
| `minebot dev [--profile <name>] [--bot <name>] [--settings file.json]` | run the plugin on your profile, restarting on every save; serves MCP for AI agents at `http://127.0.0.1:7788/mcp` (`--mcp-port`, `--no-mcp`) |
|
|
42
|
+
| `minebot test` | run `test/*.test.ts` against a fake bot |
|
|
43
|
+
| `minebot check` | permissions users will be asked for, bundle size, problems |
|
|
44
|
+
| `minebot build` | `dist/plugin.js` and `dist/manifest.json` |
|
|
45
|
+
|
|
46
|
+
Node 22 or newer.
|
package/dist/api.d.ts
ADDED
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
import type { Block } from "./generated/blocks.js";
|
|
2
|
+
import type { Item } from "./generated/items.js";
|
|
3
|
+
import type { Entity } from "./generated/entities.js";
|
|
4
|
+
import type { BlockStates } from "./generated/block-states.js";
|
|
5
|
+
import type { AttackFailure, BotControlFailure, BotEvent, BuiltInPlugin, ContainerFailure, CraftFailure, DigFailure, EquipmentSlot, GameMode, InteractFailure, InventoryFailure, MoveFailure, PlaceFailure, PluginControlFailure, ProfileEvent } from "./generated/contracts.js";
|
|
6
|
+
import type { Duration, Position, Result, Vec3 } from "./types.js";
|
|
7
|
+
/** The properties a block can have, all optional: what `findBlocks` matches on. */
|
|
8
|
+
export type BlockStateFilter<B> = B extends keyof BlockStates ? Partial<BlockStates[B]> : Record<string, never>;
|
|
9
|
+
/** A block in the world. */
|
|
10
|
+
export interface BlockInfo {
|
|
11
|
+
readonly position: Position;
|
|
12
|
+
readonly block: Block;
|
|
13
|
+
/** The block's properties, e.g. `{ age: 7 }`. */
|
|
14
|
+
readonly state: Readonly<Record<string, string | number | boolean>>;
|
|
15
|
+
}
|
|
16
|
+
/** An entity near the bot: a mob, an animal, another player, an item on the ground. */
|
|
17
|
+
export interface EntityInfo {
|
|
18
|
+
/** The id the server gave it; valid while it stays loaded. Pass it to `bot.attack` or `bot.follow`. */
|
|
19
|
+
readonly id: number;
|
|
20
|
+
readonly type: Entity;
|
|
21
|
+
readonly uuid: string | null;
|
|
22
|
+
/** The player name for players, the custom name for named mobs. */
|
|
23
|
+
readonly name: string | null;
|
|
24
|
+
readonly position: Position;
|
|
25
|
+
/** Distance from the bot in blocks. */
|
|
26
|
+
readonly distance: number;
|
|
27
|
+
}
|
|
28
|
+
/** A player on the server's player list. */
|
|
29
|
+
export interface PlayerInfo {
|
|
30
|
+
readonly name: string;
|
|
31
|
+
readonly uuid: string;
|
|
32
|
+
/** Latency in milliseconds. */
|
|
33
|
+
readonly ping: number;
|
|
34
|
+
readonly gameMode: GameMode | null;
|
|
35
|
+
}
|
|
36
|
+
/** A stack of items in the bot's inventory or in a container. */
|
|
37
|
+
export interface ItemStack {
|
|
38
|
+
readonly item: Item;
|
|
39
|
+
readonly count: number;
|
|
40
|
+
/** The slot in the inventory (or in the container, for a container's items). */
|
|
41
|
+
readonly slot: number;
|
|
42
|
+
/** The custom name, if renamed in an anvil. */
|
|
43
|
+
readonly name: string | null;
|
|
44
|
+
}
|
|
45
|
+
/** The bot's health and similar. */
|
|
46
|
+
export interface BotStatus {
|
|
47
|
+
/** 0 to 20; 0 means dead. */
|
|
48
|
+
readonly health: number;
|
|
49
|
+
/** 0 to 20. */
|
|
50
|
+
readonly food: number;
|
|
51
|
+
readonly saturation: number;
|
|
52
|
+
readonly alive: boolean;
|
|
53
|
+
readonly gameMode: GameMode | null;
|
|
54
|
+
/** e.g. `"minecraft:overworld"`. */
|
|
55
|
+
readonly dimension: string | null;
|
|
56
|
+
readonly experienceLevel: number;
|
|
57
|
+
}
|
|
58
|
+
export interface ChatMessage {
|
|
59
|
+
/** The message as plain text, without formatting codes. */
|
|
60
|
+
readonly text: string;
|
|
61
|
+
/** The sending player's name, or null for system messages (server announcements, plugin output). */
|
|
62
|
+
readonly sender: string | null;
|
|
63
|
+
readonly senderUuid: string | null;
|
|
64
|
+
readonly system: boolean;
|
|
65
|
+
}
|
|
66
|
+
/** The data each `BotEvent` hands its listeners. */
|
|
67
|
+
export interface BotEventData {
|
|
68
|
+
[BotEvent.Chat]: ChatMessage;
|
|
69
|
+
[BotEvent.PlayerJoined]: {
|
|
70
|
+
readonly player: {
|
|
71
|
+
readonly name: string;
|
|
72
|
+
readonly uuid: string;
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
[BotEvent.PlayerLeft]: {
|
|
76
|
+
readonly player: {
|
|
77
|
+
readonly name: string;
|
|
78
|
+
readonly uuid: string;
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
[BotEvent.Died]: Record<string, never>;
|
|
82
|
+
[BotEvent.Respawned]: Record<string, never>;
|
|
83
|
+
[BotEvent.HealthChanged]: {
|
|
84
|
+
readonly health: number;
|
|
85
|
+
readonly food: number;
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/** The data each `ProfileEvent` hands its listeners. */
|
|
89
|
+
export interface ProfileEventData {
|
|
90
|
+
[ProfileEvent.BotConnected]: {
|
|
91
|
+
readonly bot: ProfileBot;
|
|
92
|
+
};
|
|
93
|
+
[ProfileEvent.BotDisconnected]: {
|
|
94
|
+
readonly bot: ProfileBot;
|
|
95
|
+
readonly reason: string;
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/** A listener; return a promise to have errors logged. */
|
|
99
|
+
export type Listener<T> = (data: T) => unknown;
|
|
100
|
+
/** Starts and stops other plugins on one bot. Needs `Permission.ProfilePlugins`. */
|
|
101
|
+
export interface BotPlugins {
|
|
102
|
+
/**
|
|
103
|
+
* Starts a plugin instance on this bot, as a macro would. The profile must already have an instance of it
|
|
104
|
+
* (added on the dashboard).
|
|
105
|
+
*
|
|
106
|
+
* @example await bot.plugins.start(BuiltInPlugin.AutoEater);
|
|
107
|
+
*/
|
|
108
|
+
start(plugin: BuiltInPlugin): Promise<Result<PluginControlFailure>>;
|
|
109
|
+
stop(plugin: BuiltInPlugin): Promise<Result<PluginControlFailure>>;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Any bot of the profile, connected or not. Its name and uuid are always available; connecting and
|
|
113
|
+
* disconnecting need `Permission.ProfileBots`.
|
|
114
|
+
*/
|
|
115
|
+
export interface ProfileBot {
|
|
116
|
+
/** The bot's id in the profile. */
|
|
117
|
+
readonly id: string;
|
|
118
|
+
/** The Minecraft name. */
|
|
119
|
+
readonly name: string;
|
|
120
|
+
/** The Minecraft account's uuid. */
|
|
121
|
+
readonly uuid: string;
|
|
122
|
+
/** True while it is connected to the server. */
|
|
123
|
+
readonly connected: boolean;
|
|
124
|
+
/** True if this plugin is enabled on it (then `run(bot)` runs while it is connected). */
|
|
125
|
+
readonly enabled: boolean;
|
|
126
|
+
connect(): Promise<Result<BotControlFailure>>;
|
|
127
|
+
disconnect(): Promise<Result<BotControlFailure>>;
|
|
128
|
+
/** Disconnects and connects again, optionally after a pause: `bot.reconnect({ after: { seconds: 30 } })`. */
|
|
129
|
+
reconnect(options?: {
|
|
130
|
+
after?: Duration;
|
|
131
|
+
}): Promise<Result<BotControlFailure>>;
|
|
132
|
+
readonly plugins: BotPlugins;
|
|
133
|
+
}
|
|
134
|
+
/** The world as the bot sees it. Needs `Permission.WorldRead`. */
|
|
135
|
+
export interface World {
|
|
136
|
+
/** The block at a position, or null if that part of the world isn't loaded. */
|
|
137
|
+
block(position: Vec3): Promise<BlockInfo | null>;
|
|
138
|
+
/**
|
|
139
|
+
* Blocks near the bot (or near `from`), closest first.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* const ripe = await bot.world.findBlocks({ block: Block.Wheat, state: { age: 7 }, radius: 16, limit: 8 });
|
|
143
|
+
* const logs = await bot.world.findBlocks({ block: [Block.OakLog, Block.BirchLog], radius: 32 });
|
|
144
|
+
*/
|
|
145
|
+
findBlocks<B extends Block>(query: {
|
|
146
|
+
block: B | readonly B[];
|
|
147
|
+
state?: BlockStateFilter<B>;
|
|
148
|
+
/** In blocks, at most 64. Default 16. */
|
|
149
|
+
radius?: number;
|
|
150
|
+
/** At most 256. Default 16. */
|
|
151
|
+
limit?: number;
|
|
152
|
+
from?: Vec3;
|
|
153
|
+
}): Promise<BlockInfo[]>;
|
|
154
|
+
}
|
|
155
|
+
/** Entities and players. Needs `Permission.EntitiesRead`. */
|
|
156
|
+
export interface Entities {
|
|
157
|
+
/**
|
|
158
|
+
* Entities near the bot, closest first.
|
|
159
|
+
*
|
|
160
|
+
* @example const zombies = await bot.entities.nearby({ type: Entity.Zombie, radius: 8 });
|
|
161
|
+
*/
|
|
162
|
+
nearby(query?: {
|
|
163
|
+
type?: Entity | readonly Entity[];
|
|
164
|
+
radius?: number;
|
|
165
|
+
limit?: number;
|
|
166
|
+
}): Promise<EntityInfo[]>;
|
|
167
|
+
/** Everyone on the server's player list. */
|
|
168
|
+
players(): Promise<PlayerInfo[]>;
|
|
169
|
+
}
|
|
170
|
+
/** The bot's inventory. Reading needs `Permission.InventoryRead`, changing `Permission.InventoryManage`. */
|
|
171
|
+
export interface Inventory {
|
|
172
|
+
items(): Promise<ItemStack[]>;
|
|
173
|
+
/** The item in the main hand, or null. */
|
|
174
|
+
held(): Promise<ItemStack | null>;
|
|
175
|
+
/** How many of an item the bot has in total. */
|
|
176
|
+
count(item: Item): Promise<number>;
|
|
177
|
+
/** Holds (or wears) an item: `await bot.inventory.equip(Item.DiamondSword)`. */
|
|
178
|
+
equip(item: Item, slot?: EquipmentSlot): Promise<Result<InventoryFailure>>;
|
|
179
|
+
/** Drops items on the ground: all of that item, or `count` of them. */
|
|
180
|
+
drop(item: Item, options?: {
|
|
181
|
+
count?: number;
|
|
182
|
+
}): Promise<Result<InventoryFailure>>;
|
|
183
|
+
}
|
|
184
|
+
/** A crafting recipe, as `bot.recipes` lists it. */
|
|
185
|
+
export interface Recipe {
|
|
186
|
+
readonly result: Item;
|
|
187
|
+
/** How many one craft makes (4 sticks from 2 planks). */
|
|
188
|
+
readonly count: number;
|
|
189
|
+
/** True if it needs a crafting table's 3x3 grid; false if the inventory's 2x2 grid will do. */
|
|
190
|
+
readonly table: boolean;
|
|
191
|
+
/** What one craft takes: per ingredient, the items that will do and how many. */
|
|
192
|
+
readonly ingredients: readonly {
|
|
193
|
+
readonly items: readonly Item[];
|
|
194
|
+
readonly count: number;
|
|
195
|
+
}[];
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* A chest, barrel, shulker box, hopper, furnace, ... the bot has open (from `bot.containers.open`). One container
|
|
199
|
+
* is open per bot at a time; it stays open until `close()`, another one opens, or the plugin stops on the bot.
|
|
200
|
+
*/
|
|
201
|
+
export interface Container {
|
|
202
|
+
readonly position: Position;
|
|
203
|
+
readonly block: Block;
|
|
204
|
+
/** How many slots it has. */
|
|
205
|
+
readonly size: number;
|
|
206
|
+
/** What it holds now (`slot` is the container's slot). */
|
|
207
|
+
items(): Promise<ItemStack[]>;
|
|
208
|
+
/** How many of an item it holds. */
|
|
209
|
+
count(item: Item): Promise<number>;
|
|
210
|
+
/**
|
|
211
|
+
* Moves items from the inventory into the container: all of that item, or `count`. `value.moved` says how many went
|
|
212
|
+
* in; fewer than asked means it ran out of room or items.
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* const stored = await chest.deposit(Item.Wheat);
|
|
216
|
+
* if (!stored.ok) log.warn(`Couldn't store wheat: ${stored.reason}`);
|
|
217
|
+
*/
|
|
218
|
+
deposit(item: Item, options?: {
|
|
219
|
+
count?: number;
|
|
220
|
+
}): Promise<Result<ContainerFailure, {
|
|
221
|
+
moved: number;
|
|
222
|
+
}>>;
|
|
223
|
+
/** Moves items from the container into the inventory: all of that item, or `count`. */
|
|
224
|
+
withdraw(item: Item, options?: {
|
|
225
|
+
count?: number;
|
|
226
|
+
}): Promise<Result<ContainerFailure, {
|
|
227
|
+
moved: number;
|
|
228
|
+
}>>;
|
|
229
|
+
close(): Promise<void>;
|
|
230
|
+
}
|
|
231
|
+
/** Containers the bot can open. Needs `Permission.InventoryManage`. */
|
|
232
|
+
export interface Containers {
|
|
233
|
+
/**
|
|
234
|
+
* Opens the container at a position within reach (walk there first).
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* await bot.goto(chestPosition, { within: 3 });
|
|
238
|
+
* const opened = await bot.containers.open(chestPosition);
|
|
239
|
+
* if (opened.ok) {
|
|
240
|
+
* await opened.value.deposit(Item.Wheat);
|
|
241
|
+
* await opened.value.close();
|
|
242
|
+
* }
|
|
243
|
+
*/
|
|
244
|
+
open(position: Vec3): Promise<Result<ContainerFailure, Container>>;
|
|
245
|
+
}
|
|
246
|
+
/** One bot the plugin runs on, handed to `run(bot)`. */
|
|
247
|
+
export interface Bot extends ProfileBot {
|
|
248
|
+
readonly world: World;
|
|
249
|
+
readonly entities: Entities;
|
|
250
|
+
readonly inventory: Inventory;
|
|
251
|
+
readonly containers: Containers;
|
|
252
|
+
/** The server's Minecraft version, e.g. `"1.21.4"`. */
|
|
253
|
+
readonly version: string;
|
|
254
|
+
/**
|
|
255
|
+
* Listens to an event of this bot until the bot stops. Returns a function that stops listening.
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* bot.on(BotEvent.Chat, async message => {
|
|
259
|
+
* if (message.text === "!ping") await bot.say("pong");
|
|
260
|
+
* });
|
|
261
|
+
*/
|
|
262
|
+
on<E extends BotEvent>(event: E, listener: Listener<BotEventData[E]>): () => void;
|
|
263
|
+
status(): Promise<BotStatus>;
|
|
264
|
+
/** Where the bot stands. Needs `Permission.WorldRead`. */
|
|
265
|
+
position(): Promise<Position>;
|
|
266
|
+
/** True if the server's version has this block or item (older servers lack newer ones). */
|
|
267
|
+
supports(id: Block | Item | Entity): Promise<boolean>;
|
|
268
|
+
/**
|
|
269
|
+
* Walks to a position with the bot's pathfinder. Needs `Permission.Move`.
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* const moved = await bot.goto(chest, { within: 2 });
|
|
273
|
+
* if (!moved.ok) log.warn(`Can't reach the chest: ${moved.reason}`);
|
|
274
|
+
*/
|
|
275
|
+
goto(target: Vec3, options?: {
|
|
276
|
+
within?: number;
|
|
277
|
+
timeout?: Duration;
|
|
278
|
+
}): Promise<Result<MoveFailure>>;
|
|
279
|
+
/** Follows an entity until it is lost, `timeout` passes or the plugin stops. Needs `Permission.Move`. */
|
|
280
|
+
follow(entity: EntityInfo | number, options?: {
|
|
281
|
+
distance?: number;
|
|
282
|
+
timeout?: Duration;
|
|
283
|
+
}): Promise<Result<MoveFailure>>;
|
|
284
|
+
stopMoving(): Promise<void>;
|
|
285
|
+
/** Turns to look at a position. Needs `Permission.Move`. */
|
|
286
|
+
lookAt(target: Vec3): Promise<void>;
|
|
287
|
+
jump(): Promise<void>;
|
|
288
|
+
/** Breaks the block at a position (it must be within reach). Needs `Permission.Interact`. */
|
|
289
|
+
dig(position: Vec3): Promise<Result<DigFailure>>;
|
|
290
|
+
/** Places a block from the inventory at a position. Needs `Permission.Interact`. */
|
|
291
|
+
place(position: Vec3, item: Item): Promise<Result<PlaceFailure>>;
|
|
292
|
+
/** Right-clicks a block: opens doors, presses buttons, flips levers. Needs `Permission.Interact`. */
|
|
293
|
+
activateBlock(position: Vec3): Promise<Result<InteractFailure>>;
|
|
294
|
+
/** Uses the held item (eats food, throws a pearl). Needs `Permission.Interact`. */
|
|
295
|
+
useItem(): Promise<Result<InteractFailure>>;
|
|
296
|
+
/** Hits an entity within reach. Needs `Permission.Attack`. */
|
|
297
|
+
attack(entity: EntityInfo | number): Promise<Result<AttackFailure>>;
|
|
298
|
+
/**
|
|
299
|
+
* Crafts at least `count` of an item (default 1, rounded up to whole crafts) from the inventory. Small recipes use
|
|
300
|
+
* the inventory's 2x2 grid; bigger ones a crafting table within reach (`table`, or the nearest one). `value.crafted`
|
|
301
|
+
* says how many it made. Needs `Permission.InventoryManage`.
|
|
302
|
+
*
|
|
303
|
+
* @example
|
|
304
|
+
* const made = await bot.craft(Item.Bread, { count: 3 });
|
|
305
|
+
* if (!made.ok) log.warn(`No bread: ${made.message}`);
|
|
306
|
+
*/
|
|
307
|
+
craft(item: Item, options?: {
|
|
308
|
+
count?: number;
|
|
309
|
+
table?: Vec3;
|
|
310
|
+
}): Promise<Result<CraftFailure, {
|
|
311
|
+
crafted: number;
|
|
312
|
+
}>>;
|
|
313
|
+
/** The crafting recipes that make an item on this server, with what each takes. */
|
|
314
|
+
recipes(item: Item): Promise<Recipe[]>;
|
|
315
|
+
/** Sends a chat message. Needs `Permission.ChatSend`; commands go through `command`. */
|
|
316
|
+
say(message: string): Promise<void>;
|
|
317
|
+
/** Runs a server command, with or without the slash: `bot.command("home")`. Needs `Permission.Commands`. */
|
|
318
|
+
command(command: string): Promise<void>;
|
|
319
|
+
/** Waits. Ends early (with the rest of `run`) when the bot stops. */
|
|
320
|
+
wait(duration: Duration): Promise<void>;
|
|
321
|
+
/**
|
|
322
|
+
* Runs `body` again and again until the bot stops: at most every `every` (default one tick), backing off
|
|
323
|
+
* when it throws (the error is logged), so a mistake can't spin the CPU.
|
|
324
|
+
*
|
|
325
|
+
* @example
|
|
326
|
+
* await bot.loop(async () => {
|
|
327
|
+
* const food = await bot.inventory.count(Item.Bread);
|
|
328
|
+
* if (food < 5) log.warn("Low on bread");
|
|
329
|
+
* }, { every: { seconds: 10 } });
|
|
330
|
+
*/
|
|
331
|
+
loop(body: () => unknown, options?: {
|
|
332
|
+
every?: Duration;
|
|
333
|
+
}): Promise<never>;
|
|
334
|
+
}
|
|
335
|
+
/** The profile, handed to `runProfile(profile)`. */
|
|
336
|
+
export interface Profile {
|
|
337
|
+
readonly id: string;
|
|
338
|
+
readonly name: string;
|
|
339
|
+
/** Every bot of the profile, enabled or not. */
|
|
340
|
+
bots(): readonly ProfileBot[];
|
|
341
|
+
/** A bot by id, name or uuid. */
|
|
342
|
+
bot(idNameOrUuid: string): ProfileBot | undefined;
|
|
343
|
+
/** Listens to a profile event until the plugin stops. */
|
|
344
|
+
on<E extends ProfileEvent>(event: E, listener: Listener<ProfileEventData[E]>): () => void;
|
|
345
|
+
/** Waits. */
|
|
346
|
+
wait(duration: Duration): Promise<void>;
|
|
347
|
+
/** As `bot.loop`, for the profile. */
|
|
348
|
+
loop(body: () => unknown, options?: {
|
|
349
|
+
every?: Duration;
|
|
350
|
+
}): Promise<never>;
|
|
351
|
+
}
|
package/dist/api.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export interface DeviceStart {
|
|
2
|
+
deviceCode: string;
|
|
3
|
+
userCode: string;
|
|
4
|
+
verificationUrl: string;
|
|
5
|
+
interval: number;
|
|
6
|
+
expiresIn: number;
|
|
7
|
+
}
|
|
8
|
+
export interface ProfileEntry {
|
|
9
|
+
id: string;
|
|
10
|
+
name: string;
|
|
11
|
+
running: boolean;
|
|
12
|
+
}
|
|
13
|
+
export interface DevToken {
|
|
14
|
+
token: string;
|
|
15
|
+
address: string;
|
|
16
|
+
profileId: string;
|
|
17
|
+
profileName: string;
|
|
18
|
+
expiresAt: string;
|
|
19
|
+
}
|
|
20
|
+
export declare function startDevice(): Promise<DeviceStart>;
|
|
21
|
+
export declare function pollDevice(deviceCode: string): Promise<{
|
|
22
|
+
status: string;
|
|
23
|
+
token: string | null;
|
|
24
|
+
}>;
|
|
25
|
+
export declare function listProfiles(): Promise<ProfileEntry[]>;
|
|
26
|
+
export declare function devToken(profileId: string): Promise<DevToken>;
|
package/dist/cli/api.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { hostname } from "node:os";
|
|
2
|
+
import { apiUrl, readCredentials } from "./config.js";
|
|
3
|
+
import { fail } from "./output.js";
|
|
4
|
+
async function request(method, path, body, auth = false) {
|
|
5
|
+
const headers = { "content-type": "application/json", "user-agent": "minebot-cli" };
|
|
6
|
+
if (auth) {
|
|
7
|
+
const credentials = readCredentials();
|
|
8
|
+
if (!credentials)
|
|
9
|
+
fail("You're not signed in.", "Run `minebot login` first.");
|
|
10
|
+
headers.authorization = `Bearer ${credentials.token}`;
|
|
11
|
+
}
|
|
12
|
+
let response;
|
|
13
|
+
try {
|
|
14
|
+
response = await fetch(`${apiUrl()}${path}`, { method, headers, body: body === undefined ? undefined : JSON.stringify(body) });
|
|
15
|
+
}
|
|
16
|
+
catch (e) {
|
|
17
|
+
fail(`Couldn't reach ${apiUrl()}: ${e.message}`, "Check your internet connection, or MINEBOT_API_URL if you set it.");
|
|
18
|
+
}
|
|
19
|
+
const text = await response.text();
|
|
20
|
+
let data = {};
|
|
21
|
+
try {
|
|
22
|
+
data = text ? JSON.parse(text) : {};
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
data = { message: text.slice(0, 200) };
|
|
26
|
+
}
|
|
27
|
+
return { status: response.status, data: data };
|
|
28
|
+
}
|
|
29
|
+
function message(data, fallback) {
|
|
30
|
+
return data?.message ?? fallback;
|
|
31
|
+
}
|
|
32
|
+
export async function startDevice() {
|
|
33
|
+
const r = await request("POST", "/plugins/cli/device", { clientName: `minebot CLI on ${hostname()}` });
|
|
34
|
+
if (r.status !== 200)
|
|
35
|
+
fail(message(r.data, `Sign-in failed (HTTP ${r.status})`));
|
|
36
|
+
return r.data;
|
|
37
|
+
}
|
|
38
|
+
export async function pollDevice(deviceCode) {
|
|
39
|
+
const r = await request("POST", "/plugins/cli/token", { deviceCode });
|
|
40
|
+
if (r.status !== 200)
|
|
41
|
+
return { status: "pending", token: null };
|
|
42
|
+
return r.data;
|
|
43
|
+
}
|
|
44
|
+
export async function listProfiles() {
|
|
45
|
+
const r = await request("GET", "/plugins/cli/profiles", undefined, true);
|
|
46
|
+
if (r.status === 401)
|
|
47
|
+
fail(message(r.data, "Your sign-in expired."), "Run `minebot login` again.");
|
|
48
|
+
if (r.status !== 200)
|
|
49
|
+
fail(message(r.data, `Couldn't list your profiles (HTTP ${r.status})`));
|
|
50
|
+
return r.data;
|
|
51
|
+
}
|
|
52
|
+
export async function devToken(profileId) {
|
|
53
|
+
const r = await request("POST", "/plugins/cli/dev-token", { profileId }, true);
|
|
54
|
+
if (r.status === 401)
|
|
55
|
+
fail(message(r.data, "Your sign-in expired."), "Run `minebot login` again.");
|
|
56
|
+
if (r.status === 409)
|
|
57
|
+
fail(message(r.data, "The profile isn't running."), "Start it on the dashboard, then run `minebot dev` again.");
|
|
58
|
+
if (r.status !== 200)
|
|
59
|
+
fail(message(r.data, `Couldn't get a dev token (HTTP ${r.status})`));
|
|
60
|
+
return r.data;
|
|
61
|
+
}
|