2024-04-04 21:44:45 -04:00
|
|
|
const fs = require('fs/promises')
|
2024-02-11 21:23:41 -05:00
|
|
|
const path = require('path')
|
2024-04-04 21:44:45 -04:00
|
|
|
const readline = require('readline')
|
2024-02-16 21:37:11 -05:00
|
|
|
const json5 = require('json5')
|
2024-03-16 16:03:36 -04:00
|
|
|
const matrix = require('matrix-js-sdk')
|
2024-02-11 21:23:41 -05:00
|
|
|
|
2024-04-04 21:44:45 -04:00
|
|
|
const createBot = require('./bot.js')
|
|
|
|
const fileExists = require('./util/file_exists')
|
|
|
|
const Console = require('./util/console')
|
|
|
|
|
|
|
|
async function main () {
|
|
|
|
// config loading
|
|
|
|
const configPath = process.argv[2] ?? 'config.json5'
|
|
|
|
if (!await fileExists(configPath)) {
|
|
|
|
await fs.copyFile(path.join(__dirname, 'default.json5'), configPath)
|
|
|
|
console.info('No config file was found, so a default one was created')
|
2024-02-11 21:23:41 -05:00
|
|
|
}
|
2024-04-04 21:44:45 -04:00
|
|
|
const config = json5.parse(await fs.readFile(configPath, 'utf-8'))
|
2024-02-11 21:23:41 -05:00
|
|
|
|
2024-04-04 21:44:45 -04:00
|
|
|
// logging
|
|
|
|
const logdir = config.paths.logs ?? 'logs'
|
|
|
|
if (!await fileExists(logdir)) await fs.mkdir(logdir)
|
2024-02-11 21:23:41 -05:00
|
|
|
|
2024-04-04 21:44:45 -04:00
|
|
|
const rl = readline.createInterface({
|
|
|
|
input: process.stdin,
|
|
|
|
output: process.stdout,
|
|
|
|
prefix: '> '
|
|
|
|
})
|
2024-03-16 16:03:36 -04:00
|
|
|
|
2024-04-04 21:44:45 -04:00
|
|
|
const console = new Console({ readline: rl })
|
|
|
|
await console.createLogFile(logdir)
|
|
|
|
|
|
|
|
const matrixClients = {}
|
|
|
|
for (const key in config.matrixClients) {
|
|
|
|
const client = matrix.createClient(config.matrixClients[key])
|
|
|
|
matrixClients[key] = client
|
2024-03-16 16:03:36 -04:00
|
|
|
|
2024-04-04 21:44:45 -04:00
|
|
|
client.startClient()
|
|
|
|
}
|
2024-03-16 16:03:36 -04:00
|
|
|
|
2024-04-04 21:44:45 -04:00
|
|
|
const bots = []
|
|
|
|
for (const options of config.bots) {
|
2024-04-05 20:54:10 -04:00
|
|
|
const mergedOptions = {
|
|
|
|
console,
|
|
|
|
paths: config.paths,
|
|
|
|
...(config.all ?? {}),
|
|
|
|
...options
|
|
|
|
}
|
2024-04-04 21:44:45 -04:00
|
|
|
if (mergedOptions.matrix && typeof mergedOptions.matrix.client !== 'object') mergedOptions.matrix.client = matrixClients[mergedOptions.matrix.client]
|
2024-02-16 22:59:09 -05:00
|
|
|
|
2024-04-04 21:44:45 -04:00
|
|
|
const bot = createBot(mergedOptions)
|
|
|
|
bots.push(bot)
|
|
|
|
bot.bots = bots
|
2024-02-11 21:23:41 -05:00
|
|
|
|
2024-04-04 21:44:45 -04:00
|
|
|
bot.on('error', error => bot.console.error(error))
|
|
|
|
}
|
2024-07-06 15:14:56 -04:00
|
|
|
|
|
|
|
process.on('uncaughtException', error => {
|
|
|
|
if (error.stack.includes('protodef')) {
|
|
|
|
console.error('Uncaught protodef exception!\n' + error.stack)
|
|
|
|
return // Ignore protodef-related errors (packet-related)
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error
|
|
|
|
})
|
2024-02-16 22:59:09 -05:00
|
|
|
}
|
2024-04-04 21:44:45 -04:00
|
|
|
|
|
|
|
main()
|