chipmunkbot3/index.js

108 lines
3 KiB
JavaScript
Raw Permalink Normal View History

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')
const matrix = require('matrix-js-sdk')
2024-09-04 11:24:57 -04:00
const irc = require('matrix-org-irc')
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)
globalThis.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'
2024-04-04 21:44:45 -04:00
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-04-04 21:44:45 -04:00
const console = new Console({ readline: rl })
await console.createLogFile(logdir)
2024-09-04 11:24:57 -04:00
for (const options of config.bots) {
// pass through the options to find irc channels
2024-09-04 11:50:22 -04:00
if (!options.irc || !config.ircClients || !config.ircClients[options.irc.client]) continue
2024-09-04 11:24:57 -04:00
config.ircClients[options.irc.client].channels ??= []
config.ircClients[options.irc.client].channels.push(options.irc.channel)
}
2024-04-04 21:44:45 -04:00
const matrixClients = {}
2024-09-04 11:24:57 -04:00
if (config.matrixClients) {
for (const key in config.matrixClients) {
const client = matrix.createClient(config.matrixClients[key])
matrixClients[key] = client
client.startClient()
}
}
const ircClients = {}
if (config.ircClients) {
for (const key in config.ircClients) {
const options = config.ircClients[key]
options.autoRejoin ??= true
2024-09-04 11:24:57 -04:00
const client = new irc.Client(options.server, options.nick, options)
ircClients[key] = client
client.on('motd', () => {
if (options.oper) client.send('oper', options.oper[0], options.oper[1])
})
}
2024-04-04 21:44:45 -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-09-04 11:24:57 -04:00
if (mergedOptions.irc && typeof mergedOptions.irc.client !== 'object') mergedOptions.irc.client = ircClients[mergedOptions.irc.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
bot.on('error', error => bot.console.error(error.stack))
2024-04-04 21:44:45 -04:00
}
process.on('uncaughtException', error => {
2024-08-04 17:52:43 -04:00
if (error.stack.includes('protodef') || error.code === 'Z_BUF_ERROR') {
2024-11-07 19:44:55 -05:00
console.error('Uncaught packet error!\n' + error.stack)
return // Ignore packet errors
}
globalThis.console.error(error)
2024-11-07 19:44:55 -05:00
try {
console.end()
} catch {
}
try {
fs.appendSync(console.filename, 'Crashed due to an uncaught exception!\n' + error.stack)
} catch {
}
2024-08-04 17:52:43 -04:00
process.exit(1)
})
2024-02-16 22:59:09 -05:00
}
2024-04-04 21:44:45 -04:00
main()