mirror of
https://github.com/ChomeNS/chomens-bot-mc.git
synced 2024-11-30 10:56:55 -05:00
122 lines
3.1 KiB
JavaScript
122 lines
3.1 KiB
JavaScript
/* eslint-disable no-eval */
|
|
|
|
const moment = require('moment-timezone')
|
|
const util = require('util')
|
|
|
|
function inject (bot, _dcclient, config, rl) {
|
|
// readline > fix on log
|
|
function log (...args) {
|
|
rl.output.write('\x1b[2K\r')
|
|
console.log(args.toString())
|
|
rl._refreshLine()
|
|
};
|
|
|
|
const chatMessage = require('prismarine-chat')(bot.version)
|
|
|
|
function prefix (prefix, _message) {
|
|
const message = `[${moment().format('HH:mm:ss')} ${prefix}§r] [${bot.options.host}] `
|
|
const component = chatMessage.MessageBuilder.fromString(message).toJSON()
|
|
return chatMessage.fromNotch(component).toAnsi() + _message
|
|
}
|
|
|
|
bot.console = {}
|
|
bot.console.host = 'all'
|
|
bot.console.log = function (message) {
|
|
log(prefix('&6LOG', message))
|
|
}
|
|
bot.console.info = function (message) {
|
|
log(prefix('&aINFO', message))
|
|
}
|
|
bot.console.error = function (error) {
|
|
log(prefix('&cERROR', typeof error === 'string' ? error : error.stack))
|
|
}
|
|
|
|
// previous message is op feature to have in console :)
|
|
let previousMessage = ''
|
|
bot.on('parsed_chat', (message) => {
|
|
if (!bot.options.logging) return
|
|
if (previousMessage === message.toString()) return
|
|
previousMessage = message.toString()
|
|
bot.console.log(message.toAnsi())
|
|
})
|
|
|
|
if (!config.console) return
|
|
|
|
function handleLine (line) {
|
|
try {
|
|
if (line.toLowerCase() === '' ||
|
|
line.toLowerCase().startsWith(' ')) return
|
|
|
|
if (line.startsWith('.csvr ')) {
|
|
const host = line.substring(6)
|
|
bot.getBots().forEach((eachBot) => { eachBot.console.host = host })
|
|
bot.console.info(`Host set to: ${host}`)
|
|
return
|
|
}
|
|
|
|
if (bot.options.host !== bot.console.host && bot.console.host !== 'all') return
|
|
if (line.toLowerCase() === '.exit' || line.toLowerCase() === '.end') {
|
|
bot.end('end command')
|
|
return
|
|
}
|
|
if (line.toLowerCase().startsWith('.servereval ')) {
|
|
try {
|
|
bot.tellraw('@a', {
|
|
text: `${util.inspect(eval(`${line.substring(12)}`))}`,
|
|
color: 'green'
|
|
})
|
|
return
|
|
} catch (err) {
|
|
bot.tellraw('@a', { text: `${util.inspect(err)}`, color: 'red' })
|
|
return
|
|
}
|
|
}
|
|
if (line === '.kill') process.exit()
|
|
|
|
if (line.startsWith('.')) {
|
|
return bot.command_handler.run(
|
|
bot.username,
|
|
bot.username,
|
|
'*' + line.substring(1),
|
|
bot.uuid,
|
|
null,
|
|
'h',
|
|
'o'
|
|
)
|
|
}
|
|
bot.tellraw('@a', [
|
|
{
|
|
text: '[',
|
|
color: 'dark_gray'
|
|
},
|
|
{
|
|
text: `${bot.username} Console`,
|
|
color: 'gray'
|
|
},
|
|
{
|
|
text: '] ',
|
|
color: 'dark_gray'
|
|
},
|
|
{
|
|
text: 'chayapak ',
|
|
color: 'green'
|
|
},
|
|
{
|
|
text: '\u203a ',
|
|
color: 'dark_gray'
|
|
},
|
|
chatMessage.MessageBuilder.fromString('&7' + line)
|
|
])
|
|
} catch (e) {
|
|
bot.console.error(e)
|
|
}
|
|
}
|
|
|
|
rl.on('line', handleLine)
|
|
|
|
bot.on('end', () => {
|
|
rl.off('line', handleLine)
|
|
})
|
|
}
|
|
|
|
module.exports = { inject }
|