1 | class Connection {
|
2 | constructor (name, endpoints = []) {
|
3 | this.name = name
|
4 | this.endpoints = endpoints.map(endpoint => {
|
5 | if (endpoint.type === 'discord' && !endpoint.channel) {
|
6 | endpoint.channel = discord.client.channels.get(endpoint.id)
|
7 | }
|
8 | return endpoint
|
9 | })
|
10 | }
|
11 |
|
12 | static isThread (endpoint) { return endpoint.type === 'messenger' }
|
13 | static isChannel (endpoint) { return endpoint.type === 'discord' }
|
14 |
|
15 | addThread ({ id, name }) {
|
16 | this.endpoints.push({
|
17 | type: 'messenger',
|
18 | id: id.toString(),
|
19 | name
|
20 | })
|
21 | return this
|
22 | }
|
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 |
|
34 | has (id) {
|
35 | return this.endpoints.some(endpoint => endpoint.id === id.toString())
|
36 | }
|
37 |
|
38 | getWritableEndpoints () { return this.endpoints.filter(endpoint => !endpoint.readonly) }
|
39 |
|
40 | getThreads () { return this.endpoints.filter(Connection.isThread) }
|
41 | getOtherThreads (id) { return this.getThreads().filter(thread => thread.id !== id.toString()) }
|
42 | getWritableThreads () { return this.getWritableEndpoints().filter(Connection.isThread) }
|
43 | getOtherWritableThreads (id) { return this.getWritableThreads().filter(thread => thread.id !== id.toString()) }
|
44 |
|
45 | getChannels () { return this.endpoints.filter(Connection.isChannel).filter(channel => channel.channel) }
|
46 | getOtherChannels (id) { return this.getChannels().filter(channel => channel.id !== id) }
|
47 | getWritableChannels () { return this.getWritableEndpoints().filter(Connection.isChannel).filter(channel => channel.channel) }
|
48 | getOtherWritableChannels (id) { return this.getWritableChannels().filter(channel => channel.id !== id) }
|
49 |
|
50 | async checkChannelRenames (name) {
|
51 | if (
|
52 | name &&
|
53 | config.discord.renameChannels &&
|
54 | this.getThreads().length === 1 &&
|
55 | this.getChannels().length === 1 &&
|
56 | !this.getChannels()[0].readonly &&
|
57 | this.getChannels()[0].name !== name
|
58 | ) {
|
59 | const channel = this.getChannels()[0]
|
60 | await channel.channel.edit({ name })
|
61 | }
|
62 | return this
|
63 | }
|
64 |
|
65 | hasEndpoint (id) {
|
66 | return this.endpoints.some(endpoint => endpoint.id === id || endpoint.name === id)
|
67 | }
|
68 |
|
69 | markEndpointAsReadonly (id, readonly) {
|
70 | let endpoint = this.endpoints.find(endpoint => endpoint.id === id || endpoint.name === id)
|
71 | endpoint.readonly = readonly
|
72 | return this
|
73 | }
|
74 |
|
75 | removeEndpoint (id) {
|
76 | let index = this.endpoints.findIndex(endpoint => endpoint.id === id || endpoint.name === id)
|
77 | if (index) this.endpoints.splice(index, 1)
|
78 | return this
|
79 | }
|
80 |
|
81 | getPrintable () {
|
82 | const getLink = ({ type, id }) => type === 'messenger'
|
83 | ? `[\`${id}\`](https://facebook.com/messages/t/${id})`
|
84 | : `<#${id}>`
|
85 | const e = endpoint => `\`${endpoint.type}\`: ${getLink(endpoint)}${endpoint.readonly ? ' (readonly)' : ''}`
|
86 | return this.endpoints.map(e).join('\n')
|
87 | }
|
88 |
|
89 | rename (newName) {
|
90 | connections.list.remove(this.name)
|
91 | this.name = newName
|
92 | return this.save()
|
93 | }
|
94 |
|
95 | async save () {
|
96 | connections.list.set(this.name, this)
|
97 | await connections.save()
|
98 | return this
|
99 | }
|
100 |
|
101 | toObject () {
|
102 | return {
|
103 | [this.name]: this.endpoints
|
104 | .map(({ type, id, name, readonly }) => ({ type, id, name, readonly }))
|
105 | .map(endpoint => {
|
106 | if (!endpoint.readonly) delete endpoint.readonly
|
107 | return endpoint
|
108 | })
|
109 | }
|
110 | }
|
111 | }
|
112 |
|
113 | module.exports = Connection
|