import aiohttp


async def download_file(url, filename=None):
    """Downloads the file at the given url and then saves it under the filename given to disk.

    Params
    =======
    url: str
    filename: str (Optional)
        if not given, the function will try and determine filename from url.

    Returns
    =======
    filename: str
        Handy if no filename given
    """
    if not isinstance(url, str):
        url = str(url)
    if filename is None:
        filename = url.split("/")[-1].split("?")[0]
    async with aiohttp.ClientSession() as session:
        async with session.get(url, headers={'User-agent': 'RoxBot Discord Bot'}) as data:
            with open(filename, 'wb') as f:
                f.write(await data.read())
    return filename