Skip to content

Commit da311f4

Browse files
author
AffanTheBest
committed
Add limit per user
1 parent 65cdf0e commit da311f4

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

bot.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Imports
22
const { Bot } = require("grammy");
3-
const { BOT_TOKEN, ADMINS } = require("./config");
3+
const { BOT_TOKEN, ADMINS, LIMIT } = require("./config");
44
const { isUrl, getRandomId, getProductDetails } = require("./utils");
55
const { 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(/amazon|flipkart/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

7278
bot.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

7888
bot.hears(/^\/stop_([a-z0-9])/, async ctx => {

config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ module.exports = {
1717
DB_URL: process.env.DB_URL || '',
1818
WORKER_URL: process.env.WORKER_URL || '',
1919
API_KEY: process.env.API_KEY || '', // Generate any API Key and pass it when accessing the API.
20+
LIMIT: Number(process.env.LIMIT), // Maximum number of products can be added by a user at a time.
2021
}

0 commit comments

Comments
 (0)