Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

Hi! I have a separate help command for my discord bot.

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))

When I run it first time it works completly normal.

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But when I try to run it again this exception is raised.

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

>Solution :

From the documentation:

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

You should reload the file once it’s sent, a pretty solution:

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 setup(client):
client.add_cog(Help(client))

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading