【问题标题】:How to change code get image from a folder in github如何更改代码从github中的文件夹获取图像
【发布时间】:2021-05-17 05:44:16
【问题描述】:

我在 vsc 中制作了一个不和谐的机器人,它工作得非常好。我希望它可以在线而不需要我的电脑。我把我的代码放在 GitHub 上,然后连接到 Heroku。当我要一张图片时,一切都正常。

当我执行 !help 时,它会显示帮助菜单。但是当我执行 !png box 时,什么也没有发生。 这是vsc中的代码,当它工作时(代码和图像在同一个文件夹中)

@client.command()
async def png(ctx, *, arg):
    if(arg == 'box'):
        await ctx.send(file=discord.File('box.png'))

我需要更改代码还是我必须做什么。这些图像位于 GitHub 中名为 items 的文件夹中,如何更改代码才能正常工作。

(这也是我第一次使用 GitHub 和 Heroku,所以可能会有一个我不知道的简单答案,而且我对编码也很陌生)

【问题讨论】:

    标签: python github heroku discord.py


    【解决方案1】:

    如果图像在 Heroku 本地不可用,您可以从 github 发送。正如documentation 所示,您可以使用 aiohttp 发出 HTTP 请求,然后将 io.BytesIO 实例传递给 File。确保使用 github 上图片的直接 url。

    import io
    import aiohttp
    
    @client.command()
    async def png(ctx, *, arg):
        if(arg == 'box'):
            async with aiohttp.ClientSession() as session:
                async with session.get("https://raw.githubusercontent.com/your_github_name/your_repo_name/master/items/box.png") as resp:
                    if resp.status != 200:
                        return await ctx.send('Could not download file...')
                    data = io.BytesIO(await resp.read())
                    await ctx.send(file=discord.File(data, 'box.png'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      相关资源
      最近更新 更多