-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimeFreakQT.min.js
1 lines (1 loc) · 5.78 KB
/
AnimeFreakQT.min.js
1
const{Client:Client,GatewayIntentBits:GatewayIntentBits,Intents:Intents,TextChannel:TextChannel}=require("discord.js"),axios=require("axios"),dotenv=require("dotenv");let etsyToken,refreshToken,reviewInfo,oldReviewInfo,orderInfo,oldOrderInfo,listingInfo;dotenv.config();let timer=0;const client=new Client({intents:[GatewayIntentBits.Guilds,GatewayIntentBits.GuildMessages,GatewayIntentBits.MessageContent,GatewayIntentBits.GuildMembers]}),getFormatDate=(e=null)=>{if(e)return`${e.getMonth()+1}/${e.getDate()}/${e.getFullYear()}`;{let e=new Date;return`${e.getMonth()+1}/${e.getDate()}/${e.getFullYear()}`}},mockSpeak=(e,o)=>{e=o.content.split("");for(let o=0;o<e.length-1;o++)e[o]=o%2==0?e[o].toUpperCase():e[o].toLowerCase();return e=e.join("")},getNewEtsyToken=async()=>{let e=new URLSearchParams;try{e.append("grant_type","refresh_token"),e.append("client_id",process.env.ETSY_API),e.append("refresh_token",refreshToken||process.env.REFRESH_TOKEN);const o=await axios.post("https://api.etsy.com/v3/public/oauth/token",e,{headers:{"x-api-key":process.env.ETSY_API}});refreshToken=o.data.refresh_token,etsyToken=o.data.access_token}catch(o){o.response?(console.log(o.response.data),console.log(o.response.status),console.log(o.response.headers)):o.request?console.log(o.request):console.log("Error",o.message),e=new URLSearchParams,e.append("grant_type","refresh_token"),e.append("client_id",process.env.ETSY_API),e.append("refresh_token",refreshToken),axios.post("https://api.etsy.com/v3/public/oauth/token",e,{headers:{"x-api-key":process.env.ETSY_API}}).then((e=>{refreshToken=e.data.refresh_token,etsyToken=e.data.access_token})).catch((e=>{e.response?(console.log(e.response.data),console.log(e.response.status),console.log(e.response.headers)):e.request?console.log(e.request):console.log("Error",e.message),console.log(e.config)}))}},getShopReviews=async()=>{await axios.get(`https://openapi.etsy.com/v3/application/shops/${process.env.ANIMEFREAKQT_SHOP_ID}/reviews?limit=100`,{headers:{"x-api-key":process.env.ETSY_API,authorization:`${process.env.TOKEN_TYPE} ${etsyToken}`}}).then((e=>{reviewInfo=e.data})).catch((e=>{e.response?(console.log(e.response.data),console.log(e.response.status),console.log(e.response.headers)):e.request?console.log(e.request):console.log("Error",e.message),console.log(e.config)}))},getOrderInfo=async()=>{await axios.get(`https://openapi.etsy.com/v3/application/shops/${process.env.ANIMEFREAKQT_SHOP_ID}/receipts`,{headers:{"x-api-key":process.env.ETSY_API,authorization:"Bearer "+etsyToken}}).then((e=>{orderInfo=e.data})).catch((e=>{e.response?(console.log(e.response.data),console.log(e.response.status),console.log(e.response.headers)):e.request?console.log(e.request):console.log("Error",e.message),console.log(e.config)}))},getShopListings=()=>{axios.get(`https://openapi.etsy.com/v3/application/shops/${process.env.ANIMEFREAKQT_SHOP_ID}/listings/active`,{headers:{"x-api-key":process.env.ETSY_API,authorization:"Bearer "+etsyToken}}).then((e=>{listingInfo=e.data})).catch((e=>{e.response?(console.log(e.response.data),console.log(e.response.status),console.log(e.response.headers)):e.request?console.log(e.request):console.log("Error",e.message),console.log(e.config)}))};client.once("ready",(()=>{console.log(`Bot ${client.user.tag} ready!`);const e=client.channels.cache.get(process.env.ANIMEQT_ORDER_HISTORY),o=client.channels.cache.get(process.env.ANIMEQT_ETSY_REVIEWS);getNewEtsyToken(),setTimeout((()=>{getOrderInfo(),getShopReviews(),getShopListings()}),1e4),setInterval((()=>{getShopListings()}),18e5),setInterval((()=>{getNewEtsyToken(),console.log("Got new token")}),27e5),setInterval((()=>{try{getOrderInfo(),getShopReviews()}catch{getNewEtsyToken()}timer++,console.clear(),console.log(`Checked for updated data ${timer} time(s)`)}),45e3),setInterval((()=>{try{if(oldOrderInfo||(oldOrderInfo=orderInfo),orderInfo.count>oldOrderInfo.count){for(let o=orderInfo.count-oldOrderInfo.count-1;o>=0;o--){let t=orderInfo.results[o].grandtotal.amount/100,n=[];const s=new Date(1e3*orderInfo.results[o].transactions[0].expected_ship_date);orderInfo.results[o].transactions.map((e=>{e?.variations&&e?.quantity&&n.push(`• ${e.quantity} - ${e.variations.length>0?e.variations[0].formatted_value:e.title}\n`)})),n=n.join(""),e.send(`@everyone **NEW SALE!!** - ${getFormatDate()} - **$${t.toFixed(2)}**\n\n**Items:**\n${n}\n**Customer:**\n${orderInfo.results[o].formatted_address}\n\n**Ship Before:**\n${getFormatDate(s)}`)}oldOrderInfo=orderInfo}}catch(o){e.send(`@everyone **The bot had a boo boo but there should be a new order**\n\n**Error:\n**${o.toString()}`)}}),45e3),setInterval((()=>{if(oldReviewInfo||(oldReviewInfo=reviewInfo),reviewInfo&&reviewInfo.count>oldReviewInfo.count&&reviewInfo.count>oldReviewInfo.count){let e,t=reviewInfo.count-oldReviewInfo.count,n=0;reviewInfo.results.map((e=>n+=e.rating)),e=n/reviewInfo.results.length;for(let n=t-1;n>=0;n--){let t=[];orderInfo.results.map((e=>e.buyer_user_id==reviewInfo.results[n].buyer_user_id?t.push(e.formatted_address):null));let s=listingInfo.results.filter((e=>e.listing_id==reviewInfo.results[n].listing_id));o.send(`@everyone **NEW REVIEW!!** - ${getFormatDate()} - **Total Reviews:** ${reviewInfo.results.length} - **Average Review:** ${e.toFixed(1)}\n \n• **Item:** ${s[0].title}\n\n• **Rating:** ${reviewInfo.results[n].rating} stars\n\n• **Review:** ${""==reviewInfo.results[n].review?"*Review field left blank by customer*":reviewInfo.results[n].review}\n\n• **Customer:** \n${0==t.length?"*No customer info*":t[n]}`)}oldReviewInfo=reviewInfo,n=0}}),45e3)})),client.on("messageCreate",(e=>{e.author.bot||(e.guild==process.env.TAINTED_SOULS_GENERAL&&e.author.id==process.env.MY_ID||e.author.id==process.env.BABOOM?e.reply(mockSpeak(e.content,e)):e.guild==process.env.TAINTED_SOULS_GENERAL&&e.reply(`Hmmmm, ${e.content}, very interesting`))})),client.login(process.env.TOKEN);