Orbit Developers

Go

A complete bot using only the standard library. It replies pong to !ping and waves at hello. You'll need Go 1.22 or newer.

Run it

git clone https://github.com/z0roday/OrbitApi.git
cd OrbitApi/examples/go
export ORBIT_TOKEN='1234567890123456789.Qm9...'
go run ./cmd/bot

Then type !ping in a server your bot is in.

How it works

The orbit package is a tiny API client. It adds your token to every request, waits and retries on 429, and returns an *orbit.Error with Status and Code when something fails:

api := orbit.New(os.Getenv("ORBIT_TOKEN"))
me, err := api.Me(ctx)
_, err = api.Send(ctx, channelID, "Hello", messageID)
err = api.React(ctx, channelID, messageID, "๐Ÿ‘‹")

api.Events(ctx, handle) reads the event stream and calls handle for each event. cmd/bot remembers the last message it handled in each channel and only fetches what's new:

err := api.Events(ctx, func(event orbit.Event) error {
	return b.handle(ctx, event)
})

It also offers /ping and /roll. It registers them when it starts and answers each interaction event with a card:

err = api.SetCommands(ctx, []orbit.Command{{Name: "ping", Description: "Checks that the bot is awake"}})

asked, err := api.Interaction(ctx, id)
_, err = api.Reply(ctx, asked.ID, orbit.Reply{Embeds: []orbit.Embed{{Title: "Pong", Color: 0x5b8def}}})

When the connection drops, the bot reconnects, waiting a little longer each time, and catches up on what it missed. On 401 it stops, since that means the token was replaced.

Tests

go test ./...

The tests run the client against a fake API built with httptest.

Keep it running

Build it on your own machine and copy the single binary to the server:

GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o orbit-bot ./cmd/bot
scp orbit-bot root@SERVER_IP:/usr/local/bin/

Then use the systemd setup from the Node.js example, without WorkingDirectory and with:

ExecStart=/usr/local/bin/orbit-bot