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,34 +92,32 @@ 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 fileSize = file.size / 1000000.0;
if ((attachment.name.toLowerCase().endsWith('.jpg') || attachment.name.toLowerCase().endsWith('.png')) && fileSize > 5) { const file = fs.statSync(`${os.tmpdir()}/${attachment.name}`);
return interaction.editReply({ content: 'Images can\'t be larger than 5 MB!' }); const fileSize = file.size / 1000000.0;
}
else if (attachment.name.toLowerCase().endsWith('.gif') && fileSize > 15) {
return interaction.editReply({ content: 'Gifs can\'t be larger than 15 MB!' });
}
const b64Image = fs.readFileSync(`${os.tmpdir()}/${attachment.name}`, { encoding: 'base64' }); if ((attachment.name.toLowerCase().endsWith('.jpg') || attachment.name.toLowerCase().endsWith('.png')) && fileSize > 5) {
T.post('media/upload', { media_data: b64Image }, function(err, data) { return interaction.editReply({ content: 'Images can\'t be larger than 5 MB!' });
if (err) { }
console.log('OH NO AN ERROR!!!!!!!'); else if (attachment.name.toLowerCase().endsWith('.gif') && fileSize > 15) {
console.error(err); return interaction.editReply({ content: 'Gifs can\'t be larger than 15 MB!' });
return interaction.editReply({ content: 'OH NO!!! AN ERROR HAS occurred!!! please hold on while i find what\'s causing this issue! ' }); }
}
else { const b64Image = fs.readFileSync(`${os.tmpdir()}/${attachment.name}`, { encoding: 'base64' });
Tweet(data); T.post('media/upload', { media_data: b64Image }, function(err, data) {
} if (err) {
}); console.log('OH NO AN ERROR!!!!!!!');
}); console.error(err);
}); return interaction.editReply({ content: 'OH NO!!! AN ERROR HAS occurred!!! please hold on while i find what\'s causing this issue! ' });
}
else {
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' });