2024-02-11 21:23:41 -05:00
|
|
|
const readline = require('readline')
|
2024-02-16 22:59:09 -05:00
|
|
|
const createBot = require('./bot.js')
|
2024-02-11 21:23:41 -05:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const moment = require('moment')
|
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-02-16 21:37:11 -05:00
|
|
|
if (!fs.existsSync('config.json5')) {
|
2024-02-16 22:59:09 -05:00
|
|
|
fs.copyFileSync(path.join(__dirname, 'default.json5'), 'config.json5')
|
2024-02-16 21:37:11 -05:00
|
|
|
console.info('No config file was found, so a default one was created.')
|
|
|
|
}
|
|
|
|
const config = json5.parse(fs.readFileSync('config.json5', 'utf-8'))
|
|
|
|
|
|
|
|
const logdir = 'logs'
|
|
|
|
if (!fs.existsSync(logdir)) fs.mkdirSync(logdir)
|
2024-02-12 21:54:29 -05:00
|
|
|
|
2024-02-16 22:59:09 -05:00
|
|
|
let logfile = path.join(logdir, moment().format('YYYY-MM-DD'))
|
2024-02-16 21:37:11 -05:00
|
|
|
if (fs.existsSync(logfile)) {
|
|
|
|
const pathWithSeparator = logfile + '-'
|
2024-02-11 21:23:41 -05:00
|
|
|
let i = 0
|
2024-02-16 21:37:11 -05:00
|
|
|
while (fs.existsSync(logfile)) {
|
|
|
|
logfile = pathWithSeparator + (i++)
|
2024-02-11 21:23:41 -05:00
|
|
|
}
|
|
|
|
}
|
2024-02-16 22:59:09 -05:00
|
|
|
logfile += '.log'
|
2024-02-16 21:37:11 -05:00
|
|
|
fs.writeFileSync(logfile, '')
|
2024-02-11 21:23:41 -05:00
|
|
|
|
2024-02-16 21:37:11 -05:00
|
|
|
const rl = readline.createInterface({
|
|
|
|
input: process.stdin,
|
|
|
|
output: process.stdout,
|
|
|
|
prefix: '> '
|
|
|
|
})
|
2024-02-11 21:23:41 -05:00
|
|
|
|
2024-03-16 16:03:36 -04:00
|
|
|
const matrixClients = {}
|
|
|
|
for (const key in config.matrixClients) {
|
|
|
|
const client = matrix.createClient(config.matrixClients[key])
|
|
|
|
matrixClients[key] = client
|
|
|
|
|
|
|
|
client.startClient()
|
|
|
|
}
|
|
|
|
|
2024-02-16 22:59:09 -05:00
|
|
|
for (const options of config.bots) {
|
2024-03-16 16:03:36 -04:00
|
|
|
const mergedOptions = { ...(config.all ?? {}), ...options }
|
|
|
|
if (mergedOptions.matrix && typeof mergedOptions.matrix.client !== 'object') mergedOptions.matrix.client = matrixClients[mergedOptions.matrix.client]
|
|
|
|
|
|
|
|
const bot = createBot(mergedOptions)
|
2024-02-16 22:59:09 -05:00
|
|
|
|
|
|
|
bot.on('error', console.error)
|
2024-02-11 21:23:41 -05:00
|
|
|
|
2024-02-16 22:59:09 -05:00
|
|
|
bot.console.filepath = logfile
|
2024-02-11 21:23:41 -05:00
|
|
|
bot.console.setRl(rl)
|
|
|
|
bot.commands.loadFromDir('commands')
|
2024-02-16 22:59:09 -05:00
|
|
|
}
|