From a17fd1aea4639b7c77f608a7975ec1c2ca70e997 Mon Sep 17 00:00:00 2001 From: SrIzan10 <66965250+SrIzan10@users.noreply.github.com> Date: Wed, 25 Jan 2023 17:13:34 +0100 Subject: [PATCH] feat: makesweet command (heart locket) --- commands/fun/makesweet.ts | 98 +++++++++++++++++++++++++++++++++++++++ package-lock.json | 6 ++- package.json | 1 + 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 commands/fun/makesweet.ts diff --git a/commands/fun/makesweet.ts b/commands/fun/makesweet.ts new file mode 100644 index 0000000..dfb62b0 --- /dev/null +++ b/commands/fun/makesweet.ts @@ -0,0 +1,98 @@ +import { commandModule, CommandType } from '@sern/handler' +import { publish } from "../../plugins/publish.js"; +import FormData from 'form-data'; +import { ApplicationCommandOptionType, AttachmentBuilder } from 'discord.js'; +import axios from 'axios'; +/* +import { publish } from "#plugins"; +import { ownerOnly } from "#plugins" +*/ + +export default commandModule({ + type: CommandType.Slash, + plugins: [publish()], + //alias : [], + options: [ + { + name: 'heartlocket', + description: 'El corazón con una imagen que todos conocemos', + type: ApplicationCommandOptionType.Subcommand, + options: [ + { + name: 'imagen', + description: 'Imagen (jpg o png)', + type: ApplicationCommandOptionType.Attachment, + required: true + }, + { + name: 'texto', + description: 'El texto que poner', + type: ApplicationCommandOptionType.String, + } + ] + } + ], + execute: async (ctx, options) => { + await ctx.interaction.deferReply() + switch (options[1].getSubcommand()) { + case 'heartlocket': { + try { + // get all options + const text = options[1].getString('texto') + const image = options[1].getAttachment('imagen', true) + + // check file extension of attachment + if (!image.contentType!.includes('image/png') && !image.contentType!.includes('image/jpeg')) + return await ctx.interaction.editReply({content: 'Tienes que usar una imagen con extensión `.png` o `.jpg`!'}) + + // save in a const the content type + let fileExtension: string + if (image.contentType!.includes('image/png')) { + fileExtension = 'png' + } else if (image.contentType!.includes('image/jpeg')) { + fileExtension = 'jpg' + } else { + fileExtension = 'this shouldnt be seen, but typescript is sometimes an idiot and it needs an else so it doesnt cry' + } + + // convert image to a binary so it can be sent to the MakeSweet API. + const formData = new FormData() + const imageBinary = await axios.get(image.url, { responseType: 'arraybuffer' }).then(res => res.data) + const buffer = Buffer.from(imageBinary, 'binary'); + formData.append('images[]', buffer, `image.${fileExtension}`) + + // make the request to the actual API, but first check if there's text. + let request: any + if (text) { + request = await axios.post(`https://api.makesweet.com/make/heart-locket?text=${text}`, formData, { + headers: { + 'Authorization': process.env.MAKESWEET! + }, + responseType: 'arraybuffer' + }).then(res => res.data) + } else { + request = await axios.post(`https://api.makesweet.com/make/heart-locket`, formData, { + headers: { + 'Authorization': process.env.MAKESWEET! + }, + responseType: 'arraybuffer' + }).then(res => res.data) + } + + // make an attachment with the data that the MakeSweet API returned + const attachment = new AttachmentBuilder(request, { name: 'makesweet.gif' }) + + // finally, send the message + await ctx.interaction.editReply({ + content: 'Tu GIF está listo! 🎉', + files: [attachment] + }) + } catch (e) { + await ctx.interaction.editReply({ + content: `Algo ha pasado mal, intenta comprimir la imagen para hacerla menos pesada antes de enviarla.` + }) + } + } break; + } + }, +}); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 7fccf26..d3cda42 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,6 +22,7 @@ "discord.js": "^14.7.1", "dotenv": "^16.0.1", "express": "^4.18.1", + "form-data": "^4.0.0", "got": "^11.8.5", "libsodium-wrappers": "^0.7.10", "mongoose": "^6.5.1", @@ -1579,7 +1580,8 @@ }, "node_modules/form-data": { "version": "4.0.0", - "license": "MIT", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", "dependencies": { "asynckit": "^0.4.0", "combined-stream": "^1.0.8", @@ -4488,6 +4490,8 @@ }, "form-data": { "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", "requires": { "asynckit": "^0.4.0", "combined-stream": "^1.0.8", diff --git a/package.json b/package.json index 87adc9e..c5c6990 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "discord.js": "^14.7.1", "dotenv": "^16.0.1", "express": "^4.18.1", + "form-data": "^4.0.0", "got": "^11.8.5", "libsodium-wrappers": "^0.7.10", "mongoose": "^6.5.1",