简体   繁体   中英

Python await' outside function Discord.py

This bot check the web site with request module every 5 second If product is not solded out. Bot has to send message to discord channel. If it is solded out. 5 second later check again

 import discord
import requests
import re
import time
time.sleep(60)

client = discord.Client()

url = "https://wunder.com.tr/sneaker/adidas-sneaker/yeezy-boost-700-v3-H67799?gclid=CjwKCAjwnIr1BRAWEiwA6GpwNSZ5WT1GuFdTO1nXkwYLl__RXY0PZ6Atez4_ZYB-DSNEbzz3Z3swGRoCkAsQAvD_BwE"
a=1
b=2



@client.event
async def on_ready():
    print("----------------------")
    print("Logged In As")
    print("Username: %s"%client.user.name)
    print("ID: %s"%client.user.id)
    print("----------------------")


@client.event
async def on_message(message):
    id = client.get_guild(703666065040867391)

while a<b:
    time.sleep(5)
    req = requests.get(url)
    if re.search('(?i)SOLD OUT',req.text):
        print('Nike Fear of God Checked'+ i)

    else:
        await message.channel.send('Stokta Var')
        print('Nike Fear of God Checked')





client.run('token')

Consider using tasks in Discord.py
See this link.

But you must do from discord.ext import tasks in the begining of your code. Tasks are very helpful.

The reason you should use tasks is you cannot use await outside of an async function. Also, your code has a while True loop before client.run . That means, client.run will probably not execute.

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM