chipmunkbot3/index.js

58 lines
1.6 KiB
JavaScript
Raw Normal View History

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')
const matrix = require('matrix-js-sdk')
2024-02-11 21:23:41 -05:00
2024-04-02 17:53:10 -04:00
const configPath = process.argv[2] ?? 'config.json5'
if (!fs.existsSync(configPath)) {
fs.copyFileSync(path.join(__dirname, 'default.json5'), configPath)
2024-02-16 21:37:11 -05:00
console.info('No config file was found, so a default one was created.')
}
2024-04-02 17:53:10 -04:00
const config = json5.parse(fs.readFileSync(configPath, 'utf-8'))
2024-02-16 21:37:11 -05:00
2024-04-02 17:58:56 -04:00
const logdir = config.paths.logs ?? 'logs'
2024-02-16 21:37:11 -05:00
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
const matrixClients = {}
for (const key in config.matrixClients) {
const client = matrix.createClient(config.matrixClients[key])
matrixClients[key] = client
client.startClient()
}
2024-04-02 17:53:10 -04:00
const bots = []
2024-02-16 22:59:09 -05:00
for (const options of config.bots) {
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-04-02 17:53:10 -04:00
bots.push(bot)
bot.bots = bots
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)
2024-02-16 22:59:09 -05:00
}