mirror of
https://github.com/ChomeNS/chomens-bot-mc.git
synced 2024-11-14 10:44:55 -05:00
add *draw
This commit is contained in:
parent
85c12781d9
commit
be52cb86b2
4 changed files with 105 additions and 1 deletions
|
@ -1,3 +1,4 @@
|
|||
[
|
||||
"Discord *list fixed (finally)"
|
||||
"Discord *list fixed (finally)",
|
||||
"Added *draw"
|
||||
]
|
35
commands/draw.js
Normal file
35
commands/draw.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
const {resize} = require('../util/image');
|
||||
const axios = require('axios');
|
||||
const sharp = require('sharp');
|
||||
|
||||
module.exports = {
|
||||
name: 'draw',
|
||||
description: 'Draws an image',
|
||||
alias: [],
|
||||
trusted: 0,
|
||||
usage: '<image url (JPEG, PNG, WebP, AVIF, GIF, SVG, TIFF)>',
|
||||
execute: async function(bot, username, usernameraw, sender, prefix, args) {
|
||||
const url = args.join(' ');
|
||||
|
||||
const image = await axios.get('https://http-proxy.nongsonchome.repl.co', {
|
||||
params: {
|
||||
uri: url,
|
||||
},
|
||||
responseType: 'arraybuffer',
|
||||
});
|
||||
|
||||
const loaded = sharp(image.data);
|
||||
|
||||
const metadata = await loaded
|
||||
.metadata();
|
||||
|
||||
const {width, height} = resize(metadata.width, metadata.height);
|
||||
|
||||
const {data, info} = await loaded
|
||||
.resize({fit: 'fill', kernel: 'nearest', width, height})
|
||||
.raw()
|
||||
.toBuffer({resolveWithObject: true});
|
||||
|
||||
bot.draw(data, info);
|
||||
},
|
||||
};
|
47
plugins/draw.js
Normal file
47
plugins/draw.js
Normal file
|
@ -0,0 +1,47 @@
|
|||
/* eslint-disable max-len */
|
||||
const convert = require('color-convert');
|
||||
|
||||
function inject(bot) {
|
||||
function draw(data, info, prefix = {}) {
|
||||
const pixels = [];
|
||||
|
||||
// Data Buffer -> RGB Array
|
||||
for (let i = 0; i < data.length; i += info.channels) {
|
||||
pixels.push([
|
||||
data[i + 0],
|
||||
data[i + 1],
|
||||
data[i + 2],
|
||||
]);
|
||||
}
|
||||
|
||||
const rows = [];
|
||||
|
||||
// RGB Array -> Rows Array
|
||||
for (let i = 0; i < pixels.length; i += info.width) {
|
||||
const row = pixels.slice(i, i + info.width);
|
||||
|
||||
rows.push(row);
|
||||
}
|
||||
|
||||
const messages = [];
|
||||
|
||||
for (const row of rows) {
|
||||
const message = [{...prefix, text: ''}];
|
||||
|
||||
for (const rgb of row) {
|
||||
message.push({
|
||||
text: '⎮',
|
||||
color: `#${convert.rgb.hex(rgb)}`,
|
||||
});
|
||||
}
|
||||
|
||||
messages.push(message);
|
||||
}
|
||||
|
||||
for (const message of messages) bot.core.run(`minecraft:tellraw @a ${JSON.stringify(message)}`);
|
||||
}
|
||||
|
||||
bot.draw = draw;
|
||||
}
|
||||
|
||||
module.exports = {inject};
|
21
util/image.js
Normal file
21
util/image.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
function resize(width, height) {
|
||||
const aspectRatio = width / height;
|
||||
|
||||
let optimalWidth = Math.round(aspectRatio * 20 * (27 / 3));
|
||||
let optimalHeight = 20;
|
||||
|
||||
if (optimalWidth > 320) {
|
||||
const reduction = optimalWidth / 320;
|
||||
|
||||
optimalWidth = 320;
|
||||
|
||||
optimalHeight *= reduction;
|
||||
}
|
||||
|
||||
return {
|
||||
width: Math.floor(optimalWidth),
|
||||
height: Math.floor(optimalHeight),
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {resize};
|
Loading…
Reference in a new issue