Skip to content

Commit

Permalink
add getWeeklyLeaderboardPosition
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilloftheshadow committed Apr 10, 2022
1 parent e75118a commit eb85c31
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "amaribot.js",
"version": "1.4.3",
"version": "1.5.0",
"description": "A node.js wrapper for the AmariBot API",
"main": "src/index.js",
"types": "src/index.d.ts",
Expand Down
27 changes: 25 additions & 2 deletions src/AmariBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class AmariBot {
* @description This is the main class that you initalize to perform all the requests to the API
* @param {string} token - The token you use to authenticate to the API
* @param {object} options - Additional options for the API handler
* @param {string} options.token - Your API token from the AmariBot website
* @param {boolean} [options.debug=false] - Controls whether debug mode is enabled for the library
* @param {string} [options.baseURL="https://amaribot.com/api/"] - The base URL for the API requests, defaults to the amaribot.com API
* @param {string} [options.version="v1"] - The base URL for the API requests, defaults v1
Expand Down Expand Up @@ -193,7 +192,7 @@ class AmariBot {
* @throws {RatelimitError}
* @returns {Promise<number>} The user's position
*/
async getLeaderboardPosition(guildId, userId, options = {}) {
async getLeaderboardPosition(guildId, userId) {
if (this.debug) console.debug(`Event: getLeaderboardPosition\n - Guild: ${guildId}\n - Options: ${JSON.stringify(options, null, 2)}`)

if (typeof guildId !== "string") throw new TypeError("guildId must be a string")
Expand All @@ -206,6 +205,30 @@ class AmariBot {
return position + 1 // the position is from an array which is 0 based
}

/**
* Get a user's position in the weekly leaderboard
*
* @public
* @async
* @param {string} guildId - The guild ID to fetch the user from.
* @param {string} userId - The user ID to fetch in the guild.
* @throws {APIError}
* @throws {RatelimitError}
* @returns {Promise<number>} The user's position
*/
async getWeeklyLeaderboardPosition(guildId, userId) {
if (this.debug) console.debug(`Event: getWeeklyLeaderboardPosition\n - Guild: ${guildId}\n - Options: ${JSON.stringify(options, null, 2)}`)

if (typeof guildId !== "string") throw new TypeError("guildId must be a string")
if (typeof userId !== "string") throw new TypeError("userId must be a string")

const lb = await this.getRawWeeklyLeaderboard(guildId, { limit: 500000 })
const userData = lb.rawData.data.find((x) => x.id == userId)
if (!userData) throw new Error(`User ${userId} not found`)
const position = lb.rawData.data.indexOf(userData)
return position + 1 // the position is from an array which is 0 based
}

/**
* Get the exp needed to reach the next level up
*
Expand Down
7 changes: 4 additions & 3 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ declare module "amaribot.js" {
public version: string
public requestHandler: RequestHandler
public constructor(token: string, options: AmariBotOptions)
public getUserLevel(guildId: string, userId: string|array): Promise<User>|Promise<UserGroup>
public getUserLevel(guildId: string, userId: string | array): Promise<User> | Promise<UserGroup>
public getGuildLeaderboard(guildId: string, options?: GetLeaderboardOptions): Promise<Leaderboard>
public getRawGuildLeaderboard(guildId: string, options?: GetRawLeaderboardOptions): Promise<Leaderboard>
public getWeeklyLeaderboard(guildId: string, options?: GetLeaderboardOptions): Promise<Leaderboard>
public getRawWeeklyLeaderboard(guildId: string, options?: GetRawLeaderboardOptions): Promise<Leaderboard>
public getGuildRewards(guildId: string, options?: GetRewardOptions): Promise<Rewards>
public getLeaderboardPosition(guildId: string, userId: string, options?: GetRewardOptions): Promise<number>
public getLeaderboardPosition(level: number): number
public getLeaderboardPosition(guildId: string, userId: string): Promise<number>
public getWeeklyLeaderboardPosition(guildId: string, userId: string): Promise<number>
public getLevelExp(level: number): number
private _request(endpoint: string, method?: string, query: any): Promise<any>
}

Expand Down
17 changes: 14 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,24 @@ describe("getLeaderboardPosition", async (done) => {
it(`should return a number greater than 0`, async () => {
position = await AmariBot.getLeaderboardPosition(guildId, userId)
expect(position).to.be.a("number")
})
}).timeout(15000)
it(`should be greater than 0`, async () => {
expect(position).to.be.above(0)
})
}).timeout(15000)
})

describe("getLeaderboardPosition", async (done) => {
describe("getWeeklyLeaderboardPosition", async (done) => {
let position
it(`should return a number greater than 0`, async () => {
position = await AmariBot.getWeeklyLeaderboardPosition(guildId, userId)
expect(position).to.be.a("number")
}).timeout(15000)
it(`should be greater than 0`, async () => {
expect(position).to.be.above(0)
}).timeout(15000)
})

describe("getLevelExp", async (done) => {
let nextExp
it(`should return 35 for level 0`, async () => {
nextExp = AmariBot.getLevelExp(0)
Expand Down

0 comments on commit eb85c31

Please sign in to comment.