botv12/plugins/console.js

81 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) => {
2024-12-18 15:04:53 -03:00
const args = l.split(' ');
const cmdName = args[0].toLowerCase();
2024-10-26 22:10:16 -04:00
try {
2024-12-18 15:04:53 -03:00
const cmd = cmds[cmdName];
if (!cmd) {
rl.prompt(false);
return;
}
if (cmd.consoleIndex) {
const index2 = args.splice(1, 1)[0];
if (index2 === '*') {
for (let i = 0; i < bots.length; i++) {
const context = new Command(uuid, user, nick, args.join(' '), 'console', 'console', 'console', '', bots[i])
context.verify = 2
cmd.execute(context)
2024-10-26 22:10:16 -04:00
}
} else {
2024-12-18 15:04:53 -03:00
const context = new Command(uuid, user, nick, args.join(' '), 'console', 'console', 'console', '', bots[+index2])
context.verify = 2
cmd.execute(context)
2024-10-26 22:10:16 -04:00
}
2024-12-18 15:04:53 -03:00
} else {
const context = new Command(uuid, user, nick, l, 'console', 'console', 'console', '', consoleBotStub)
context.verify = 2
cmd.execute(context)
2024-10-26 22:10:16 -04:00
}
} catch (e) {
console.log(e)
}
2024-12-18 15:04:53 -03:00
rl.prompt(false);
2024-10-26 22:10:16 -04:00
})
rl.prompt()
function consoleWrite (text) {
cursorTo(process.stdout, 0)
clearLine(process.stdout, 0)
process.stdout.write(text + '\n')
rl.prompt(true)
}
2024-12-18 15:04:53 -03:00
2024-10-26 22:10:16 -04:00
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
}