forked from lnp2pBot/bot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.ts
More file actions
61 lines (55 loc) · 1.84 KB
/
app.ts
File metadata and controls
61 lines (55 loc) · 1.84 KB
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
import 'dotenv/config';
import { SocksProxyAgent } from 'socks-proxy-agent';
import { start } from './bot/start';
import { connect as mongoConnect } from './db_connect';
import { resubscribeInvoices } from './ln';
import { logger } from './logger';
import { Telegraf } from 'telegraf';
import { delay } from './util';
import { imageCache } from './util/imageCache';
import { createIndexes } from './models/indexes';
import { CommunityContext } from './bot/modules/community/communityContext';
(async () => {
process.on('unhandledRejection', e => {
if (e) {
logger.error(`Unhandled Rejection: ${e}`);
}
});
process.on('uncaughtException', e => {
if (e) {
logger.error(`Uncaught Exception: ${e}`);
}
});
const mongoose = mongoConnect();
mongoose.connection
.once('open', async () => {
logger.info('Connected to Mongo instance.');
// Create database indexes for optimized queries
await createIndexes();
// Initialize image cache for faster order creation
await imageCache.initialize();
// Use configurable bot handler timeout, default to 60 seconds
const handlerTimeout = parseInt(
process.env.BOT_HANDLER_TIMEOUT || '60000',
);
let options: Partial<Telegraf.Options<CommunityContext>> = {
handlerTimeout,
};
if (process.env.SOCKS_PROXY_HOST) {
const agent = new SocksProxyAgent(process.env.SOCKS_PROXY_HOST);
options = {
...options,
telegram: {
agent: agent as any,
},
};
}
const bot = start(String(process.env.BOT_TOKEN), options);
// Wait 1 seconds before try to resubscribe hold invoices
await delay(1000);
await resubscribeInvoices(bot);
})
.on('error', (error: Error) =>
logger.error(`Error connecting to Mongo: ${error}`),
);
})();