chomens-bot-js/plugins/console.js

119 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-11-27 02:35:28 -05:00
/* eslint-disable no-eval */
2022-11-15 21:33:16 -05:00
/* eslint-disable require-jsdoc */
/* eslint-disable max-len */
2022-11-27 02:35:28 -05:00
const moment = require('moment-timezone')
const util = require('util')
2022-11-09 07:04:14 -05:00
2022-11-27 02:35:28 -05:00
function inject (bot, _dcclient, config, rl) {
2022-11-20 02:29:11 -05:00
// readline > fix on log
2022-11-27 02:35:28 -05:00
function log (...args) {
rl.output.write('\x1b[2K\r')
console.log(args.toString())
rl._refreshLine()
2022-11-20 02:29:11 -05:00
};
2022-11-09 07:04:14 -05:00
2022-11-27 02:35:28 -05:00
const chatMessage = require('prismarine-chat')(bot.version)
2022-11-27 02:35:28 -05:00
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
}
2022-11-27 02:35:28 -05:00
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))
}
bot.on('parsed_chat', (message) => {
2022-11-27 02:35:28 -05:00
bot.console.log(message.toAnsi())
})
2022-11-20 00:41:35 -05:00
2022-11-29 08:01:37 -05:00
if (!config.console) return
2022-11-27 02:35:28 -05:00
function handleLine (line) {
try {
if (line.toLowerCase() === '' ||
2022-11-27 02:35:28 -05:00
line.toLowerCase().startsWith(' ')) return
2022-11-14 06:53:52 -05:00
if (line.startsWith('.csvr ')) {
2022-11-27 02:35:28 -05:00
const host = line.substring(6)
bot.getBots().forEach((eachBot) => { eachBot.console.host = host })
bot.console.info(`Host set to: ${host}`)
return
}
2022-11-27 02:35:28 -05:00
if (bot.options.host !== bot.console.host && bot.console.host !== 'all') return
if (line.toLowerCase() === '.exit' || line.toLowerCase() === '.end') {
2022-11-27 02:35:28 -05:00
bot.end('end command')
return
}
if (line.toLowerCase().startsWith('.servereval ')) {
try {
bot.tellraw('@a', {
text: `${util.inspect(eval(`${line.substring(12)}`))}`,
2022-11-27 02:35:28 -05:00
color: 'green'
})
return
} catch (err) {
2022-11-27 02:35:28 -05:00
bot.tellraw('@a', { text: `${util.inspect(err)}`, color: 'red' })
return
}
}
2022-11-27 02:35:28 -05:00
if (line === '.kill') process.exit()
2022-11-14 05:45:49 -05:00
if (line.startsWith('.')) {
return bot.command_handler.run(
2022-11-27 02:35:28 -05:00
bot.username,
bot.username,
'*' + line.substring(1),
bot.uuid,
null,
'h',
'o'
)
2022-11-09 07:04:14 -05:00
}
bot.tellraw('@a', [
{
text: '[',
2022-11-27 02:35:28 -05:00
color: 'dark_gray'
},
{
text: `${bot.username} Console`,
2022-11-27 02:35:28 -05:00
color: 'gray'
},
{
text: '] ',
2022-11-27 02:35:28 -05:00
color: 'dark_gray'
},
{
text: 'chayapak ',
2022-11-27 02:35:28 -05:00
color: 'green'
},
{
text: '\u203a ',
2022-11-27 02:35:28 -05:00
color: 'dark_gray'
},
2022-11-27 02:35:28 -05:00
chatMessage.MessageBuilder.fromString('&7' + line)
])
} catch (e) {
2022-11-27 02:35:28 -05:00
bot.console.error(e)
}
2022-11-26 06:44:00 -05:00
}
2022-11-27 02:35:28 -05:00
rl.on('line', handleLine)
2022-11-26 06:44:00 -05:00
2022-11-30 04:45:20 -05:00
bot.on('end', () => {
2022-11-27 02:35:28 -05:00
rl.off('line', handleLine)
})
}
2022-11-09 07:04:14 -05:00
2022-11-27 02:35:28 -05:00
module.exports = { inject }