Skip to main content
ranking() declares a ranking, from any file of src/, and the name of its export is the name of the ranking. It takes an object, RankingConfig, or nothing.
src/levels/best.ts
How to use a ranking, and where its scores are, is in the guide. This page lists what exists.

ranking()

Declares a ranking: a score per member, kept in order from one restart to the next. Export the result from any file of src/: the name of the export is the name of the ranking. Per server unless scope: 'global' is given, the highest score first unless order: 'lowest' is.

RankingConfig

What a file gives to ranking().
StoreScope
'guild' (the default): a ranking per server, and a function that runs in a server reads and writes the ranking of that server, with nothing to say. 'global': one ranking for the whole bot.
StoreStorage
Where the scores are kept: 'file' (the default), a file in the data/ folder of the project, or 'redis', the Redis of REDIS_URL in .env, where a ranking is a sorted set.
RankingOrder
Which end comes first: 'highest' (the default), the highest score first, or 'lowest', the lowest first (a time, a number of tries).

RankingOrder

Which end of a ranking comes first. StoreScope and StoreStorage are the ones of store().

Ranking

What ranking() returns: a declaration the framework finds in the exports of a file. It is a ScopedRanking for the server the code runs in, plus:

in()

The same ranking, for one server: what a task, or a function that runs outside a server, uses to read and write the ranking of that server.

ScopedRanking

A ranking, for one server or for the whole bot.

add()

Adds points (negative to take away) to the score of id, starting from 0, in one go: nothing added meanwhile is lost, even by another process. Resolves with the new score.

set()

Sets the score of id, whatever it was.

score()

The score of id, or undefined when it has none.

rankOf()

Where id is, from 1, or undefined when it has no score.

top()

The first count (10 by default) members, after skipping skip (0 by default): top(10, 10) is the second page.

remove()

Forgets the score of id. true when it had one.

size()

How many members have a score.

clear()

Forgets every score. Resolves with how many there were.

RankingEntry

A member of a ranking, as top() lists them.
string
required
The id given to add() or set(): a member, or anything else.
number
required
The score.
number
required
Where it is, from 1. Equal scores are ordered by id.

Where the scores live

With storage: 'file', one entry per member in data/<name>.json, written like the entries of a store ([[server id, member id], score, null]). top(), rankOf() and size() put every score of the server in order on each call; add() is an update of the entry, in one go. With storage: 'redis', one sorted set per server: <name>:<server id>, <name> for a global ranking, dev: in front in chapterjs dev. add() is ZINCRBY, set() ZADD, score() ZSCORE, rankOf() ZREVRANK (ZRANK for order: 'lowest'), top() ZREVRANGE … WITHSCORES (ZRANGE), remove() ZREM, size() ZCARD, clear() DEL. Equal scores are ordered by the bytes of their id, as Redis does: in reverse for the highest first. Both storages give the same order.

Errors