尝试使用一个命令多次从会话中获取JSON X次

时间:2019-11-08 02:22:31

标签: python discord.py

首先,我想对Python / Coding感到陌生,并且主要通过在线阅读资源/论坛来介绍它。

我正试图为我的Discord机器人重复一个齿轮。我差不多完成了,但是我只停留在这个问题上,对aiohttp的工作原理了解得还不够。

from discord.ext import commands
import discord
import asyncio
import random
import aiohttp

client = discord.Client()
bot = commands.Bot(command_prefix='$')

class auto(commands.Cog):

    def __init__(self, bot):
        self.bot = bot
        self.session = aiohttp.ClientSession(loop=self.bot.loop)

    @bot.command(pass_context=True)
    async def auto(self, ctx):
        timeout = 1*5
        async with self.session.get("https://nekos.life/api/v2/img/neko") as resp:
            messages = await resp.json()
        while True:
            embed = discord.Embed(colour=discord.Colour.blue())
            embed.set_image(url=messages['url'])
            await ctx.send(embed=embed)
            await asyncio.sleep(timeout)

def setup(bot):
    bot.add_cog(auto(bot))

由此产生的预期结果是,它将每五秒钟发布一次不同的嵌入式图像。

实际结果是每五秒钟发布一次图像,但是它是同一张图像。它永远不会改变。除非您重新运行该命令。

我知道问题很可能是它从API中提取了一次,然后发送图像,然后在此处暂停并仅每五秒运行一次发送。我只是不知道一种方法来修复它,使其循环回去。

有关我使用的URL的一些信息:每次重新加载页面时,它将提供一个新链接。如果您打开显示的链接,则可以自己查看。它将为您提供JSON数据和指向指定链接的“ URL”点。

0 个答案:

没有答案