const Twit = require("twit");
const axios = require("axios");
var T = new Twit({
consumer_key: process.env.TWAPIKEY,
consumer_secret: process.env.TWAPISECRET,
app_only_auth: true
});
const discordApiRoot = "https://discordapp.com/api";
var fortniteChannelId = "594748481483177996";
var sinceId = "1178097123155484675";
function fetchAndPostUpdate() {
T.get('statuses/user_timeline', { user_id: "908090746200260608", since_id: sinceId, trim_user: true, tweet_mode: "extended"}, function(err, data, response) {
const latestTweets = data;
if (latestTweets.length > 0) {
sinceId = latestTweets[0].id_str;
}
for (i = 0; i < latestTweets.length; i++) {
var tweet = latestTweets[i];
if (!tweet.entities.media) {
continue;
}
var imgURL = tweet.entities.media[0].media_url_https.replace(".jpg", "?format=png&name=large");
var tweetText = tweet.full_text;
var richTitle = "";
if (tweetText.includes("Item Shop")) {
tweetText = "What's up, GUCCI Fortnite players! Here's today's Item Shop! Don't forget - use code **TIGERR**!";
} else if (tweetText.includes("News")) {
var newsData = tweetText.split("\n");
var newsTitle = newsData[0].split(": ")[1];
var newsText = newsData[1].split("\"")[1];
tweetText = "There's a GUCCI new update on Fortnite - check it out!";
richTitle = "**" + newsTitle + "**\n" + newsText;
} else if (tweetText.includes("LTM")) {
var LTMData = tweetText.split(": ");
var LTMName = LTMData.split(" is now live")[0];
tweetText = "Oh look, a new LTM just dropped! But it isn't GUCCI unless there's planes...";
richTitle = "**NEW LTM OUT NOW:** " + LTMName;
}
axios({
method: "POST",
url: discordApiRoot + "/channels/" + fortniteChannelId + "/messages",
headers: {
"Authorization": "Bot " + process.env.BOTTOKEN,
"Content-Type": "application/json"
},
data: {
content: tweetText,
embed: {
title: richTitle,
color: 0x794BC4,
image: {
url: imgURL
}
}
}
}).then(function() {
}).catch(function(error) {
console.log("DISCORD ERROR: Error while posting FNBR update.");
console.log(error);
});
}
});
}
setInterval(function() {
fetchAndPostUpdate();
}, 60000);