UNPKG

@amelix/phoenix.js

Version:

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

101 lines (100 loc) 4.62 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 ServerChannel_1 = require("./ServerChannel"); const hostLength = config_1.hostname.length; /** Represents a message in a channel. */ class Message extends Base_1.default { constructor(client, data) { var _a, _b; super(client, data); this._invitesCached = false; this.content = data.content; this.edited = !!data.edited; let author = client.users.get(data.author); if (author) { this.author = author; } let server = client.servers.get(data.server); if (server) this.server = server; else this.server = data.server; let channel = client.channels.get(data.channel); if (channel && channel instanceof ServerChannel_1.default) this.channel = channel; else this.channel = data.channel; if (this.author) { let member = this.server.members.get(this.author.id); if (member) this.member = member; } this.editable = ((_a = this.author) === null || _a === void 0 ? void 0 : _a.id) === this.client.user.id; this.deletable = ((_b = this.author) === null || _b === void 0 ? void 0 : _b.id) === this.client.user.id || this.server.owner.id === this.client.user.id; } /** Edit this message. */ edit(newContent) { return __awaiter(this, void 0, void 0, function* () { // would make this delete the message instead, but I don't want users accidentally deleting messages, so would want a popup window confirming it first if (!newContent) throw new Error("Cannot edit messages to contain nothing!"); if (!this.editable) throw new Error(`Cannot edit messages not made by the client.`); else if (this.content === newContent) return; let res = yield this.client.request("PUT", `/channels/${this.channel.id}/${this.id}`, { content: newContent }); this.content = res.body.content; this._invitesCached = false; yield this.fetchInvites(); }); } /** Delete this message. */ delete() { return __awaiter(this, void 0, void 0, function* () { if (!this.deletable) throw new Error(`Insufficient Permissions`); // else if (insert permissions stuff) { ... } yield this.client.request("DELETE", `/channels/${this.channel.id}/${this.id}`); }); } /** Go through all Phoenix invites in this message and cache Invite objects for them, returning the invites. */ fetchInvites() { return __awaiter(this, void 0, void 0, function* () { if (this._invitesCached) return this.invites; this.invites = new Map(); this.invalidInvites = []; const regex = new RegExp(`/${config_1.hostname}/invite/`, "gi"); let result; while ((result = regex.exec(this.content))) { let str = this.content.slice(result.index + hostLength), id = ""; for (let char of str) { if ([" ", "\n"].includes(char)) break; id += char; } if (id) { let invite = yield this.client.fetchInvite(id); if (!invite) this.invalidInvites.push(id); else this.invites.set(id, invite); } } this._invitesCached = true; return this.invites; }); } } exports.default = Message;