You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
4.4 KiB
130 lines
4.4 KiB
6 years ago
|
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
|
||
|
|
||
|
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] in [".png", ".jpg", ".jpeg"]
|
||
|
|
||
|
class Client(discord.Client):
|
||
|
async def on_ready(self):
|
||
|
print("Logged on as", self.user)
|
||
|
self.emoji_lock = Lock()
|
||
|
|
||
|
async def on_message(self, message):
|
||
|
if message.author == self.user:
|
||
|
return
|
||
|
|
||
|
if message.channel.name != "suggestions":
|
||
|
return
|
||
|
|
||
|
lowermsg = message.content.lower()
|
||
|
if "why" in lowermsg and ("happening" in lowermsg or "habbening" in lowermsg):
|
||
|
await message.channel.send("https://axyllia.root.sx/seedot.png")
|
||
|
|
||
|
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
|
||
|
|
||
|
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("<DEL EMOJI>", emoji.name + ":", emoji.id)
|
||
|
await emoji.delete()
|
||
|
print("[DEL EMOJI]", emoji.name + ":", 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("<DEL EMOJI>", emoji.name + ":", emoji.id)
|
||
|
await emoji.delete()
|
||
|
print("[DEL EMOJI]", emoji.name + ":", emoji.id)
|
||
|
except:
|
||
|
pass
|
||
|
|
||
|
print("<NEW EMOJI>", name + ":", image)
|
||
|
ret = await message.guild.create_custom_emoji(name=name, image=image_data)
|
||
|
if not isinstance(ret, int):
|
||
|
print("[NEW EMOJI]", name + ":", image)
|
||
|
else:
|
||
|
time = "%d minutes and %d seconds" % (ret / 1000 / 60, ret / 1000 % 60)
|
||
|
print("[RATE LIMIT] Wait", time)
|
||
|
await message.channel.send("We're being rate limited for %s 😔" % time)
|
||
|
except:
|
||
|
await message.add_reaction("⁉")
|
||
|
raise
|
||
|
|
||
|
logging.basicConfig(level=logging.WARNING)
|
||
|
client = Client()
|
||
|
client.run(open("token").read().strip())
|