I write a bot in python, the bot functions: Show the price of a stock, set a limit on the price, and when this limit is reached, sends a message to discord. At this stage, the bot can only monitor one action. How to make it asynchronous, so that it can monitor several stocks at the same time, and when the limit is reached, it sends a particular stock to discord with a message that the price has reached the mark.
from bs4 import BeautifulSoup
import discord
from discord.ext import commands
from config import settings
bot = commands.Bot(command_prefix = settings['prefix'], help_command=None)
@bot.event
async def on_message(message):
await bot.process_commands(message)
channel = message.channel
co = '{0.content}'.format(message).split()
tes=co[0]
if tes=='price':
yo=co[1]
print(yo)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0'
}
r = requests.get(
('https://finance.yahoo.com/quote/') + yo + ('?p=') + yo + ('.tsrc=fin-srch'),
headers=headers)
soup = BeautifulSoup(r.text, 'lxml')
content = soup.find('div', {"class": 'My(6px) Pos(r) smartphone_Mt(6px)'}).find('span').text
print(content)
await channel.send(f'{yo} - {content}')
return content
elif tes=='limit':
p=co[1]
su=co[2]
price=float(su)
while True:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0'
}
r = requests.get(
('https://finance.yahoo.com/quote/') + p + ('?p=') + p + ('.tsrc=fin-srch'),
headers=headers)
soup = BeautifulSoup(r.text, 'lxml')
content = soup.find('div', {"class": 'My(6px) Pos(r) smartphone_Mt(6px)'}).find('span').text
con=float(content)
if price<=con:
await channel.send("достиг")
break
print(content)
return content
bot.run(settings['token'])