@amelix/phoenix.js
Version:
A feature-rich API wrapper for the critically acclaimed chatting app Phoenix.. or something.
64 lines (63 loc) • 3.22 kB
JavaScript
"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 Base_1 = require("./Base");
const ServerChannel_1 = require("./ServerChannel");
/** Represents an invite by which servers can be joined. */
class Invite extends Base_1.default {
constructor(client, data) {
super(client, data);
this.uses = data.uses;
this.maxUses = data.maxUses;
this.expires = data.expires;
this.invitedUsers = data.invitedUsers;
let createdBy = client.users.get(data.createdBy);
if (createdBy)
this.createdBy = createdBy;
else
this.createdBy = data.createdBy;
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;
}
/** Edit this invite. */
edit(data) {
return __awaiter(this, void 0, void 0, function* () {
if ((data.expires && data.expires < Date.now() + 60000) || (data.expiresAfter && data.expiresAfter > 60000))
throw new Error("Expiry time has either already passed or is too short. Minimum expiry time is 1 minute");
if (data.maxUses && data.maxUses <= this.uses)
throw new Error(`Property "maxUses" cannot be equal to or smaller than the uses of this invite.`);
if (this.client.user.id !== this.createdBy.id && this.client.user.id !== this.server.owner.id)
throw new Error("Insufficient Permissions");
const res = yield this.client.request("PUT", `/channels/${this.channel.id}/invites/${this.id}`, {}, data);
this.id = res.body.id;
this.maxUses = res.body.maxUses;
this.expires = res.body.expires;
this.invitedUsers = res.body.invitedUsers;
});
}
/** Permanently delete this invite. */
delete() {
return __awaiter(this, void 0, void 0, function* () {
if (this.client.user.id !== this.createdBy.id && this.client.user.id !== this.server.owner.id)
throw new Error("Insufficient Permissions");
yield this.client.request("DELETE", `/channels/${this.channel.id}/invites/${this.id}`);
});
}
}
exports.default = Invite;