【问题标题】:how to loop retrieving data from covid library?如何循环从covid库中检索数据?
【发布时间】:2021-06-04 11:26:26
【问题描述】:
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启动时,从库中接收当前数据,发送消息时,发送消息,只接收一次,启动后不再更新此数据,我需要确保即使机器人正在运行,数据也会更新和发送

【问题讨论】:

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


    【解决方案1】:

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


    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)
    

    【讨论】:

    • discord.ext.commands.errors.CommandInvokeError: 命令引发异常:AttributeError: 'Group' object has no attribute 'get_data'
    • 哦,是的,你应该为你的函数和对象选择不同的名字。没看到,我更新一下答案
    猜你喜欢
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多