mirror of
https://github.com/SrIzan10/vinci.git
synced 2026-06-06 01:07:00 +00:00
feat: makesweet command (heart locket)
This commit is contained in:
98
commands/fun/makesweet.ts
Normal file
98
commands/fun/makesweet.ts
Normal file
@@ -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;
|
||||
}
|
||||
},
|
||||
});
|
||||
6
package-lock.json
generated
6
package-lock.json
generated
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user