League management for Discord: divisions, promotion and relegation, playoffs and stats.
Features · Quick start · Running a season · Database · Website data · Deployment · Development · Troubleshooting
Touchline started as the NBL's Discord bot and now runs any league with any number of tiers. Admins create divisions and teams from Discord. One league-wide season moves through a regular season, a D1 championship playoff, promotion and relegation (direct or through playoffs), and then rolls over into the next season with every team in its new division. A /dashboard panel runs everything, a league board in a channel keeps itself up to date, and read-only views let a website show the same data.
| Area | What it does |
|---|---|
| Divisions and groups | Any number of divisions (D1 at the top, then D2, D3 and so on), each with a name, code, colour and emoji. Divisions can be split into optional groups (conferences). Each division has its own rules: ranking by points scored or by points per win, the games limit between two teams, and whether draft picks are used. |
| Seasons | One season for the whole league: setup → regular season → postseason → offseason → next season. Each season records which division every team played in, so standings from past seasons keep their old divisions. |
| Promotion and relegation | Set between each pair of divisions: a number of direct swaps, plus playoff swaps decided by separate promotion and relegation brackets or by head-to-head series between the two divisions. Standings show the zones as they change. |
| D1 playoffs | A seeded championship bracket of 2 to 16 teams, with byes for the top seeds, best-of per round, and seeding overall or by group. Before the first game, admins can swap seeds to settle a tie by hand. |
| Dashboard | /dashboard for admins: divisions, teams (owners, logos), season steps, playoffs, matches, transactions and server settings, all inside one message. |
| League board | A channel showing an overview, a standings image per division coloured by zone, and the brackets. It edits itself about 10 seconds after every result. Playoff results, champions and promotions are announced too. |
| Score reporting | /report-score, /report-stats and /report-stats-bulk. In the regular season a game counts in the two teams' division, up to the division's games limit. In the postseason it goes to the open playoff series between the two teams. |
| Stats and cards | Standings, leaderboards, averages and totals, player and team comparisons, player cards, match tables and season summaries, per division or for the whole league. |
| Transactions and trades | Signing offers, releases, trades with approval, ownership transfers, admin force moves, a transaction blocker and an audit log. A player plays for one team per season, and an owner owns one team. |
| Draft picks | Per division, turned on in its rules. Picks are traded within their division. |
| Website data | Read-only views in a Supabase api schema, readable with the public (publishable) key. Discord ids and private tables stay hidden. |
| Monitoring | Health, readiness and metrics endpoints, and error alerts to a Discord webhook. |
Requires Python 3.12 and a Supabase project (or any Postgres reachable through PostgREST).
git clone https://github.com/organpy/Touchline.git touchline && cd touchline
python -m venv .venv && source .venv/bin/activate # Windows: .\.venv\Scripts\Activate.ps1
pip install -r requirements.txt
cp .env.example .env # then fill in the values below
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f db/migrations/init.sql
python bot.pyInvite the bot with the bot and applications.commands scopes, then run /dashboard in your server. The Settings section is the place to start.
Settings come from environment variables or a .env file in the project folder.
| Variable | Default | Purpose |
|---|---|---|
DISCORD_TOKEN |
Bot token from the Discord developer portal | |
APPLICATION_ID |
The bot's application id | |
BOT_OWNER_ID |
Your Discord user id (owner-only commands) | |
SUPABASE_URL |
https://<project>.supabase.co |
|
SUPABASE_KEY |
The service_role or sb_secret_... key. The bot refuses to start with the public anon key |
|
DATABASE_URL |
Postgres connection string, used only by psql and the scripts |
|
ERROR_WEBHOOK_URL |
Discord webhook that receives every error log | |
HEALTH_CHECK_HOST / HEALTH_CHECK_PORT |
127.0.0.1 / 8080 |
Where the health server listens. Use 0.0.0.0 when probed from outside the host |
Keep .env private. It is git-ignored.
First setup
Open /dashboard → Settings:
- Admin role: who can use the dashboard and admin commands. Without one, only server administrators can.
- Channels: transactions, admin commands (leave unset to allow everywhere), trading, announcements and the league board.
- League name: shown in embed footers and on the board, for example National Bobble League.
- Signing offers: how long offers last and whether the league must approve signings.
Changes save straight away.
Divisions and groups
- Dashboard → Divisions → Add division with a name, a code (
D1) and an optional colour and emoji. New divisions go to the bottom tier. Use Move up and Move down to reorder. - Rules: ranking (
points_scoredorwin_pointswith points per win), games per matchup and draft picks. - Groups: add groups such as East and West, then put teams in them from the team page.
Ties in the standings are broken by point difference, then points for, then head-to-head, wins and finally name.
Teams, owners and logos
Dashboard → Teams → Add team with a name and a short code. On the team page:
- pick its division (and group) for the season
- Set owner (one team per owner)
- upload a logo. It is squared to 512×512, stored in the
team-logosSupabase Storage bucket and used on standings images and cards. Create the bucket once, as public, in Supabase → Storage.
Large leagues get a division-first team picker, so selects never cut off at 25 teams.
Promotion and relegation
Movement is set on the upper division of each pair: Dashboard → Divisions → D1 → Pro/rel below.
- Direct swaps: the bottom N of the upper division go down and the top N of the lower division go up.
- Playoff swaps: P more places decided by playoffs, in one of two modes:
- separate: the upper division plays a relegation bracket (the losers advance, and whoever is left is relegated) and the lower division plays a promotion bracket. Sizes are written like
4 / 4(upper / lower). A relegation bracket must have P×2, P×4, ... teams, so no team gets a bye. - cross: P head-to-head series, the best upper playoff team against the worst lower one. The winner takes the upper place.
- separate: the upper division plays a relegation bracket (the losers advance, and whoever is left is relegated) and the lower division plays a promotion bracket. Sizes are written like
The division page warns you when the zones overlap for the number of teams the division has.
D1 playoffs
Dashboard → Divisions → D1 → Championship: turn it on, choose the number of teams (2-16), best-of per round (for example 1, 3, 5) and seeding (overall or by group). When the bracket isn't a full power of two, the top seeds get byes.
Reporting games
Owners report with /report-score (scores only), /report-stats (one game with player stats) or /report-stats-bulk. The preview shows where the game will count:
- Regular season: both teams must be in the same division. The division's games limit applies.
- Postseason: the game goes to the open series between the two teams, and the series moves on when someone has enough wins.
Standings and the league board update automatically. A game entered by mistake can be voided in Dashboard → Matches.
Ending the regular season and the postseason
Dashboard → Season shows the next step, and a preview before anything changes:
- Start regular season: checks every division has enough teams and that the zones fit.
- End regular season: locks the standings and creates every bracket (championship, promotion, relegation, cross).
- Dashboard → Playoffs: follow each bracket. You can swap seeds before the first game, or decide a series by forfeit.
- Finish postseason: when every bracket is done, review the champion and every promotion and relegation, then confirm.
Need to recalculate after fixing data? Season → Recalculate standings does it.
Starting the next season
In the offseason, Season → Start next season asks for a name (suggested from the last one). Teams move to their new divisions, active rosters carry over, and the old season keeps its standings, brackets and divisions for /standings, /history and the website.
Transactions, trades and draft picks
Dashboard → Transactions opens the existing panels: players, rosters, draft picks, trades, offers, overrides, rules, the blocker and the audit log. Owners use /sign, /release, /trade, /transfer-ownership, /roster and /picks. Admins also have /admin-force-sign, /admin-force-release and /admin-force-pick.
Draft picks only exist in divisions with Draft picks turned on in their rules.
The schema is a single file, db/migrations/init.sql. Apply it by hand with psql (DATABASE_URL is the Supabase Postgres connection string):
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f db/migrations/init.sqlIt turns on row level security and removes every privilege from the public anon and authenticated roles on base tables. Only the bot's service role reads and writes them.
Updating an existing database (hardening release)
init.sql already includes everything below, so a new database needs nothing extra. A database created from an earlier init.sql needs one migration, which is safe to run while the bot is stopped:
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f db/migrations/20261108_hardening.sqlIt adds:
-
Safe names: team, division, group and season names, and player names, can no longer hold backticks,
<>, line breaks,@everyoneor@here. The checks are added asNOT VALID, so older rows never block the migration, but new and changed rows must pass. To find older names that break the rule, and then check them all:SELECT id, name FROM teams WHERE NOT is_safe_name(name); -- same for divisions, division_groups, seasons SELECT id, display_name FROM players WHERE NOT is_safe_name(display_name) OR length(btrim(display_name)) NOT BETWEEN 1 AND 64; ALTER TABLE teams VALIDATE CONSTRAINT ck_teams_name_safe; -- once the rows are fixed (and the others likewise)
-
Match locking: the games-per-matchup limit and playoff series status are checked inside the database, one reporter at a time per pair or series.
-
record_match: saves a game and its player stats in one transaction. Reporting the same game twice stores it once. -
get_player_career: the per-season rows behind the player card.
Upgrading from the old NBL schema
The new schema doesn't keep the NBL/MBL/NCBA data. Back up first:
pg_dump "$DATABASE_URL" --format=custom --file=nbl-backup-$(date +%Y%m%d).dump
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f db/reset_legacy.sql # drops the old league tables
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f db/migrations/init.sqlbot_config (server settings such as the admin role and channels) is kept. The old dated migrations are in db/migrations/legacy/ for reference only. After upgrading, delete data/.command_sync_hash so the renamed commands sync.
The SQL tests run inside one transaction and roll back. Run them on a Supabase branch or a local Postgres (run db/tests/local_roles.sql and init.sql first on plain Postgres):
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f db/tests/league_schema_test.sql # ALL LEAGUE TESTS PASSED
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f db/tests/execute_trade_proposal_test.sql # ALL TRADE TESTS PASSEDinit.sql creates read-only views in an api schema: divisions, division_groups, seasons, teams, season_teams, players, rosters, matches, player_stats, standings, brackets, bracket_entries, series and season_movements. They leave out Discord ids, trades, signings, server settings and voided games.
To turn them on, add api under Supabase → Settings → API → Exposed schemas. Then any site can read them with the publishable key:
curl "$SUPABASE_URL/rest/v1/standings?select=*,teams(name,logo_url)&order=rank" \
-H "apikey: $SUPABASE_PUBLISHABLE_KEY" \
-H "Accept-Profile: api"With supabase-js: supabase.schema('api').from('standings').select('*'). Standings are written by the bot after every result, so the website and Discord always agree. The base tables stay locked: the same request without Accept-Profile: api is refused.
deploy/touchline.service runs the bot under systemd from /opt/touchline as an unprivileged touchline user, restarts it on failure and stops it cleanly.
sudo useradd --system --home-dir /opt/touchline --shell /usr/sbin/nologin touchline
sudo git clone https://github.com/organpy/Touchline.git /opt/touchline
cd /opt/touchline
sudo python3.12 -m venv .venv
sudo .venv/bin/pip install -r requirements.txt
sudo cp .env.example .env && sudo nano .env
sudo chown -R touchline:touchline /opt/touchline && sudo chmod 600 .env
sudo cp deploy/touchline.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable --now touchline
journalctl -u touchline -fTo update: sudo -u touchline git -C /opt/touchline pull, reinstall requirements if they changed, then sudo systemctl restart touchline. Moving from the old nblbot unit? Stop and disable it before you enable touchline.
The health server answers on /health (liveness), /ready (503 while disconnected from Discord) and /metrics. With ERROR_WEBHOOK_URL set, every error log is posted to that channel, at most 10 per minute and never with pings.
- Admins: a server administrator, or a member with the admin role set in Dashboard → Settings. Every admin command checks this, and every admin panel checks it again on each click, so removing someone's rights takes effect straight away. Only a real server administrator can change the admin role.
- Rate limit: each person gets 8 command uses per 30 seconds. Commands that draw images or run heavy queries (
/standings,/match-table,/player-card,/season-summary,/historyand the three report commands) cost 3. Autocomplete and button clicks are not counted. At most 2 images render at once. - Names: team, division, group, season and player names are checked when they are saved (see above), and the bot mentions nobody unless a message names a specific person to ping, so a name like
@everyonecan never ping anyone. - Reporting: the database enforces the games limit and series status under a lock, so two owners reporting at the same moment can't both squeeze in. A game and its stats save together, and pressing Confirm twice saves it once.
- Database load: team and league lookups come from caches that reload when something changes.
/metricsshowstotal_queriesand a count per table, which is the easiest way to see what a change costs.
pip install -r requirements-dev.txt
pytest test/ -qscripts/seed_demo.py fills a test database with divisions, teams and a season in setup (it previews without --apply):
python scripts/seed_demo.py --apply --divisions 4 --teams 8scripts/preview_images.py renders the standings, playoff brackets, match tables and player cards with dummy data (no Discord or database needed) into preview_output/, with an index.html gallery. Add --open to open it in your browser.
python scripts/preview_images.py --openOn a dev server, set ENABLE_DEMO_COMMANDS=true to get admin-only /demo commands that run on made-up data and save nothing:
| Command | What it does |
|---|---|
/demo bracket |
A bracket image of any kind (championship, promotion, relegation, promotion/relegation), 2–16 teams, any best-of, at a chosen stage |
/demo playoffs |
An interactive playoff: play a game, finish a series or round, play to the end, decide by forfeit, swap seeds before the first game, reset |
/demo standings, /demo match-table, /demo player-card |
Each image with dummy data and options |
/demo gallery |
Every preview image at once |
/demo errors |
Every kind of error reply |
Slash commands sync on startup only when their definitions changed, tracked in data/.command_sync_hash. Delete that file to force a sync. Domain rules (standings, brackets, movements, the season lifecycle and reporting) live in services/ with no Discord code, and most tests target them directly.
bot.py entry point: caches, cog discovery, command sync, health server
cogs/
dashboard/ /dashboard pages (divisions, teams, season, playoffs, matches, settings)
board/ auto-updating league board and announcements
score/ /report-score, /report-stats, /report-stats-bulk
stats/ standings, leaderboards, cards, history
transactions/ signings, releases, trades, picks
admin/ force commands and transaction panels
help/ /help and /admin-help
services/ league rules: standings, brackets, movements, lifecycle, reporting
db/
migrations/init.sql the schema, RPCs and website views
reset_legacy.sql removes the old NBL schema
tests/ SQL tests
league_repository.py divisions, seasons, standings, brackets
utils/ caches, image generators, logo storage, config
scripts/ demo data, deployment checks, command docs
deploy/ systemd unit
test/ pytest suite
Commands don't show up in Discord. Delete data/.command_sync_hash and restart; the bot then syncs on startup. Discord can take a minute to show new commands, and the bot must have been invited with applications.commands.
The bot exits saying SUPABASE_KEY is the anon key. It needs the service_role (or sb_secret_...) key from Supabase → Settings → API. The publishable key is only for websites reading the api views.
The league board doesn't update. Check the board channel in Dashboard → Settings, and that the bot can send messages, embed links and attach files there. Refresh board redraws it immediately. If you delete the board messages, the bot posts them again on its next refresh.
"Games can only be reported during the regular season or the playoffs." The season is in setup or the offseason. Start the regular season from Dashboard → Season.
"These teams don't have an open playoff series right now." In the postseason, a game counts only when the two teams meet in a series that is ready, so both of its teams must be decided. Check Dashboard → Playoffs.
"A later playoff series already has results. Void those games first." A result that fed a later series can't be voided while that series has games. Void the later games in Dashboard → Matches first, newest first.
"Rate Limited". You used too many commands too fast: 8 per 30 seconds, with image and report commands costing 3. The message says how long to wait.
A name is refused. Names can't contain backticks, < >, line breaks, @everyone or @here. Remove those and try again. Discord display names with such characters are cleaned automatically when a player is added.
"Every game between these teams has already been recorded." The pair has played all the games its division allows (Dashboard → Divisions → Rules). If a game was entered by mistake, void it in Dashboard → Matches and report it again.
Team logos don't appear. Create a public team-logos bucket in Supabase Storage, then upload the logo again from the team page.
MIT. See LICENSE.