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