botv12/plugins/console.js

70 lines
2.1 KiB
JavaScript
Raw Normal View History

2024-11-19 20:05:19 -05:00
import { createInterface, cursorTo, clearLine } from 'node:readline'
import settings from '../settings.js'
import cmds from '../util/commands.js'
import { bots } from '../index.js'
2024-10-26 22:10:16 -04:00
import Command from '../util/Command.js'
2024-11-19 20:05:19 -05:00
import parse2 from '../util/chatparse_console.js'
import { userInfo } from 'node:os'
2024-10-26 22:10:16 -04:00
const consoleBotStub = {
host: {
2024-11-19 20:05:19 -05:00
host: 'bot console ',
2024-10-26 22:10:16 -04:00
port: 3
},
tellraw: (_unused, data) => console.log(parse2(data))
}
2024-11-19 20:05:19 -05:00
const uuid = '4d616465-6c69-6e65-2075-7775203a3300'
2024-10-26 22:10:16 -04:00
const user = userInfo().username // OS user the bot is running as
2024-11-19 20:07:29 -05:00
const nick = user
2024-10-26 22:10:16 -04:00
const rl = createInterface({
input: process.stdin,
output: process.stdout,
prompt: '\x1b[0m> '
})
rl.on('line', (l) => {
try {
if (cmds[l.split(' ')[0].toLowerCase()]) {
if (cmds[l.split(' ')[0].toLowerCase()].consoleIndex) {
const tmpcmd = l.split(' ')
const index2 = tmpcmd.splice(1, 1)[0]
if (index2 === '*') {
2024-11-19 20:07:29 -05:00
for (let i = 0; i < bots.length; i++) {
2024-11-19 20:05:19 -05:00
const cmd = new Command(uuid, user, nick, tmpcmd.join(' '), 'console', 'console', 'console', '', bots[i], 2, {})
2024-10-26 22:10:16 -04:00
cmds[l.split(' ')[0].toLowerCase()].execute(cmd)
}
} else {
2024-11-19 20:05:19 -05:00
const cmd = new Command(uuid, user, nick, tmpcmd.join(' '), 'console', 'console', 'console', '', bots[+index2], 2, {})
2024-10-26 22:10:16 -04:00
cmds[l.split(' ')[0].toLowerCase()].execute(cmd)
}
} else {
2024-11-19 20:05:19 -05:00
const cmd = new Command(uuid, user, nick, l, 'console', 'console', 'console', '', consoleBotStub, 2, {})
2024-10-26 22:10:16 -04:00
cmds[l.split(' ')[0].toLowerCase()].execute(cmd)
}
}
} catch (e) {
console.log(e)
}
rl.prompt(false)
})
rl.prompt()
function consoleWrite (text) {
cursorTo(process.stdout, 0)
clearLine(process.stdout, 0)
process.stdout.write(text + '\n')
rl.prompt(true)
}
export default function load (b) {
b.info = (msg) => {
consoleWrite(`[${b.id}] [info] ${msg}`)
}
b.displayChat = (type, subtype, msg) => {
if (settings.displaySubtypesToConsole) {
consoleWrite(`[${b.id}] [${type}] [${subtype}] ${msg}`)
} else {
consoleWrite(`[${b.id}] [${type}] ${msg}`)
}
}
2024-11-19 20:05:19 -05:00
}