chipmunkbot3/commands/image.js

74 lines
2.4 KiB
JavaScript
Raw Permalink Normal View History

2024-04-02 17:53:10 -04:00
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')
2024-02-11 21:23:41 -05:00
2024-04-02 17:53:10 -04:00
const FILE_DOES_NOT_EXIST_ERROR = new DynamicCommandExceptionType(filename => new TextMessage(['File ', filename, ' does not exist']))
2024-02-11 21:23:41 -05:00
2024-04-02 17:53:10 -04:00
module.exports = {
2024-04-05 20:54:10 -04:00
register (dispatcher, { bot }) {
2024-04-02 17:53:10 -04:00
const node = dispatcher.register(
literal('image')
.then(
literal('render')
.then(
2024-04-05 20:54:10 -04:00
argument('location', location(bot.paths.images))
.executes(c => this.renderCommand(c))
2024-04-02 17:53:10 -04:00
)
)
.then(
literal('list')
.executes(c => this.listCommand(c))
2024-04-02 17:53:10 -04:00
.then(
2024-04-05 20:54:10 -04:00
argument('location', path(bot.paths.images))
.executes(c => this.locationListCommand(c))
2024-04-02 17:53:10 -04:00
)
)
)
2024-02-11 21:23:41 -05:00
2024-04-02 17:53:10 -04:00
node.description = 'Renders images'
node.permissionLevel = 0
},
2024-02-11 21:23:41 -05:00
2024-04-02 17:53:10 -04:00
renderCommand (context) {
const source = context.source
const bot = source.bot
2024-02-11 21:23:41 -05:00
2024-04-02 17:53:10 -04:00
const src = context.getArgument('location')
2024-02-11 21:23:41 -05:00
2024-04-02 17:53:10 -04:00
if (!isUrl(src) && !fs.existsSync(src)) throw FILE_DOES_NOT_EXIST_ERROR.create()
2024-02-11 21:23:41 -05:00
2024-04-02 17:53:10 -04:00
source.sendFeedback([
{ text: 'Attempting to convert image ', ...bot.styles.primary },
{ text: src, ...bot.styles.secondary },
], true)
2024-02-11 21:23:41 -05:00
2024-04-02 17:53:10 -04:00
convertImage(src, (err, lines) => {
if (err) {
source.sendError(err.toString(), false)
return
2024-02-11 21:23:41 -05:00
}
2024-04-02 17:53:10 -04:00
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)
2024-02-11 21:23:41 -05:00
})
2024-04-02 17:53:10 -04:00
})
},
2024-02-11 21:23:41 -05:00
async listCommand (context) {
await this.listImages(context, context.source.bot.paths.images)
2024-04-02 17:53:10 -04:00
},
async locationListCommand (context) {
await this.listImages(context, context.getArgument('location'))
2024-04-02 17:53:10 -04:00
},
async listImages (context, path) {
const source = context.source
const bot = source.bot
const list = await bot.listFiles(path)
source.sendFeedback(['', { text: 'Images - ', ...bot.styles.primary }, ...list], false)
2024-02-11 21:23:41 -05:00
}
}