2022-08-28 17:03:15 +02:00
import { SlashCommandBuilder } from 'discord.js' ;
import { EmbedBuilder } from 'discord.js' ;
2022-08-14 22:28:37 +02:00
import Twit from 'twit' ;
import fetch from 'node-fetch' ;
import os from 'node:os' ;
import fs from 'node:fs' ;
import db from '../../models/index.js' ;
2022-08-15 15:14:03 +02:00
import wordToCensor from '../../json/censor.json' assert { type : 'json' } ;
2022-08-14 22:28:37 +02:00
const { twiConsumer , twiConsumerSecret , twiToken , twiTokenSecret , twiChannel , twiLogChannel } = process . env ;
const Blacklists = db . Blacklists ;
export default {
data : new SlashCommandBuilder ( )
. setName ( 'tweet' )
2022-10-31 13:13:09 +01:00
. setDescription ( 'Send tweet from Haha yes twitter account. Please do not use it for advertisement and keep it in English!' )
2022-08-14 22:28:37 +02:00
. addStringOption ( option =>
option . setName ( 'content' )
2022-10-31 13:13:09 +01:00
. setDescription ( 'The content of the tweet you want me to send.' )
2022-08-14 22:28:37 +02:00
. setRequired ( false ) )
. addAttachmentOption ( option =>
option . setName ( 'image' )
. setDescription ( 'Optional attachment (Image only.)' )
. setRequired ( false ) ) ,
2022-08-28 17:03:15 +02:00
category : 'fun' ,
2022-08-14 23:28:24 +02:00
ratelimit : 3 ,
cooldown : 3600 ,
2022-08-28 17:03:15 +02:00
async execute ( interaction , args , client ) {
2022-09-01 01:43:59 +02:00
const content = args . content ;
const attachment = args . image ;
2022-08-28 17:03:15 +02:00
if ( ! content && ! attachment ) {
2022-08-16 22:25:23 +02:00
return interaction . reply ( { content : 'Uh oh! You are missing any content for me to tweet!' , ephemeral : true } ) ;
}
2022-08-15 15:14:03 +02:00
await interaction . deferReply ( { ephemeral : false } ) ;
2022-08-28 17:03:15 +02:00
let tweet = content ;
2022-08-14 22:28:37 +02:00
const date = new Date ( ) ;
// If account is less than 6 months old don't accept the tweet ( alt prevention )
if ( interaction . user . createdAt > date . setMonth ( date . getMonth ( ) - 6 ) ) {
2022-10-31 13:13:09 +01:00
await interaction . editReply ( { content : 'Your Discord account is too new to be able to use this command!' } ) ;
2022-08-15 15:14:03 +02:00
return ;
2022-08-14 22:28:37 +02:00
}
2022-08-27 17:40:10 +02:00
// Reset the current date so it checks correctly for the 1 year requirement.
date . setTime ( Date . now ( ) ) ;
2022-08-14 22:28:37 +02:00
// If account is less than 1 year old don't accept attachment
if ( attachment && interaction . user . createdAt > date . setFullYear ( date . getFullYear ( ) - 1 ) ) {
2022-10-31 13:13:09 +01:00
await interaction . editReply ( { content : 'Your Discord account needs to be 1 year or older to be able to send attachments!' } ) ;
2022-08-15 15:14:03 +02:00
return ;
2022-08-14 22:28:37 +02:00
}
// remove zero width space
if ( tweet ) {
tweet = tweet . replace ( ' ' , '' ) ;
}
if ( tweet ) {
// Detect banned word (Blacklist the user directly)
2022-09-10 09:37:06 +02:00
if ( wordToCensor . includes ( tweet ) || wordToCensor . includes ( tweet . substring ( 0 , tweet . length - 1 ) ) || wordToCensor . includes ( tweet . substring ( 1 , tweet . length ) ) ) {
2022-08-14 22:28:37 +02:00
const body = { type : 'tweet' , uid : interaction . user . id , reason : 'Automatic ban from banned word.' } ;
Blacklists . create ( body ) ;
await interaction . editReply ( { content : 'Sike, you just posted cringe! Enjoy the blacklist :)' } ) ;
2022-08-15 15:14:03 +02:00
return ;
2022-08-14 22:28:37 +02:00
}
// Very simple link detection
if ( new RegExp ( '([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?' ) . test ( tweet ) && ! tweet . includes ( 'twitter.com' ) ) {
2022-08-15 15:14:03 +02:00
await interaction . editReply ( { content : 'You may not tweet links outside of twitter.com' } ) ;
return ;
}
2022-08-14 22:28:37 +02:00
// Do not allow discord invites
2022-10-31 13:13:54 +01:00
if ( tweet . includes ( 'discord.gg' ) || tweet . includes ( 'discord.com/invite/' ) || tweet . includes ( 'discordapp.com/invite/' ) ) {
2022-10-31 13:13:09 +01:00
await interaction . editReply ( { content : 'No discord invites allowed.' } ) ;
2022-08-15 15:14:03 +02:00
return ;
}
}
2022-08-14 22:28:37 +02:00
const T = new Twit ( {
consumer _key : twiConsumer ,
consumer _secret : twiConsumerSecret ,
access _token : twiToken ,
access _token _secret : twiTokenSecret ,
} ) ;
try {
// Make sure there is an attachment and if its an image
if ( attachment ) {
if ( attachment . name . toLowerCase ( ) . endsWith ( '.jpg' ) || attachment . name . toLowerCase ( ) . endsWith ( '.png' ) || attachment . name . toLowerCase ( ) . endsWith ( '.gif' ) ) {
fetch ( attachment . url )
. then ( res => {
const dest = fs . createWriteStream ( ` ${ os . tmpdir ( ) } / ${ attachment . name } ` ) ;
res . body . pipe ( dest ) ;
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 ) {
return interaction . editReply ( { content : 'Images can\'t be larger than 5 MB!' } ) ;
}
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' } ) ;
T . post ( 'media/upload' , { media _data : b64Image } , function ( err , data ) {
if ( err ) {
console . log ( 'OH NO AN ERROR!!!!!!!' ) ;
console . error ( err ) ;
2022-10-31 13:13:09 +01:00
return interaction . editReply ( { content : 'OH NO!!! AN ERROR HAS OCCURRED!!! Please hold on while I find what\'s causing this issue! ' } ) ;
2022-08-14 22:28:37 +02:00
}
else {
Tweet ( data ) ;
}
} ) ;
} ) ;
} ) ;
}
else {
await interaction . editReply ( { content : 'File type not supported, you can only send jpg/png/gif' } ) ;
2022-08-15 15:14:03 +02:00
return ;
2022-08-14 22:28:37 +02:00
}
}
else {
Tweet ( ) ;
}
}
catch ( err ) {
console . error ( err ) ;
await interaction . editReply ( { content : 'Oh no, an error has occurred :(' } ) ;
2022-08-15 15:14:03 +02:00
return ;
2022-08-14 22:28:37 +02:00
}
function Tweet ( data ) {
let options = {
status : tweet ,
} ;
if ( data && tweet ) {
options = {
status : tweet ,
media _ids : new Array ( data . media _id _string ) ,
} ;
}
else if ( data ) {
options = {
media _ids : new Array ( data . media _id _string ) ,
} ;
}
T . post ( 'statuses/update' , options , function ( err , response ) {
if ( err ) {
// Rate limit exceeded
if ( err . code == 88 ) return interaction . editReply ( { content : err . interaction } ) ;
// Tweet needs to be a bit shorter.
if ( err . code == 186 ) return interaction . editReply ( { content : ` ${ err . interaction } Your interaction was ${ tweet . length } characters, you need to remove ${ tweet . length - 280 } characters (This count may be inaccurate if your interaction contained link) ` } ) ;
// Status is a duplicate.
if ( err . code == 187 ) return interaction . editReply ( { content : err . interaction } ) ;
// To protect our users from spam and other malicious activity, this account is temporarily locked.
if ( err . code == 326 ) return interaction . editReply ( { content : err . interaction } ) ;
console . error ( 'OH NO!!!!' ) ;
console . error ( err ) ;
2022-10-31 13:13:09 +01:00
return interaction . editReply ( { content : 'OH NO!!! AN ERROR HAS OCCURRED!!! Please hold on while I find what\'s causing this issue!' } ) ;
2022-08-14 22:28:37 +02:00
}
const tweetid = response . id _str ;
const FunnyWords = [ 'oppaGangnamStyle' , '69' , '420' , 'cum' , 'funnyMan' , 'GUCCISmartToilet' , 'TwitterForClowns' , 'fart' , 'mcDotnamejeffDotxyz' , 'ok' , 'hi' , 'howAreYou' , 'WhatsNinePlusTen' , '21' ] ;
const TweetLink = ` https://twitter.com/ ${ FunnyWords [ Math . floor ( ( Math . random ( ) * FunnyWords . length ) ) ] } /status/ ${ tweetid } ` ;
// Im too lazy for now to make an entry in config.json
2022-08-28 17:03:15 +02:00
let channel = client . channels . resolve ( twiChannel ) ;
2022-08-14 22:28:37 +02:00
channel . send ( TweetLink ) ;
2022-08-28 17:03:15 +02:00
const Embed = new EmbedBuilder ( )
2022-08-15 15:14:03 +02:00
. setAuthor ( { name : interaction . user . username , iconURL : interaction . user . displayAvatarURL ( ) } )
. setDescription ( tweet )
2022-08-28 17:03:15 +02:00
. addFields (
{ name : 'Link' , value : TweetLink , inline : true } ,
{ name : 'Tweet ID' , value : tweetid , inline : true } ,
{ name : 'Channel ID' , value : interaction . channel . id , inline : true } ,
{ name : 'Message ID' , value : interaction . id , inline : true } ,
{ name : 'Author' , value : ` ${ interaction . user . username } ( ${ interaction . user . id } ) ` , inline : true } ,
)
2022-08-14 22:28:37 +02:00
. setTimestamp ( ) ;
if ( interaction . guild ) {
2022-08-28 17:03:15 +02:00
Embed . addFields (
{ name : 'Guild' , value : ` ${ interaction . guild . name } ( ${ interaction . guild . id } ) ` , inline : true } ,
{ name : 'message link' , value : ` https://discord.com/channels/ ${ interaction . guild . id } / ${ interaction . channel . id } / ${ interaction . id } ` , inline : true } ,
) ;
2022-08-14 22:28:37 +02:00
}
else {
2022-08-28 17:03:15 +02:00
Embed . addFields ( { name : 'message link' , value : ` https://discord.com/channels/@me/ ${ interaction . channel . id } / ${ interaction . id } ` } ) ;
2022-08-14 22:28:37 +02:00
}
if ( attachment ) Embed . setImage ( attachment . url ) ;
2022-08-28 17:03:15 +02:00
channel = client . channels . resolve ( twiLogChannel ) ;
2022-08-15 15:14:03 +02:00
channel . send ( { embeds : [ Embed ] } ) ;
2022-08-14 22:28:37 +02:00
return interaction . editReply ( { content : ` Go see ur epic tweet ${ TweetLink } ` } ) ;
} ) ;
}
} ,
} ;