UNPKG

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