-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.py
42 lines (31 loc) · 1.03 KB
/
files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""Helper script with file functions."""
import os
import aiohttp
import aiofiles
import discord
import logs
def load_files():
global loaded_files
loaded_files = os.listdir("drive")
logs.debug(f"Loaded files:\n{loaded_files}")
load_files()
async def download_file(file: discord.Attachment) -> bytes:
async with aiohttp.ClientSession() as session:
async with session.get(file.url) as resp:
if resp.status == 200:
return await resp.read()
else:
return None
async def save_file(filename: str, file_bytes: bytes):
async with aiofiles.open(f"drive/{filename}", "wb") as f:
await f.write(file_bytes)
async def read_file(filename: str) -> str:
try:
async with aiofiles.open(f"drive/{filename}", "r") as f:
return await f.read()
except FileNotFoundError:
return None
async def get_files(ctx: discord.AutocompleteContext) -> list:
return loaded_files
async def remove_file(filename: str):
os.remove(f"drive/{filename}")