# phBot walk scripts and script commands: the format and how plugins add their own

A phBot walk script is a plain `.txt` file with one command per line, and each line is a command name followed by comma-separated arguments: `walk,6430,1099,-32`. phBot runs the lines in order. A plugin adds its own command by defining a Python function with that name that takes one list of arguments, and from then on the command can appear in any script. Everything below is read from the source of my [phBot plugins](https://www.mohamedgado.com/projects/phbot-plugins), mainly xControl 1.9.1, xCaravan 1.0.0 and xJupiter 4.5.

## What does a phBot walk script look like?

The two plugins that ship walk scripts inside their Python files show the line format. This is the start of the route embedded in xCaravan:

```text
walk,6430,1099,-32
walk,6428,1113,0
walk,6419,1134,-6
cast,Job - Caravan Bugle
begintargettrading
walk,6259,1093,0
```

Each line is a command, then its arguments after commas. `walk` takes three numbers in xCaravan's script. The same routes end with these lines:

```text
settletargettrading
terminate,transport
use,returnscroll
```

xJupiter embeds its routes as Python lists of strings, and its `walk` lines carry four numbers, for example `walk,-32749,-20832,101,-134`. The code never explains what each number means. Its own comment contradicts itself on the layout, and the helper that reads the position takes the third and fourth fields. So I do not describe the fields here. The easy way to get correct lines is to record a walk in phBot and copy what it writes.

Two other commands show up in the scripts. xCaravan's trader variant swaps `begintargettrading` for `wait,8000` and `settletargettrading` for `wait,5000`. xJupiter ends its attack script with `AttackArea,<range>`, where the range comes from its own `_attack_range()` helper.

## How does a plugin add a script command?

It does not register anything. I searched all of my plugins for a registration call and there is none. phBot calls a plugin function by the name of the command, and the function receives one list, so a script line `inject,3091,0` calls `inject(args)` in xPacketTool.

Two conventions hold in every command I wrote:

- **`args[0]` is the command name.** The values start at `args[1]`. xPacketTool reads `int(args[1], 16)` for the opcode. xControl does not rely on it: each command removes the leading command name from the list first, so it works whether or not phBot includes it.
- **The function returns `0`.** Every script command in xControl and xPacketTool ends with `return 0`, including after an error, which is caught and logged. I did not find a command that returns anything else, so I do not know what phBot does with a different value.

A command that has to wait can simply block. `wait_party` below sleeps in a loop until its condition holds, and the script stays on that line meanwhile.

## Which script commands does xControl add?

xControl adds five. Arguments are split on commas, so spaces inside an argument are kept.

| Command | Arguments | Behaviour |
| --- | --- | --- |
| `chat` | `chat,<type>,<message>` | Types: `all`, `party`, `guild`, `union`, `stall`, `global`. Type is case-insensitive. |
| `chat` | `chat,private,<name>,<message>` | `note` works the same way. These two need at least three arguments. |
| `pinvite` | `pinvite,<name>[,<name>...]` | Looks for each name among nearby players. |
| `wait_party` | `wait_party,<count>[,<timeout ms>]` | Waits until the party has at least `count` members. Timeout defaults to 60000. |
| `use_scroll` | `use_scroll,<keyword>` | Uses the first item whose name contains the keyword. |
| `leaveparty` | none | Leaves the party. |

Details worth knowing, all from the code:

- `chat` joins everything after the type back together with commas, so a message with a comma in it arrives intact. With fewer than two arguments it logs the usage line and does nothing.
- `pinvite` sends an invite if the bot is already in a party, and creates a party with that player if it is not. It waits half a second between names. A name that is not nearby is logged and skipped.
- `wait_party` checks the party every half second. When the timeout passes it logs the count it found and returns `0` anyway, so the script carries on with a short party.
- `use_scroll` matches the keyword against both the item name and the server name, ignoring case. If the main inventory has no match, it looks in the grab pet, moves the item to a free slot in the inventory, waits a second and uses it. A full inventory stops it with a log line.
- `leaveparty` logs that it is not in a party if it is not in one.

More on the chat commands is in the [xControl commands reference](https://www.mohamedgado.com/writing/phbot-xcontrol-commands).

## How do xCaravan and xJupiter run scripts?

Neither one asks you to load a file by hand. They write or hold the script and hand it to phBot with two calls from the plugin API.

xCaravan keeps the routes as Python strings (`ROUTE_SCRIPT`, `HUNTER_ROUTE_SCRIPT`, and `TRADER_ROUTE_SCRIPT`, which is the hunter route with two lines replaced). `_route_script()` picks one by the route mode and passes it to `start_script(script)`. Its fallback to get home after a run is `start_script('use,returnscroll\n')`. It calls `stop_script()` in several places, including right after a teleport, because the comment there says phBot can resume the loaded script on its own.

xJupiter builds a file per target. `_write_script` saves the lines to `<config dir>/xJupiter/<name>.txt`, one command per line with a trailing newline. The attack script is the target's `walk` lines plus `AttackArea`. The way back is the same walk list reversed. Then it calls `set_training_script(path)` followed by `start_bot()`. xControl's `SETSCRIPT` chat command uses `set_training_script` too, and passing an empty string clears the script. The calls themselves are covered in [the phBot Python API](https://www.mohamedgado.com/writing/phbot-plugin-python-api).

## What does a script that uses them look like?

This gathers a party, uses a scroll and starts the others:

```text
pinvite,Alpha,Bravo,Charlie
wait_party,4,90000
use_scroll,Random Awaken
chat,party,START
```

`pinvite` invites three players, `wait_party` holds the script for up to 90 seconds until four members are in, `use_scroll` uses the first item matching `Random Awaken`, and `chat,party,START` sends `START` to the party. Whether the bots react depends on xControl's leader list on each of them. I have not put `walk` lines in this example because I cannot give you valid coordinates for your server.

## What goes wrong in script commands?

- **Wrong argument count.** `chat,party` with no message logs a usage line and sends nothing. `chat,private,Gado` needs the message as a third argument.
- **A bad number.** `wait_party,four` raises on `int()`. The code catches it, logs `wait_party error`, and returns `0`, so the script moves on without waiting.
- **Treating a timeout as success.** `wait_party` returns `0` on timeout as well as on success. Only the log tells you which happened.
- **Names that are not nearby.** `pinvite` only sees nearby players, and each miss is skipped with a log line.
- **Case and spelling.** `use_scroll` ignores case, but the keyword has to be part of the item name as the client shows it.
- **Commas in your own text.** Each comma starts a new argument. `chat` and `use_scroll` join the rest back, but a command you write yourself will see the split list.

When a line does nothing, read the phBot log first. The xControl commands log a line starting with `Plugin:` when they skip or fail, so the reason is usually there.

