Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

Downloading PNG/GIF/MP3 files from Discord using Python Rewrite

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...