Beispiel #1
0
    async def clippy(self, ctx, *, text: str = ""):
        """I *know* you wanted some help with something - what was it?"""
        image = Image.open('images/clippy.png')
        image_size = image.size
        image_height = image.size[1]
        image_width = image.size[0]
        draw = ImageDraw.Draw(image)

        text = Nullify.resolve_mentions(text, ctx=ctx, escape=False)
        # Remove any non-ascii chars
        text = ''.join([i for i in text if ord(i) < 128])

        clippy_errors = [
            "I guess I couldn't print that... whatever it was.",
            "It looks like you're trying to break things!  Maybe I can help.",
            "Whoops, I guess I wasn't coded to understand that.",
            "After filtering your input, I've come up with... well... nothing.",
            "Nope.", "y u du dis to clippy :("
        ]

        if not len(text):
            text = random.choice(clippy_errors)

        for xs in range(30, 2, -1):
            font = ImageFont.truetype('fonts/comic.ttf', size=xs)
            #340 is the width we want to set the image width to
            lines = self.text_wrap(text, font, 340)
            line_height = font.getsize('hg')[1]
            (x, y) = (25, 20)
            color = 'rgb(0, 0, 0)'  # black color
            text_size = draw.textsize(text, font=font)

            for line in lines:
                text_size = draw.textsize(line, font=font)
                image_x = (image_width / 2) - (text_size[0] / 2)
                draw.text((image_x, y), line, fill=color, font=font)
                y = y + line_height
            if y < 182:  # Check if the text overlaps. 182 was found by trial and error.
                image = Image.open('images/clippy.png')
                draw = ImageDraw.Draw(image)
                (x, y) = (25, 20)
                for line in lines:
                    text_size = draw.textsize(line, font=font)
                    image_x = (image_width / 2) - (text_size[0] / 2)
                    draw.text((image_x, y), line, fill=color, font=font)
                    y = y + line_height
                image.save('images/clippynow.png')
                await ctx.send(file=discord.File(fp='images/clippynow.png'))

                # Remove the png
                os.remove("images/clippynow.png")
                break