7 Commits

Author SHA1 Message Date
Rene Arumetsa
fb08bc56d7 chore(teams): log role batch vs bot_top when edit_role_positions fails
A 50013 on positioning despite Manage Roles points to a role in the reorder
batch sitting at/above the bot's top role. Log the batch and bot_top so the
offending role is identifiable from the journal.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XPRsW4tazVtYi2jNzVQkre
2026-09-04 00:54:00 +03:00
Rene Arumetsa
a280ba05cc feat(teams): add Fienta as primary Discord->team source (sheet fallback)
The registration-log sheet only has in-game nicknames, so matching Discord
users to teams failed for ~10 of 40 teams. Fienta collects each competitor's
Discord username (+ sometimes user ID) and team name per ticket, giving a
reliable Discord-identity -> team mapping (validated: 201 usernames, 42 teams).

- core/fienta.py: token-auth client; fetch /events/{id}/tickets?attendees=true,
  parse competitor/coach/substitute tickets into {username|id -> team} and
  {team -> game}; exclude visitor/supporter/LAN/early-bird/waiting-list. No-op
  when FIENTA_API_TOKEN/FIENTA_EVENT_ID unset.
- member_sync: resolve_team() tries Fienta (id, then username) then the sheet;
  all_managed_team_names() and team_dividers() merge both sources.
- /teamsync + hourly task refresh Fienta alongside the sheet; enabled when
  either source is configured.
- config + .env.example: FIENTA_API_TOKEN, FIENTA_EVENT_ID.
- tests: fienta parsing (game detection, inclusion rules, id/username mapping).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XPRsW4tazVtYi2jNzVQkre
2026-09-04 00:50:54 +03:00
Rene Arumetsa
3c8927184b feat(teams): grant each participant their game's divider role
Reuse the existing CS2/LoL divider roles as participant tags: sync_team_role
now also adds the divider role for the member's team's game, and strips any
other configured divider role (switched game / dropped out). Matched by ID
against config.TEAM_DIVIDERS, so only the divider roles are ever touched.

