11// Imports
22const { Bot } = require ( "grammy" ) ;
3- const { BOT_TOKEN , ADMINS } = require ( "./config" ) ;
3+ const { BOT_TOKEN , ADMINS , LIMIT } = require ( "./config" ) ;
44const { isUrl, getRandomId, getProductDetails } = require ( "./utils" ) ;
55const { manageProducts, manageUsers } = require ( "./db" ) ;
66
@@ -47,19 +47,25 @@ bot.command('track', async ctx => {
4747 if ( isUrl ( productUrl ) ) {
4848 const merchant = productUrl . replace ( 'www.' , '' ) . split ( '//' ) [ 1 ] . split ( '.' ) [ 0 ] ;
4949 if ( merchant . match ( / a m a z o n | f l i p k a r t / gi) ) {
50- const sentMsg = await ctx . reply ( `Tracking ${ merchant } product...` , { reply_to_message_id : ctx . message . message_id } ) ;
51- const details = await getProductDetails ( productUrl , merchant ) ;
52- if ( details . ok ) {
53- try {
54- const tracking_id = getRandomId ( ) ;
55- await manageProducts ( { tracking_id, userId : ctx . from . id , merchant, title : details . title , link : details . link , initPrice : details . price , price : details . price } , 'update' ) ;
56- await ctx . api . editMessageText ( ctx . chat . id , sentMsg . message_id ,
57- `<a href="${ details . image } "> </a>\nTracking <b>${ details . title } </b>\n\nCurrent Price: <b>${ details . price } </b>\nLink: <a href="${ details . link } ">${ merchant } </a>\n\nTo stop tracking send /stop_${ tracking_id } ` ,
58- { parse_mode : "HTML" , reply_markup }
59- ) ;
60- } catch ( e ) { }
61- } else {
62- await ctx . api . editMessageText ( ctx . chat . id , sentMsg . message_id , `Sorry, I couldn't track this product. Make sure you've sent correct product link.` , { parse_mode : "Markdown" , reply_markup } ) ;
50+ const noOfProducts = ( await manageProducts ( { userId : ctx . from . id } , 'read' ) ) ?. result ?. length ;
51+ if ( noOfProducts < LIMIT ) {
52+ const sentMsg = await ctx . reply ( `Tracking ${ merchant } product...` , { reply_to_message_id : ctx . message . message_id } ) ;
53+ const details = await getProductDetails ( productUrl , merchant ) ;
54+ if ( details . ok ) {
55+ try {
56+ const tracking_id = getRandomId ( ) ;
57+ await manageProducts ( { tracking_id, userId : ctx . from . id , merchant, title : details . title , link : details . link , initPrice : details . price , price : details . price } , 'update' ) ;
58+ await ctx . api . editMessageText ( ctx . chat . id , sentMsg . message_id ,
59+ `<a href="${ details . image } "> </a>\nTracking <b>${ details . title } </b>\n\nCurrent Price: <b>${ details . price } </b>\nLink: <a href="${ details . link } ">${ merchant } </a>\n\nTo stop tracking send /stop_${ tracking_id } ` ,
60+ { parse_mode : "HTML" , reply_markup }
61+ ) ;
62+ } catch ( e ) { }
63+ } else {
64+ await ctx . api . editMessageText ( ctx . chat . id , sentMsg . message_id , `Sorry, I couldn't track this product. Make sure you've sent correct product link.` , { parse_mode : "Markdown" , reply_markup } ) ;
65+ }
66+ } else {
67+ ctx . reply ( "I'm sorry, but you can't add more products as you've already reached the maximum limit.\n\nPlease delete atleast one product. And try again.\n\nTo get list send /list" ,
68+ { reply_to_message_id : ctx . message . message_id } )
6369 }
6470 } else {
6571 ctx . reply ( `Sorry, I can't track this product. Cuz the link you sent is not a amazon or flipkart product link.` ) ;
@@ -70,9 +76,13 @@ bot.command('track', async ctx => {
7076} ) ;
7177
7278bot . command ( 'list' , async ctx => {
73- const products = await manageProducts ( { userId : ctx . from . id } , 'read' ) ;
74- const list = products . result . map ( ( product ) => `<b>${ product . title } </b>\nLast Price: ${ product . price } \nLink: <a href="${ product . link } ">${ product . merchant } </a>\nTo stop send /stop_${ product . tracking_id } ` ) . join ( '\n\n' ) ;
75- ctx . reply ( `Here is your tracking list:\n\n${ list } ` , { reply_to_message_id : ctx . message . message_id , parse_mode : "HTML" , disable_web_page_preview : true } ) ;
79+ try {
80+ const products = await manageProducts ( { userId : ctx . from . id } , 'read' ) ;
81+ const list = products . result . map ( ( product ) => `<b>${ product . title } </b>\nLast Price: ${ product . price } \nLink: <a href="${ product . link } ">${ product . merchant } </a>\nTo stop send /stop_${ product . tracking_id } ` ) . join ( '\n\n' ) ;
82+ ctx . reply ( `Here is your tracking list:\n\n${ list } ` , { reply_to_message_id : ctx . message . message_id , parse_mode : "HTML" , disable_web_page_preview : true } ) ;
83+ } catch ( e ) {
84+ ctx . reply ( 'An error has beem occured please report it to admin. This my be due to you\'ve added too many products.' )
85+ }
7686} )
7787
7888bot . hears ( / ^ \/ s t o p _ ( [ a - z 0 - 9 ] ) / , async ctx => {
0 commit comments