The finished code is in
examples/discord-bot on GitHub. Clone it to skip ahead.
Why
/link instead of /stats PlayerName#TAG? Riot requires VALORANT apps to get each player’s opt-in through Riot Sign On (RSO) before showing their match history. A bot that looks up any Riot ID won’t get a production key. This bot only shows match data for players who signed in with /link. Leaderboards and server status don’t need opt-in. Details are in Getting a production key.Prerequisites
- Python 3.10 or newer
- A Riot API key from the Riot Developer Portal. A development key is enough to start the bot, but most VALORANT endpoints return
403until you have a production key. - A Discord application with a bot user. Create one in the Discord Developer Portal, copy the bot token, and invite the bot to your server with the
botandapplications.commandsscopes. - Later: RSO client credentials from Riot, which are issued after your production key is approved. Until then
/linkis disabled, and everything else runs normally.
Project setup
1
Get the code
riot.py: Riot API access with cachingrso.py: the “Sign in with Riot” flow and linked-account storagebot.py: Discord slash commandstest_example.py: offline tests for the Riot and RSO logic
2
Install dependencies
valaw, discord.py, and python-dotenv.3
Add your tokens
.env
DEV_GUILD_ID is optional. When it’s set, slash commands sync to that one server instantly instead of waiting for global commands to appear.4
Run it
/ in your server and the commands appear.How it works
One shared client
Create onevalaw.Client for the whole bot and reuse it. It holds an aiohttp session, so creating one per command wastes connections. RiotService in riot.py owns the client, and the bot closes it on shutdown:
bot.py
Intents.default() is enough.
Account linking with RSO
/link sends the user a one-time “Sign in with Riot” button. valaw’s create_RSO_link builds the URL. The random state value ties the sign-in to the Discord user and protects against CSRF:
rso.py
aiohttp web server, started with the bot, trades the code for an access token. It then asks Riot who signed in using GET_getByAccessToken:
rso.py
/link URL to a friend, and the friend’s Riot sign-in would silently attach to the sender’s Discord account. The page shows the Discord username, not the display name, because anyone can copy a display name.
Two more details keep linking safe:
/unlinkcancels unfinished sign-ins.revoke()bumps a per-user counter, and the callback and confirmation both check it after waiting on Riot. A sign-in that was in progress can’t re-link an account the user just removed.- Failed sign-ins are logged with the cause, but never the authorization code or access token. The player sees a generic error page.
links.json. That’s fine for one bot process. Move to SQLite or Postgres when you outgrow it.
To turn it on, add your RSO credentials to .env:
.env
Caching
Most VALORANT data barely changes. Game content changes once per patch, a finished match never changes, and leaderboards update every few minutes. Caching keeps the bot fast and well under Riot’s rate limits:riot.py
TTLCache is a small in-memory dictionary with expiry times. If you run more than one bot process, swap it for Redis.
Rate-limit handling
The bot doesn’t need any rate-limit code of its own.valaw.Client waits to stay under your key’s limits and retries 429 Too Many Requests responses, so the bot just calls it:
riot.py
Finding the right region
Account calls go to a cluster (americas, europe, asia), but match data lives on the player’s region (na, eu, ap, kr, …). Don’t make users type their region. Ask Riot for the player’s active shard:
riot.py
Turning IDs into names
Match data refers to maps by asset path and agents by UUID. Look them up in the cached content response with valaw’s content lookups:riot.py
/lastmatch command gets its stats from player_summary, so the bot doesn’t compute ACS or headshot percentage itself.
The /lastmatch command
/lastmatch checks for a linked account before calling Riot. It then defers, because Discord only waits 3 seconds for a response and a cold lookup can take longer:
bot.py
One error handler for every command
Commands don’t containtry/except. Errors bubble up to one tree-wide handler, which turns Riot’s status codes into messages a user can act on:
riot.py
Before you apply for a production key
Riot reviews your bot before it gets a production key. This example already covers the basics: RSO opt-in,/unlink, anonymous leaderboard entries stay anonymous, and the legal notice in /about. You’ll also need a landing page with screenshots of each command. Change BOT_NAME in bot.py so the legal notice uses your bot’s name. See the Discord bot checklist.
Ideas for next steps
- Match tracking. Poll linked players’ match lists every few minutes and post new results to a channel.
- Persistent cache. Move
TTLCacheto Redis so restarts don’t start from zero. - Deploy it. Any always-on host with HTTPS works for the RSO callback, such as a small VPS behind Caddy, Railway, or Fly.io.
Full source on GitHub
Clone the complete bot from examples/discord-bot.

