Change of twitter library and allow upload of images

This commit is contained in:
loicbersier 2019-11-25 23:15:12 +01:00
parent f3b8fe11ec
commit 8c103ace29

View file

@ -1,5 +1,8 @@
const { Command } = require('discord-akairo'); const { Command } = require('discord-akairo');
const Twitter = require('twitter-lite'); const Twit = require('twit');
const fetch = require('node-fetch');
const os = require('os');
const fs = require('fs');
const rand = require('../../rand.js'); const rand = require('../../rand.js');
//const Filter = require('bad-words'); //const Filter = require('bad-words');
//let filter = new Filter(); //let filter = new Filter();
@ -33,6 +36,17 @@ class tweetCommand extends Command {
} }
async exec(message, args) { async exec(message, args) {
const client = this.client;
let date = new Date();
let Attachment = (message.attachments).array();
let T = new Twit({
consumer_key: twiConsumer,
consumer_secret: twiConsumerSecret,
access_token: twiToken,
access_token_secret: twiTokenSecret
});
/* /*
// Censor words // Censor words
let censor = reload('../../json/censor.json'); let censor = reload('../../json/censor.json');
@ -47,8 +61,12 @@ class tweetCommand extends Command {
return message.channel.send(`You have been blacklisted for the following reasons: \`\`${blacklist.get('reason')}\`\` be less naughty less time.`); return message.channel.send(`You have been blacklisted for the following reasons: \`\`${blacklist.get('reason')}\`\` be less naughty less time.`);
} }
// If account is younger than 6 months old don't accept attachment
if (Attachment[0] && message.author.createdAt > date.setMonth(date.getMonth() - 6)) {
return message.channel.send('Your account need to be 6 months or older to be able to send attachment!');
}
// Don't let account new account use this command to prevent spam // Don't let account new account use this command to prevent spam
let date = new Date();
if (message.author.createdAt > date.setDate(date.getDate() - 7)) { if (message.author.createdAt > date.setDate(date.getDate() - 7)) {
return message.channel.send('Your account is too new to be able to use this command!'); return message.channel.send('Your account is too new to be able to use this command!');
} }
@ -70,47 +88,85 @@ class tweetCommand extends Command {
} }
try { try {
let client = new Twitter({ // Make sure there is an attachment and if its an image
consumer_key: twiConsumer, if (Attachment[0]) {
consumer_secret: twiConsumerSecret, if (Attachment[0].name.endsWith('.jpg') || Attachment[0].name.endsWith('.png') || Attachment[0].name.endsWith('.gif')) {
access_token_key: twiToken, fetch(Attachment[0].url)
access_token_secret: twiTokenSecret .then(res => {
}); const dest = fs.createWriteStream(`${os.tmpdir()}/${Attachment[0].name}`);
res.body.pipe(dest);
const response = await client.post('statuses/update', { dest.on('finish', () => {
status: text let b64Image = fs.readFileSync(`${os.tmpdir()}/${Attachment[0].name}`, { encoding: 'base64'});
}); T.post('media/upload', { media_data: b64Image }, function (err, data) {
if (err) {
const tweetid = response.id_str; console.log('OH NO AN ERROR!!!!!!!');
return console.error(err);
const publicEmbed = this.client.util.embed() } else {
.setAuthor('Some user of discord said...') Tweet(data);
.setDescription(args.text) }
.addField('Link', `https://twitter.com/HahaYesDB/status/${tweetid}`) });
.setTimestamp(); });
});
// Im too lazy for now to make an entry in config.json } else {
let channel = this.client.channels.get('597964498921455637'); return message.channel.send('File type not supported, you can send jpg/png/gif');
channel.send({embed: publicEmbed}); }
} else {
const Embed = this.client.util.embed() Tweet();
.setAuthor(message.author.username, message.author.displayAvatarURL()) }
.setDescription(args.text)
.addField('Link', `https://twitter.com/HahaYesDB/status/${tweetid}`, true)
.addField('Tweet ID', tweetid, true)
.addField('Author', `${message.author.username} (${message.author.id})`, true)
.addField('Guild', `${message.guild.name} (${message.guild.id})`, true)
.setTimestamp();
channel = this.client.channels.get(twiChannel);
channel.send({embed: Embed});
return message.channel.send(`Go see ur epic tweet https://twitter.com/HahaYesDB/status/${tweetid}`);
} catch(err) { } catch(err) {
console.error(err); console.error(err);
return message.channel.send('Oh no, an error has occured :('); return message.channel.send('Oh no, an error has occured :(');
} }
function Tweet(data) {
let options = {
status: text
};
if (data) {
options = {
status: text,
media_ids: new Array(data.media_id_string)
};
}
T.post('statuses/update', options, function (err, response) {
if (err) {
console.error('OH NO!!!!');
return console.error(err);
}
const tweetid = response.id_str;
const publicEmbed = client.util.embed()
.setAuthor('Some user of discord said...')
.setDescription(args.text)
.addField('Link', `https://twitter.com/HahaYesDB/status/${tweetid}`)
.setTimestamp();
if (Attachment[0]) publicEmbed.setImage(Attachment[0].url);
// Im too lazy for now to make an entry in config.json
let channel = client.channels.get('597964498921455637');
channel.send({embed: publicEmbed});
const Embed = client.util.embed()
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setDescription(args.text)
.addField('Link', `https://twitter.com/HahaYesDB/status/${tweetid}`, true)
.addField('Tweet ID', tweetid, true)
.addField('Author', `${message.author.username} (${message.author.id})`, true)
.addField('Guild', `${message.guild.name} (${message.guild.id})`, true)
.setTimestamp();
if (Attachment[0]) Embed.setImage(Attachment[0].url);
channel = client.channels.get(twiChannel);
channel.send({embed: Embed});
return message.channel.send(`Go see ur epic tweet https://twitter.com/HahaYesDB/status/${tweetid}`);
});
}
} }
} }