Skip to content

Instantly share code, notes, and snippets.

@laundmo
Last active August 15, 2020 20:59
Show Gist options
  • Save laundmo/0dcd99732692fdecc93f310e03a0721a to your computer and use it in GitHub Desktop.
Save laundmo/0dcd99732692fdecc93f310e03a0721a to your computer and use it in GitHub Desktop.
How to detect APNGs sent in Discord using discord.py
import io
def is_apng(a: bytes) -> bool:
acTL = a.find(b"\x61\x63\x54\x4C")
if acTL > 0:
iDAT = a.find(b"\x49\x44\x41\x54")
if acTL < iDAT:
return True
return False
async def message_contains_apng(message: discord.Message) -> bool:
for embed in message.embeds:
if embed.type == "image":
async with aiohttp.ClientSession() as session:
async with session.get(embed.url) as r:
try:
raw = await r.read()
if is_apng(raw):
return True
except Exception as e:
print(e)
return False
for a in message.attachments:
f = io.BytesIO()
await a.save(f)
if is_apng(f.read()):
return True
return False

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to http://unlicense.org/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment