gaber-elsayed

INSTGRAM PROFILE (JS)

Aug 19th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. const fetch = require("node-fetch");
  2.  
  3. module.exports = {
  4. name: "instagram",
  5. usage: ["Get the most popular post from an instagram account```{prefix}instagram <user>```"],
  6. enabled: true,
  7. aliases: ["insta"],
  8. category: "Stats",
  9. memberPermissions: [],
  10. botPermissions: [ "SEND_MESSAGES", "EMBED_LINKS" ],
  11. //Settings for command
  12. nsfw: false,
  13. ownerOnly: false,
  14. cooldown: 5000,
  15.  
  16. // Execute contains content for the command
  17. async execute(client, message, args, data){
  18. try{
  19. let user = !args[0] ? "KSJaay" : args[0];
  20. let instagram = await fetchInstagram(user);
  21.  
  22. if(!instagram){
  23. return message.channel.send("Unable to find the provided channel.");
  24. }
  25.  
  26. // Get the top liked image
  27. let topImage;
  28. if(instagram.imageData.length > 0){
  29. topImage = instagram.imageData[0];
  30. }else{
  31. topImage = {
  32. likes: 0,
  33. comments: 0,
  34. image: message.client.user.displayAvatarURL()
  35. }
  36. }
  37.  
  38. return client.embed.send(message, {
  39. title: instagram.name,
  40. url: `https://www.instagram.com/${instagram.id}`,
  41. thumbnail: {
  42. url: instagram.image
  43. },
  44. description: instagram.bio,
  45. fields: [
  46. {
  47. name: 'Followers',
  48. value: instagram.followers,
  49. inline: true,
  50. },
  51. {
  52. name: 'Following',
  53. value: instagram.following,
  54. inline: true,
  55. },
  56. ],
  57. image: {
  58. url: topImage.image
  59. },
  60. footer: {
  61. text: `👍 ${topImage.likes} 💬 ${topImage.comments}`,
  62. icon_url: '',
  63. }
  64. })
  65.  
  66.  
  67.  
  68. async function fetchInstagram(id){
  69. let url = `https://www.instagram.com/${id}/?__a=1`;
  70. // Get profile information using url
  71. let results = await fetch(url)
  72.  
  73. // If there's no results return false
  74. if(results.status !== 200){
  75. return false;
  76. }
  77.  
  78. results = await results.json();
  79.  
  80. // Get information about the user
  81. let data = {
  82. id: results.graphql.user.username,
  83. name: results.graphql.user.full_name,
  84. bio: results.graphql.user.biography,
  85. followers: results.graphql.user.edge_followed_by.count,
  86. following: results.graphql.user.edge_follow.count,
  87. image: results.graphql.user.profile_pic_url_hd
  88. }
  89.  
  90. // Get information about each image
  91. let images = results.graphql.user.edge_owner_to_timeline_media;
  92. let imgArr = [];
  93. for(let i=0; i < images.edges.length; i++){
  94. let imageData = {
  95. image: images.edges[i].node.display_url,
  96. likes: images.edges[i].node.edge_liked_by.count,
  97. comments: images.edges[i].node.edge_media_to_comment.count
  98. }
  99. imgArr.push(imageData)
  100. }
  101.  
  102. // Sort likes from most to least
  103. await imgArr.sort(function(a, b){return a.likes - b.likes});
  104.  
  105. data.imageData = imgArr;
  106.  
  107. return data;
  108. }
  109.  
  110. }catch(err){
  111. client.logger.error(`Ran into an error while executing ${data.cmd.name}`)
  112. console.log(err)
  113. return client.embed.send(message, {
  114. description: `An issue has occured while running the command. If this error keeps occuring please contact our development team.`,
  115. color: `RED`,
  116. author: {
  117. name: `Uh Oh!`,
  118. icon_url: `${message.author.displayAvatarURL()}`,
  119. url: "",
  120. }
  121. });
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment