webhook-proxy/controller/helper/uploadWithPayload.js
2024-03-17 01:01:22 +01:00

59 lines
1.9 KiB
JavaScript

import axios from "axios";
import FormData from "form-data";
import fs from "fs";
import { encode, decode } from "base64-arraybuffer";
import { Button, Embed, MessageBuilder, Webhook } from "discord-webhooks-node";
import Config from "../../config/config.js";
// https://github.com/gaurishhs/discord-webhooks-node
// The 'data' parameter contains the usual webhook json stuff, like embeds etc.
export const sendFileWithPayload = async (url, fData, filename, api_resp) => {
//const form = new FormData();
//form.append("file0", fData, "myfilename.png");
//form.append("payload_json", JSON.stringify(data));
//await axios.post(url, form);
//fs.unlinkSync(path);
try {
const webhook = new Webhook({
url: url,
});
let e = {};
if(Config.showTags){
let text = "";
for(var i = 0;i<api_resp.tags.length;i++){
text += `${api_resp.tags[i].names[0]} - `;
}
text = text.substring(0, 1023);
e.fields = [
{
"name": "Tags",
"value": `${text}`
}
];
}
webhook.execute({
content: 'Hello world!',
embeds: [
{
"title": "We got a new Upload",
"description": `New Upload - [URL](${Config.booru_url}/post/${api_resp.id}) - ${api_resp.safety}`,
"color": 5814783,
"image": {
"url": `attachment://${filename}`
},
"flags": 4096,
...e
}
],
files: [
{
name: filename,
file: Buffer.from(fData),
},
],
}).then(() => console.log('Sent!')).catch((err) => console.error('Failed! ', err));
} catch (e) {
}
}