-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
65 lines (57 loc) · 2.45 KB
/
index.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
import Path from "path";
import "reflect-metadata";
import snoowrap from "snoowrap";
import { container, instanceCachingFactory } from "tsyringe";
import Application from "./Application";
import authorizer from "./shared/authorization/Authorizer";
import Configuration from "./shared/configuration/Configuration";
import { Mode } from "./shared/configuration/Mode";
import Feature from "./shared/features/Feature";
import ExampleFeature from "./features/exampleFeature/ExampleFeature";
import { constructor } from "tsyringe/dist/typings/types";
import R7InsightLogger from "r7insight_node";
require('dotenv').config({ path: Path.join(__dirname, "../variables.env") });
let clientID = process.env.CLIENT_ID;
let clientSecret = process.env.CLIENT_SECRET;
let refreshToken = process.env.REFRESH_TOKEN;
if (clientID == null || clientSecret == null || refreshToken == null) {
authorizer.
start().
then(authorizationResult => {
clientID = authorizationResult.clientID;
clientSecret = authorizationResult.clientSecret;
refreshToken = authorizationResult.refreshToken;
onAuthorized();
});
} else {
onAuthorized();
}
function onAuthorized() {
// Get configuration values
const mode = process.env.RSGBOT_ENV === 'production' ? Mode.production : Mode.development;
const subreddit = process.env.SUBREDDIT ?? 'RSGBot';
const loggingPat = process.env.LOGGING_PAT;
// Register shared objects
container.registerInstance(Configuration, new Configuration(mode, subreddit));
container.registerInstance(snoowrap, new snoowrap({
userAgent: 'RSGBot v0.1',
clientId: clientID,
clientSecret: clientSecret,
refreshToken: refreshToken
}));
container.register(R7InsightLogger, {
useFactory: instanceCachingFactory(() => loggingPat == null ? null : new R7InsightLogger({
token: loggingPat,
region: 'us'
}))
});
// Register your feature in the development block while developing. Register in the production block when you're ready to merge into master.
if (mode === Mode.development) {
container.register(Feature as constructor<Feature>, ExampleFeature); // Cast abstract class constructor to concrete constructor - https://github.com/microsoft/tsyringe/issues/108
} else {
// Production features
}
// Start application
const application = container.resolve(Application);
application.start();
}