The first thing you need to do to use the API is create an API client. There are 3 types of client:
Creating an unauthenticated client is very simple:
const client = polympics.UnauthenticatedClient();
However, if you want to do anything other than read-only operations, you’ll need to authenticate. To use app credentials, see the following example:
const credentials = { username: 'A3', password: 'YOUR-TOKEN-HERE'};
const client = polympics.AppClient({ credentials: credentials });
Creating a user-authenticated client is very similar:
const credentials = Credentials('S4', 'YOUR-TOKEN-HERE');
const client = polympics.UserClient({ credentials: credentials });
All three clients take an additional parameter, apiUrl
. This is location at which the API is hosted, for example http://127.0.0.1:8000
or https://api.polytopia.fun
.
You can get an account by Discord ID using getAccount
. For example:
const account = await client.getAccount('12345678901234');
console.log(account.name);
console.log(account.permissions);
console.log(account.team.name);
Note that Discord IDs are strings, because they require more precision than JavaScript's default numeric type has.
You can get a team by ID using getTeam
. For example:
const team = await client.getTeam(31);
console.log(team.name);
console.log(team.memberCount);
You can get an award by ID using getAward
. For example:
const award = await client.getAward(14);
console.log(award.title);
console.log(award.imageUrl);
You can list all accounts using listAccounts
. For example:
const accountPaginator = client.listAccounts();
for (const account of await accountPaginator.nextPage()) {
console.log(account.name);
}
Note that this will only fetch the first page of results (20, by default). To fetch more, you should call nextPage()
repeatedly until it returns an empty array. You can also get a specific page:
const accountPaginator = client.listAccounts();
const data = await accountPaginator.getPage(4);
console.log(data.result); // Total result count.
for (const account of data.data) {
console.log(account.name);
}
You can use the search
and team
options to narrow down results:
const accountPaginator = client.listAccounts({
team: team, search: 'bob'
});
console.log(`Members from team ${team.name} with "bob" in their name:`);
for (const account of await accountPaginator.nextPage()) {
console.log(account.name);
}
You can list all teams using listTeams
. For example:
const teamPaginator = client.listTeams();
for (const team of await teamPaginator.nextPage()) {
console.log(team.name);
}
This supports the same pagination system as listAccounts
, as well as the search
option.
Registering a user is a simple call to createAccount
:
const team = await client.getTeam(5);
const account = await client.createAccount({
id: '1234567',
name: 'Artemis',
discriminator: '8472',
avatarUrl: 'https://picsum.photos/200',
team: team
});
console.assert(account.name === 'Artemis');
console.assert(account.team.id === 5);
Note that this requires an AppClient
or UserClient
with the manageAccountDetails
permission.
You can also chose the permissions to grant the user:
await client.createAccount({
id: '1234567',
name: 'Artemis',
discriminator: '8472',
permissions: polympics.PolympicsPermissions.manageTeams
& polympics.PolympicsPermissions.manageAccountDetails
});
Note, when granting permissions to a user:
Editing a user's account can be done with updateAccount
:
let account = await client.getAccount('41129492792313');
account = await client.updateAccount(account, {
name: 'Artemis', discriminator: '3910'
});
console.assert(account.name === 'Artemis');
This requires an AppClient
or UserClient
with the manageAccountDetails
permission.
You can similarly update a user's team:
account = await client.updateAccount(account, { team: team });
This requires an AppClient
or UserClient
with the manageAccountTeams
permission, or a UserClient
authenticated with the given account.
By setting team
to null
, you can remove a user from a team;
account = await client.updateAccount(account, { team: null });
This requires permissions as explained above for adding a user to a team,
with the addition that you can remove a user from a team if you are a member
of that team and have the manageOwnTeam
permission.
You can also update user permissions with the grantPermissions
and revokePermissions
options, subject to the rules outlined in "Creating an account".
Example:
account = await client.updateAccount(account, {
grantPermissions: polympics.PolympicsPermissions.manageOwnTeam,
revokePermissions: polympics.PolympicsPermissions.manageTeams
});
Using the discordToken
option, you can update a user's name,
discriminator and avatar URL to match Discord. This requires no permissions,
since user tokens can be authenticated with Discord.
Example:
account = await client.updateAccount(account, { discordToken: token });
You can delete a user's account with the deleteAccount
method:
const account = await client.g, for the team's name:
```js
teametAccount('124214913289');
await client.deleteAccount(account);
This requires an AppClient
or UserClient
with the manageAccountDetails
permission, or just a UserClient
associated with the account being deleted.
You can create a team using the createTeam
method. It accepts one parameter, name
, for the team's name:
const team = await client.createTeam({ name: 'Gods of Olympus' });
console.assert(team.name === 'Gods of Olympus');
This requires an AppClient
or UserClient
with the manageTeams
permission.
You can edit a team using the updateTeam
method. It accepts the same name
parameter as createTeam
:
let team = await client.getTeam(13);
team = await client.updateTeam(team, { name: 'Cool Kidz' });
console.assert(team.name === 'Cool Kidz');
This requires an AppClient
or UserClient
with the manageTeams
permission, or just a UserClient
with the manageOwnTeam
permission who is a member of the given team.
You can delete a team with the deleteTeam
method:
let team = await client.getTeam(28);
await client.deleteTeam(team);
This requires an AppClient
or UserClient
with the manageTeams
permission, or just a UserClient
with the manageOwnTeam
permission who is a member of the given team.
You can create an award with the createAward
method:
const account_1 = await client.getAccount(508140149014901);
const team = await client.getTeam(123);
const award = await client.createAward({
title: "Perfect 10 Gold",
imageUrl: "https://link.to/icon.png",
team: team,
accounts: [account_1]
})
console.log(award.id);
console.log(award.title);
This requires an AppClient
or UserClient
with the manageAwards
permission.
You can edit an award with the updateAward
method:
let award = await client.getAward(12);
award = await client.updateAward(award, { title: "Gold - Perfect 10" });
This requires an AppClient
or UserClient
with the manageAwards
permission.
You can delete an award with the deleteAward
method:
const award = await client.getAward(52);
await client.deleteAward(award)
This requires an AppClient
or UserClient
with the manageAwards
permission.
To give an award to a player, use giveAward
:
const account = await client.getAccount(130914109419411);
const award = await client.getAward(19);
await client.giveAward(award, account);
This requires an AppClient
or UserClient
with the manageAwards
permission.
To give an award to a player, use takeAward
:
const account = await client.getAccount(8713710931790741);
const award = await client.getAward(13);
await client.takeAward(award, account);
This requires an AppClient
or UserClient
with the manageAwards
permission.
An AppClient
can create user sessions, which can in turn be used by a UserClient
as authentication. More usefully, user session can be passed to the frontend, so that the user they are for can manipulate the API client-side. Since an attacker intercepting these credentials could authenticate as the user, these have a short lifetime, by default 30 minutes.
Example:
const account = await client.getAccount('1318219824080');
const session = await client.createSession(account);
console.log(session.expiresAt);
const userClient = polympics.UserClient(session);
This requires an AppClient
with the authenticateUsers
permission.
Alternatively, you can use a Discord user authentication token to create a user session (these can be obtained using Discord OAuth2, which is beyond the scope of this library). This has the advantage that you do not need to be otherwise authenticated, so it can be used on the frontend (eg. with the OAuth2 implicit grant flow).
Example:
const session = await client.discordAuthenticate(token)
const userClient = polympics.UserClient(session)
Note that the token used must be authorised for the identify
scope.
The token of an AppClient
can be reset using resetToken
. Note that the client will automatically update to use the new token. This function returns an AppCredentials
object, which can be used in place of credentials, and also provides the attribute name
, which is the human-readable name of the app.
await client.resetToken();
This requires an AppClient
(you cannot reset a user token, since they are short-lived anyway).
When authenticated with an AppClient
, you can use getSelf
to get metadata on the authenticated app. Note that unlike resetToken
, this does not return the app's new token.
const app = await client.getSelf();
console.log(app.name);
A UserClient
can get the account of the user it has authenticated as using the same method:
const account = await client.getSelf();
console.log(account.name);
If the API returns an error, the wrapper will raise a PolympicsError
. This has the code
attribute (the HTTP status code that was used, eg. 404
or 500
).
There are also the following subclasses:
ServerError
indicates a server-side issue that it may be beyond the client's capability to resolve.EmptyResponse
indicates that the server returned no response, but the wrapper expected one.DataError
indicates an issue in the parameters passed to the API. This could indicate an issue in the library, but it will also be raised when a resource is not found. The issues
attribute gives more detail, which can also be seen in the string representation of the error.ClientError
indicates a client-side issue not covered by DataError
. The detail
attribute gives more information, in a human-readable format.Generated using TypeDoc