-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.ts
94 lines (81 loc) · 2.38 KB
/
app.ts
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
'use strict';
// Check dependencies & node version
try {
eval('{ let a = async () => {}; }');
} catch (e) {
console.log("We require Node.js version 8 or later; you're using " + process.version);
process.exit(1);
}
const fs = require('fs');
try {
// add dependecies here
require('websocket');
} catch (e) {
console.log('Dependencies unmet! Installing them...');
require('child_process').execSync('npm install --production', {stdio: 'inherit'});
// exit here if needed, not sure if it is.
}
// Load config
try {
global.Config = require('./config/config.js');
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') throw e; // Shouldn't happen
console.log('config.js not found, creating one with default settings...');
fs.copyFileSync('config/config-example.js', 'config/config.js');
console.log('Please restart the bot.');
process.exit(1);
}
// Setup globals
import { Tools } from './tools';
global.Tools = Tools;
global.toId = Tools.toId;
Config.nickid = toId(Config.nick);
if (!toId(Config.nick)) {
console.log(`${Config.nick ? 'An invalid' : 'No'} nickname was provided, please edit the bot's username in config.js`);
process.exit(1);
}
if (!Config.primaryRoom) console.log("No primary room set, commands requiring auth will not work in PMs.");
global.debug = (msg: string) => {
if (!Config.debugMode) return;
console.log("[DEBUG] " + msg);
};
global.log = (msg: string) => {
if (!Config.verboseMode) return;
console.log("[LOG] " + msg);
};
import { Rooms } from './rooms';
global.Rooms = Rooms;
import { Chat } from './chat';
global.Chat = Chat; // handles the connection too
global.Mafia = require('./mafia.js');
Chat.loadCommands();
global.sendMessage = Chat.sendMessage;
global.sendPM = Chat.sendPM;
if (Config.webhookCrashURL) {
const https = require('https');
function reportError(e: Error) { // eslint-disable-line no-inner-declarations
const data = {
content: e.stack,
};
const reqOptions = {
hostname: "discordapp.com",
path: `/api/webhooks/${Config.webhookCrashURL}`,
agent: false,
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
},
};
const req = https.request(reqOptions, (res: any) => {});
req.on('error', (e: Error) => {
console.error(`Error while making request: ${e.stack}`);
return;
});
req.write(JSON.stringify(data));
req.end();
}
process.on('uncaughtException', (e) => {
reportError(e);
});
}
Chat.client.connect();