forked from TheBastionBot/Bastion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbastion.js
93 lines (82 loc) · 2.96 KB
/
bastion.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
/**
* @file The starting point of Bastion
* @author Sankarsan Kampa (a.k.a k3rn31p4nic)
* @license GPL-3.0
*/
const Tesseract = xrequire('tesseract');
const BASTION = new Tesseract.Client({
settingsDirectory: './settings',
monitorsDirectory: './monitors',
disabledEvents: [
'USER_NOTE_UPDATE',
'TYPING_START',
'RELATIONSHIP_ADD',
'RELATIONSHIP_REMOVE'
]
});
if (BASTION.shard) process.title = `Bastion-Shard-${BASTION.shard.id}`;
else process.title = 'BastionBot';
BASTION.package = xrequire('./package.json');
BASTION.Constants = Tesseract.Constants;
BASTION.colors = Tesseract.Constants.Colors;
BASTION.permissions = Tesseract.Permissions.FLAGS;
xrequire('./prototypes/Number.prototype');
xrequire('./prototypes/Number');
xrequire('./prototypes/String.prototype');
xrequire('./prototypes/Array.prototype');
xrequire('./prototypes/Array');
xrequire('./prototypes/Object');
const WebhookHandler = xrequire('./handlers/webhookHandler.js');
BASTION.webhook = new WebhookHandler(BASTION.credentials.webhooks);
BASTION.log = xrequire('./handlers/logHandler');
BASTION.methods = xrequire('./handlers/methodHandler');
const StringHandler = xrequire('./handlers/stringHandler');
BASTION.i18n = new StringHandler();
const Sequelize = xrequire('sequelize');
BASTION.database = new Sequelize(BASTION.credentials.database.URI, {
operatorsAliases: false,
logging: false
});
BASTION.database.authenticate().then(() => {
// Populate Database/Implement model definitions
xrequire('./utils/models')(Sequelize, BASTION.database);
// Load Bastion Events
xrequire('./handlers/eventHandler')(BASTION);
// Load Bastion Modules
const Modules = xrequire('./handlers/moduleHandler');
BASTION.commands = Modules.commands;
BASTION.aliases = Modules.aliases;
// Start Bastion
BASTION.login(BASTION.credentials.token).then(() => {
/**
* Using <Model>.findOrCreate() won't require the use of
* <ModelInstance>.save() but <Model>.findOrBuild() is used instead because
* <Model>.findOrCreate() creates a race condition where a matching row is
* created by another connection after the `find` but before the `insert`
* call. However, it is not always possible to handle this case in SQLite,
* specifically if one transaction inserts and another tries to select
* before the first one has committed. TimeoutError is thrown instead.
*/
BASTION.database.models.settings.findOrBuild({
where: {
botID: BASTION.user.id
}
}).spread((settingsModel, initialized) => {
if (initialized) {
return settingsModel.save();
}
}).catch(BASTION.log.error);
}).catch(e => {
BASTION.log.error(e.toString());
process.exit(1);
});
}).catch(err => {
BASTION.log.error(err);
});
process.on('unhandledRejection', rejection => {
/* eslint-disable no-console */
console.warn('\n[unhandledRejection]');
console.warn(rejection);
console.warn('[/unhandledRejection]\n');
/* eslint-enable no-console */
});