首页 > 解决方案 > 如何循环从covid库中检索数据?

问题描述

covid = Covid()
covid.get_data()       

confirmed = covid.get_total_confirmed_cases()
active = covid.get_total_active_cases()
recovered = covid.get_total_recovered()
deaths = covid.get_total_deaths()

@bot.group(invoke_without_command=True)
async def covid(ctx):
    em = discord.Embed(title = "Covid-19", description = "test", color = ctx.author.color) 
    em.add_field(name = "test2", value = active)#
    em.add_field(name = "test3", value = confirmed)
    em.add_field(name = "test4", value = deaths) 
    em.add_field(name = "test5", value = recovered) 

    await ctx.send(embed = em)
")

当bot启动时,它从库中接收当前数据,当它发送消息时,它发送它,它只接收一次,启动后它不再更新这些数据,我需要确保数据即使机器人正在运行,也会更新和发送

标签: pythonpython-3.xdiscorddiscord.py

解决方案


为什么不在命令执行时重新加载数据?


covidObject = Covid()


@bot.group(invoke_without_command=True)
async def covid(ctx):
    covidObject.get_data()       

    confirmed = covidObject.get_total_confirmed_cases()
    active = covidObject.get_total_active_cases()
    recovered = covidObject.get_total_recovered()
    deaths = covidObject.get_total_deaths()

    em = discord.Embed(title = "Covid-19", description = "test", color = ctx.author.color) 
    em.add_field(name = "test2", value = active)#
    em.add_field(name = "test3", value = confirmed)
    em.add_field(name = "test4", value = deaths) 
    em.add_field(name = "test5", value = recovered) 

    await ctx.send(embed = em)

推荐阅读