首页 > 解决方案 > 如何在任务中使用 guild.id

问题描述

我正在尝试创建一个循环来检查服务器何时擦除的命令,但我发现你不能ctx.guild.id在任务中使用无论如何都可以创建命令循环但仍然可以使用公会:

@tasks.loop(seconds=120)
async def pop_status():
    for x in collection.find():
        if x["_id"] == client.get_guild(id):
            wipe = x["pop"]
    response = requests.get('https://api.battlemetrics.com/servers/' + wipe)
    pass_times = response.json()
    Server_wipe = pass_times['data']['attributes']['details']['rust_last_wipe']
    print(Server_wipe)

标签: discord.py

解决方案


有几个选项,您可以guild在启动任务时在任务中传递参数,也可以在任务中获取它:

@tasks.loop(seconds=120)
async def pop_status(guild):
    # you can use the guild argument

# You have to pass it when starting it, example in a command
@bot.command()
async def start(ctx):
    pop_status.start(ctx.guild)

让公会进入循环本身

@tasks.loop(seconds=120)
async def pop_status():
    guild = bot.get_guild(ID_HERE)

PS:您不应该真正使用该requests库,它是阻塞的,您应该aiohttp改用


推荐阅读