chipmunkbot3/commands/image.js

77 lines
2.5 KiB
JavaScript

const { literal, argument, DynamicCommandExceptionType } = require('brigadier-commands')
const { location, path, isUrl } = require('../util/command/argument/location')
const TextMessage = require('../util/command/text_message')
const fs = require('fs')
const FILE_DOES_NOT_EXIST_ERROR = new DynamicCommandExceptionType(filename => new TextMessage(['File ', filename, ' does not exist']))
module.exports = {
register (dispatcher, { bot }) {
const node = dispatcher.register(
literal('image')
.then(
literal('render')
.then(
argument('location', location(bot.paths.images))
.executes(this.renderCommand)
)
)
.then(
literal('list')
.executes(this.listCommand.bind(this))
.then(
argument('location', path(bot.paths.images))
.executes(this.locationListCommand.bind(this))
)
)
)
node.description = 'Renders images'
node.permissionLevel = 0
},
renderCommand (context) {
const source = context.source
const bot = source.bot
const src = context.getArgument('location')
if (!isUrl(src) && !fs.existsSync(src)) throw FILE_DOES_NOT_EXIST_ERROR.create()
source.sendFeedback([
{ text: 'Attempting to convert image ', ...bot.styles.primary },
{ text: src, ...bot.styles.secondary },
], true)
convertImage(src, (err, lines) => {
if (err) {
source.sendError(err.toString(), false)
return
}
lines.forEach((line, i) => {
bot.exploits.execute(`at ${player.UUID} run summon armor_stand ~ ~${(i * -0.05) + (lines.length * 0.05) - 0.3} ~ ${SNBT.stringify(nbt.comp({ CustomName: nbt.string(line), CustomNameVisible: nbt.byte(1), Invisible: nbt.byte(1), Marker: nbt.byte(1), Health: nbt.float(0), DeathTime: nbt.int(99999) }))}`)
if ((i + 1) >= lines.length) source.sendFeedback({ text: 'Finished rendering!', ...bot.styles.primary }, true)
})
})
},
listCommand (context) {
this.listImages(context, context.source.bot.paths.images)
},
locationListCommand (context) {
this.listImages(context, context.getArgument('location'))
},
async listImages (context, path) {
const source = context.source
const bot = source.bot
try {
const list = await bot.listFiles(path)
source.sendFeedback(['', { text: 'Images - ', ...bot.styles.primary }, ...list], false)
} catch (error) {
source.sendError(error.toString())
}
}
}