Skip to content

Events and logs

Following logs as they happen

follow() keeps checking the logs for you and only hands you entries you haven't seen yet, oldest first. It also waits until the server's next update before asking again, so you're not wasting requests.

for data, new in api.follow("Events", "KillLogs", "ModCalls", sections=["Players"]):
    for event in new["Events"]:
        print(event["Type"], event)
    for kill in new["KillLogs"]:
        print(f'{kill["KillerUsername"]} downed {kill["VictimUsername"]} with {kill["Weapon"]}')

When an entry changes, for example a mod call gets resolved or a shot gets its hit recorded, it shows up again with the same Id. If you're storing entries, use Id as the key so the new version replaces the old one.

If your bot restarts, you can carry on from where you left off by saving follower.cursor:

follower = api.follow("Events", cursor=saved_cursor)
data, new = follower.poll()      # one request, if you'd rather run your own loop
saved_cursor = follower.cursor

The loop keeps going if the internet drops, you get rate limited or the API has a bad moment. It waits a bit longer after each failure in a row (up to a minute) and logs a warning. It only stops for problems that waiting won't fix, like a wrong API key, and raises those.

Reacting to events

If you just want to run some code whenever something happens in game, register a handler with @api.on(...) and call run_forever():

from oxrpy import OxfordAPI

api = OxfordAPI(key)

@api.on("Kill")
def on_kill(kill):
    print(f'{kill["KillerUsername"]} downed {kill["VictimUsername"]}')

@api.on("Join", "Leave")
def on_join_or_leave(entry):
    print(entry["Username"], entry["Type"])

@api.on("SpeedCamera")
def on_speeding(event):
    if event["SpeedMph"] > event["Limit"] + 30:
        api.pm(event["UserId"], "Slow down!")

api.run_forever()

You can listen for:

  • Any entry type: Kill, ModCall, RadioCall, Join, Leave, Command, CommandResult, Shot, or any of the game events, such as Downed, Jailed, RobberyStarted, SpeedCamera or IncidentOpened. Oxford's docs list every event.
  • A whole log, such as "KillLogs" or "Events", to get everything in it.
  • "poll", which gets the whole response after every request. Pass run_forever(sections=["Players"]) to have live data in it too.
  • "error", which gets the exception when a request fails and is about to be retried.
  • "ServerOnline", "ServerOffline" and "ServerRestart", which get the whole response when the server comes up, goes down, or restarts. A restart means live data started over; logs carry on.
@api.on("ServerOffline")
def went_down(data):
    print("The server is offline")

@api.on("ServerOnline")
def came_back(data):
    print(data["Server"]["Name"], "is back up")

oxrpy works out which logs to ask for from your handlers. Entries arrive oldest first. By default, run_forever() ignores whatever was already in the logs when it started, so you don't get a flood of old kills on startup. Pass skip_existing=False if you want them. If a handler raises, the error is logged and the bot carries on. Call api.stop() from a handler, or from another thread, to make run_forever() return.

Carrying on after a restart

Bots restart: crashes, updates, the host rebooting. Without help, anything that happens while your bot is down is missed. Pass save_to and oxrpy remembers where it got to, then picks up exactly there next time. That includes what happened while the bot was down, as long as the logs still hold it (the last 500 kills, 1,000 events and so on).

api.run_forever(save_to="cursor.json")

# or with follow()
for data, new in api.follow("KillLogs", save_to="cursor.json"):
    ...

The position is saved after your handlers have run for each check, so a crash part-way through means those entries come round again rather than being lost. To keep it somewhere other than a file, like your bot's database, pass any object with load() and save(state) methods.