UNPKG

4.61 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const messenger_1 = require("./messenger");
4const discord_js_1 = require("discord.js");
5class Connection {
6 constructor(name, endpoints = []) {
7 this.disabled = false;
8 this.name = name;
9 this.endpoints = endpoints;
10 }
11 static isThread(endpoint) { return endpoint.type === 'messenger'; }
12 static isChannel(endpoint) { return endpoint.type === 'discord'; }
13 addThread(id) {
14 this.endpoints.push({
15 type: 'messenger',
16 id
17 });
18 return this;
19 }
20 addChannel(id) {
21 this.endpoints.push({
22 type: 'discord',
23 id
24 });
25 return this;
26 }
27 async addEndpoint({ id, type, readonly }) {
28 if (type === 'discord') {
29 if (!discord.client.channels.has(id))
30 throw new Error(`Discord channel \`${id}\` not found!`);
31 await this
32 .addChannel(id)
33 .save();
34 }
35 else {
36 try {
37 await messenger_1.getThread(id);
38 }
39 catch (err) {
40 throw new Error(`Messenger thread \`${id}\` not found!`);
41 }
42 await this
43 .addThread(id)
44 .save();
45 }
46 if (readonly)
47 this.markEndpointAsReadonly(id, true);
48 }
49 has(id) {
50 return this.endpoints.some(endpoint => endpoint.id === id);
51 }
52 getWritableEndpoints() { return this.endpoints.filter(endpoint => !endpoint.readonly); }
53 getThreads() { return this.endpoints.filter(Connection.isThread); }
54 getWritableThreads() { return this.getWritableEndpoints().filter(Connection.isThread); }
55 getOtherWritableThreads(id) { return this.getWritableThreads().filter(thread => thread.id !== id.toString()); }
56 getChannels() { return this.endpoints.filter(Connection.isChannel); }
57 getWritableChannels() { return this.getWritableEndpoints().filter(Connection.isChannel); }
58 getOtherWritableChannels(id) { return this.getWritableChannels().filter(channel => channel.id !== id); }
59 async checkChannelRenames(name) {
60 if (!name ||
61 !config.discord.renameChannels ||
62 this.getThreads().length !== 1 ||
63 this.getChannels().length !== 1)
64 return this;
65 const channel = discord.client.channels.get(this.getChannels()[0].id);
66 if (!channel ||
67 !(channel instanceof discord_js_1.TextChannel) ||
68 name === channel.name)
69 return this;
70 await channel.edit({ name });
71 return this;
72 }
73 hasEndpoint(id) {
74 return this.endpoints.some(endpoint => endpoint.id === id);
75 }
76 markEndpointAsReadonly(id, readonly) {
77 let endpoint = this.endpoints.find(endpoint => endpoint.id === id);
78 endpoint.readonly = readonly;
79 return this;
80 }
81 removeEndpoint(id) {
82 let index = this.endpoints.findIndex(endpoint => endpoint.id === id);
83 if (index !== -1)
84 this.endpoints.splice(index, 1);
85 return this;
86 }
87 getPrintable() {
88 const getLink = ({ type, id }) => type === 'messenger'
89 ? `[\`${id}\`](https://facebook.com/messages/t/${id})`
90 : `<#${id}>`;
91 const e = (endpoint) => `\`${endpoint.type}\`: ${getLink(endpoint)}${endpoint.readonly ? ' (readonly)' : ''}`;
92 return this.endpoints.map(e).join('\n');
93 }
94 rename(newName) {
95 connections.delete(this.name);
96 this.name = newName;
97 return this;
98 }
99 delete() {
100 connections.delete(this.name);
101 return connections.save();
102 }
103 disable() {
104 if (this.disabled)
105 return this;
106 this.disabled = true;
107 return this;
108 }
109 enable() {
110 if (!this.disabled)
111 return this;
112 this.disabled = false;
113 return this;
114 }
115 async save() {
116 connections.set(this.name, this);
117 await connections.save();
118 return this;
119 }
120 toYAMLObject() {
121 return {
122 [(this.disabled ? '_' : '') + this.name]: this.cleanEndpoints
123 };
124 }
125 toObject() {
126 return {
127 name: this.name,
128 endpoints: this.cleanEndpoints,
129 disabled: this.disabled
130 };
131 }
132 get cleanEndpoints() {
133 return this.endpoints
134 .map(endpoint => {
135 if (!endpoint.readonly)
136 delete endpoint.readonly;
137 return endpoint;
138 });
139 }
140}
141exports.default = Connection;