Skip to content

File

attrs class File

Representation of a file.

Used for sending files to discord.

Attr attributes:

Name Type Description
file Union[IOBase, BinaryIO, Path, str]

Location of file to send or the bytes.

file_name Optional[str]

Set a filename that will be displayed when uploaded to discord. If you leave this empty, the file will be called file by default

Source code in naff/models/discord/file.py
@define(kw_only=False)
class File:
    """
    Representation of a file.

    Used for sending files to discord.

    """

    file: Union["IOBase", BinaryIO, "Path", str] = field(repr=True)
    """Location of file to send or the bytes."""
    file_name: Optional[str] = field(repr=True, default=None)
    """Set a filename that will be displayed when uploaded to discord. If you leave this empty, the file will be called `file` by default"""

    def open_file(self) -> BinaryIO:
        """
        Opens the file.

        Returns:
            A file-like BinaryIO object.

        """
        if isinstance(self.file, (IOBase, BinaryIO)):
            return self.file
        else:
            return open(str(self.file), "rb")

method open_file(self)

Opens the file.

Returns:

Type Description
BinaryIO

A file-like BinaryIO object.

Source code in naff/models/discord/file.py
def open_file(self) -> BinaryIO:
    """
    Opens the file.

    Returns:
        A file-like BinaryIO object.

    """
    if isinstance(self.file, (IOBase, BinaryIO)):
        return self.file
    else:
        return open(str(self.file), "rb")

function open_file(file)

Opens the file.

Parameters:

Name Type Description Default
file Union[naff.models.discord.file.File, io.IOBase, BinaryIO, pathlib.Path, str]

The target file or path to file.

required

Returns:

Type Description
BinaryIO

A file-like BinaryIO object.

Source code in naff/models/discord/file.py
def open_file(file: UPLOADABLE_TYPE) -> BinaryIO:
    """
    Opens the file.

    Args:
        file: The target file or path to file.

    Returns:
        A file-like BinaryIO object.

    """
    match file:
        case File():
            return file.open_file()
        case IOBase() | BinaryIO():
            return file
        case Path() | str():
            return open(str(file), "rb")
        case _:
            raise ValueError(f"{file} is not a valid file")