简体   繁体   中英

How to loop in Discord.py?

I've tried to make the loop function but the bot ignores commands from the user as well as I searched all over the internet how to fix it but I still haven't got any answers. Here is my code:

from discord.ext import tasks, commands
import discord
import json
import requests

# My function
@tasks.loop(seconds=10)
async def covloop():
  while True:
    time.sleep(10)
    cov = requests.get("https://covid19.th-stat.com/api/open/today")
    covdate = json.loads(cov.text)['UpdateDate']
    cf = json.loads(cov.text)['NewConfirmed']
    with open("data.json", 'r+') as file:
      data = json.load(file)
      updateTime = data['date']
      print(updateTime)
      if updateTime == covdate:
        pass
      elif updateTime != covdate:
        await bot.get_channel(827873382263029770).send('New confirmed: {}'.format(cf))
        data['date'] = covdate
        file.seek(0)
        json.dump(data,file)
        file.truncate()
  print(str(covdate))

bot = commands.Bot(command_prefix=["$"],case_insensitive=True)

@bot.event
async def on_ready():
  print("We've logged in as {0.user}".format(bot))
  covloop.start()

So it's actually simple, just remove while True.

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