2019-09-26 02:13:20 +00:00
|
|
|
/*
|
2020-06-12 14:03:08 +00:00
|
|
|
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
2019-09-26 02:13:20 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as path from "path";
|
|
|
|
import {
|
2019-10-31 15:55:34 +00:00
|
|
|
LogLevel,
|
2019-09-26 02:13:20 +00:00
|
|
|
LogService,
|
|
|
|
MatrixClient,
|
2019-10-03 04:20:37 +00:00
|
|
|
PantalaimonClient,
|
2019-09-26 02:13:20 +00:00
|
|
|
RichConsoleLogger,
|
|
|
|
SimpleFsStorageProvider
|
|
|
|
} from "matrix-bot-sdk";
|
|
|
|
import config from "./config";
|
2020-06-12 14:03:08 +00:00
|
|
|
import { Healthz } from "./health/healthz";
|
2021-09-27 14:52:28 +00:00
|
|
|
import { Mjolnir } from "./Mjolnir";
|
2022-01-06 11:19:19 +00:00
|
|
|
import { patchMatrixClient } from "./utils";
|
2019-11-07 01:46:49 +00:00
|
|
|
|
2021-07-22 06:38:44 +00:00
|
|
|
config.RUNTIME = {};
|
2019-09-26 02:13:20 +00:00
|
|
|
|
|
|
|
LogService.setLogger(new RichConsoleLogger());
|
2019-10-31 15:55:34 +00:00
|
|
|
LogService.setLevel(LogLevel.fromString(config.logLevel, LogLevel.DEBUG));
|
|
|
|
|
|
|
|
LogService.info("index", "Starting bot...");
|
2019-09-26 02:13:20 +00:00
|
|
|
|
2020-06-12 14:03:08 +00:00
|
|
|
Healthz.isHealthy = false; // start off unhealthy
|
|
|
|
if (config.health.healthz.enabled) {
|
|
|
|
Healthz.listen();
|
|
|
|
}
|
|
|
|
|
2019-10-03 04:20:37 +00:00
|
|
|
(async function () {
|
2022-02-15 15:44:41 +00:00
|
|
|
let bot: Mjolnir | null = null;
|
|
|
|
try {
|
|
|
|
const storagePath = path.isAbsolute(config.dataPath) ? config.dataPath : path.join(__dirname, '../', config.dataPath);
|
|
|
|
const storage = new SimpleFsStorageProvider(path.join(storagePath, "bot.json"));
|
|
|
|
|
|
|
|
let client: MatrixClient;
|
|
|
|
if (config.pantalaimon.use) {
|
|
|
|
const pantalaimon = new PantalaimonClient(config.homeserverUrl, storage);
|
|
|
|
client = await pantalaimon.createClientWithCredentials(config.pantalaimon.username, config.pantalaimon.password);
|
|
|
|
} else {
|
|
|
|
client = new MatrixClient(config.homeserverUrl, config.accessToken, storage);
|
|
|
|
}
|
|
|
|
patchMatrixClient();
|
|
|
|
config.RUNTIME.client = client;
|
|
|
|
|
|
|
|
bot = await Mjolnir.setupMjolnirFromConfig(client);
|
2022-04-01 15:40:10 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(`Failed to setup mjolnir from the config ${config.dataPath}: ${err}`);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
try {
|
2022-02-15 15:44:41 +00:00
|
|
|
await bot.start();
|
|
|
|
} catch (err) {
|
2022-04-01 15:40:10 +00:00
|
|
|
console.error(`Mjolnir failed to start: ${err}`);
|
|
|
|
throw err;
|
2019-10-03 04:20:37 +00:00
|
|
|
}
|
2022-02-15 15:44:41 +00:00
|
|
|
})();
|