-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstart.js
149 lines (134 loc) · 4.41 KB
/
start.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
const winston = require('winston');
const prompt = require('prompt');
const connect = require('./system/core/connect');
const main = require('./main');
const mongoose = require('bluebird').promisifyAll(require('mongoose'));
const GlobalFn = require('./system/globals');
const Login = require('./config/models/login.js');
const Settings = require('./config/models/settings.js');
const _ = require('lodash');
const replica_event = require('./system/replica/replica_event');
const start = startBot;
const configDB = require('./config/database');
// configuration ===============================================================
mongoose.connect(configDB.url, {
server: {
reconnectTries: Number.MAX_VALUE
}
}, function(err) {
if (err) {
return winston.error(err)
} else {
console.log('Database connection established!');
}
});
process.on('SIGINT', function() {
mongoose.connection.close(function() {
console.log('Mongoose default connection disconnected through app termination');
process.exit(0);
});
});
let HOST,
PORT;
// Check if login info exists;
settingsExists();
Login.findOne({}, function(err, result) {
if (err) {
winston.error('Error ' + err);
} else if (!result) {
winston.warn('Login info not found, creating config... \n');
prompt.start();
prompt.get(schema, function(err, result) {
if (result.dimension === 1) {
HOST = 'chat.d1.funcom.com';
PORT = 7105;
} else if (result.dimension === 2) {
HOST = 'chat.dt.funcom.com';
PORT = 7109;
} else {
winston.warn('Invalid Dimension Selected');
process.exitCode = 1;
}
const newLogin = new Login();
newLogin.username = result.user;
newLogin.password = result.password;
newLogin.dimension = result.dimension;
newLogin.botname = _.capitalize(result.botname);
newLogin.owner = _.capitalize(result.owner);
// Save Login info to db
newLogin.save(function(err) {
if (err) {
throw err;
}
winston.info('Config saved!');
GlobalFn.Login = result.username;
GlobalFn.Pass = result.password;
GlobalFn.botname = _.capitalize(result.botname);
GlobalFn.owner = _.capitalize(result.owner);
start(HOST, PORT);
});
});
} else {
if (result.dimension === 1) {
HOST = 'chat.d1.funcom.com';
PORT = 7105;
} else if (result.dimension === 2) {
HOST = 'chat.dt.funcom.com';
PORT = 7109;
} else {
winston.info('Invalid Dimension Selected');
process.exitCode = 1;
}
//TODO Fix me. global login details
GlobalFn.Login = result.username;
GlobalFn.Pass = result.password;
GlobalFn.owner = result.owner;
GlobalFn.botname = result.botname;
start(HOST, PORT);
}
});
function settingsExists() {
Settings.find({}, function(err, result) {
if (result.length > 0) {
winston.info('Settings found.');
} else {
winston.warn('No Settings found, writing defaults now!');
let newSettings = new Settings({});
newSettings.save(function(err) {
if (err) {
winston.error(err);
process.exitCode = 1;
}
});
}
});
}
var schema = {
properties: {
user: {
description: 'Account Username',
pattern: /^[a-zA-Z\0-9]+$/,
message: 'User must be only letters or numbers',
required: true
},
password: {
description: 'Account Password',
required: true
},
botname: {
description: 'Enter the character name the bot will run on',
required: true
},
dimension: {
description: 'Choose dimension (1 - Rubi-Ka, 2 - Test)',
type: 'number',
pattern: /^[1-2]+$/,
message: 'Numbers only, 1 OR 2',
default: 1
},
owner: {
description: 'Enter the name of the character you wish to be super-admin',
required: true
}
}
};