Removed useless logging and updated image fetch

This commit is contained in:
Supositware 2023-04-04 16:06:05 +00:00
parent b960829e72
commit bd7f0d12e5

View file

@ -4,6 +4,8 @@ import Twit from 'twit';
import fetch from 'node-fetch'; import fetch from 'node-fetch';
import os from 'node:os'; import os from 'node:os';
import fs from 'node:fs'; import fs from 'node:fs';
import util from 'node:util';
import stream from 'node:stream';
import db from '../../models/index.js'; import db from '../../models/index.js';
import wordToCensor from '../../json/censor.json' assert {type: 'json'}; import wordToCensor from '../../json/censor.json' assert {type: 'json'};
@ -29,8 +31,7 @@ export default {
async execute(interaction, args, client) { async execute(interaction, args, client) {
const content = args.content; const content = args.content;
const attachment = args.image; const attachment = args.image;
console.log(args);
console.log(attachment);
if (!content && !attachment) { if (!content && !attachment) {
return interaction.reply({ content: 'Uh oh! You are missing any content for me to tweet!', ephemeral: true }); return interaction.reply({ content: 'Uh oh! You are missing any content for me to tweet!', ephemeral: true });
} }
@ -91,11 +92,11 @@ export default {
// Make sure there is an attachment and if its an image // Make sure there is an attachment and if its an image
if (attachment) { if (attachment) {
if (attachment.name.toLowerCase().endsWith('.jpg') || attachment.name.toLowerCase().endsWith('.png') || attachment.name.toLowerCase().endsWith('.gif')) { if (attachment.name.toLowerCase().endsWith('.jpg') || attachment.name.toLowerCase().endsWith('.png') || attachment.name.toLowerCase().endsWith('.gif')) {
fetch(attachment.url) const streamPipeline = util.promisify(stream.pipeline);
.then(res => { const res = await fetch(attachment.url);
const dest = fs.createWriteStream(`${os.tmpdir()}/${attachment.name}`); if (!res.ok) return interaction.editReply('An error has occured while trying to download your image.');
res.body.pipe(dest); await streamPipeline(res.body, fs.createWriteStream(`${os.tmpdir()}/${attachment.name}`));
dest.on('finish', () => {
const file = fs.statSync(`${os.tmpdir()}/${attachment.name}`); const file = fs.statSync(`${os.tmpdir()}/${attachment.name}`);
const fileSize = file.size / 1000000.0; const fileSize = file.size / 1000000.0;
@ -117,8 +118,6 @@ export default {
Tweet(data); Tweet(data);
} }
}); });
});
});
} }
else { else {
await interaction.editReply({ content: 'File type not supported, you can only send jpg/png/gif' }); await interaction.editReply({ content: 'File type not supported, you can only send jpg/png/gif' });