python - 如何在 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()
解决方案
所以其实很简单,只要去掉while True即可。
推荐阅读
- django - 如何从消费者类外部发送 channel_session 数据
- java - 如何使用动态编程查找 Boggle 板上的所有单词?
- reactjs - 绕过/覆盖 React ag-Grid 的分页
- php - 如何使用php同时传递选中和未选中复选框的值?
- c++ - 如何解决“type_info's has hidden visibility. they should all have public visibility.”?
- vb.net - 无法在 vb.net framework 2.0 和 VB2005 中使用 ConfigurationManager
- ios - 如何关闭打开的子视图?
- react-native - 如何在本机反应中使用图像源内的状态值?
- c# - 自动填写 facebook.com 的登录表单
- java - 在 Eclipse 中导入 servlet 项目时出错