So someone helped me to download Image/Gif in Discord by using Python Rewrite (Bot)
My bot creates a backup for a server/channel.
This is my code (I understand until the portion my friend wrote)
async def history(ctx, channel_name, path, atcpath):
if channel_name == "Default":
channel_name = ctx.channel.name
files = glob.glob(f"{atcpath}/*")
for f in files:
os.remove(f)
channel = discord.utils.get(ctx.guild.channels, name=channel_name)
messages = await channel.history(limit=1000).flatten()
jsonstruct = getJson(path)
i = len(messages)
jsonstruct.clear()
for message in messages:
content = message.content
try:
if message.content == "":
jsonstruct[i] = f"."
else:
jsonstruct[i] = message.content
except:
print ("fail")
pass
#THE PORTION MY FRIEND WROTE
if (len(message.attachments) > 0):
async with aiohttp.ClientSession() as session:
url = message.attachments[0].proxy_url
async with session.get(url) as resp:
if resp.status == 200:
f_data = await resp.read()
f_path = f"{i}.png"
print (atcpath)
f = open(atcpath + "/" + f_path, mode='wb')
f.write(f_data)
f.close()
i -= 1
It is working fine, but the problem is that it downloads even the GIF file as a png.
I wanted to convert all Image types to png but not GIFs.
Even the Music files are not downloading by this code.
So my code should know what type of file it is, MP3, PNG, GIF, and then according to that name the path of the file? But I don't know how to do that as the code which is written by my friend is going over my head.
Any help would be appreciated, and sorry for my broken English.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…