簡體   English   中英

Python 在 function Discord.py 外等待

[英]Python await' outside function Discord.py

如果產品未售罄,該機器人每 5 秒檢查一次帶有請求模塊的 web 站點。 機器人必須向 discord 通道發送消息。 如果賣光了。 5秒后再次檢查

 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')

考慮使用 Discord.py 中的任務
請參閱鏈接。

但是你必須在你的代碼開頭做from discord.ext import tasks 任務非常有幫助。

您應該使用任務的原因是您不能在異步 function 之外使用 await。 此外,您的代碼在 client.run 之前有一個 while True 循環。 這意味着,client.run 可能不會執行。

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM