Last active
May 10, 2024 18:59
-
-
Save RepSklvska/91df6009a99bffa9c1f8a614bbc322c0 to your computer and use it in GitHub Desktop.
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
// Image manipulation with @pixi/node | |
// https://github.com/pixijs/node | |
import { CommandInteraction, CacheType, AttachmentBuilder } from 'discord.js' | |
import { Application, Assets, Sprite } from '@pixi/node' | |
import path from 'path' | |
export const drawImage = async (interaction: CommandInteraction<CacheType>) => { | |
await interaction.deferReply({ ephemeral: true }) | |
// This package requires the new asset loader to be used. | |
// Initialize the new assets loader | |
await Assets.init() | |
// The application will create a renderer using WebGL. It will also setup the ticker | |
// and the root stage Container. | |
const app = new Application() | |
// load a sprite | |
const bunnyTexture = await Assets.load(path.join(process.cwd(), 'assets/bunny.png')) | |
// create sprite from texture | |
const bunny = Sprite.from(bunnyTexture) | |
// Setup the position of the bunny | |
bunny.x = app.renderer.width / 2 | |
bunny.y = app.renderer.height / 2 | |
// Rotate around the center | |
bunny.anchor.x = 0.5 | |
bunny.anchor.y = 0.5 | |
// Add the bunny to the scene we are building. | |
app.stage.addChild(bunny) | |
// Extract the stage to buffer | |
app.renderer.render(app.stage) | |
const base64Image = app.renderer.view.toDataURL!('image/png') | |
const base64Data = base64Image.replace(/^data:image\/png;base64,/, '') | |
const buffer = Buffer.from(base64Data, 'base64') | |
// | |
const attachment = new AttachmentBuilder(buffer, { name: 'image.png' }) | |
await interaction.editReply({ files: [attachment] }) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment