1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const discord_js_1 = require("discord.js");
|
7 | const Connection_1 = __importDefault(require("./Connection"));
|
8 | const messenger_1 = require("./messenger");
|
9 | class ConnectionsManager extends discord_js_1.Collection {
|
10 | async load() {
|
11 | const log = logger.withScope('CM:init');
|
12 | try {
|
13 |
|
14 | let parsed = await config.loadConnections();
|
15 | if (!parsed || typeof parsed !== 'object')
|
16 | return this.save();
|
17 | for (let [name, endpoints] of Object.entries(parsed)) {
|
18 | log.trace(name, endpoints);
|
19 | if (name.startsWith('_')) {
|
20 | if (name === '__comment')
|
21 | continue;
|
22 | name = name.substr(1);
|
23 | if (Array.isArray(endpoints)) {
|
24 | await new Connection_1.default(name, endpoints).disable();
|
25 | }
|
26 | continue;
|
27 | }
|
28 | if (!Array.isArray(endpoints) ||
|
29 | endpoints.some(el => typeof el !== 'object'))
|
30 | throw new Error(`Incorrect connection syntax: ${name}`);
|
31 | try {
|
32 | await new Connection_1.default(name, await ConnectionsManager.validateEndpoints(endpoints)).save();
|
33 | }
|
34 | catch (err) {
|
35 | log.warn(err.message);
|
36 | log.warn(`Disabling temporarily connection ${name}, you can re-enable it once the error is fixed.`);
|
37 | await new Connection_1.default(name, endpoints).disable().save();
|
38 | }
|
39 | }
|
40 | await this.save();
|
41 | }
|
42 | catch (err) {
|
43 | if (err.code !== 'ENOENT')
|
44 | throw err;
|
45 | await this.save();
|
46 | }
|
47 | }
|
48 | static async validateEndpoints(endpoints) {
|
49 | const newEndpoints = [];
|
50 | for (let endpoint of endpoints) {
|
51 | if (!endpoint.type || !endpoint.id) {
|
52 | throw new Error(`Type or ID missing on endpoint: ${JSON.stringify(endpoint)}`);
|
53 | }
|
54 | if (endpoint.type !== 'discord' && endpoint.type !== 'messenger') {
|
55 | throw new Error(`Endpoint type incorrect, should be 'messenger' or 'discord', actual: ${endpoint.type}`);
|
56 | }
|
57 | if (typeof endpoint.id !== 'string') {
|
58 | throw new Error(`Endpoint ID is not a string (wrap it in single quotes): ${endpoint.id}`);
|
59 | }
|
60 | if (endpoint.type === 'discord' && !discord.client.channels.get(endpoint.id)) {
|
61 | throw new Error(`Channel ${endpoint.id} not found!`);
|
62 | }
|
63 | if (endpoint.type === 'messenger') {
|
64 | try {
|
65 | await messenger_1.getThread(endpoint.id);
|
66 | }
|
67 | catch (err) {
|
68 | throw new Error(`Thread ${endpoint.id} not found!`);
|
69 | }
|
70 | }
|
71 | newEndpoints.push(endpoint);
|
72 | }
|
73 | return newEndpoints;
|
74 | }
|
75 | static async createAutomaticDiscordChannel(threadID, name) {
|
76 | const log = logger.withScope('CM:createAutomaticDiscordChannel');
|
77 |
|
78 | const channel = await discord.guilds[0].createChannel(name, 'text');
|
79 | await channel.overwritePermissions(discord.guilds[0].roles.find(role => role.name === '@everyone').id, { VIEW_CHANNEL: false });
|
80 | log.debug(`Channel created, name: ${name}, id: ${channel.id}`);
|
81 |
|
82 | if (discord.category && discord.category.guild.id === channel.guild.id)
|
83 | await channel.setParent(discord.category);
|
84 |
|
85 | const connection = new Connection_1.default(name)
|
86 | .addChannel(channel.id)
|
87 | .addThread(threadID.toString());
|
88 | await connection.save();
|
89 | log.trace('new connection', connection, 1);
|
90 | return connection;
|
91 | }
|
92 | async getWithCreateFallback(threadID, name) {
|
93 | const log = logger.withScope('CM:getWithCreateFallback');
|
94 | let connection = this.getWith(threadID);
|
95 | if (!connection) {
|
96 | if (!config.discord.createChannels)
|
97 | return log.debug(`Channel creation disabled, ignoring. Name: ${name}, id: ${threadID}`);
|
98 | connection = await ConnectionsManager.createAutomaticDiscordChannel(threadID, name);
|
99 | }
|
100 | return connection;
|
101 | }
|
102 | getWith(id) {
|
103 | return this.find(connection => connection.has(id));
|
104 | }
|
105 | hasWith(id) {
|
106 | return this.some(connection => connection.has(id));
|
107 | }
|
108 | get(name) {
|
109 |
|
110 |
|
111 | return super.get(name) || new Connection_1.default(name);
|
112 | }
|
113 | hasEndpoint(id) {
|
114 | return this.some(connection => connection.has(id));
|
115 | }
|
116 | save() {
|
117 | const yamlConnections = Object.assign({
|
118 | __comment: 'This is your connections.yml file. More info at https://github.com/miscord/miscord/wiki/Connections.yml'
|
119 | }, ...this.map(connection => connection.toYAMLObject()));
|
120 | return config.saveConnections(yamlConnections);
|
121 | }
|
122 | }
|
123 | exports.default = ConnectionsManager;
|