Last active
March 3, 2018 02:40
-
-
Save Vexs/8d20bfcdbdd0ce041f8c170b6a90727b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from discord.ext import commands | |
from PIL import Image, ImageDraw, ImageFont | |
import aiohttp | |
import io | |
import re | |
@commands.command(pass_context=True) | |
async def angry(self, ctx, *, string='missing\n arguments'): | |
def make_angry(avatar_bytes, text): | |
avatar = Image.open(avatar_bytes).convert('RGBA') | |
angry_base = Image.open('assets/angry.png').convert('RGBA') | |
avatar = avatar.resize((62, 62), resample=3) | |
angry_base.paste(avatar, box=(14, 14)) | |
fnt = ImageFont.truetype('assets/LeviWindows.ttf', 22) | |
d = ImageDraw.Draw(angry_base) | |
d.fontmode = "1" # This turns AA off on text. Undocumented. | |
d.multiline_text((109, -1), "I am angry\n ANGRY ABOUT {}".format(text.upper()), | |
fill=(0, 0, 0), font=fnt, spacing=-5) | |
image_file_object = io.BytesIO() | |
angry_base.save(image_file_object, format='png') | |
image_file_object.seek(0) | |
avatar.close() | |
angry_base.close() | |
return image_file_object | |
# Hacky method of allowing optional arguments, IE, the first person mentioned's avatar is used | |
if not ctx.message.mentions: | |
avatar_url = ctx.message.author.avatar_url | |
else: | |
avatar_url = ctx.message.mentions[0].avatar_url | |
string = re.sub(r"<@!?[0-9]*> ?", '', string) | |
with aiohttp.ClientSession() as session: | |
async with session.get(avatar_url) as resp: | |
avy_bytes = io.BytesIO(await resp.read()) | |
# Gotta run it in executor if we don't want to clog up the bot | |
angry_image = await self.bot.loop.run_in_executor(None, make_angry, avy_bytes, string) | |
await self.bot.send_file(ctx.message.channel, fp=angry_image, filename='angry.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
0/10 never again. @Vex