discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ValueError: I/O operation on closed file

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ValueError: I/O operation on closed file

嗨!我的 discord 机器人有一个单独的帮助命令。

class Help(commands.Cog): 
    def __init__(self,client):
        self.client = client
        self.eSantaLogo = discord.File("resources\embedImages\eSanta.png", filename="eSanta.png")

    @commands.group(aliases=["help","commands"], invoke_without_command=True)
    async def help_(self,ctx):
        prefix = ''.join(get_prefix(self.client, ctx))
        embed=discord.Embed(title="eSanta Command Groups",
                            url="https://esanta.me", 
                            color=discord.Color.dark_purple(), 
                            timestamp=datetime.datetime.now(),
                            description=f'Current server prefix: {prefix}')

        embed.set_thumbnail(url='attachment://eSanta.png')
        embed.set_footer(text=f"Requested by {ctx.author.name}")

        embed.add_field(name='**Categories**',value=f"{prefix}help moderator\n{prefix}help fun", inline=True)
        await ctx.send(file=self.eSantaLogo,embed=embed)

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

当我第一次 运行 它完全正常工作时。

但是当我再次尝试 运行 时,出现了这个异常。

Traceback (most recent call last):
  File "C:\Users\notri\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "E:\bot\bot.py", line 104, in on_command_error
    raise error
  File "C:\Users\notri\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\notri\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 1353, in invoke
    await super().invoke(ctx)
  File "C:\Users\notri\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\notri\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ValueError: I/O operation on closed file

来自文档:

File objects are single use and are not meant to be reused in multiple abc.Messageable.send()s.

您应该在文件发送后重新加载,一个很好的解决方案:

class Help(commands.Cog): 
    def __init__(self,client):
        self.client = client

    @property
    def eSantaLogo(self):
        return discord.File("resources\embedImages\eSanta.png", filename="eSanta.png")

    @commands.group(aliases=["help","commands"], invoke_without_command=True)
    async def help_(self,ctx):
        prefix = ''.join(get_prefix(self.client, ctx))
        embed=discord.Embed(title="eSanta Command Groups",
                            url="https://esanta.me", 
                            color=discord.Color.dark_purple(), 
                            timestamp=datetime.datetime.now(),
                            description=f'Current server prefix: {prefix}')

        embed.set_thumbnail(url='attachment://eSanta.png')
        embed.set_footer(text=f"Requested by {ctx.author.name}")

        embed.add_field(name='**Categories**',value=f"{prefix}help moderator\n{prefix}help fun", inline=True)
        await ctx.send(file=self.eSantaLogo,embed=embed)

def 设置(客户端): client.add_cog(帮助(客户端))