首页 > 解决方案 > 如何在 Discord.py 中循环?

问题描述

我试图使循环功能,但机器人忽略用户的命令,以及我在互联网上搜索如何修复它,但我仍然没有得到任何答案。这是我的代码:

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

标签: pythondiscorddiscord.py

解决方案


所以其实很简单,只要去掉while True即可。


推荐阅读