2019-02-24 23:03:53 +01:00
const { Command } = require ( 'discord-akairo' ) ;
2019-11-25 23:15:12 +01:00
const Twit = require ( 'twit' ) ;
const fetch = require ( 'node-fetch' ) ;
const os = require ( 'os' ) ;
const fs = require ( 'fs' ) ;
2019-02-24 23:03:53 +01:00
const rand = require ( '../../rand.js' ) ;
2019-10-06 20:29:42 +02:00
const TwitterBlacklist = require ( '../../models' ) . TwitterBlacklist ;
2019-02-25 03:18:04 +01:00
const { twiConsumer , twiConsumerSecret , twiToken , twiTokenSecret , twiChannel } = require ( '../../config.json' ) ;
2019-02-24 23:03:53 +01:00
class tweetCommand extends Command {
constructor ( ) {
super ( 'tweet' , {
aliases : [ 'tweet' ] ,
category : 'general' ,
2019-11-09 12:04:01 +01:00
clientPermissions : [ 'SEND_MESSAGES' , 'EMBED_LINKS' ] ,
2019-11-08 19:42:22 +01:00
cooldown : 3600000 ,
2019-10-07 00:01:03 +02:00
ratelimit : 3 ,
2019-02-24 23:03:53 +01:00
args : [
{
id : 'text' ,
type : 'string' ,
match : 'rest'
}
] ,
description : {
2020-02-26 06:56:47 +01:00
content : 'Send tweet from Haha yes twitter account. Please do not use it for advertisement (NOTE: all the tweet sent using this command are logged, so don\'t say nasty thing or you might get blacklisted from it!)' ,
2019-02-24 23:03:53 +01:00
usage : '[text]' ,
examples : [ 'this epic tweet is in my epic twitter' ]
}
} ) ;
}
async exec ( message , args ) {
2020-07-16 09:25:03 +02:00
if ( args . text ) {
// Very simple link detection
if ( args . text . includes ( 'http' ) && ! args . text . includes ( 'twitter.com' ) ) return message . channel . send ( 'You may not tweet links outside of twitter.com' ) ;
// Do not allow discord invites
if ( args . text . includes ( 'discord.gg' ) || args . text . includes ( 'discord.com/invite/' ) ) return message . channel . send ( 'No discord invite allowed.' ) ;
}
2019-11-25 23:15:12 +01:00
let Attachment = ( message . attachments ) . array ( ) ;
2020-03-18 18:26:36 +01:00
if ( ! Attachment [ 0 ] && ! args . text ) return message . channel . send ( 'You need to input something for me to tweet!' ) ;
let date = new Date ( ) ;
2019-11-28 15:36:18 +01:00
// see if user is not banned
const blacklist = await TwitterBlacklist . findOne ( { where : { userID : message . author . id } } ) ;
2020-02-26 06:56:22 +01:00
2019-11-28 15:36:18 +01:00
if ( blacklist ) {
return message . channel . send ( ` You have been blacklisted for the following reasons: \` \` ${ blacklist . get ( 'reason' ) } \` \` be less naughty less time. ` ) ;
}
2020-03-04 02:08:04 +01:00
// Don't let account new account use this command to prevent spam
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!' ) ;
}
2019-11-28 15:36:18 +01:00
// 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!' ) ;
2020-07-16 09:25:03 +02:00
}
2019-11-28 15:40:34 +01:00
const client = this . client ;
2019-11-25 23:19:37 +01:00
2019-11-25 23:15:12 +01:00
let T = new Twit ( {
consumer _key : twiConsumer ,
consumer _secret : twiConsumerSecret ,
access _token : twiToken ,
access _token _secret : twiTokenSecret
} ) ;
2019-08-13 16:53:16 +02:00
/ *
2019-10-06 20:29:42 +02:00
// Censor words
2019-02-28 17:35:53 +01:00
let censor = reload ( '../../json/censor.json' ) ;
let uncensor = reload ( '../../json/uncensor.json' ) ;
2019-02-26 15:28:08 +01:00
filter . addWords ( ... censor ) ;
filter . removeWords ( ... uncensor ) ;
2019-08-13 16:53:16 +02:00
* /
2019-02-26 00:23:12 +01:00
2019-07-01 00:41:14 +02:00
// remove zero width space
2019-11-25 23:23:28 +01:00
let text = '' ;
if ( args . text ) {
text = args . text . replace ( ' ' , '' ) ;
/ *
//Filter out swear word
text = filter . clean ( text ) ;
* /
text = rand . random ( text , message ) ;
}
2019-02-24 23:03:53 +01:00
try {
2019-11-25 23:15:12 +01:00
// Make sure there is an attachment and if its an image
if ( Attachment [ 0 ] ) {
if ( Attachment [ 0 ] . name . endsWith ( '.jpg' ) || Attachment [ 0 ] . name . endsWith ( '.png' ) || Attachment [ 0 ] . name . endsWith ( '.gif' ) ) {
fetch ( Attachment [ 0 ] . url )
. then ( res => {
const dest = fs . createWriteStream ( ` ${ os . tmpdir ( ) } / ${ Attachment [ 0 ] . name } ` ) ;
res . body . pipe ( dest ) ;
dest . on ( 'finish' , ( ) => {
2019-11-25 23:41:44 +01:00
let file = fs . statSync ( ` ${ os . tmpdir ( ) } / ${ Attachment [ 0 ] . name } ` ) ;
let fileSize = file . size / 1000000.0 ;
if ( ( Attachment [ 0 ] . name . endsWith ( '.jpg' ) || Attachment [ 0 ] . name . endsWith ( '.png' ) ) && fileSize > 5 ) {
return message . channel . send ( 'Images can\'t be larger than 5 MB!' ) ;
} else if ( Attachment [ 0 ] . name . endsWith ( '.gif' ) && fileSize > 15 ) {
return message . channel . send ( 'Gifs can\'t be larger than 15 MB!' ) ;
}
2019-11-25 23:15:12 +01:00
let b64Image = fs . readFileSync ( ` ${ os . tmpdir ( ) } / ${ Attachment [ 0 ] . name } ` , { encoding : 'base64' } ) ;
T . post ( 'media/upload' , { media _data : b64Image } , function ( err , data ) {
if ( err ) {
console . log ( 'OH NO AN ERROR!!!!!!!' ) ;
2019-11-25 23:27:11 +01:00
console . error ( err ) ;
2020-07-16 09:42:07 +02:00
return message . channel . send ( 'OH NO!!! AN ERROR HAS occurred!!! please hold on while i find what\'s causing this issue! ' ) ;
2019-11-25 23:15:12 +01:00
} else {
Tweet ( data ) ;
}
} ) ;
} ) ;
} ) ;
} else {
2019-11-26 18:12:48 +01:00
return message . channel . send ( 'File type not supported, you can only send jpg/png/gif' ) ;
2019-11-25 23:15:12 +01:00
}
} else {
Tweet ( ) ;
}
2019-02-24 23:03:53 +01:00
} catch ( err ) {
2019-02-24 23:41:37 +01:00
console . error ( err ) ;
2020-07-16 09:42:07 +02:00
return message . channel . send ( 'Oh no, an error has occurred :(' ) ;
2019-02-24 23:03:53 +01:00
}
2019-11-25 23:15:12 +01:00
function Tweet ( data ) {
let options = {
status : text
} ;
2019-11-25 23:21:30 +01:00
if ( data && args . text ) {
2019-11-25 23:15:12 +01:00
options = {
status : text ,
media _ids : new Array ( data . media _id _string )
} ;
2019-11-25 23:21:30 +01:00
} else if ( data ) {
options = {
media _ids : new Array ( data . media _id _string )
} ;
2019-11-25 23:15:12 +01:00
}
T . post ( 'statuses/update' , options , function ( err , response ) {
if ( err ) {
2019-11-28 15:46:25 +01:00
if ( err . code == 88 ) return message . channel . send ( err . message ) ; // Rate limit exceeded
2019-12-16 13:11:29 +01:00
if ( err . code == 186 ) return message . channel . send ( ` ${ err . message } Your message was ${ text . length } characters, you need to remove ${ text . length - 280 } characters (This count may be inaccurate if your message contained link) ` ) ; // Tweet needs to be a bit shorter.
2019-11-28 15:46:25 +01:00
if ( err . code == 187 ) return message . channel . send ( err . message ) ; // Status is a duplicate.
if ( err . code == 326 ) return message . channel . send ( err . message ) ; // To protect our users from spam and other malicious activity, this account is temporarily locked.
2019-11-25 23:15:12 +01:00
console . error ( 'OH NO!!!!' ) ;
2019-11-25 23:27:11 +01:00
console . error ( err ) ;
2020-07-16 09:42:07 +02:00
return message . channel . send ( 'OH NO!!! AN ERROR HAS occurred!!! please hold on while i find what\'s causing this issue! ' ) ;
2019-11-25 23:15:12 +01:00
}
const tweetid = response . id _str ;
const publicEmbed = client . util . embed ( )
. setAuthor ( 'Some user of discord said...' )
2019-11-25 23:25:07 +01:00
. setDescription ( text )
2019-11-27 13:11:14 +01:00
. addField ( 'Link' , ` https://twitter.com/i/status/ ${ tweetid } ` )
2019-11-25 23:15:12 +01:00
. setTimestamp ( ) ;
if ( Attachment [ 0 ] ) publicEmbed . setImage ( Attachment [ 0 ] . url ) ;
// Im too lazy for now to make an entry in config.json
2020-03-04 02:08:04 +01:00
let channel = client . channels . resolve ( '597964498921455637' ) ;
2019-11-25 23:15:12 +01:00
channel . send ( { embed : publicEmbed } ) ;
const Embed = client . util . embed ( )
. setAuthor ( message . author . username , message . author . displayAvatarURL ( ) )
. setDescription ( args . text )
2019-11-27 13:11:14 +01:00
. addField ( 'Link' , ` https://twitter.com/i/status/ ${ tweetid } ` , true )
2019-11-25 23:15:12 +01:00
. addField ( 'Tweet ID' , tweetid , true )
2020-08-21 15:39:15 +02:00
. addField ( 'Messsage ID' , message . id , true )
2019-11-25 23:15:12 +01:00
. addField ( 'Author' , ` ${ message . author . username } ( ${ message . author . id } ) ` , true )
. setTimestamp ( ) ;
2020-04-27 17:03:36 +02:00
if ( message . guild ) Embed . addField ( 'Guild' , ` ${ message . guild . name } ( ${ message . guild . id } ) ` , true ) ;
2019-11-25 23:15:12 +01:00
if ( Attachment [ 0 ] ) Embed . setImage ( Attachment [ 0 ] . url ) ;
2020-03-04 02:08:04 +01:00
channel = client . channels . resolve ( twiChannel ) ;
2019-11-25 23:15:12 +01:00
channel . send ( { embed : Embed } ) ;
2019-11-27 13:11:14 +01:00
return message . channel . send ( ` Go see ur epic tweet https://twitter.com/i/status/ ${ tweetid } ` ) ;
2019-11-25 23:15:12 +01:00
} ) ;
}
2019-02-24 23:03:53 +01:00
}
}
module . exports = tweetCommand ;