T
T
TheFlamer2021-06-18 21:39:23
Python
TheFlamer, 2021-06-18 21:39:23

Command raised an exception: AttributeError: 'File' object has no attribute 'startswith' what to do?

The code:

@bot.command() #22
async def change_banner(ctx):
    file = discord.File('my_image.png')
    await ctx.send(file = file)
    await ctx.guild.edit(banner = file)  # При помощи метода read() читаем изображение, и ставим его как баннер на сервере

    os.remove(file)  # Удаляем скачанное изображение


Mistake:
Ignoring exception in command change_banner:
Traceback (most recent call last):
File "/home/container/.local/lib/python3.9/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "/home/container/bot.py", line 1923, in change_banner
await ctx.guild.edit(banner = file) # При помощи метода read() читаем изображение, и ставим его как баннер на сервере
File "/home/container/.local/lib/python3.9/site-packages/discord/guild.py", line 1186, in edit
banner = utils._bytes_to_base64_data(banner_bytes)
File "/home/container/.local/lib/python3.9/site-packages/discord/utils.py", line 323, in _bytes_to_base64_data
mime = _get_mime_type_for_image(data)
File "/home/container/.local/lib/python3.9/site-packages/discord/utils.py", line 310, in _get_mime_type_for_image
if data.startswith(b'\x89\x50\x4E\x47\x0D\x0A\x1A\x0A'):
AttributeError: 'File' object has no attribute 'startswith'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/container/.local/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/home/container/.local/lib/python3.9/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/home/container/.local/lib/python3.9/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: AttributeError: 'File' object has no attribute 'startswith'


Error in await ctx.guild.edit(banner = file)

The meaning of the command is that a banner should be placed on the server (there are 30 boosts)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
TanderOFF, 2021-06-19
@TanderOFF

@client.command(aliases=['banner'])
async def __avasstar(ctx):   
    with open('image.jpg', 'rb') as f:  #с пк берёт картинку
        icon = f.read() #читаем её
    await ctx.guild.edit(banner = icon)   #устанавливаем

V
Vindicar, 2021-06-19
@Vindicar

The docs say :

banner (bytes) – A bytes-like object representing the banner. Could be None to denote removal of the banner.

Those. as I understand it, you need to give not a file, but an array of bytes (built-in data type bytes or the like), which contains data from the desired file.
Try to open the file you are looking for as usual via open() in 'rb' mode, do a read(), and then return the result as a banner.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question