-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
158 lines (137 loc) · 4.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"use strict";
require('dotenv').config()
const path = require("path");
const Config = require("./core/config.js");
const Router = require("./core/router.js");
const Server = require("./core/server");
const MoudleLoader = require("./core/module-loader");
const SlackCatBot = require("./core/slack-cat-bot.js");
const { RTMClient } = require("@slack/rtm-api");
const SlackCatEvents = require('./core/events');
const Sequelize = require("sequelize");
// Global Base Modules.
global.BaseModule = require("./core/base-module.js");
global.BaseStorageModule = require("./core/base-storage-module.js");
global.HolidayOverride = require("./core/HolidayOverride");
const {
testMsg,
testReaction,
testMemberJoin
} = require("./core/models/MockMessageData");
class SlackCat {
constructor(pathToModules, configPath, dbPath, verboseDbLogging = false) {
this.pathToModules = pathToModules;
global.config = new Config(configPath);
this.initDatabase(dbPath, verboseDbLogging);
}
initDatabase(dbPath, verboseDbLogging) {
const poolConfig = {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
};
const dbConfig = config.getKey("db");
if (!dbConfig) {
global.database = new Sequelize(null, null, null, {
dialect: "sqlite",
storage: dbPath, // global.
logging: verboseDbLogging,
pool: poolConfig,
});
return;
}
console.log("Using remote db...");
const { dialect, username, password, port, host, dbName, ssl } = dbConfig;
const sequelizeConfig = {
dialect: dialect,
port: port,
logging: verboseDbLogging,
pool: poolConfig,
dialectOptions: {
ssl: ssl
}
};
if (host) {
sequelizeConfig["host"] = host;
}
global.database = new Sequelize(
dbName,
username,
password,
sequelizeConfig,
);
}
start() {
// Run debug cmds.
if (process.argv.length > 2) {
global.DEBUG = true;
this.runDebugCommand();
return;
}
const rtm = new RTMClient(config.getKey("slack_access_token"));
let router;
rtm.start();
rtm.on("authenticated", data => {
console.log('Incoming RTM authenticated event');
if (!router) {
const bot = new SlackCatBot(data);
const server = new Server();
const moduleLoader = new MoudleLoader(bot, server, this.pathToModules);
const modules = moduleLoader.getModules();
// Fix me :(((((((((((
bot.setModules(modules);
router = new Router(bot, modules, server);
server.start(() => {
SlackCatEvents.publish(SlackCatEvents.EventTypes.SetupComplete, []);
});
}
});
rtm.on("message", data => {
router.handle(data);
});
rtm.on("reaction_added", data => {
router.handle(data);
});
rtm.on("member_joined_channel", data => {
router.handle(data);
});
}
async runDebugCommand() {
// Reaction debug msg
try {
const MockBot = require(path.join(__dirname + "/core", "mock-bot.js"));
let server;
// Copy array.
const args = process.argv.slice(0);
if (args.includes("--with-server")) {
server = new Server();
}
const bot = new MockBot();
const moduleLoader = new MoudleLoader(bot, server, this.pathToModules);
const modules = moduleLoader.getModules();
// Fix me :(((((((((((
bot.setModules(modules);
const router = new Router(bot, modules, server);
if (server) {
server.start();
}
if (args.includes("member_joined_channel")) {
await router.handle(testMemberJoin);
} else if (process.argv.includes("--reaction")) {
testReaction.reaction = args[2].replace(new RegExp(":", "g"), "");
console.log("Executing reaction: " + testReaction.reaction);
await router.handle(testReaction);
} else {
// Regular debug message
testMsg.text = args.splice(2, args.length - 1).join(" ");
await router.handle(testMsg);
}
} catch (e) {
console.trace(e);
// Use debug cmd to fail tests on ci. Obvi a hack.
process.exitCode = 1
}
}
}
module.exports = { SlackCat, SlackCatEvents };