Discord bot used to dab on /rheg/ shitposters
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.

199 lines
7.6 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
from datetime import datetime, timedelta
6 years ago
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):
6 years ago
return splitext(basename(str))[1].lower() in [".png", ".jpg", ".jpeg"]
6 years ago
class Client(discord.Client):
async def on_ready(self):
print("Logged on as", self.user)
self.emoji_lock = Lock()
6 years ago
self.pin_lock = Lock()
6 years ago
self.lastime = None
6 years ago
self.lastime_doors = None
async def on_reaction_remove(self, reaction, user):
message = reaction.message
6 years ago
if message.author == self.user:
return
if message.channel.name != "suggestions":
return
6 years ago
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
6 years ago
lowermsg = message.content.lower()
6 years ago
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
6 years ago
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()
6 years ago
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
6 years ago
# 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("<DEL EMOJI>", emoji.name + ":", emoji.id)
# await emoji.delete()
# print("[DEL EMOJI]", emoji.name + ":", ("%s#%s" % (message.author.name, message.author.discriminator)) + ":", emoji.id)
# except:
# pass
6 years ago
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()
6 years ago
print("[DEL EMOJI]", emoji.name + ":", ("%s#%s" % (message.author.name, message.author.discriminator)) + ":", emoji.id)
6 years ago
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()
6 years ago
print("[DEL EMOJI]", emoji.name + ":", ("%s#%s" % (message.author.name, message.author.discriminator)) + ":", emoji.id)
6 years ago
except:
pass
print("<NEW EMOJI>", name + ":", image)
ret = await message.guild.create_custom_emoji(name=name, image=image_data)
if not isinstance(ret, int):
6 years ago
print("[NEW EMOJI]", name + ":", ("%s#%s" % (message.author.name, message.author.discriminator)) + ":", image)
6 years ago
else:
time = "%d minutes and %d seconds" % (ret / 1000 / 60, ret / 1000 % 60)
6 years ago
if ret > 60 * 60 * 1000:
time = "%d hours, %d minutes and %d seconds" % (ret / 1000 / 60 / 60, ret / 1000 / 60 % 60, ret / 1000 % 60)
6 years ago
print("[RATE LIMIT] Wait", time)
await message.channel.send("We're being rate limited by discord for %s 😔" % time)
6 years ago
except:
await message.add_reaction("")
raise
logging.basicConfig(level=logging.WARNING)
client = Client()
client.run(open("token").read().strip())