|
| 1 | +import io |
| 2 | +import os |
| 3 | +import random |
| 4 | +import disnake |
| 5 | + |
| 6 | +from disnake import ApplicationCommandInteraction |
| 7 | +from disnake.ext import commands |
| 8 | +from utils.bot import OGIROID |
| 9 | + |
| 10 | +from PIL import Image, ImageDraw, ImageFont |
| 11 | +from io import BytesIO |
| 12 | +import textwrap |
| 13 | + |
| 14 | + |
| 15 | +class ImageCommands(commands.Cog, name="Image"): |
| 16 | + """Image Commands!""" |
| 17 | + |
| 18 | + def __init__(self, bot: OGIROID): |
| 19 | + self.bot = bot |
| 20 | + |
| 21 | + @commands.slash_command( |
| 22 | + name="trigger", |
| 23 | + brief="Trigger", |
| 24 | + description="For when you're feeling triggered.", |
| 25 | + ) |
| 26 | + @commands.cooldown(1, 10, commands.BucketType.user) |
| 27 | + async def triggered(self, inter: ApplicationCommandInteraction, member: disnake.Member = None): |
| 28 | + """Time to get triggered.""" |
| 29 | + if not member: |
| 30 | + member = inter.author |
| 31 | + trigImg = await self.bot.session.get(f"https://some-random-api.ml/canvas/triggered?avatar={member.display_avatar.url}") |
| 32 | + imageData = io.BytesIO(await trigImg.read()) |
| 33 | + await inter.send(file=disnake.File(imageData, "triggered.gif")) |
| 34 | + |
| 35 | + @commands.slash_command( |
| 36 | + name="sus", |
| 37 | + brief="Sus-Inator4200", |
| 38 | + description="Check if your friend is kinda ***SUS***", |
| 39 | + ) |
| 40 | + @commands.cooldown(1, 10, commands.BucketType.user) |
| 41 | + async def amongus(self, inter: ApplicationCommandInteraction, member: disnake.Member = None): |
| 42 | + """Check if your friends are sus or not""" |
| 43 | + await inter.send("Testing for sus-ness...") |
| 44 | + if not member: |
| 45 | + member = inter.author |
| 46 | + impostor = random.choice(["true", "false"]) |
| 47 | + apikey = os.getenv("SRA_API_KEY") |
| 48 | + uri = f"https://some-random-api.ml/premium/amongus?username={member.name}&avatar={member.display_avatar.url}&impostor={impostor}&key={apikey}" |
| 49 | + resp = await self.bot.session.get(uri) |
| 50 | + if 300 > resp.status >= 200: |
| 51 | + fp = io.BytesIO(await resp.read()) |
| 52 | + await inter.send(file=disnake.File(fp, "amogus.gif")) |
| 53 | + else: |
| 54 | + await inter.send("Couldnt get image :(") |
| 55 | + |
| 56 | + @commands.slash_command(name="invert", brief="invert", description="Invert the colours of your icon") |
| 57 | + @commands.cooldown(1, 5, commands.BucketType.user) |
| 58 | + async def invert(self, inter: ApplicationCommandInteraction, member: disnake.Member = None): |
| 59 | + """Invert your profile picture.""" |
| 60 | + if not member: |
| 61 | + member = inter.author |
| 62 | + trigImg = await self.bot.session.get(f"https://some-random-api.ml/canvas/invert/?avatar={member.display_avatar.url}") |
| 63 | + imageData = io.BytesIO(await trigImg.read()) |
| 64 | + await inter.send(file=disnake.File(imageData, "invert.png")) |
| 65 | + |
| 66 | + @commands.slash_command(name="pixelate", brief="pixelate", description="Turn yourself into 144p!") |
| 67 | + @commands.cooldown(1, 5, commands.BucketType.user) |
| 68 | + async def pixelate(self, inter: ApplicationCommandInteraction, member: disnake.Member = None): |
| 69 | + """Turn yourself into pixels""" |
| 70 | + if not member: |
| 71 | + member = inter.author |
| 72 | + trigImg = await self.bot.session.get(f"https://some-random-api.ml/canvas/pixelate/?avatar={member.display_avatar.url}") |
| 73 | + imageData = io.BytesIO(await trigImg.read()) |
| 74 | + await inter.send(file=disnake.File(imageData, "pixelate.png")) |
| 75 | + |
| 76 | + @commands.slash_command(name="jail", brief="jail", description="Go to jail!") |
| 77 | + @commands.cooldown(1, 5, commands.BucketType.user) |
| 78 | + async def jail(self, inter: ApplicationCommandInteraction, member: disnake.Member = None): |
| 79 | + """Go to horny jail""" |
| 80 | + if not member: |
| 81 | + member = inter.author |
| 82 | + |
| 83 | + trigImg = await self.bot.session.get(f"https://some-random-api.ml/canvas/jail?avatar={member.display_avatar.url}") |
| 84 | + imageData = io.BytesIO(await trigImg.read()) |
| 85 | + await inter.send(file=disnake.File(imageData, "jail.png")) |
| 86 | + |
| 87 | + @commands.slash_command(name="urltoqr", description="Converts a URL to a QR code.") |
| 88 | + async def urltoqr(self, inter: ApplicationCommandInteraction, url: str, size: int): |
| 89 | + url = url.replace("http://", "").replace("https://", "") |
| 90 | + qr = f"https://api.qrserver.com/v1/create-qr-code/?size={size}x{size}&data={url}" |
| 91 | + embed = disnake.Embed(title=f"URL created for: {url}", color=0xFFFFFF) |
| 92 | + embed.set_image(url=qr) |
| 93 | + embed.set_footer(text=f"Requested by: {inter.author.name}") |
| 94 | + return await inter.send(embed=embed) |
| 95 | + |
| 96 | + @staticmethod |
| 97 | + def draw_multiple_line_text(image, text, font, text_color, text_start_height): |
| 98 | + draw = ImageDraw.Draw(image) |
| 99 | + image_width, image_height = image.size |
| 100 | + y_text = text_start_height |
| 101 | + lines = textwrap.wrap(text, width=45) |
| 102 | + for line in lines: |
| 103 | + nothing1, nothing2, line_width, line_height = font.getbbox(line) |
| 104 | + # draw shadow on text |
| 105 | + draw.text(((image_width - line_width) / 2 + 2, y_text + 2), line, font=font, fill=(0, 0, 0)) |
| 106 | + draw.text(((image_width - line_width) / 2, y_text), line, font=font, fill=text_color) |
| 107 | + y_text += line_height |
| 108 | + # Return the bottom pixel of the text |
| 109 | + return y_text |
| 110 | + |
| 111 | + # Command to get information about a Quote user |
| 112 | + @commands.slash_command(name="quote", description="Generates an image with a quote and random background") |
| 113 | + async def quote(self, inter): |
| 114 | + """Generates an image with a quote and random background""" |
| 115 | + await inter.response.defer() |
| 116 | + # Use api.quotable.io/random to get a random quote |
| 117 | + main = await self.bot.session.get("https://api.quotable.io/random") |
| 118 | + data = await main.json() |
| 119 | + quote = data["content"] |
| 120 | + author = data["author"] |
| 121 | + |
| 122 | + # Use unsplash.com to get a random image |
| 123 | + resolution = "1080x1080" |
| 124 | + response = await self.bot.session.get(f"https://source.unsplash.com/random/{resolution}") |
| 125 | + image_bytes = io.BytesIO(await response.read()) |
| 126 | + image = Image.open(image_bytes) |
| 127 | + |
| 128 | + draw = ImageDraw.Draw(image) |
| 129 | + font = ImageFont.truetype("utils/data/Roboto-Italic.ttf", 50) |
| 130 | + font2 = ImageFont.truetype("utils/data/Roboto-Bold.ttf", 50) |
| 131 | + if len(quote) > 350: |
| 132 | + text_start_height = (image.height - font.getbbox(quote)[3]) / 2 - 500 |
| 133 | + elif len(quote) > 250: |
| 134 | + text_start_height = (image.height - font.getbbox(quote)[3]) / 2 - 200 |
| 135 | + elif len(quote) > 150: |
| 136 | + text_start_height = (image.height - font.getbbox(quote)[3]) / 2 - 50 |
| 137 | + else: |
| 138 | + text_start_height = (image.height - font.getbbox(quote)[3]) / 2 |
| 139 | + end = self.draw_multiple_line_text(image, quote, font, text_color=(255, 255, 255), text_start_height=text_start_height) |
| 140 | + # Draw the author shadow |
| 141 | + draw.text(((image.width - font2.getbbox(author)[2]) / 2 + 2, end + 50), author, font=font2, fill=(0, 0, 0)) |
| 142 | + # Draw the author |
| 143 | + draw.text(((image.width - font2.getbbox(author)[2]) / 2, end + 50), author, font=font2, fill=(255, 255, 255)) |
| 144 | + with BytesIO() as image_binary: |
| 145 | + image.save(image_binary, "PNG") |
| 146 | + image_binary.seek(0) |
| 147 | + await inter.send(file=disnake.File(fp=image_binary, filename="image.png")) |
| 148 | + |
| 149 | + |
| 150 | +def setup(bot): |
| 151 | + bot.add_cog(ImageCommands(bot)) |
0 commit comments