modernirc
Version:
IRC library for creating modern IRC bots
78 lines (69 loc) • 1.9 kB
JavaScript
/**
* IRCJs - The modern IRC bot library
*/
import { createClient } from './client.js'
import { mergeDeep } from './util.js'
import { createLogger } from './log.js'
import { loadModules } from './module.js'
import { initEvents } from './events.js'
import { createApi } from './api/worker.js'
export * from './format.js'
/**
* Initializes the library enabling the communication with the chosen IRC server.
* Returns an object with functions to handle specific commands or messages.
* @param options {Object} Uplink, Identity and Modules options
* @returns {Promise<import('node:events').EventEmitter>}
*/
export async function createBot(options = {}) {
// we exclude the context from the deep merge as it has no sense (and can create bugs with circular references)
const { context, ...restOptions } = options
const mergedOptions = mergeDeep({
uplink: {
host: 'localhost',
port: 6667,
password: null,
tls: false,
rejectUnauthorized: false,
autoPong: true,
},
identity: {
nickname: 'modernbot',
username: 'modernbot',
realname: 'modernbot',
password: null,
},
autojoin: [
/* { name: '#Channel', key: 'chankey' } */
],
modules: {},
logger: {},
api: {
enabled: false,
listen: {
port: 9999,
host: null,
},
auth: {
key: null, // Mandatory: Change it if you want to use it
},
},
context: {},
autostart: true,
}, restOptions)
const logger = createLogger(mergedOptions.logger)
const fullOptions = {
...mergedOptions,
logger,
}
const bot = await createClient(fullOptions)
bot.context = context
if (fullOptions.api.enabled) {
createApi(bot, fullOptions)
}
initEvents(bot, fullOptions)
await loadModules(bot, fullOptions)
if (fullOptions.autostart) {
bot.init()
}
return bot
}