aboutsummaryrefslogtreecommitdiffstats
path: root/bot/exts/halloween/spookyavatar.py
blob: 2d7df6782f2cfe82313c6259263667319322b718 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import logging
import os
from io import BytesIO

import aiohttp
import discord
from PIL import Image
from discord.ext import commands

from bot.utils.halloween import spookifications

log = logging.getLogger(__name__)


class SpookyAvatar(commands.Cog):
    """A cog that spookifies an avatar."""

    def __init__(self, bot: commands.Bot):
        self.bot = bot

    async def get(self, url: str) -> bytes:
        """Returns the contents of the supplied URL."""
        async with aiohttp.ClientSession() as session:
            async with session.get(url) as resp:
                return await resp.read()

    @commands.command(name='savatar', aliases=('spookyavatar', 'spookify'),
                      brief='Spookify an user\'s avatar.')
    async def spooky_avatar(self, ctx: commands.Context, user: discord.Member = None) -> None:
        """A command to print the user's spookified avatar."""
        if user is None:
            user = ctx.message.author

        async with ctx.typing():
            embed = discord.Embed(colour=0xFF0000)
            embed.title = "Is this you or am I just really paranoid?"
            embed.set_author(name=str(user.name), icon_url=user.avatar_url)

            image_bytes = await ctx.author.avatar_url.read()
            im = Image.open(BytesIO(image_bytes))
            modified_im = spookifications.get_random_effect(im)
            modified_im.save(str(ctx.message.id)+'.png')
            f = discord.File(str(ctx.message.id)+'.png')
            embed.set_image(url='attachment://'+str(ctx.message.id)+'.png')

        await ctx.send(file=f, embed=embed)
        os.remove(str(ctx.message.id)+'.png')


def setup(bot: commands.Bot) -> None:
    """Spooky avatar Cog load."""
    bot.add_cog(SpookyAvatar(bot))