UNPKG

blox-api

Version:

Roblox web API wrapper for Node.js

138 lines 4.78 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GroupReference = void 0; const request = __importStar(require("superagent")); const cheerio = __importStar(require("cheerio")); const auth_1 = require("../lib/auth"); /** * A reference to a Roblox group */ class GroupReference { /** * Create a new [[GroupReference]] * * @param groupId The ID of the group on Roblox * @param session A session used for authenticating API calls */ constructor(groupId, session) { this.groupId = groupId; this.session = session; } /** * Get a list of rolesets within the group */ async getRoles() { const httpRes = await request .get(`https://groups.roblox.com/v1/groups/${this.groupId}/roles`); return httpRes.body.roles; } /** * Get the roleset of a group corresponding to a specific rank * * @param rank Rank of the roleset */ async getRolesetByRank(rank) { const roles = await this.getRoles(); for (const role of roles) { if (role.rank === rank) { return role; } } return undefined; } /** * Updates a user's rank in the group to a new rank * * @param userId ID of the user to update * @param newRank New rank for the user */ async setUserRank(userId, newRank) { const csrfToken = await auth_1.getCSRFToken(this.session); const roleset = await this.getRolesetByRank(newRank); if (!roleset) { throw new Error("There is no roleset with the rank " + newRank); } await request .patch(`https://groups.roblox.com/v1/groups/${this.groupId}/users/${userId}`) .set("Cookie", this.session.cookie) .set("X-CSRF-TOKEN", csrfToken) .send({ roleId: roleset.id, }); } /** * Exiles a user from the group * * @param userId User to exile */ async exileUser(userId) { const csrfToken = await auth_1.getCSRFToken(this.session); await request .delete(`https://groups.roblox.com/v1/groups/${this.groupId}/users/${userId}`) .set("Cookie", this.session.cookie) .set("X-CSRF-TOKEN", csrfToken); } /** * Get the internal join request ID for a specific user * * @param username Username of the pending user */ async getJoinRequestId(username) { const httpRes = await request .get(`https://www.roblox.com/groups/${this.groupId}/joinrequests-html`) .query({ username, }) .set("Cookie", this.session.cookie); const $ = cheerio.load(httpRes.text); const found = $("#JoinRequestsList").find("tr"); const len = found.length; if (len === 1) { throw new Error("Join request does not exist"); } for (let i = 1; i < len - 1; i++) { const data = found.eq(i).find("td"); if (data.eq(1).text() === username) { return data.eq(3).find("span").attr("data-rbx-join-request"); } } throw new Error("Join request does not exist"); } /** * Accept a user's request to join a group * * @param username Username of the pending user */ async acceptJoinRequest(username) { const joinRequestId = await this.getJoinRequestId(username); const csrfToken = await auth_1.getCSRFToken(this.session); await request .post("https://www.roblox.com/group/handle-join-request") .set("Cookie", this.session.cookie) .set("X-CSRF-TOKEN", csrfToken) .send({ groupJoinRequestId: joinRequestId, }); } } exports.GroupReference = GroupReference; //# sourceMappingURL=GroupReference.js.map