dolfies / discord.py-self

A fork of the popular discord.py for user accounts.
https://discordpy-self.rtfd.io/en/latest/
MIT License
665 stars 155 forks source link

Cannot upload file with upload_files() using bytes #507

Closed Vazgen005 closed 1 year ago

Vazgen005 commented 1 year ago

Summary

title?

Reproduction Steps

test

Code

import discord
from discord.ext import commands
from io import BytesIO

# region config
TOKEN = "TOKEN"
# endregion

bot = commands.Bot(command_prefix='>', self_bot=True)

@bot.command()
async def ping(ctx):
    await ctx.send('pong')

@bot.command()
async def test(ctx):
    file_bytes = None
    with open (".\\file.webm", "rb") as fp:
        file_bytes = fp.read()
    files = await ctx.upload_files(discord.File(fp=BytesIO(file_bytes), filename=f"file.webm"))
    await ctx.send("large_file", files=files)

bot.run(TOKEN)

Expected Results

image

Actual Results

➤ py .\test.py
2023-05-10 20:07:02 INFO     discord.client Logging in using static token.
2023-05-10 20:07:03 INFO     discord.http Found user agent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36, build number 196590.
2023-05-10 20:07:04 INFO     discord.gateway Connected to Gateway (Session ID: 5a51e37a2a6ed01f76f023f79e7f0239).
2023-05-10 20:07:11 ERROR    discord.ext.commands.bot Ignoring exception in command test
Traceback (most recent call last):
  File "C:\Users\vaz05\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\utils.py", line 202, in __get__
    return getattr(instance, self.name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'File' object has no attribute '_cs_size'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\vaz05\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\ext\commands\core.py", line 236, in wrapped
    ret = await coro(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vaz05\Desktop\code\Python\selfbot-v2\test.py", line 20, in test
    files = await ctx.upload_files(discord.File(fp=BytesIO(file_bytes), filename=f"file.webm"))
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vaz05\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\abc.py", line 1609, in upload_files
    data = await self._state.http.get_attachment_urls(channel.id, [f.to_upload_dict(i) for i, f in mapped_files.items()])
                                                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vaz05\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\abc.py", line 1609, in <listcomp>
    data = await self._state.http.get_attachment_urls(channel.id, [f.to_upload_dict(i) for i, f in mapped_files.items()])
                                                                   ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vaz05\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\file.py", line 183, in to_upload_dict
    'file_size': self.size,
                 ^^^^^^^^^
  File "C:\Users\vaz05\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\utils.py", line 204, in __get__
    value = self.function(instance)
            ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vaz05\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\file.py", line 177, in size
    return os.fstat(self.fp.fileno()).st_size
                    ^^^^^^^^^^^^^^^^
io.UnsupportedOperation: fileno

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\vaz05\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\ext\commands\bot.py", line 1169, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\vaz05\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\ext\commands\core.py", line 1020, in invoke
    await injected(*ctx.args, **ctx.kwargs)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vaz05\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\ext\commands\core.py", line 245, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: UnsupportedOperation: fileno

System Information

- Python v3.11.2-final
- discord.py-self v2.1.0-alpha
    - discord.py-self metadata: v2.1.0a4695+g88958faf
- aiohttp v3.8.4
- system info: Windows 10 10.0.22621

Checklist

Additional Information

No response

Sanceilaks commented 1 year ago

rewrite it in rust

dolfies commented 1 year ago

Should be good now!