Advertisement
samiroexpikachu

Amazon

Mar 16th, 2024 (edited)
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const axios = require('axios');
  2.  
  3. module.exports = {
  4.   config: {
  5.     name: "amazon",
  6.     version: "1.0",
  7.     author: "Samir Œ",
  8.     shortDescription: "Search for products on Amazon",
  9.     longDescription: "Search for products on Amazon and display details of a random item.",
  10.     category: "Utility",
  11.     guide: "{prefix}amazon <search_query>",
  12.   },
  13.  
  14.   onStart: async function ({ message, args }) {
  15.     const searchQuery = args.join(" ");
  16.     if (!searchQuery) {
  17.       return message.reply("Please provide a search query.");
  18.     }
  19.  
  20.     try {
  21.       const response = await axios.get(`https://apis-samir.onrender.com/amazon/search?search=${encodeURIComponent(searchQuery)}`);
  22.       const products = response.data;
  23.  
  24.       if (products.length === 0) {
  25.         return message.reply("No products found.");
  26.       }
  27.  
  28.       const randomIndex = Math.floor(Math.random() * products.length);
  29.       const product = products[randomIndex];
  30.  
  31.       const messageBody = `Title: ${product.title}\n\nRank: ${product.rank}\n\nPrice: ${product.price}\n\nRating: ${product.rating}`;
  32.       const imageStream = await global.utils.getStreamFromURL(product.image_url);
  33.  
  34.       if (!imageStream) {
  35.         return message.reply("Failed to retrieve image.");
  36.       }
  37.  
  38.       return message.reply({
  39.         body: messageBody,
  40.         attachment: imageStream
  41.       });
  42.     } catch (error) {
  43.       console.error(error);
  44.       return message.reply("Failed to search for products on Amazon.");
  45.     }
  46.   }
  47. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement