cubbot/src/Cubbot.ts

106 lines
3.2 KiB
TypeScript
Raw Permalink Normal View History

2020-04-19 17:02:07 +00:00
import assert from 'assert'
import * as mc from 'minecraft-protocol'
import { default as pino } from 'pino'
import moduleList from './modules'
import Module, { IModuleType } from './utils/Module'
2020-04-20 21:07:23 +00:00
import Client from './modules/Connection'
2020-04-19 17:02:07 +00:00
interface IConfig {
/** Minecraft protocol options */
client: mc.ClientOptions,
/** Logger options */
log: pino.LoggerOptions,
/** Modules to load and options */
modules: { [name: string]: object }
}
/** Modules manager */
export default class Cubbot {
private logger: pino.Logger
private _client?: mc.Client
public get client() {
return this._client
}
private _registeredModules = new Map(moduleList.map(m => [m.name, m]))
private _modules?: Map<string, Module<{}>>
public get modules() {
return this._modules
}
constructor(private readonly config: IConfig) {
this.logger = pino(this.config.log)
this.logger.warn('Cubbot')
assert.ok(this.config.client)
}
/** Add addition module */
public registerModule(module: IModuleType, name?: string) {
const n: string = name || module.name
if (this._registeredModules.has(n)) {
2020-04-20 21:07:23 +00:00
this.logger.warn({ msg: 'Overriding module', value: n })
2020-04-19 17:02:07 +00:00
}
this._registeredModules.set(n, module)
}
2020-04-28 19:06:52 +00:00
public getModule<T extends Module<{}>>(module: IModuleType, load: boolean): T | undefined {
return this.getModuleByName(module.name, load) as T | undefined
2020-04-19 17:02:07 +00:00
}
2020-04-28 19:06:52 +00:00
public getModuleByName(name: string, load: boolean) {
2020-04-19 17:02:07 +00:00
if (this.modules!.has(name)) {
return this.modules!.get(name)!
}
2020-04-28 19:06:52 +00:00
if (load) {
this.logger.debug('Loading module %s', name)
assert.ok(this._registeredModules.has(name), `Unknown module ${name}`)
const mType = this._registeredModules.get(name)!
const conf: { log?: string } = this.config.modules[name] || {}
const mLogger = this.logger.child({ name, level: conf.log })
mLogger.trace('Logger created')
const module = new mType(this.client!, mLogger, conf, this.getModule.bind(this))
this._modules!.set(name, module)
return module
}
2020-04-19 17:02:07 +00:00
}
/** Start bot */
public mount() {
if (!this.client) {
this.logger.debug('Creating client')
this._client = mc.createClient(this.config.client)
this.logger.debug('Loading modules')
this._modules = new Map<string, Module<{}>>()
2020-04-28 19:06:52 +00:00
this.getModule<Client>(Client, true)!.setEngine(this)
Object.keys(this.config.modules).forEach(m => this.getModuleByName(m, true))
2020-04-19 17:02:07 +00:00
}
}
/** Stop bot */
2020-04-20 21:07:23 +00:00
public umount(exit: boolean = false) {
2020-04-19 17:02:07 +00:00
if (this.client) {
this.logger.debug('Unloading modules')
this.modules!.forEach(m => m.umount())
this._modules = undefined
this.logger.debug('Removing client')
//TODO: disconnect
this._client = undefined
2020-04-20 21:07:23 +00:00
if (exit) {
this.logger.warn('Stopped')
this.logger.flush()
setTimeout(process.exit, 100)
}
2020-04-19 17:02:07 +00:00
}
}
}