From d8d5da775224882e4accf8253652fd45bcd338b7 Mon Sep 17 00:00:00 2001 From: sushil-rgb Date: Thu, 2 Nov 2023 02:32:29 +0545 Subject: [PATCH] added discord menu commands (!general, !help, !about, !ping) --- discordsFunctionalities/runBot.py | 20 +++++---- discordsFunctionalities/sendMessages.py | 55 ++++++++++++++++++++++++- main.py | 6 +-- 3 files changed, 68 insertions(+), 13 deletions(-) diff --git a/discordsFunctionalities/runBot.py b/discordsFunctionalities/runBot.py index 40293d6..12f1c09 100644 --- a/discordsFunctionalities/runBot.py +++ b/discordsFunctionalities/runBot.py @@ -33,24 +33,28 @@ async def on_message(message): asin_pattern = r"""^[A-Z0-9]{10}$""" regex_pattern = """^hi|hello|hey|yo""" amazon_pattern = '(https?://)?(www\.)?amazon\.(com|in|co\.uk)/.+' + # If the message is a greeting and is sent in a direct message: if message.guild is None and re.match(regex_pattern, message.content, re.IGNORECASE): - await message.author.send(f"Hey {username}. Type '!help' to know the list of commands.") - # If the message is !help and is sent in a direct message: - elif message.content == '!help': - await message.author.send('Paste the Amazon products link to know the ASIN or ISBN respectively.\nPaste the ASIN/ISBN to get the product details.') + await message.author.send(f"Hey {username}. Type '!general' to know the overview of bot.") + # If the message is !general and is sent in a direct message: + elif message.content == '!commands': + await menu(message.content, message.author) + elif message.content == '!general' or message.content == '!help': + await menu(message.content, message.author) + elif message.content == '!about': + await menu(message.content, message.author) + elif message.content == '!ping': + await menu(message.content, message.author, client) # If the message is an Amazon product link and is sent in a direct message: elif message.guild is None and re.search(amazon_pattern, user_message): await asin_isbn(message.author, user_message) # IF the message is an ASIN/ISBN and is sent in a direct message: elif message.guild is None and (re.match(asin_pattern, message.content)): - # await export_to_db(user_message) - # await message.author.send('Please wait. Fetching data from Amazon.') await message.author.send(f"Please wait. Fetching data from Amazon.") await getdataByasin(user_message, message.author) - # If the message is not a valid link and is send in a direct message: else: - await message.author.send(f"Invalid link. Please try a valid Amazon product link.") + await message.author.send(f"Invalid command. Type '!general | !help' or '!commands' to know the purpose of the bot.") # Run the client with the TOKEN: client.run(Token) diff --git a/discordsFunctionalities/sendMessages.py b/discordsFunctionalities/sendMessages.py index 389acd6..15fbf5e 100644 --- a/discordsFunctionalities/sendMessages.py +++ b/discordsFunctionalities/sendMessages.py @@ -3,11 +3,61 @@ import sys import os + sys.path.append(os.getcwd()) from scrapers.scraper import Amazon -async def on_ready(): +async def menu(message, user, bot = None): + if message == '!general' or message == '!help': + embed = discord.Embed(title = "General", description = "General overview of bot", color = 0xff9900) + embed.add_field(name = '!commands', value = "List of available commands and their explanation", inline = False) + embed.add_field(name = '!about', value = "Provides the information about the bot and its purpose", inline = False) + embed.add_field(name = "!ping", value = "Check the bot's response time to the server.") + embed.set_footer(text = 'Powered by Python', icon_url = 'https://logos-download.com/wp-content/uploads/2016/10/Python_logo_icon.png') + embed.set_author(name = "Sushil", url = "https://www.github.com/sushil-rgb", icon_url = "https://avatars.githubusercontent.com/u/107347115?s=400&u=7a5fbfe85d59d828d52b407c999474c8938325c7&v=4") + embed.timestamp = datetime.datetime.now() + + await user.send(embed = embed) + + if message == '!commands': + embed = discord.Embed(title ='Bot menu', description = "List of available commands and their explanation", color = 0xff9900) + embed.add_field(name = "ASIN", value = "Extracts ASIN from the provided product link.", inline = False) + embed.add_field(name = "Paste product link", value = "Extracts ASIN from the provided product link.", inline = False) + embed.set_footer(text = 'Powered by Python', icon_url = 'https://logos-download.com/wp-content/uploads/2016/10/Python_logo_icon.png') + embed.set_author(name = "Sushil", url = "https://www.github.com/sushil-rgb", icon_url = "https://avatars.githubusercontent.com/u/107347115?s=400&u=7a5fbfe85d59d828d52b407c999474c8938325c7&v=4") + embed.timestamp = datetime.datetime.now() + + await user.send(embed = embed) + + if message == '!about': + embed = discord.Embed(title = "About", description = "Provides the information about the bot and its purpose", color = 0xff9900) + embed.add_field(name = "Purpose", value = "The purpose of this bot is to extract product ASIN by product link and retrieve product information by pasting ASIN.", inline = False) + embed.add_field(name = "Example Usage:", + value = "`[product link]` - Extracts ASIN from the provided product link. \n" + "`[B0CK3ZWT7X]` - Retrieves detailed product information using the provided ASIN.", + inline = False + ) + embed.set_footer(text = 'Powered by Python', icon_url = 'https://logos-download.com/wp-content/uploads/2016/10/Python_logo_icon.png') + embed.set_author(name = "Sushil", url = "https://www.github.com/sushil-rgb", icon_url = "https://avatars.githubusercontent.com/u/107347115?s=400&u=7a5fbfe85d59d828d52b407c999474c8938325c7&v=4") + embed.timestamp = datetime.datetime.now() + + await user.send(embed = embed) + + if message == '!ping': + latency = bot.latency + embed = discord.Embed(title = "Ping", + description = f"Pong! Bot latency is {latency * 1000:.2f}ms.", + color = 0x008000, + ) + embed.set_footer(text = 'Powered by Python', icon_url = 'https://logos-download.com/wp-content/uploads/2016/10/Python_logo_icon.png') + embed.set_author(name = "Sushil", url = "https://www.github.com/sushil-rgb", icon_url = "https://avatars.githubusercontent.com/u/107347115?s=400&u=7a5fbfe85d59d828d52b407c999474c8938325c7&v=4") + embed.timestamp = datetime.datetime.now() + + await user.send(embed = embed) + + +async def on_ready(bot): """ This function prints a message when the bot is ready to use. """ @@ -46,7 +96,7 @@ async def getdataByasin(userInput, user): datas = await Amazon(userInput, None).dataByAsin(userInput) name = datas['Name'] hyperlink = datas['Hyperlink'] - embed = discord.Embed(title=name, url=hyperlink, color=0xff9900) + embed = discord.Embed(title = name, url = hyperlink, color = 0xff9900) embed.add_field(name = 'Price', value = datas['Price'], inline = False) embed.add_field(name = 'Availability', value = datas['Availability'], inline = False) embed.add_field(name = "Store", value = f"[{datas['Store']}]({datas['Store link']})", inline = False) @@ -59,3 +109,4 @@ async def getdataByasin(userInput, user): except Exception as e: await user.send('Content loading error. Please try again in few minutes.') + diff --git a/main.py b/main.py index cbe2c76..bd3bba4 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,5 @@ from mongo_database.mongo import export_to_mong -from cli_integration.cli_parser import cli_imp +from mongo_database.mongo import export_to_mong from tools.tool import rand_proxies from scrapers.scraper import Amazon import asyncio @@ -10,9 +10,9 @@ async def main(): - # return cli_imp() - base_url = "https://www.amazon.com/s?k=Madewell&qid=1693942614&ref=sr_pg_1" + base_url = "https://www.amazon.com/s?k=gaming+chairs&_encoding=UTF8&content-id=amzn1.sym.12129333-2117-4490-9c17-6d31baf0582a&pd_rd_r=7cd6c025-439a-4e19-9e29-817277d8de15&pd_rd_w=oFRoZ&pd_rd_wg=JjbKC&pf_rd_p=12129333-2117-4490-9c17-6d31baf0582a&pf_rd_r=AW0QRVHS91TFCEPGS1VD&ref=pd_gw_unk" status = await Amazon(base_url, None).status() + if status == 503: return "503 response. Please try again later." # Type True if you want to export to CSV and avoid MongoDB