UNPKG

@amelix/phoenix.js

Version:

A feature-rich API wrapper for the critically acclaimed chatting app Phoenix.. or something.

146 lines (145 loc) 6.42 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const config_1 = require("../config"); const Base_1 = require("./Base"); const Member_1 = require("./Member"); const TextChannel_1 = require("./TextChannel"); class Server extends Base_1.default { constructor(client, data) { super(client, data); this.name = data.name; this.cssLastUpdated = data.cssLastUpdated; if (data.defaultIcon) this.iconURL = config_1.defaultIcon; else this.iconURL = `https://${config_1.hostname}${config_1.apiPath}/servers/${this.id}/icon`; this.iconLastUpdated = data.iconLastUpdated; this.owner = data.owner; this.channels = new Map(); this.members = new Map(); this.membersCached = false; } /** * Create a channel in this server. Defaults to a text channel if not specified. * @returns Created channel */ createChannel(data) { return __awaiter(this, void 0, void 0, function* () { const res = yield this.client.request("POST", `/servers/${this.id}/channels`, {}, data); let channel = res.body; if (data.type === "text") channel = new TextChannel_1.default(this.client, channel); this.client.channels.set(channel.id, channel); this.channels.set(channel.id, channel); return channel; }); } /** Fetch and cache a single member from this server. */ fetchMember(id) { return __awaiter(this, void 0, void 0, function* () { if (this.members.has(id)) return this.members.get(id); const res = yield this.client.request("GET", `/servers/${this.id}/members/${id}`).catch(e => { }); if (!res) return; const member = new Member_1.default(this.client, res.body); this.members.set(id, member); return member; }); } fetchMembers() { return __awaiter(this, void 0, void 0, function* () { if (this.membersCached) return this.members; let res = yield this.client.request("GET", `/servers/${this.id}/members`); for (let arr of res.body) { let user = this.client.users.get(arr[0]); if (user) arr[1].user = user; this.members.set(arr[0], new Member_1.default(this.client, arr[1])); } this.membersCached = true; return this.members; }); } /** Delete the server's icon, replacing it with the defualt. */ deleteIcon() { return __awaiter(this, void 0, void 0, function* () { if (this.client.user.id !== this.owner.id) throw new Error("Insufficient Permissions"); yield this.client.request("DELETE", `/servers/${this.id}/icon`); }); } /** Fetch this server's CSS. */ fetchCSS() { return __awaiter(this, void 0, void 0, function* () { if (this.css) return this.css; let res = yield this.client.request(`GET`, `/servers/${this.id}/css`); this.css = res.body; return res.body; }); } /** Post new CSS to this server. * This is not available to bots as there is no permission system yet and bots cannot own servers. * @returns Minified CSS */ editCSS(css) { return __awaiter(this, void 0, void 0, function* () { if (this.client.user.id !== this.owner.id) throw new Error("Insufficient Permissions"); else if (!css) throw new Error("No CSS provided"); let res = yield this.client.request("POST", `/servers/${this.id}/css`, {}, css); return res.body; }); } /** Delete this server's custom CSS. * This is not available to bots as there is no permission system yet and bots cannot own servers. */ deleteCSS() { return __awaiter(this, void 0, void 0, function* () { if (this.client.user.id !== this.owner.id) throw new Error("Insufficient Permissions"); yield this.client.request("DELETE", `/servers/${this.id}/css`); delete this.css; }); } /** Edit this server's name or transfer ownership. */ edit(data, password) { return __awaiter(this, void 0, void 0, function* () { // Permission stuffs if (this.client.user.id !== this.owner.id) throw new Error("Insufficient Permissions"); if (!Object.keys(data)) throw new Error("No data provided"); let write = data; if (data.owner && !password) throw new Error("Transferring ownership requires password!"); else write.password = password; yield this.client.request("PUT", `/servers/${this.id}`, write); return this; }); } /** Delete the server. */ delete(password) { return __awaiter(this, void 0, void 0, function* () { // Permission stuff ... if (this.client.user.id !== this.owner.id) throw new Error(`Insufficient Permissions`); if (!password) throw new Error("Password required to delete servers"); yield this.client.request("DELETE", `/servers/${this.id}`, { "Transfer-Encoding": "chunked" }, `{"password": "${password}"}`); }); } } exports.default = Server;