Last active
September 19, 2021 20:26
-
-
Save CodyJasonBennett/36b90d78db8e13d42d2c3e277d2d3f01 to your computer and use it in GitHub Desktop.
THREE SSR Discord.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE > | |
<html> | |
<head> | |
<meta charset="UTF-8" /> | |
<style> | |
body { | |
margin: 0; | |
} | |
canvas { | |
display: block; | |
} | |
</style> | |
</head> | |
<body> | |
<script type="module"> | |
import * as THREE from 'https://cdn.skypack.dev/[email protected]/build/three.module.js'; | |
const blocks = [ | |
{ | |
name: 'stone', | |
src: | |
'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wgARCAH/Af8DASIAAhEBAxEB/8QAGQABAQEBAQEAAAAAAAAAAAAAAAECAwQF/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEAMQAAABgEsOIAAAAAAFlOwFlPUAABjeD5AG8bPrgZ1k8wEsOIAAAAAAFlOwAAEsOIAAAAAAFlOwFlPUAABjeD5AG8bPrgZ1k8wEsOIAAAAAAFlOwAAAAFlPUAAAAAABnWT54AAAAGs6PoAAAAAY3g+QBrOj2gAASw4gA7gAAWU9QAAAAAAGdZPngAAAAazo+gAAAABjeD5AGs6PaAABLDiADuBnWTxAWU7gAASw4AWU7gAAAAazo+gBnWT54FlOwAAAAFlPUBnWTzAAAAAZ1k8QFlO4AAEsOAFlO4AAAAGs6PoAZ1k+eBZTsAAAABZT1AZ1k8wAAAAFlPUBnWTzAAAAAZ1k8QGs6PaBZT1AZ1k8wAAEsOIAAAAAAAA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ki/aNgIT0MRRllEWgQki/aNgIT0MRRllEWgQki/aNiJqP8Av0D/KITRiKL9I25E1H+AX6B/lEJoxFF+kbciaj/AL9A/yiE0Yii/SNuRNR/gF+gf5RCaMRRfpG3Ai0iE0YgiYDYiYHyPAItIhNGIImA2ImB8jwCLSITRiCJgNiJgfI8Ai0iE0YgiYDYiYHyPAIT2L9A/zkYgCE9i/QP8AORiAIT2L9A/zkYgCE9i/QP8AORiAJuRtRCSITRiKMQBMRNyNqISRCaMRRiAJiJuRtRCSITRiKMQBMRNyNqISRCaMRRiAJuRCeRiAJgNqISBiOBCeRiAJgNqISBiOBCeRiAJgNqISBiOBCeRiAJgNqISBiORCafI9A24EJIxHQhNPkegbcCEkYjoQmnyPQNuBCSMR0ITT5HoG3AhJGID/AP/Z', | |
}, | |
]; | |
const block = window.location.search.replace('?block=', ''); | |
const target = blocks.find(({ name }) => name === block); | |
new THREE.TextureLoader().load(target.src, texture => { | |
const { innerWidth, innerHeight } = window; | |
const renderer = new THREE.WebGLRenderer({ | |
antialias: false, | |
preserveDrawingBuffer: true, | |
}); | |
renderer.setPixelRatio(2); | |
renderer.setSize(innerWidth, innerHeight); | |
document.body.appendChild(renderer.domElement); | |
const camera = new THREE.PerspectiveCamera(70, innerWidth / innerHeight); | |
camera.position.z = 2; | |
const scene = new THREE.Scene(); | |
const geometry = new THREE.BoxGeometry(); | |
const material = new THREE.MeshBasicMaterial({ map: texture }); | |
const mesh = new THREE.Mesh(geometry, material); | |
scene.add(mesh); | |
renderer.render(scene, camera); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require('puppeteer'); | |
const { Client } = require('discord.js'); | |
const renderBlock = async block => { | |
const browser = await puppeteer.launch({ | |
headless: true, | |
args: ['--use-gl', '--use-cmd-decoder=passthrough'], | |
}); | |
const page = await browser.newPage(); | |
await page.goto(`file://${__dirname}/index.html?block=${block.toLowerCase()}`); | |
await page.waitForSelector('canvas'); | |
const image = await page.evaluate(() => document.querySelector('canvas').toDataURL()); | |
await browser.close(); | |
return image; | |
}; | |
const bot = new Client(); | |
bot.login(process.env.TOKEN); | |
bot.on('message', async msg => { | |
if (!msg.content.startsWith('!block ')) return; | |
const block = msg.content.replace('!block', '').trim(); | |
const image = await renderBlock(block); | |
if (!image) return msg.channel.send(`Block: ${block} not found.`); | |
const imageStream = new Buffer.from(image.split(',')[1], 'base64'); | |
return msg.channel.send({ files: [imageStream] }); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment