'Run asynchronous external loop with Discord loops
I can‘t figure out how to add an asynchronous loop to other loops that I use with Discord. I want to have a coroutine that reloads a page every 10 minutes while the code is still executing the other asyncs during the 10 minutes. For me it only runs the reload page code with this. Without the loop the other code works as intended. What do I need to change?
driver.get('https://Google.com')
#my Loop
async def reload():
while (True):
driver.refresh() #reloads the page
asyncio.sleep(600)
global start_count
start_count = 0
async def check_urls(urls):
for url in urls:
if any(x in url.lower() for x in keywords) and all(x not in url.lower() for x in blacklist):
driver.get(url)
print(f'Opened {url}')
@client.event
async def on_message(message):
global start_count
if start_count == 0:
. . .
else:
if message.channel.id in channels:
if message.embeds:
for embed in message.embeds:
toembed = embed.to_dict()
if str(toembed['type']).lower() != 'link':
urls = re.findall("(?:(?:https?|ftp):\/\/|\b(?:[a-z\d]+\.))(?:(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))?\))+(?:\((?:[^\s()<>]+|(?:\(?:[^\s()<>]+\)))?\)|[^\s`!()\[\]{};:'.,<>?«»“”‘’]))?",toembed['title'])
if urls:
await check_urls(urls)
try:
urls2 = re.findall("(?:(?:https?|ftp):\/\/|\b(?:[a-z\d]+\.))(?:(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))?\))+(?:\((?:[^\s()<>]+|(?:\(?:[^\s()<>]+\)))?\)|[^\s`!()\[\]{};:'.,<>?«»“”‘’]))?",toembed['description'])
if urls2:
await check_urls(urls2)
except:
pass
try:
for field in toembed['fields']:
urls3 = re.findall("(?:(?:https?|ftp):\/\/|\b(?:[a-z\d]+\.))(?:(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))?\))+(?:\((?:[^\s()<>]+|(?:\(?:[^\s()<>]+\)))?\)|[^\s`!()\[\]{};:'.,<>?«»“”‘’]))?",str(field))
if urls3:
await check_urls(urls3)
except:
pass
if message.content != '':
print(message.content)
urls4 = re.findall("(?:(?:https?|ftp):\/\/|\b(?:[a-z\d]+\.))(?:(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))?\))+(?:\((?:[^\s()<>]+|(?:\(?:[^\s()<>]+\)))?\)|[^\s`!()\[\]{};:'.,<>?«»“”‘’]))?",message.content)
if urls4:
await check_urls(urls4)
client.run(token)
async def main():
await asyncio.gather(
reload,
check_urls("urls"),
on_message("message"),
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
