diff --git a/package-lock.json b/package-lock.json index 8cb7aad..da13204 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "amaribot.js", - "version": "1.4.2", + "version": "1.5.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "amaribot.js", - "version": "1.4.2", + "version": "1.5.0", "license": "MIT", "dependencies": { "@sapphire/async-queue": "^1.1.4", diff --git a/package.json b/package.json index 1c105dc..f55a582 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/AmariBot.js b/src/AmariBot.js index dd84eee..b136a4e 100644 --- a/src/AmariBot.js +++ b/src/AmariBot.js @@ -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 @@ -193,7 +192,7 @@ class AmariBot { * @throws {RatelimitError} * @returns {Promise} 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") @@ -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} 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 * diff --git a/src/index.d.ts b/src/index.d.ts index e69d943..586c72f 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -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|Promise + public getUserLevel(guildId: string, userId: string | array): Promise | Promise public getGuildLeaderboard(guildId: string, options?: GetLeaderboardOptions): Promise public getRawGuildLeaderboard(guildId: string, options?: GetRawLeaderboardOptions): Promise public getWeeklyLeaderboard(guildId: string, options?: GetLeaderboardOptions): Promise public getRawWeeklyLeaderboard(guildId: string, options?: GetRawLeaderboardOptions): Promise public getGuildRewards(guildId: string, options?: GetRewardOptions): Promise - public getLeaderboardPosition(guildId: string, userId: string, options?: GetRewardOptions): Promise - public getLeaderboardPosition(level: number): number + public getLeaderboardPosition(guildId: string, userId: string): Promise + public getWeeklyLeaderboardPosition(guildId: string, userId: string): Promise + public getLevelExp(level: number): number private _request(endpoint: string, method?: string, query: any): Promise } diff --git a/test.js b/test.js index d4f48b1..995636a 100644 --- a/test.js +++ b/test.js @@ -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)