Skip to content

Many servers and app keys

Many servers in one app

If you're building something lots of servers use (a bot, a CAD, a dashboard), each server's owner gives your app their server key. AsyncServerPool runs one set of handlers across all of them:

from oxrpy import AsyncServerPool

pool = AsyncServerPool(app_key=os.environ["OXFD_APP_KEY"])   # app key optional
pool.add("guild-1234", key_from_guild_1234)
pool.add("guild-5678", key_from_guild_5678)

@pool.on("Kill")
async def on_kill(server, kill):           # the server's name comes first
    await post_to_guild(server, kill)

@pool.on("error")
async def on_error(server, exc):
    print(f"{server}: {exc}")

await pool.run_forever(save_dir="positions/")
  • With an app key, the pool paces every server's requests together. Your app's limits count across all the servers it calls, so separate clients would trip over each other.
  • If an owner regenerates their key, that one server stops being followed. You hear about it through your error handler and pool.failed, and every other server carries on.
  • You can pool.add() and await pool.remove() servers while it's running, for example when someone links or unlinks their server.
  • pool["guild-1234"] is that server's normal async client, for reads and commands.
  • await pool.each(lambda api: api.get_players()) and await pool.run_command_on_all("announce ...") do something on every server at once.
  • save_dir keeps each server's position, so restarts don't miss anything.

App keys

If you're making something that lots of servers will use, you can ask Oxford Roleplay for an app key (it starts with oxa_). It gives your app its own rate limits. You still need the server key for whichever server you're talking to:

api = OxfordAPI(server_key_for_that_server, app_key=os.environ["OXFD_APP_KEY"])