// STL VIEWER // const StlThumbnailer = require("stl-thumbnailer-node"); client.on(Events.MessageCreate, async msg => { if (msg.author.bot) return; console.log(msg.attachments.size); if (msg.channelId != config.stl.channel) return; if ( msg.attachments.size == 0 || !msg.attachments.first().name.endsWith(".stl") ) { let tmp = await msg.reply(config.stl.texts.invalidAttachment); return setTimeout(() => { tmp.delete(); msg.delete(); }, 5000); } msg.react(config.stl.loadingemoji); fs.writeFileSync( __dirname + "/input.stl", Buffer.from(await (await fetch(msg.attachments.first().url)).arrayBuffer()) ); try { return new StlThumbnailer({ filePath: __dirname + "/input.stl", requestThumbnails: [ { width: 500, height: 500, }, ], }).then(function (thumbnails) { // thumbnails is an array (in matching order to your requests) of Canvas objects // you can write them to disk, return them to web users, etc // see node-canvas documentation at https://github.com/Automattic/node-canvas thumbnails[0].toBuffer(async function (err, buf) { if (err) return console.error(err); try { await msg.channel.send({ content: "", embeds: [ new EmbedBuilder() .setAuthor({ name: msg.author.username }) .setImage("attachment://thumbnail") .setColor("Random") .setTimestamp() .setTitle(path.basename(msg.attachments.first().name, ".stl")), ], attachments: [ new AttachmentBuilder() .setFile(Buffer.from(new Uint8Array(buf))) .setName("thumbnail"), msg.attachments.first(), ], }); return msg.delete(); } catch (e) { msg.react("⚠️"); console.error(e.rawError.errors.attachments["0"].id._errors); } }); }); } catch (e) { msg.react("⚠️"); console.error(e); } });