Discord.py Rewrite Giphy Cog Error: Unclosed Connector

Discord.py Rewrite Giphy Cog Error: Unclosed Connector

我正在尝试为我的 discord.py 机器人构建一个 Giphy cog,它抛出了以下错误

Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x000001CA509CDA60>
Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x000001CA51897BE0>, 77055.671)]']
connector: <aiohttp.connector.TCPConnector object at 0x000001CA509CDA30>

有谁知道是什么原因造成的,我该如何解决?

这是我的代码:

import os
import aiohttp
import random
import discord
from discord.ext import commands
from dotenv import load_dotenv

load_dotenv()

prefix = os.getenv("CLIENT_PREFIX")
giphy_api = os.getenv("GIPHY_API_KEY")

command_attrs = {'hidden': False}


class FunCog(commands.Cog, name='Fun Commands', command_attrs=command_attrs):
    def __init__(self, client):
        self.client = client

    @commands.command(name='gif')
    async def _gif(self, ctx, *, search, json=None):
        embed = discord.Embed(colour=discord.Colour.blue())
        session = aiohttp.ClientSession()

        if search == '':
            response = await session.get('https://api.giphy.com/v1/gifs/random?api_key=' + giphy_api)
            data = json.loads(await response.text())
            embed.set_image(url=data['data']['images']['original']['url'])
        else:
            search.replace(' ', '+')
            response = await session.get(
                'http://api.giphy.com/v1/gifs/search?q=' + search + '&api_key=' + giphy_api + '&limit=10')
            data = json.loads(await response.text())
            gif_choice = random.randint(0, 9)
            embed.set_image(url=data['data'][gif_choice]['images']['original']['url'])
            await session.close()
            await ctx.send(embed=embed)


def setup(client):
    client.add_cog(FunCog(client))

出于安全原因,我省略了 Giphy API 密钥。 如果有帮助,我正在使用 discord.py rewritepython 3.8.6

基本上我希望能够通过标签搜索 gif,它会使用来自 giphy 的随机 gif 响应指定标签。

--编辑--

  1. 将错误记录移至我的 Events.py 文件
  2. 将 API 密钥移至我的 .env 文件
  3. 删除了 tenor_api env,因为它不会被使用并且与这个问题无关
  4. 稍微更新了代码以解决一些缺失的参数。

---编辑---

感谢 Fixator10 的回复,解决了我在这个 post 中遇到的问题。

如果有人想使用它,这是我的工作代码:

import os
import aiohttp
import random
import discord
import json
from discord.ext import commands
from dotenv import load_dotenv

load_dotenv()

prefix = os.getenv("CLIENT_PREFIX")
giphy_api = os.getenv("GIPHY_API_KEY")

command_attrs = {'hidden': False}


class FunCog(commands.Cog, name='Fun Commands', command_attrs=command_attrs):
    def __init__(self, client):
        self.client = client
        self.session = aiohttp.ClientSession()

    def cog_unload(self):
        self.client.loop.create_task(self.session.close())

    @commands.command(name='gif')
    async def _gif(self, ctx, *, search):
        session = self.session
        embed = discord.Embed(colour=discord.Color.dark_gold())

        if search == '':
            response = await session.get('https://api.giphy.com/v1/gifs/random?api_key=' + giphy_api)
            data = json.loads(await response.text())
            embed.set_image(url=data['data']['images']['original']['url'])
        else:
            search.replace(' ', '+')
            response = await session.get(
                'http://api.giphy.com/v1/gifs/search?q=' + search + '&api_key=' + giphy_api + '&limit=10')
            data = json.loads(await response.text())
            gif_choice = random.randint(0, 9)
            embed.set_image(url=data['data'][gif_choice]['images']['original']['url'])
            await ctx.send(embed=embed)


def setup(client):
    client.add_cog(FunCog(client))

它可能不是最好的,因为它只使用一个标签,但我可能会在其他时间改进它 xD

这是由您的会话行为引起的。如果搜索为空,则不要关闭会话。用两个词:使用后应该关闭会话。

最简单的解决方案 - 是使用上下文管理器:

    # https://docs.aiohttp.org/en/stable/#client-example
    async with aiohttp.ClientSession() as session:
        async with session.get('http://python.org') as response:

否则,您可以为您的 Cog 创建一个会话并在卸载时关闭它:

class MyCog(commands.Cog):
    def __init__(client):
        self.client = client
        self.session = aiohttp.ClientSession()

    # https://discordpy.readthedocs.io/en/stable/ext/commands/api.html#discord.ext.commands.Cog.cog_unload
    def cog_unload(self):
        # since close is coroutine, 
        # and we need to call it in sync func
        # lets create asyncio task
        self.client.loop.create_task(self.session.close()) 
        # or, we can use detach:
        # self.session.detach()

    @commands.command()
    async def mycmd(ctx):
        async with self.session.get("https://example.com") as response:
            await ctx.send(response.status)