chipmunkbot3/commands/image.js
2024-02-11 21:23:41 -05:00

77 lines
2.8 KiB
JavaScript

const name = 'image'
const description = 'Renders an image.'
const usages = [
'render <name/url>',
'list'
]
const aliases = ['image']
const enabled = true
const permLevel = 0
const fs = require('fs')
const convertImage = require('../util/convert-image.js')
const nbt = require('prismarine-nbt')
const SNBT = require('../util/snbt.js')
function execute (bot, cmd, player, args, handler) {
const subCmd = args.shift()
let src, files, primary, msg
switch (subCmd) {
case 'render':
src = args.join(' ').replace(/§.?/g, '')
if (/https?:\/\//.test(src)) {
bot.core.run(`/minecraft:tellraw @a ${JSON.stringify([
{ text: 'Attempting to convert image at ', color: bot.colors.primary },
{ text: src, color: bot.colors.secondary },
'.'
])}`)
} else {
src = `./images/${src}`
if (!src.endsWith('.jpg')) { src += '.jpg' }
if (src.match(/\//g).length > 2) { throw new Error('Invalid image name.') }
if (!fs.existsSync(src)) { throw new Error('Invalid image name.') }
bot.core.run(`/minecraft:tellraw @a ${JSON.stringify([
{ text: 'Attempting to convert image ', color: bot.colors.primary },
{ text: args.join(' ').replace(/§.?/g, ''), color: bot.colors.secondary },
'.'
])}`)
}
convertImage(src, (err, lines) => {
if (err) {
bot.core.run(`minecraft:tellraw @a ${JSON.stringify({ text: err.message, color: bot.colors.error })}`)
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) bot.core.run(`minecraft:tellraw @a ${JSON.stringify({ text: 'Finished rendering!', color: bot.colors.primary })}`)
})
})
break
case 'list':
files = fs.readdirSync('./images')
files.filter((file) => file.endsWith('.jpg'))
primary = false
msg = [{ text: 'Images - ', color: 'gray' }]
files.forEach((file) => {
msg.push({
text: `${file} `,
color: (((primary = !primary)) ? bot.colors.primary : bot.colors.secondary),
clickEvent: { action: 'run_command', value: `${bot.prefix}${name} render ${file}` },
hoverEvent: { action: 'show_text', value: 'Click to render the image' }
})
})
bot.core.run(`/tellraw @a ${JSON.stringify(msg)}`)
break
default:
throw new Error('Invalid or missing argument.')
}
}
module.exports = { name, description, usages, aliases, enabled, execute, permLevel }