forked from destinygg/chat-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (79 loc) · 2.94 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
/* eslint-disable no-process-exit */
const { argv } = require('yargs');
const DestinyChat = require('./lib/services/destinychat');
const TwitchChat = require('./lib/services/twitch-chat');
const CommandRouter = require('./lib/message-routing/command-router');
const Services = require('./lib/services/service-index');
const loadConfig = require('./lib/configuration/config-loader');
const MessageRouter = require('./lib/message-routing/message-router');
const ChatServiceRouter = require('./lib/message-routing/chat-service-router');
const {
registerCommandsFromFiles,
setupCommandsAndCachesFromDb,
} = require('./lib/configuration/configure-commands');
const { configureReporter } = require('./lib/services/metrics/metrics-reporter');
const config = loadConfig(argv.config);
const chatToConnectTo = argv.chat || config.chatToConnectTo;
config.chatToConnectTo = chatToConnectTo;
if (config === null) {
// eslint-disable-next-line no-console
console.log('WARNING: Config file not found, no config loaded. Shutting down.');
process.exit(0);
}
const services = new Services(config, chatToConnectTo);
configureReporter(config.influx, new Map([['chat', config.chatToConnectTo]]));
const { logger } = services;
services
.prepareAsyncServices()
.then(() => {
registerCommandsFromFiles(services.commandRegistry, chatToConnectTo, config);
logger.info('Config loaded! Starting bot!');
return setupCommandsAndCachesFromDb(
services.sql,
services.commandRegistry,
services.scheduledCommands,
services.spamDetection,
services.logger,
).catch((err) => {
logger.warn(`Problem loading commands/banned phrases from sql. Reason: ${err}`);
});
})
.then(() => {
logger.info(`Configuring for ${chatToConnectTo} chat`);
const commandRouter = new CommandRouter(services);
const messageRouter = new MessageRouter({}, services);
let bot = null;
if (chatToConnectTo === 'twitch') {
bot = new TwitchChat(config.twitch, services);
} else if (chatToConnectTo === 'dgg') {
bot = new DestinyChat(config.dggChat, services);
} else {
logger.error('Config property: "chatToConnectTo" not set to one of "dgg" or "twitch"');
process.exit(1);
}
if(config.hasOwnProperty('scheduledCommands')){
config.scheduledCommands.forEach(commandToSchedule => services.fakeScheduler.createMessage(commandToSchedule));
}
const chatServiceRouter = new ChatServiceRouter(
config.chatToConnectTo,
bot,
messageRouter,
commandRouter,
logger,
services.punishmentStream,
services.scheduledCommands,
services.fakeScheduler,
services.messageRelay,
);
chatServiceRouter.create();
})
.catch((err) => {
logger.error('Problem starting up services, shutting down...');
logger.error(err);
process.exit(1);
});
process.on('uncaughtException', (err) => {
logger.error('Uncaught exception. Crashing.');
logger.error(err);
process.exit(1);
});