import discord import logging import aiohttp from os import listdir from os.path import splitext, basename from urllib.parse import urlparse from asyncio import Lock from datetime import datetime, timedelta def url(str): try: parse = urlparse(str) if not parse.scheme or not parse.netloc or not parse.path: return None return parse except: return None def checkext(str): return splitext(basename(str))[1].lower() in [".png", ".jpg", ".jpeg"] class Client(discord.Client): async def on_ready(self): print("Logged on as", self.user) self.emoji_lock = Lock() self.pin_lock = Lock() self.lastime = None self.lastime_doors = None async def on_reaction_remove(self, reaction, user): message = reaction.message if message.author == self.user: return if message.channel.name != "suggestions": return if reaction.emoji == "📌": print("[UNPIN]", ("%s#%s" % (user.name, user.discriminator)) + ":", message.id) async def on_reaction_add(self, reaction, user): message = reaction.message if message.author == self.user: return if message.channel.name != "suggestions": return if reaction.emoji == "📌": print("[PIN]", ("%s#%s" % (user.name, user.discriminator)) + ":", message.id) if reaction.count >= 4: async with self.pin_lock: chan_pins = await message.channel.pins() if len(chan_pins) >= 50: pins = [] for pin in chan_pins: for react in pin.reactions: if react.emoji == "📌": pins.append((pin, react.count)) pins.sort(key=lambda x: x[1], reverse=True) await pins[0][0].unpin() print("[PINNED]", ("%s#%s" % (message.author.name, message.author.discriminator)) + ":", message.id) await message.pin() async def on_message(self, message): if isinstance(message.channel, discord.DMChannel): print("[DM]", ("%s#%s" % (message.author.name, message.author.discriminator)) + ":", message.content) return if message.author == self.user: return lowermsg = message.content.lower() if "doors" in lowermsg or "🚪🚪" in lowermsg: if not self.lastime_doors or self.lastime + timedelta(minutes=5) < datetime.now(): await message.channel.send("End your fucking life, you worthless piece of shit.") self.lastime_doors = datetime.now() if message.channel.name != "suggestions": return if "why" in lowermsg and ("happening" in lowermsg or "habbening" in lowermsg): if not self.lastime or self.lastime + timedelta(hours=1) < datetime.now(): await message.channel.send("https://i.imgur.com/SFRw7aJ.png") # seedot.png self.lastime = datetime.now() name = None image_name = None image = None remove = [] if message.attachments: for attach in message.attachments: if not checkext(attach.url): continue image_name = splitext(attach.filename.replace(" ", "_"))[0] image = attach.url break for part in message.content.split(): if not name and part[0] == ":" and part[-1] == ":": name = part[1:-1] elif part.startswith("<:") and part.endswith(">") and len(part) >= 20 and part[-20] == ":": if not name: name = part[2:-20] try: remove.append(int(part[-19:-1])) except: pass elif not image: parsed = url(part) if not parsed or not checkext(parsed.path): continue image_name = splitext(basename(parsed.path))[0] image = part # if image and remove: # async with self.emoji_lock: # for emoji in message.guild.emojis: # if len(message.guild.emojis) <= 35: # break # # if emoji.id in remove: # if emoji.id == remove[0]: # try: # print("", emoji.name + ":", emoji.id) # await emoji.delete() # print("[DEL EMOJI]", emoji.name + ":", ("%s#%s" % (message.author.name, message.author.discriminator)) + ":", emoji.id) # except: # pass async with self.emoji_lock: for emoji in message.guild.emojis: if len(message.guild.emojis) <= 35: break if emoji.id in remove: try: print("", emoji.name + ":", emoji.id) await emoji.delete() print("[DEL EMOJI]", emoji.name + ":", ("%s#%s" % (message.author.name, message.author.discriminator)) + ":", emoji.id) except: pass if not image: return if not name: name = image_name if len(name) > 32 or len(name) < 2: await message.add_reaction("⁉") return try: async with aiohttp.ClientSession() as session: async with session.get(image) as resp: if resp.content_type not in ["image/png", "image/jpeg"]: return image_data = bytes() while True: new_data = await resp.content.read(1024) if not new_data: break image_data += new_data if len(image_data) >= 256 * 1024: await message.add_reaction("⁉") return async with self.emoji_lock: if len(message.guild.emojis) >= 50: emoji = sorted(message.guild.emojis, key=lambda x: x.created_at)[0] try: print("", emoji.name + ":", emoji.id) await emoji.delete() print("[DEL EMOJI]", emoji.name + ":", ("%s#%s" % (message.author.name, message.author.discriminator)) + ":", emoji.id) except: pass print("", name + ":", image) ret = await message.guild.create_custom_emoji(name=name, image=image_data) if not isinstance(ret, int): print("[NEW EMOJI]", name + ":", ("%s#%s" % (message.author.name, message.author.discriminator)) + ":", image) else: time = "%d minutes and %d seconds" % (ret / 1000 / 60, ret / 1000 % 60) if ret > 60 * 60 * 1000: time = "%d hours, %d minutes and %d seconds" % (ret / 1000 / 60 / 60, ret / 1000 / 60 % 60, ret / 1000 % 60) print("[RATE LIMIT] Wait", time) await message.channel.send("We're being rate limited by discord for %s 😔" % time) except: await message.add_reaction("⁉") raise logging.basicConfig(level=logging.WARNING) client = Client() client.run(open("token").read().strip())