49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import md from 'minecraft-data'
|
|
import { vsprintf } from 'sprintf-js'
|
|
import Module from '../utils/Module'
|
|
|
|
/** Message packet payload */
|
|
interface IMessage { text?: string, extra?: Array<{text: string}>, translate?: string, with?: IMessage[] }
|
|
|
|
/** Handle chat display and replies */
|
|
export default class Chat extends Module<{ reply?: string }> {
|
|
|
|
/** Translation dictionary (en only) */
|
|
private dict!: { [key: string]: string }
|
|
|
|
/** Convert payload to string */
|
|
public parse(msg: IMessage) {
|
|
let text = ''
|
|
if (msg.text) {
|
|
text += msg.text
|
|
}
|
|
if (msg.translate && msg.translate in this.dict) {
|
|
text += vsprintf(this.dict[msg.translate], (msg.with || []).map(this.parse.bind(this)))
|
|
}
|
|
if (msg.extra) {
|
|
text += msg.extra.map(({text: t}) => t).join()
|
|
}
|
|
return text
|
|
}
|
|
|
|
protected mount() {
|
|
this.client.on('login', () => {
|
|
// FIXME: use this.client.version when 'minecraft-data' include 1.15 language
|
|
this.dict = md('1.14').language
|
|
})
|
|
|
|
this.client.on('chat', packet => {
|
|
const message = JSON.parse(packet.message)
|
|
this.logger.info(this.parse(message))
|
|
})
|
|
}
|
|
|
|
//TODO: chat write
|
|
//TODO: reply
|
|
|
|
protected getConf() {
|
|
return { }
|
|
}
|
|
|
|
}
|