Skip to content

Instantly share code, notes, and snippets.

@CortexPE
Created May 22, 2019 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CortexPE/60ea32c0683a85811b1830db606e5f30 to your computer and use it in GitHub Desktop.
Save CortexPE/60ea32c0683a85811b1830db606e5f30 to your computer and use it in GitHub Desktop.
Prevents flashbangs (very bright images) to be uploaded to discord servers
import aiohttp
import discord
from datetime import datetime
from io import BytesIO
from PIL import Image
from PIL import ImageStat
TOKEN = "YOUR DISCORD TOKEN HERE"
THRESHOLD = 192 # brightness threshold... How close it is to being fully white
IMAGE_SIZE = 500 # miminum image size
class AntiFlashbang(discord.Client):
async def on_ready(self):
print("Logged in as")
print(self.user.name)
print(self.user.id)
print("Invitation Link: https://discordapp.com/oauth2/authorize?client_id={}&scope=bot&permissions=8192".format(self.user.id))
print("------")
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="flashbangs"))
async def on_message(self, message):
if message.author.id == self.user.id or message.author.bot or message.guild is None:
return
if len(message.attachments) > 0:
url = message.attachments[-1].url
async with aiohttp.ClientSession() as cs:
async with cs.get(url, headers={
"User-agent": "Anti-Flashbang v1.0"
}) as r:
if r.status in range(200, 209):
if r.headers["Content-Type"].split("/")[0] == "image":
img = Image.open(BytesIO(await r.read())).convert("L")
size = max(img.size[0], img.size[1])
if size > IMAGE_SIZE:
stat = ImageStat.Stat(img)
if stat.mean[0] > THRESHOLD:
print("[{}] NEUTRALIZED FLASHBANG FROM @{}#{} at {}#{}".format(datetime.now(), message.author.name, message.author.discriminator, message.guild.name, message.channel.name))
await message.delete()
await message.channel.send(":warning: | **No Flashbangs** {}".format(message.author.mention), delete_after=10.0)
await cs.close()
client = AntiFlashbang()
client.run(TOKEN)
discord.py
pillow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment