【问题标题】:Python await' outside function Discord.pyPython等待'外部函数Discord.py
【发布时间】:2020-04-26 15:44:59
【问题描述】:

此机器人每 5 秒检查一次带有请求模块的网站 如果产品没有售罄。机器人必须向不和谐频道发送消息。 如果卖光了。 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')

【问题讨论】:

    标签: python python-3.x request python-requests discord.py


    【解决方案1】:

    考虑在 Discord.py 中使用任务
    请参阅 this 链接。

    但您必须在代码开头使用from discord.ext import tasks。任务很有帮助。

    你应该使用任务的原因是你不能在异步函数之外使用等待。此外,您的代码在 client.run 之前有一个 while True 循环。这意味着,client.run 可能不会执行。

    希望对你有帮助

    【讨论】:

    • 我收到 await 错误 SyntaxError: 'await' outside function
    • 您回答了,但没有解释任何内容。请考虑编辑您的答案以使其更好。
    猜你喜欢
    • 2021-06-26
    • 1970-01-01
    • 2019-03-02
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多