- TeamSyncResult gains divider_added / divider_removed; summary tallies both
- /teamsync report and log line surface divider_assigned / divider_removed
- tests: grants the game divider, swaps it on a game switch

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XPRsW4tazVtYi2jNzVQkre
2026-09-04 00:00:41 +03:00
b813ed5f81 Merge pull request 'fix(teams): resolve dividers from tab name + full title, not last notice row' (#9) from feat/teamsync-reposition into master
All checks were successful
Test & Deploy / test (push) Successful in 5s
Test & Deploy / deploy (push) Successful in 7s
Reviewed-on: #9
2026-09-03 20:35:24 +00:00
Rene Arumetsa
c19e67b5ab fix(teams): resolve dividers from tab name + full title, not last notice row
Section titles are stacked single-cell rows: the "TipiLAN 2026 CS2" title
sits above notice rows ("If a team withdraws..."), and parse_team_sections
kept only the LAST one, so the notice clobbered the title and resolve_divider
saw no game/year keywords -> None -> teams never positioned.

- parse_team_sections now accumulates all single-cell rows above a header, so
  the game/year title survives alongside the notices.
- _refresh_teams_sync resolves the divider from "<tab name> <section title>",
  so the game is taken reliably from the CS2/LoL worksheet name while the year
  still comes from the title, keeping year-scoped TEAM_DIVIDER_*_2026 vars.
- regression test: title survives notice rows and still resolves.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XPRsW4tazVtYi2jNzVQkre
2026-09-03 23:32:58 +03:00
b20bca9244 Merge pull request 'feat(teams): resolve team dividers by role ID instead of name' (#8) from feat/teamsync-reposition into master
All checks were successful
Test & Deploy / test (push) Successful in 5s
Test & Deploy / deploy (push) Successful in 6s
Reviewed-on: #8
2026-09-03 19:38:23 +00:00
Rene Arumetsa
d48a436e26 feat(teams): resolve team dividers by role ID instead of name
Divider placement matched the divider role by its exact Discord name, so
renaming the role in Discord silently broke positioning. Switch the
TEAM_DIVIDER_<SUFFIX> config to hold a role ID; resolve the ID to the
role's current name in apply_team_role_positions and keep the existing
name-based ordering maths downstream unchanged.

- config._parse_team_dividers now parses values as ints (rejects non-ints)
- resolve_divider / _team_divider cache / get_team_dividers return IDs
- apply_team_role_positions resolves each ID via guild.get_role once
- .env.example documents IDs and ships the CS2/LoL divider role IDs
- resolve_divider tests updated to assert IDs

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XPRsW4tazVtYi2jNzVQkre
2026-09-03 22:34:34 +03:00
11 changed files with 554 additions and 65 deletions

View File

@@ -10,18 +10,27 @@ DISCORD_TOKEN=
SHEET_ID=your-google-sheet-id-here
# Separate spreadsheet holding tournament team registrations (Team Name + lineup
# of Discord usernames). Optional; drives /teamsync + hourly team-role sync on
# the economy/community bot. Leave unset to disable team-role sync entirely.
# of Discord usernames). Optional; a FALLBACK source for /teamsync + hourly
# team-role sync on the economy/community bot. Leave unset to disable it.
TEAM_SHEET_ID=
# Fienta ticketing - PRIMARY source for team-role sync. The registration collects
# each competitor's Discord username + team name per ticket, so matching is by
# real Discord handle (not game nickname). Get an API token from the Fienta admin
# (organizer settings) and the event's numeric ID from its dashboard URL. Leave
# unset to use only the sheet.
FIENTA_API_TOKEN=
FIENTA_EVENT_ID=
# Where each game's team roles get positioned in the role list. The key suffix
# is matched against the section's title row in the sheet ("TipiLAN 2026 CS2
# Registration Log") - every underscore-separated part must appear in it, so
# CS2_2026 matches only the 2026 CS2 block while a plain CS2 would match any
# year. The value is the EXACT Discord role name to place those teams under.
# year. The value is the Discord role ID to place those teams under (matching by
# ID means renaming the divider role never breaks positioning).
# Optional: sections that match nothing still get their roles, just unpositioned.
TEAM_DIVIDER_CS2_2026=
TEAM_DIVIDER_LOL_2026=
TEAM_DIVIDER_CS2_2026=1498736834656604251
TEAM_DIVIDER_LOL_2026=1498736949706490017
# Path to Google service account credentials JSON
GOOGLE_CREDS_PATH=credentials.json

5
.gitignore vendored
View File

@@ -9,4 +9,7 @@ pocketbase.exe
pocketbase
pb_data/
pb_migrations/
logs/
logs/
fientalog
fientatickets
fientaorders

25
bot.py
View File

@@ -21,7 +21,7 @@ import psutil
import config
import strings as S
from core import economy, pb_client, sheets
from core import economy, fienta, pb_client, sheets
from core.admin import is_bot_admin
from core.member_sync import SyncResult, sync_all_team_roles
from commands.dev_member_commands import register_dev_member_commands
@@ -339,31 +339,35 @@ async def before_birthday_daily():
@tasks.loop(hours=1)
async def team_sync_hourly():
"""Reload the tournament registration sheet and re-apply team roles.
"""Reload the tournament registration (Fienta + sheet) and re-apply team roles.
Economy profile only (the tournament players live in the community guild).
Runs the first iteration immediately on start, so this also covers the
initial load at boot. No-op when TEAM_SHEET_ID is unset.
initial load at boot. No-op when neither Fienta nor the team sheet is set.
"""
if IS_DEV_PROFILE or not config.TEAM_SHEET_ID:
if IS_DEV_PROFILE or not (config.TEAM_SHEET_ID or config.FIENTA_API_TOKEN):
return
try:
rosters = await sheets.refresh_teams()
fienta_teams = await fienta.refresh_teams()
except Exception as e:
log.error("team_sync_hourly: failed to load team sheet: %s", e)
log.error("team_sync_hourly: failed to load team data: %s", e)
return
if not rosters:
if not rosters and not fienta_teams:
return
guild = bot.get_guild(config.GUILD_ID)
if guild is None:
log.warning("team_sync_hourly: guild %s not found", config.GUILD_ID)
return
summary = await sync_all_team_roles(guild, log)
if summary.assigned or summary.removed or summary.created or summary.positioned or summary.errors:
if (summary.assigned or summary.removed or summary.created or summary.positioned
or summary.divider_assigned or summary.divider_removed or summary.errors):
log.info(
"team_sync_hourly: assigned=%d, removed=%d, created=%d, positioned=%d, errors=%d",
"team_sync_hourly: assigned=%d, removed=%d, created=%d, positioned=%d, "
"divider_assigned=%d, divider_removed=%d, errors=%d",
summary.assigned, summary.removed, len(summary.created),
summary.positioned, len(summary.errors),
summary.positioned, summary.divider_assigned, summary.divider_removed,
len(summary.errors),
)
for err in summary.errors:
log.warning("team_sync_hourly: %s", err)
@@ -472,7 +476,8 @@ async def on_ready():
log.info("Birthday daily task started (fires 09:00 Tallinn time)")
# Start hourly tournament team-role sync (economy/community guild)
if not IS_DEV_PROFILE and config.TEAM_SHEET_ID and not team_sync_hourly.is_running():
if (not IS_DEV_PROFILE and (config.TEAM_SHEET_ID or config.FIENTA_API_TOKEN)
and not team_sync_hourly.is_running()):
team_sync_hourly.start()
log.info("Team-role sync task started (hourly, from the registration sheet)")

View File

@@ -14,7 +14,7 @@ import logging
import discord
from discord import app_commands
from core import sheets
from core import fienta, sheets
from core.admin import bot_admin_check
from core.member_sync import sync_all_team_roles
import strings as S
@@ -38,24 +38,28 @@ def register_economy_team_commands(
try:
rosters = await sheets.refresh_teams()
fienta_teams = await fienta.refresh_teams()
except Exception as e:
await interaction.followup.send(
S.TEAMSYNC_UI["refresh_error"].format(error=e), ephemeral=True
)
return
if not rosters:
if not rosters and not fienta_teams:
await interaction.followup.send(S.TEAMSYNC_UI["disabled"], ephemeral=True)
return
summary = await sync_all_team_roles(guild, log)
await interaction.followup.send(_format_summary(summary), ephemeral=True)
log.info(
"/teamsync - scanned=%d, assigned=%d, removed=%d, created=%d, positioned=%d, errors=%d",
"/teamsync - scanned=%d, assigned=%d, removed=%d, created=%d, "
"positioned=%d, divider_assigned=%d, divider_removed=%d, errors=%d",
summary.scanned,
summary.assigned,
summary.removed,
len(summary.created),
summary.positioned,
summary.divider_assigned,
summary.divider_removed,
len(summary.errors),
)
@@ -74,6 +78,10 @@ def _format_summary(summary) -> str:
lines.append(S.TEAMSYNC_UI["created"].format(roles=", ".join(unique)))
if summary.positioned:
lines.append(S.TEAMSYNC_UI["positioned"].format(count=summary.positioned))
if summary.divider_assigned:
lines.append(S.TEAMSYNC_UI["divider_assigned"].format(count=summary.divider_assigned))
if summary.divider_removed:
lines.append(S.TEAMSYNC_UI["divider_removed"].format(count=summary.divider_removed))
if summary.errors:
lines.append(S.TEAMSYNC_UI["errors"].format(count=len(summary.errors)))

View File

@@ -28,6 +28,13 @@ SHEET_ID = os.getenv("SHEET_ID")
TEAM_SHEET_ID = os.getenv("TEAM_SHEET_ID")
GOOGLE_CREDS_PATH = os.getenv("GOOGLE_CREDS_PATH", "credentials.json")
# Fienta ticketing: the registration collects each competitor's Discord username
# (+ sometimes Discord user ID) and team name per ticket, giving a reliable
# Discord-identity -> team mapping the nickname-only sheet cannot. Primary source
# for team-role sync; the sheet stays as a fallback. Unset -> Fienta is skipped.
FIENTA_API_TOKEN = os.getenv("FIENTA_API_TOKEN", "")
FIENTA_EVENT_ID = os.getenv("FIENTA_EVENT_ID", "")
_LEGACY_GUILD_ID = _env_int("GUILD_ID", 0)
GUILD_ID_DEV = _env_int("GUILD_ID_DEV", _LEGACY_GUILD_ID)
GUILD_ID_ECONOMY = _env_int("GUILD_ID_ECONOMY", _LEGACY_GUILD_ID)
@@ -72,32 +79,39 @@ BOT_ADMIN_ROLES: dict[int, set[int]] = _parse_admin_roles(os.getenv("DISCORD_ADM
_TEAM_DIVIDER_PREFIX = "TEAM_DIVIDER_"
def _parse_team_dividers() -> dict[str, str]:
"""Collect TEAM_DIVIDER_<SUFFIX> env vars into {suffix: divider role name}.
def _parse_team_dividers() -> dict[str, int]:
"""Collect TEAM_DIVIDER_<SUFFIX> env vars into {suffix: divider role ID}.
The suffix says which sheet sections the divider covers, the value is the
exact Discord role name their teams get positioned under:
Discord role ID their teams get positioned under:
TEAM_DIVIDER_CS2_2026="====== COUNTER-STRIKE 2 2026 ======"
TEAM_DIVIDER_CS2_2026=1498736834656604251
Every underscore-separated part of the suffix must appear in the section's
title row, so `CS2_2026` matches only "TipiLAN 2026 CS2 Registration Log"
while a plain `CS2` would match that section in any year. Defining a var is
what switches positioning on for those sections; teams whose section matches
nothing are still granted their role, just never moved.
Matching by ID (not name) means renaming the divider role in Discord never
breaks positioning. Every underscore-separated part of the suffix must appear
in the section's title row, so `CS2_2026` matches only "TipiLAN 2026 CS2
Registration Log" while a plain `CS2` would match that section in any year.
Defining a var is what switches positioning on for those sections; teams
whose section matches nothing are still granted their role, just never moved.
"""
dividers: dict[str, str] = {}
dividers: dict[str, int] = {}
for key, value in os.environ.items():
if not key.startswith(_TEAM_DIVIDER_PREFIX):
continue
suffix = key[len(_TEAM_DIVIDER_PREFIX):].strip().lower()
name = value.strip()
if suffix and name:
dividers[suffix] = name
raw = value.strip()
if not suffix or not raw:
continue
try:
dividers[suffix] = int(raw)
except ValueError:
raise SystemExit(
f"{key}: expected a Discord role ID (integer), got {raw!r}"
)
return dividers
TEAM_DIVIDERS: dict[str, str] = _parse_team_dividers()
TEAM_DIVIDERS: dict[str, int] = _parse_team_dividers()
PB_URL = os.getenv("PB_URL", "http://127.0.0.1:8090")
PB_ADMIN_EMAIL = os.getenv("PB_ADMIN_EMAIL", "")

190
core/fienta.py Normal file
View File

@@ -0,0 +1,190 @@
"""Fienta ticketing integration - authoritative Discord -> team mapping.
The tournament registration on Fienta collects, per competitor ticket, the
player's Discord username (and sometimes Discord user ID), their team name
(order-level, echoed onto each attendee), and the ticket type (which names the
game). That gives a reliable Discord-identity -> team mapping the nickname-only
registration sheet cannot, so this is the PRIMARY source for team-role sync,
with :mod:`core.sheets` kept as a fallback.
Enabled by ``FIENTA_API_TOKEN`` + ``FIENTA_EVENT_ID``; when either is unset all
caches stay empty and every getter is a no-op, so team sync silently falls back
to the sheet. Only real team members get roles - competitor, coach/manager and
substitute tickets are included; visitors, supporters, LAN-access, early-bird
and waiting-list tickets are excluded.
"""
from __future__ import annotations
import logging
import aiohttp
import config
log = logging.getLogger(__name__)
_API_BASE = "https://fienta.com/api/v1"
_PAGE_SIZE = 1000 # Fienta's max per page; one page covers a whole tournament
# Ticket-type titles that represent an actual team member who should get a role.
_INCLUDED_KEYWORDS = ("competitor", "coach", "manager", "substitute")
# ...unless the title also matches one of these (visitors etc. are never players).
_EXCLUDED_KEYWORDS = (
"visitor", "supporter", "lan area", "early bird", "waiting list", "waitlist",
)
# Attendee custom-field keys. Fienta appends the field id to the machine name;
# these come from GET /events/{id}/custom-fields for event 176532.
_F_DISCORD_USERNAME = "discord_username_134871"
_F_DISCORD_USERID = "discord_user_id_135840"
_F_TEAM_NAME = "team_name_134821"
_GAME_KEYWORDS: list[tuple[str, tuple[str, ...]]] = [
("CS2", ("counter-strike", "counter strike", "cs2", "csgo", "cs:go")),
("LoL", ("league of legends", "league", "lol")),
]
# Caches, rebuilt by refresh_teams()/parse_tickets().
_by_username: dict[str, str] = {} # discord username (lower) -> team name
_by_userid: dict[str, str] = {} # discord user id (str) -> team name
_team_game: dict[str, str] = {} # team name -> "CS2" / "LoL"
_team_names: set[str] = set()
def _norm_username(name: str) -> str:
"""Normalise a Discord username for matching: lowercased, no leading @."""
return name.strip().lower().lstrip("@")
def _detect_game(ticket_type_title: str) -> str | None:
"""Return the game code for a ticket-type title, or None if unrecognised."""
title = ticket_type_title.lower()
for game, keywords in _GAME_KEYWORDS:
if any(k in title for k in keywords):
return game
return None
def _is_included(ticket_type_title: str) -> bool:
"""True when this ticket type is an actual team member (not a visitor etc.)."""
title = ticket_type_title.lower()
if any(k in title for k in _EXCLUDED_KEYWORDS):
return False
return any(k in title for k in _INCLUDED_KEYWORDS)
def _game_divider_ids() -> dict[str, int]:
"""Map game code -> divider role id, derived from ``config.TEAM_DIVIDERS``.
Reuses the same ``TEAM_DIVIDER_<SUFFIX>`` role IDs the sheet path uses: a
suffix like ``cs2_2026`` contributes its id to game ``CS2``.
"""
out: dict[str, int] = {}
for suffix, rid in config.TEAM_DIVIDERS.items():
parts = suffix.split("_")
if any(p in ("cs2", "cs", "csgo") for p in parts):
out.setdefault("CS2", rid)
if any(p in ("lol", "league") for p in parts):
out.setdefault("LoL", rid)
return out
def parse_tickets(tickets: list[dict]) -> None:
"""Rebuild the caches from a list of Fienta ticket objects.
Pure/synchronous so it can be unit-tested without hitting the API.
"""
global _by_username, _by_userid, _team_game, _team_names
by_username: dict[str, str] = {}
by_userid: dict[str, str] = {}
team_game: dict[str, str] = {}
for ticket in tickets:
rows = ticket.get("rows") or []
if not rows:
continue
row = rows[0]
title = (row.get("ticket_type") or {}).get("title", "")
if not _is_included(title):
continue
attendee = row.get("attendee") or {}
team = (attendee.get(_F_TEAM_NAME) or "").strip()
if not team:
continue
game = _detect_game(title)
# Keep the first non-None game seen for a team (all its tickets agree).
team_game[team] = game or team_game.get(team)
uname = _norm_username(attendee.get(_F_DISCORD_USERNAME) or "")
uid = (attendee.get(_F_DISCORD_USERID) or "").strip()
if uname:
by_username[uname] = team
if uid.isdigit():
by_userid[uid] = team
_by_username = by_username
_by_userid = by_userid
_team_game = team_game
_team_names = set(team_game)
async def refresh_teams() -> set[str]:
"""Fetch competitor tickets from Fienta and rebuild the caches.
No-op returning an empty set when ``FIENTA_API_TOKEN`` / ``FIENTA_EVENT_ID``
are unset, so the caller transparently falls back to the sheet.
"""
if not config.FIENTA_API_TOKEN or not config.FIENTA_EVENT_ID:
parse_tickets([])
return set()
url = f"{_API_BASE}/events/{config.FIENTA_EVENT_ID}/tickets"
headers = {"Authorization": f"Bearer {config.FIENTA_API_TOKEN}"}
tickets: list[dict] = []
async with aiohttp.ClientSession() as session:
page = 1
while True:
params = {"attendees": "true", "per_page": str(_PAGE_SIZE), "page": str(page)}
async with session.get(url, headers=headers, params=params) as resp:
resp.raise_for_status()
data = await resp.json()
batch = data.get("tickets") or []
tickets.extend(batch)
if len(batch) < _PAGE_SIZE:
break
page += 1
parse_tickets(tickets)
log.info(
"Fienta: %d tickets -> %d teams, %d discord usernames, %d discord ids",
len(tickets), len(_team_names), len(_by_username), len(_by_userid),
)
return set(_team_names)
def get_team_for_username(username: str) -> str | None:
"""Team the given Discord username is registered on, or None."""
return _by_username.get(_norm_username(username))
def get_team_for_userid(user_id: int) -> str | None:
"""Team the given Discord user ID is registered on, or None (IDs are sparse)."""
return _by_userid.get(str(user_id))
def all_team_names() -> set[str]:
"""Every team name seen in the included Fienta tickets."""
return set(_team_names)
def get_team_game(team: str) -> str | None:
"""Game code ("CS2"/"LoL") for a team, or None."""
return _team_game.get(team)
def get_team_dividers() -> dict[str, int]:
"""{team name -> divider role id}, via each team's game and config dividers."""
game_div = _game_divider_ids()
return {
team: game_div[game]
for team, game in _team_game.items()
if game and game in game_div
}

View File

@@ -11,9 +11,32 @@ from zoneinfo import ZoneInfo
import discord
import config
from . import sheets
from . import fienta, sheets
log = logging.getLogger(__name__)
def resolve_team(member: discord.Member) -> str | None:
"""Team a member is registered on: Fienta first (by ID, then username),
then the sheet by username. Fienta is authoritative; the sheet is fallback."""
return (
fienta.get_team_for_userid(member.id)
or fienta.get_team_for_username(member.name)
or sheets.get_team_for_username(member.name)
)
def all_managed_team_names() -> set[str]:
"""Union of every team name from Fienta and the sheet - the only role names
team sync ever adds or removes."""
return fienta.all_team_names() | sheets.all_team_names()
def team_dividers() -> dict[str, int]:
"""{team -> divider role id} merged from both sources; Fienta wins on overlap."""
merged = dict(sheets.get_team_dividers())
merged.update(fienta.get_team_dividers())
return merged
_PLACEHOLDER = {"-", "x", "n/a", "none", "ei"}
_TZ = ZoneInfo("Europe/Tallinn")
@@ -65,11 +88,13 @@ class TeamSyncResult:
added: str | None = None # team role name granted, if any
removed: list[str] = field(default_factory=list) # stale team roles taken away
created: str | None = None # team role name auto-created in the guild, if any
divider_added: str | None = None # game divider role granted as participant tag
divider_removed: list[str] = field(default_factory=list) # stale divider roles taken away
errors: list[str] = field(default_factory=list)
@property
def changed(self) -> bool:
return bool(self.added or self.removed)
return bool(self.added or self.removed or self.divider_added or self.divider_removed)
@dataclass
@@ -80,6 +105,8 @@ class TeamSyncSummary:
removed: int = 0
created: list[str] = field(default_factory=list)
positioned: int = 0 # team roles moved under a divider
divider_assigned: int = 0 # members given their game divider role
divider_removed: int = 0 # stale game divider roles taken away
changes: list[str] = field(default_factory=list) # human-readable per-member lines
errors: list[str] = field(default_factory=list)
@@ -260,18 +287,21 @@ async def sync_team_role(
* grants the role for the team they're registered on (auto-creating that
role in the guild when it does not exist yet);
* removes any *other* team role they still carry (left / switched teams).
* removes any *other* team role they still carry (left / switched teams);
* grants their game's divider role as a participant tag (and strips any
other configured divider role they still carry, i.e. switched game).
Only role NAMES present in the team sheet are ever added or removed, so no
Only team role NAMES present in the team sheet and the configured divider
role IDs (``config.TEAM_DIVIDERS``) are ever added or removed, so no
unrelated role is ever at risk. When ``TEAM_SHEET_ID`` is unset the caches
are empty and this is a no-op returning an unchanged result.
"""
result = TeamSyncResult()
team_name = sheets.get_team_for_username(member.name)
all_teams = sheets.all_team_names()
team_name = resolve_team(member)
all_teams = all_managed_team_names()
if not all_teams:
return result # feature switched off (no team sheet loaded)
return result # feature switched off (no Fienta token and no team sheet)
desired: discord.Role | None = None
if team_name:
@@ -307,6 +337,35 @@ async def sync_team_role(
except discord.HTTPException as e:
result.errors.append(f"Tiimirolli eemaldamise viga kasutajale {member}: {e}")
# --- Participant divider role (the game's divider role doubles as a tag) ---
# Grant the divider role for the member's team's game, and strip any other
# configured divider role (switched game / dropped out). Matched by ID, so
# only the roles named in config.TEAM_DIVIDERS are ever touched.
divider_ids = set(config.TEAM_DIVIDERS.values())
want_divider_id = team_dividers().get(team_name) if team_name else None
want_divider = guild.get_role(want_divider_id) if want_divider_id else None
if want_divider is not None and want_divider not in member.roles:
try:
await member.add_roles(want_divider, reason="Team sync: mänguosaleja")
result.divider_added = want_divider.name
except discord.Forbidden:
log.debug("No permission to add divider role for %s, skipping", member)
except discord.HTTPException as e:
result.errors.append(f"Eraldajarolli viga kasutajale {member}: {e}")
stale_dividers = [
r for r in member.roles if r.id in divider_ids and r.id != want_divider_id
]
if stale_dividers:
try:
await member.remove_roles(*stale_dividers, reason="Team sync: mäng vahetus")
result.divider_removed = [r.name for r in stale_dividers]
except discord.Forbidden:
log.debug("No permission to remove divider roles for %s, skipping", member)
except discord.HTTPException as e:
result.errors.append(f"Eraldajarolli eemaldamise viga kasutajale {member}: {e}")
return result
@@ -363,16 +422,32 @@ async def apply_team_role_positions(
"""Move every team role directly beneath its configured divider role.
Driven by the ``TEAM_DIVIDER_*`` config: teams whose sheet section matched
one are placed under that role, the rest are left exactly where they are.
Returns ``(roles_moved, errors)``; a no-op returns ``(0, [])``.
one are placed under that divider role, the rest are left exactly where they
are. Dividers are configured by role ID, so a rename never breaks placement;
the ID is resolved to the role's current name here, and the ordering maths
downstream is name-based. Returns ``(roles_moved, errors)``; a no-op returns
``(0, [])``.
"""
placements: dict[str, list[str]] = {}
for team, divider in sheets.get_team_dividers().items():
placements.setdefault(divider, []).append(team)
if not placements:
team_divider_ids = team_dividers() # {team: divider role ID}, both sources
if not team_divider_ids:
return 0, [] # no dividers configured, or nothing matched
errors: list[str] = []
# Resolve each configured divider ID to its role once, then key placements by
# that role's current name for the name-based positioning maths below.
placements: dict[str, list[str]] = {}
resolved: dict[int, discord.Role | None] = {}
for team, divider_id in team_divider_ids.items():
if divider_id not in resolved:
resolved[divider_id] = guild.get_role(divider_id)
if resolved[divider_id] is None:
errors.append(f"Eraldajarolli ID {divider_id} ei leitud serverist")
divider = resolved[divider_id]
if divider is not None:
placements.setdefault(divider.name, []).append(team)
if not placements:
return 0, errors
by_name: dict[str, discord.Role] = {}
for role in sorted(guild.roles, key=lambda r: r.position):
by_name.setdefault(role.name, role)
@@ -405,6 +480,16 @@ async def apply_team_role_positions(
return 0, errors
except discord.HTTPException as e:
errors.append(f"Tiimirollide järjestamine ebaõnnestus: {e}")
# 50013 here despite Manage Roles usually means a role in the batch sits
# at/above the bot's top role. Log the batch vs bot_top to pinpoint it.
log.warning(
"edit_role_positions failed (%s); bot_top=%d; batch=%s",
e, bot_top,
sorted(
((r.name, r.position, target) for r, target in positions.items()),
key=lambda x: -x[1],
),
)
return 0, errors
log.info("Positioned %d team role(s) under their dividers", len(positions))
return len(positions), errors
@@ -433,12 +518,20 @@ async def sync_all_team_roles(
summary.assigned += 1
if res.removed:
summary.removed += len(res.removed)
if res.divider_added:
summary.divider_assigned += 1
if res.divider_removed:
summary.divider_removed += len(res.divider_removed)
if res.changed:
bits: list[str] = []
if res.added:
bits.append(f"+{res.added}")
if res.removed:
bits.append("-" + ", -".join(res.removed))
if res.divider_added:
bits.append(f"+[{res.divider_added}]")
if res.divider_removed:
bits.append("-[" + "], -[".join(res.divider_removed) + "]")
summary.changes.append(f"{member.display_name}: {', '.join(bits)}")
# Placement runs after the grant/remove pass so roles created this run are

View File

@@ -280,7 +280,7 @@ _LINEUP_HEADER_PREFIX = "lineup"
_team_roster: dict[str, list[str]] = {} # team name -> [Discord username, ...]
_team_by_username: dict[str, str] = {} # normalized username -> team name
_team_names: set[str] = set() # universe of all team names
_team_divider: dict[str, str] = {} # team name -> divider role name
_team_divider: dict[str, int] = {} # team name -> divider role ID
@dataclass(frozen=True)
@@ -362,7 +362,11 @@ def parse_team_sections(rows: list[list]) -> list[TeamSection]:
lineup_col = _find_col(rows[i], lambda c: c.strip().lower().startswith(_LINEUP_HEADER_PREFIX))
if name_col is None or lineup_col is None:
if (text := _merged_title(rows[i])) is not None:
title = text
# Accumulate every single-cell row above the header, not just the
# last one: sheets stack the "TipiLAN 2026 CS2" title above notice
# rows ("If a team withdraws..."), and the later notices must not
# clobber the title whose keywords resolve_divider needs.
title = f"{title} {text}".strip() if title else text
i += 1
continue
rosters: dict[str, list[str]] = {}
@@ -390,8 +394,8 @@ def parse_team_rosters(rows: list[list]) -> dict[str, list[str]]:
return rosters
def resolve_divider(title: str, dividers: dict[str, str] | None = None) -> str | None:
"""Return the divider role name configured for a section title, if any.
def resolve_divider(title: str, dividers: dict[str, int] | None = None) -> int | None:
"""Return the divider role ID configured for a section title, if any.
A ``TEAM_DIVIDER_<SUFFIX>`` entry matches when every underscore-separated
part of its suffix appears as a whole word in the title, so ``CS2`` matches
@@ -402,15 +406,15 @@ def resolve_divider(title: str, dividers: dict[str, str] | None = None) -> str |
if dividers is None:
dividers = config.TEAM_DIVIDERS
haystack = title.lower()
best_name: str | None = None
best_id: int | None = None
best_parts = 0
for suffix, role_name in dividers.items():
for suffix, role_id in dividers.items():
parts = [p for p in suffix.split("_") if p]
if not parts or len(parts) <= best_parts:
continue
if all(re.search(rf"\b{re.escape(p)}\b", haystack) for p in parts):
best_name, best_parts = role_name, len(parts)
return best_name
best_id, best_parts = role_id, len(parts)
return best_id
def build_username_index(rosters: dict[str, list[str]]) -> dict[str, str]:
@@ -441,10 +445,13 @@ def _refresh_teams_sync() -> dict[str, list[str]]:
spreadsheet = client.open_by_key(config.TEAM_SHEET_ID)
rosters: dict[str, list[str]] = {}
dividers: dict[str, str] = {}
dividers: dict[str, int] = {}
for ws in spreadsheet.worksheets():
for section in parse_team_sections(ws.get_all_values()):
divider = resolve_divider(section.title)
# Match on the tab name too ("CS2"/"LoL"): it names the game reliably
# even when the game/year title row is shadowed by notice rows, while
# the section title still supplies the year for year-scoped dividers.
divider = resolve_divider(f"{ws.title} {section.title}")
for team, players in section.rosters.items():
rosters.setdefault(team, []).extend(players)
if divider:
@@ -483,8 +490,8 @@ def get_team_rosters() -> dict[str, list[str]]:
return _team_roster
def get_team_dividers() -> dict[str, str]:
"""Current {team: divider role name} cache.
def get_team_dividers() -> dict[str, int]:
"""Current {team: divider role ID} cache.
Only teams whose section title matched a configured TEAM_DIVIDER_* entry
appear here, so an empty dict means positioning is switched off.

View File

@@ -110,6 +110,8 @@ TEAMSYNC_UI: dict[str, str] = {
"removed": " Tiimirolle eemaldatud: {count}",
"created": "🆕 Loodud uusi tiimirolle: {roles}",
"positioned": "📍 Eraldaja alla paigutatud: {count}",
"divider_assigned": "🏷️ Mängurolle (eraldaja) antud: {count}",
"divider_removed": " Mängurolle (eraldaja) eemaldatud: {count}",
"errors": "⚠️ Vead: {count}",
"no_changes": "✨ Kõik tiimirollid olid juba korras.",
"changes_header": "**Muudatused:**",

101
tests/test_fienta.py Normal file
View File

@@ -0,0 +1,101 @@
"""Tests for the Fienta ticket parsing that feeds team-role sync.
Covers the risky bit: turning raw Fienta ticket JSON into a reliable
{discord identity -> team} + {team -> game} mapping, including which ticket
types count as team members and how the game is detected.
"""
from __future__ import annotations
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
import config # noqa: E402
from core import fienta # noqa: E402
def _ticket(ttype: str, *, team="", discord="", discord_id="", nick=""):
return {
"order_id": 1,
"rows": [{
"ticket_type": {"title": ttype},
"attendee": {
fienta._F_TEAM_NAME: team,
fienta._F_DISCORD_USERNAME: discord,
fienta._F_DISCORD_USERID: discord_id,
fienta._F_DISCORD_USERNAME.replace("discord", "x"): "",
"nickname_134815": nick,
},
}],
}
CS2 = "Counter-Strike 2 Tournament - competitor ticket"
LOL = "League of Legends Tournament - competitor ticket"
def test_parse_maps_username_and_userid_to_team():
fienta.parse_tickets([
_ticket(CS2, team="KONE", discord="ar7enchik", discord_id="123"),
])
assert fienta.get_team_for_username("ar7enchik") == "KONE"
assert fienta.get_team_for_username("AR7ENCHIK") == "KONE" # case-insensitive
assert fienta.get_team_for_userid(123) == "KONE"
assert fienta.get_team_game("KONE") == "CS2"
def test_parse_detects_game_from_ticket_type():
fienta.parse_tickets([
_ticket(CS2, team="KONE", discord="a"),
_ticket(LOL, team="Ööbik", discord="b"),
])
assert fienta.get_team_game("KONE") == "CS2"
assert fienta.get_team_game("Ööbik") == "LoL"
def test_parse_excludes_non_player_ticket_types():
fienta.parse_tickets([
_ticket("Visitor's Ticket", team="", discord=""),
_ticket("Early Bird - visitor ticket", team="X", discord="ghost"),
_ticket("Counter-Strike 2 Tournament Waiting List", team="WL", discord="waiter"),
_ticket("LAN area - Access Ticket", team="", discord=""),
_ticket(CS2, team="KONE", discord="real"),
])
assert fienta.all_team_names() == {"KONE"}
assert fienta.get_team_for_username("ghost") is None
assert fienta.get_team_for_username("waiter") is None
assert fienta.get_team_for_username("real") == "KONE"
def test_parse_includes_coach_and_substitute():
fienta.parse_tickets([
_ticket("Counter-Strike 2 Coach/Manager - competitor ticket", team="KONE", discord="coach"),
_ticket("CS2 Substitute Player - competitor ticket", team="KONE", discord="sub"),
])
assert fienta.get_team_for_username("coach") == "KONE"
assert fienta.get_team_for_username("sub") == "KONE"
def test_parse_skips_tickets_without_team_or_discord():
fienta.parse_tickets([
_ticket(CS2, team="", discord="noteam"), # no team -> skipped
_ticket(CS2, team="KONE", discord=""), # team but no discord -> team known, no user
])
assert fienta.get_team_for_username("noteam") is None
assert "KONE" in fienta.all_team_names()
def test_get_team_dividers_maps_via_game(monkeypatch):
monkeypatch.setattr(config, "TEAM_DIVIDERS", {"cs2_2026": 100, "lol_2026": 200})
fienta.parse_tickets([
_ticket(CS2, team="KONE", discord="a"),
_ticket(LOL, team="Ööbik", discord="b"),
])
assert fienta.get_team_dividers() == {"KONE": 100, "Ööbik": 200}
def test_strips_leading_at_from_discord_username():
fienta.parse_tickets([_ticket(CS2, team="KONE", discord="@handle")])
assert fienta.get_team_for_username("handle") == "KONE"

View File

@@ -13,6 +13,7 @@ from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
import config # noqa: E402
from core import member_sync, sheets # noqa: E402
from tests.conftest import run # noqa: E402
@@ -113,18 +114,18 @@ def test_parse_team_sections_title_does_not_leak_to_untitled_block():
# --- resolve_divider (section title -> configured divider role) ------------
DIVIDERS = {
"cs2_2026": "====== COUNTER-STRIKE 2 2026 ======",
"lol_2026": "===== LEAGUE OF LEGENDS 2026 =====",
"cs2_2026": 1498736834656604251,
"lol_2026": 1498736949706490017,
}
def test_resolve_divider_matches_game_and_year():
assert sheets.resolve_divider(
"[merged] TipiLAN 2026 CS2 Registration Log", DIVIDERS
) == "====== COUNTER-STRIKE 2 2026 ======"
) == 1498736834656604251
assert sheets.resolve_divider(
"[merged] TipiLAN 2026 LoL Registration Log", DIVIDERS
) == "===== LEAGUE OF LEGENDS 2026 ====="
) == 1498736949706490017
def test_resolve_divider_year_scoped_key_ignores_other_years():
@@ -132,9 +133,9 @@ def test_resolve_divider_year_scoped_key_ignores_other_years():
def test_resolve_divider_prefers_the_most_specific_match():
dividers = {"cs2": "== CS2 ALL YEARS ==", "cs2_2026": "== CS2 2026 =="}
assert sheets.resolve_divider("TipiLAN 2026 CS2 Log", dividers) == "== CS2 2026 =="
assert sheets.resolve_divider("TipiLAN 2025 CS2 Log", dividers) == "== CS2 ALL YEARS =="
dividers = {"cs2": 111, "cs2_2026": 222}
assert sheets.resolve_divider("TipiLAN 2026 CS2 Log", dividers) == 222
assert sheets.resolve_divider("TipiLAN 2025 CS2 Log", dividers) == 111
def test_resolve_divider_no_config_or_no_title_is_none():
@@ -142,6 +143,22 @@ def test_resolve_divider_no_config_or_no_title_is_none():
assert sheets.resolve_divider("", DIVIDERS) is None
def test_title_survives_notice_rows_below_it():
# Real sheets stack the game/year title above single-cell notice rows; the
# title's keywords must survive so resolve_divider still matches (regression:
# notice rows used to overwrite the title, yielding a None divider).
rows = [
["TipiLAN 2026 CS2 Registration Log"] + [""] * 4,
["This log is updated automatically."] + [""] * 4,
["If a team from the Top 32 withdraws, ..."] + [""] * 4,
["No", "Team Name", "Lineup (nickname, citizenship)", "", ""],
["1", "GENESIS", "kapa (EST)", "", ""],
]
section = sheets.parse_team_sections(rows)[0]
assert "CS2" in section.title and "2026" in section.title
assert sheets.resolve_divider(section.title, DIVIDERS) == 1498736834656604251
# --- plan_team_positions (pure role-ordering maths) ------------------------
def test_plan_moves_teams_directly_under_their_divider():
@@ -237,6 +254,9 @@ class FakeGuild:
self.created.append(name)
return role
def get_role(self, rid):
return next((r for r in self.roles if r.id == rid), None)
def test_sync_creates_missing_team_role_and_removes_old_one(monkeypatch):
old_team = FakeRole(1, "OldTeam")
@@ -276,6 +296,43 @@ def test_sync_uses_existing_team_role(monkeypatch):
assert genesis in member.roles
def test_sync_grants_game_divider_role(monkeypatch):
genesis = FakeRole(3, "GENESIS")
cs2_div = FakeRole(100, "====== CS2 2026 ======")
member = FakeMember(1, "kapa", roles=[])
guild = FakeGuild([genesis, cs2_div])
monkeypatch.setattr(sheets, "get_team_for_username", lambda n: "GENESIS")
monkeypatch.setattr(sheets, "all_team_names", lambda: {"GENESIS"})
monkeypatch.setattr(sheets, "get_team_dividers", lambda: {"GENESIS": 100})
monkeypatch.setattr(config, "TEAM_DIVIDERS", {"cs2_2026": 100, "lol_2026": 200})
result = run(member_sync.sync_team_role(member, guild))
assert result.divider_added == "====== CS2 2026 ======"
assert cs2_div in member.roles
def test_sync_swaps_divider_role_on_game_switch(monkeypatch):
genesis = FakeRole(3, "GENESIS")
cs2_div = FakeRole(100, "CS2")
lol_div = FakeRole(200, "LoL")
member = FakeMember(1, "kapa", roles=[lol_div]) # was LoL, now on a CS2 team
guild = FakeGuild([genesis, cs2_div, lol_div])
monkeypatch.setattr(sheets, "get_team_for_username", lambda n: "GENESIS")
monkeypatch.setattr(sheets, "all_team_names", lambda: {"GENESIS"})
monkeypatch.setattr(sheets, "get_team_dividers", lambda: {"GENESIS": 100})
monkeypatch.setattr(config, "TEAM_DIVIDERS", {"cs2_2026": 100, "lol_2026": 200})
result = run(member_sync.sync_team_role(member, guild))
assert result.divider_added == "CS2"
assert result.divider_removed == ["LoL"]
ids = {r.id for r in member.roles}
assert 100 in ids and 200 not in ids
def test_sync_strips_team_role_when_not_registered(monkeypatch):
old_team = FakeRole(1, "OldTeam")
member = FakeMember(1, "ghost", roles=[old_team])