From e9a2640d4e870aa67b71fc96d49745b65641e024 Mon Sep 17 00:00:00 2001 From: hundredrab Date: Sun, 21 Oct 2018 22:18:21 +0530 Subject: Add cog for avatar inversion. --- bot/cogs/spookyavatar.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 bot/cogs/spookyavatar.py (limited to 'bot/cogs') diff --git a/bot/cogs/spookyavatar.py b/bot/cogs/spookyavatar.py new file mode 100644 index 00000000..11f4705c --- /dev/null +++ b/bot/cogs/spookyavatar.py @@ -0,0 +1,45 @@ +from discord.ext import commands +import discord +import aiohttp +from PIL import ImageOps +from PIL import Image +from io import BytesIO + + +class SpookyAvatar: + + """ + A cog that spookifies an avatar. + """ + + def __init__(self, bot): + self.bot = bot + + async def get(self, 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 repository(self, ctx, user: discord.Member=None): + """ + A command to print the user's spookified avatar. + """ + if user is None: + user = ctx.message.author + + 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) + resp = await self.get(user.avatar_url) + im = Image.open(BytesIO(resp)) + im = im.convert('RGB') + inv = ImageOps.invert(im) + inv.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) + + +def setup(bot): + bot.add_cog(SpookyAvatar(bot)) -- cgit v1.2.3 From 138bb28e9d1cfef8cab48d216e550efafcd85454 Mon Sep 17 00:00:00 2001 From: hundredrab Date: Sun, 21 Oct 2018 22:52:15 +0530 Subject: Fix issues related to spookyavatar cog. --- bot/cogs/spookyavatar.py | 18 +++++++++++------- bot/resources/spookifications.py | 7 +++++++ 2 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 bot/resources/spookifications.py (limited to 'bot/cogs') diff --git a/bot/cogs/spookyavatar.py b/bot/cogs/spookyavatar.py index 11f4705c..d06b259a 100644 --- a/bot/cogs/spookyavatar.py +++ b/bot/cogs/spookyavatar.py @@ -1,9 +1,11 @@ +import aiohttp +from io import BytesIO + from discord.ext import commands import discord -import aiohttp -from PIL import ImageOps from PIL import Image -from io import BytesIO + +from bot.resources import spookifications class SpookyAvatar: @@ -16,12 +18,15 @@ class SpookyAvatar: self.bot = bot async def get(self, url): + """ + 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 repository(self, ctx, user: discord.Member=None): + async def spookyavatar(self, ctx, user: discord.Member=None): """ A command to print the user's spookified avatar. """ @@ -33,9 +38,8 @@ class SpookyAvatar: embed.set_author(name=str(user.name), icon_url=user.avatar_url) resp = await self.get(user.avatar_url) im = Image.open(BytesIO(resp)) - im = im.convert('RGB') - inv = ImageOps.invert(im) - inv.save(str(ctx.message.id)+'.png') + modified_im = spookifications.inversion(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) diff --git a/bot/resources/spookifications.py b/bot/resources/spookifications.py new file mode 100644 index 00000000..aeb8388b --- /dev/null +++ b/bot/resources/spookifications.py @@ -0,0 +1,7 @@ +from PIL import ImageOps + + +def inversion(im): + im = im.convert('RGB') + inv = ImageOps.invert(im) + return inv -- cgit v1.2.3 From 595ea5439ec727285a5e9d9718fa56a24c15e0ff Mon Sep 17 00:00:00 2001 From: hundredrab Date: Sun, 21 Oct 2018 22:57:43 +0530 Subject: Fix formatting and add docstrings. --- bot/cogs/spookyavatar.py | 3 ++- bot/resources/spookifications.py | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'bot/cogs') diff --git a/bot/cogs/spookyavatar.py b/bot/cogs/spookyavatar.py index d06b259a..b11c72ee 100644 --- a/bot/cogs/spookyavatar.py +++ b/bot/cogs/spookyavatar.py @@ -25,7 +25,8 @@ class SpookyAvatar: async with session.get(url) as resp: return await resp.read() - @commands.command(name='savatar', aliases=['spookyavatar', 'spookify'], brief='Spookify an user\'s avatar.') + @commands.command(name='savatar', aliases=['spookyavatar', 'spookify'], + brief='Spookify an user\'s avatar.') async def spookyavatar(self, ctx, user: discord.Member=None): """ A command to print the user's spookified avatar. diff --git a/bot/resources/spookifications.py b/bot/resources/spookifications.py index aeb8388b..880b24e7 100644 --- a/bot/resources/spookifications.py +++ b/bot/resources/spookifications.py @@ -2,6 +2,10 @@ from PIL import ImageOps def inversion(im): + """Inverts an image. + + Returns an inverted image when supplied with an Image object. + """ im = im.convert('RGB') inv = ImageOps.invert(im) return inv -- cgit v1.2.3 From b54d59176e904cdbde1291d73570cf8c86175a09 Mon Sep 17 00:00:00 2001 From: hundredrab Date: Mon, 22 Oct 2018 11:49:26 +0530 Subject: Add more spookifications. --- bot/cogs/spookyavatar.py | 2 +- bot/resources/bat-clipart.png | Bin 0 -> 12313 bytes bot/resources/bloody-pentagram.png | Bin 0 -> 7006 bytes bot/resources/spookifications.py | 42 +++++++++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 bot/resources/bat-clipart.png create mode 100644 bot/resources/bloody-pentagram.png (limited to 'bot/cogs') diff --git a/bot/cogs/spookyavatar.py b/bot/cogs/spookyavatar.py index b11c72ee..9a73a019 100644 --- a/bot/cogs/spookyavatar.py +++ b/bot/cogs/spookyavatar.py @@ -39,7 +39,7 @@ class SpookyAvatar: embed.set_author(name=str(user.name), icon_url=user.avatar_url) resp = await self.get(user.avatar_url) im = Image.open(BytesIO(resp)) - modified_im = spookifications.inversion(im) + 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') diff --git a/bot/resources/bat-clipart.png b/bot/resources/bat-clipart.png new file mode 100644 index 00000000..7df26ba9 Binary files /dev/null and b/bot/resources/bat-clipart.png differ diff --git a/bot/resources/bloody-pentagram.png b/bot/resources/bloody-pentagram.png new file mode 100644 index 00000000..4e6da07a Binary files /dev/null and b/bot/resources/bloody-pentagram.png differ diff --git a/bot/resources/spookifications.py b/bot/resources/spookifications.py index 880b24e7..43e8b038 100644 --- a/bot/resources/spookifications.py +++ b/bot/resources/spookifications.py @@ -1,5 +1,11 @@ +import logging +from random import choice, randint + +from PIL import Image from PIL import ImageOps +log = logging.getLogger() + def inversion(im): """Inverts an image. @@ -9,3 +15,39 @@ def inversion(im): im = im.convert('RGB') inv = ImageOps.invert(im) return inv + + +def pentagram(im): + """Adds pentagram to image.""" + im = im.convert('RGB') + wt, ht = im.size + penta = Image.open('bot/resources/bloody-pentagram.png') + penta = penta.resize((wt, ht)) + im.paste(penta, (0, 0), penta) + return im + + +def bat(im): + """Adds a bat silhoutte to the image. + + The bat silhoutte is of a size at least one-fifths that of the original + image and may be rotated upto 90 degrees anti-clockwise.""" + im = im.convert('RGB') + wt, ht = im.size + bat = Image.open('bot/resources/bat-clipart.png') + bat_size = randint(wt//5, wt) + rot = randint(0, 90) + bat = bat.resize((bat_size, bat_size)) + bat = bat.rotate(rot) + x = randint(0, wt-bat_size) + y = randint(0, wt-bat_size) + im.paste(bat, (x, y), bat) + return im + + +def get_random_effect(im): + """Randomly selects and applies an effect.""" + effects = [inversion, pentagram, bat] + effect = choice(effects) + log.info("Spookyavatar's chosen effect:" + str(effect)) + return effect(im) -- cgit v1.2.3 From 75d9ba2bad71f4423d72197d408d1e61282ebeb2 Mon Sep 17 00:00:00 2001 From: hundredrab Date: Fri, 26 Oct 2018 12:59:33 +0530 Subject: Various minor fixes. --- bot/__main__.py | 2 +- bot/cogs/spookyavatar.py | 6 ++++-- bot/resources/spookifications.py | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) (limited to 'bot/cogs') diff --git a/bot/__main__.py b/bot/__main__.py index ccd69b0b..f218443a 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -5,7 +5,7 @@ from traceback import format_exc from discord.ext import commands -HACKTOBERBOT_TOKEN = environ.get('HACKTOBERBOT_TOKEN') +HACKTOBERBOT_TOKEN = 'NDk5NjAzOTk1MzkyODY4MzU1.Dq3ITw.F0f45KYcZN1NgAyPIbrsgoz49Yo'#environ.get('HACKTOBERBOT_TOKEN') log = logging.getLogger() if HACKTOBERBOT_TOKEN: diff --git a/bot/cogs/spookyavatar.py b/bot/cogs/spookyavatar.py index 9a73a019..8a424b1f 100644 --- a/bot/cogs/spookyavatar.py +++ b/bot/cogs/spookyavatar.py @@ -1,8 +1,9 @@ -import aiohttp from io import BytesIO +import os -from discord.ext import commands +import aiohttp import discord +from discord.ext import commands from PIL import Image from bot.resources import spookifications @@ -44,6 +45,7 @@ class SpookyAvatar: 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): diff --git a/bot/resources/spookifications.py b/bot/resources/spookifications.py index 43e8b038..2211049f 100644 --- a/bot/resources/spookifications.py +++ b/bot/resources/spookifications.py @@ -49,5 +49,5 @@ def get_random_effect(im): """Randomly selects and applies an effect.""" effects = [inversion, pentagram, bat] effect = choice(effects) - log.info("Spookyavatar's chosen effect:" + str(effect)) + log.info("Spookyavatar's chosen effect: " + effect.__name__) return effect(im) -- cgit v1.2.3 From 0f506a7636c98409eae88f1ec44d785503309ff9 Mon Sep 17 00:00:00 2001 From: hundredrab Date: Fri, 26 Oct 2018 18:39:29 +0530 Subject: Add whitespace. --- bot/cogs/spookyavatar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/cogs') diff --git a/bot/cogs/spookyavatar.py b/bot/cogs/spookyavatar.py index 8a424b1f..691be1b1 100644 --- a/bot/cogs/spookyavatar.py +++ b/bot/cogs/spookyavatar.py @@ -28,7 +28,7 @@ class SpookyAvatar: @commands.command(name='savatar', aliases=['spookyavatar', 'spookify'], brief='Spookify an user\'s avatar.') - async def spookyavatar(self, ctx, user: discord.Member=None): + async def spookyavatar(self, ctx, user: discord.Member = None): """ A command to print the user's spookified avatar. """ -- cgit v1.2.3 From 7ac3946023f48dfebeb0335f66421773cde2d900 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Tue, 20 Nov 2018 13:30:38 +0100 Subject: Cleaning up bats a bit --- bot/cogs/hacktober/candy_collection.py | 4 ++-- bot/cogs/resources/candy_collection.json | 8 -------- bot/constants.py | 1 + bot/resources/halloween/candy_collection.json | 8 ++++++++ bot/utils/spookifications.py | 12 +++++++----- 5 files changed, 18 insertions(+), 15 deletions(-) delete mode 100644 bot/cogs/resources/candy_collection.json create mode 100644 bot/resources/halloween/candy_collection.json (limited to 'bot/cogs') diff --git a/bot/cogs/hacktober/candy_collection.py b/bot/cogs/hacktober/candy_collection.py index 59eadd93..508793a2 100644 --- a/bot/cogs/hacktober/candy_collection.py +++ b/bot/cogs/hacktober/candy_collection.py @@ -5,9 +5,9 @@ import json import functools import os -json_location = os.path.join(os.getcwd(), 'resources', 'candy_collection.json') +from bot.constants import HACKTOBER_CHANNEL_ID -HACKTOBER_CHANNEL_ID = 498804484324196362 +json_location = os.path.join("bot", "resources", "halloween", "candy_collection.json") # chance is 1 in x range, so 1 in 20 range would give 5% chance (for add candy) ADD_CANDY_REACTION_CHANCE = 20 # 5% diff --git a/bot/cogs/resources/candy_collection.json b/bot/cogs/resources/candy_collection.json deleted file mode 100644 index 6313dd10..00000000 --- a/bot/cogs/resources/candy_collection.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "msg_reacted": [ - - ], - "records": [ - - ] -} diff --git a/bot/constants.py b/bot/constants.py index e69de29b..7d00a360 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -0,0 +1 @@ +HACKTOBER_CHANNEL_ID = 498804484324196362 diff --git a/bot/resources/halloween/candy_collection.json b/bot/resources/halloween/candy_collection.json new file mode 100644 index 00000000..6313dd10 --- /dev/null +++ b/bot/resources/halloween/candy_collection.json @@ -0,0 +1,8 @@ +{ + "msg_reacted": [ + + ], + "records": [ + + ] +} diff --git a/bot/utils/spookifications.py b/bot/utils/spookifications.py index 2211049f..5f2369ae 100644 --- a/bot/utils/spookifications.py +++ b/bot/utils/spookifications.py @@ -21,7 +21,7 @@ def pentagram(im): """Adds pentagram to image.""" im = im.convert('RGB') wt, ht = im.size - penta = Image.open('bot/resources/bloody-pentagram.png') + penta = Image.open('bot/resources/halloween/bloody-pentagram.png') penta = penta.resize((wt, ht)) im.paste(penta, (0, 0), penta) return im @@ -34,14 +34,16 @@ def bat(im): image and may be rotated upto 90 degrees anti-clockwise.""" im = im.convert('RGB') wt, ht = im.size - bat = Image.open('bot/resources/bat-clipart.png') - bat_size = randint(wt//5, wt) + bat = Image.open('bot/resources/halloween/bat-clipart.png') + bat_size = randint(wt//10, wt//7) rot = randint(0, 90) bat = bat.resize((bat_size, bat_size)) bat = bat.rotate(rot) - x = randint(0, wt-bat_size) - y = randint(0, wt-bat_size) + x = randint(wt-(bat_size * 3), wt-bat_size) + y = randint(10, bat_size) im.paste(bat, (x, y), bat) + im.paste(bat, (x + bat_size, y + (bat_size // 4)), bat) + im.paste(bat, (x - bat_size, y - (bat_size // 2)), bat) return im -- cgit v1.2.3 From f11c7cf7e4b0d0dc8e6f7dabb835795588e9eecb Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Tue, 20 Nov 2018 13:35:45 +0100 Subject: flake8 --- bot/cogs/hacktober/candy_collection.py | 13 +++++++------ bot/cogs/hacktober/monstersurvey.py | 5 ++--- bot/cogs/hacktober/spookyavatar.py | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) (limited to 'bot/cogs') diff --git a/bot/cogs/hacktober/candy_collection.py b/bot/cogs/hacktober/candy_collection.py index 508793a2..f5f17abb 100644 --- a/bot/cogs/hacktober/candy_collection.py +++ b/bot/cogs/hacktober/candy_collection.py @@ -1,9 +1,10 @@ -import discord -from discord.ext import commands -import random -import json import functools +import json import os +import random + +import discord +from discord.ext import commands from bot.constants import HACKTOBER_CHANNEL_ID @@ -126,7 +127,7 @@ class CandyCollection: for i in range(9): o = discord.Object(id=recent_msg.id + i) - msg = await channel.history(limit=1, before=o).next() + msg = await next(channel.history(limit=1, before=o)) ten_recent.append(msg.id) return ten_recent @@ -140,7 +141,7 @@ class CandyCollection: o = discord.Object(id=msg_id + 1) # Use history rather than get_message due to # poor ratelimit (50/1s vs 1/1s) - msg = await self.hacktober_channel.history(limit=1, before=o).next() + msg = await next(self.hacktober_channel.history(limit=1, before=o)) if msg.id != msg_id: return None diff --git a/bot/cogs/hacktober/monstersurvey.py b/bot/cogs/hacktober/monstersurvey.py index 9f33e31b..45587fe1 100644 --- a/bot/cogs/hacktober/monstersurvey.py +++ b/bot/cogs/hacktober/monstersurvey.py @@ -1,7 +1,6 @@ import json import logging import os -from typing import Optional, Union from discord import Embed from discord.ext import commands @@ -91,7 +90,7 @@ class MonsterSurvey: @monster_group.command( name='vote' ) - async def monster_vote(self, ctx: Context, name = None): + async def monster_vote(self, ctx: Context, name=None): """Casts a vote for a particular monster, or displays a list of monsters that can be voted for if one is not given.""" if name is None: @@ -133,7 +132,7 @@ class MonsterSurvey: @monster_group.command( name='show' ) - async def monster_show(self, ctx: Context, name = None): + async def monster_show(self, ctx: Context, name=None): """ Shows the named monster. If one is not named, it sends the default voting embed instead. :param ctx: diff --git a/bot/cogs/hacktober/spookyavatar.py b/bot/cogs/hacktober/spookyavatar.py index 694eb317..ad8a9242 100644 --- a/bot/cogs/hacktober/spookyavatar.py +++ b/bot/cogs/hacktober/spookyavatar.py @@ -1,5 +1,5 @@ -from io import BytesIO import os +from io import BytesIO import aiohttp import discord -- cgit v1.2.3