Counting music plays from a Discord voice channel
How the Melodius Discord bot records what each listener actually heard: who counts as a listener, played time minus pauses, skips, and linking Discord to accounts without asking for a password.
Melodius for Discord is a music bot linked to Melodius accounts. Its most interesting feature is invisible: songs heard in a voice channel count toward each listener's Melodius history and stats, just like songs played in the app. Doing that honestly turned out to need more care than "the bot played a song, log it".
The goal
A play should be recorded for the people who actually heard it, for as long as they heard it. If ten people are in a channel but three are deafened and two have never linked Melodius, five plays should be recorded, each with its own listening time.
Who counts as a listener
When a song starts, the bot looks at the voice channel and keeps a member only if all of these hold:
- they are not a bot;
- they are not deafened, by themselves or by the server;
- they have not turned tracking off in the bot's settings;
- their Discord account is connected to Melodius.
Only tracks from sources Melodius can identify are recorded, live streams are skipped, and durations must be plausible. A track with no identifier or a 30-hour "song" is not a play.
Measuring played time
Played time is wall-clock time since the song started, minus time spent paused, capped at the song's length. Each session tracks its pauses as they happen:
def played_ms(self) -> int:
now = time.monotonic()
paused = self.paused_total + (now - self.paused_at if self.paused_at is not None else 0.0)
return max(0, min(self.track.duration, int((now - self.started - paused) * 1000)))Monotonic time matters here: the system clock can jump, a monotonic clock cannot, so a clock correction never produces negative or inflated listening time. Listeners who leave the channel early are recorded with the time they were actually present.
What gets recorded
Each play sent to Melodius carries the listener's Discord id, the source and identifier, title, artists, the ISRC when it is valid, artwork, duration, start time, milliseconds played and whether it was skipped. Inputs are trimmed and validated before they leave the bot: titles and artist names are length-limited, at most ten artists are kept, artwork must be an HTTPS URL, and an ISRC must be twelve characters or it is dropped.
The ISRC is what lets Melodius tie a play from Discord to the same song in the app, using the same matching described in matching songs across catalogues.
Linking without passwords
The bot never asks for a password or a token. Anyone who signed in to Melodius with Discord, or connected Discord for Rich Presence, is already linked: the bot recognises them by their Discord id. It reads Melodius through a database role that can only select from the tables it needs, and keeps its own data in its own schema. Even if the bot were compromised, it could not write to Melodius's database through that connection.
The result is that /melodius stats in Discord and the stats page in the app tell the same story. The bot is in the Melodius support server, and the full feature list is in the case study.
