MemeCog

Bases: Cog

Class that holds meme commands This class extends commands.Cog from discord.

Parameters:
  • bot

    Discord API client

  • logger

    Logger object for logging purposes

Attributes:
  • bot

    Discord API client

  • logger

    Logger object for logging purposes

  • hidden (bool) –

    Attribute that determines if this list of command should show in the help command or not. If false, will show in help.

  • __cog_name__ (str) –

    Command designation for the help command

Source code in /Users/caoma/Documents/Programming/GIT/SkyWizz/skywizz/cogs/meme_commands.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
class MemeCog(commands.Cog):
    """
        Class that holds meme commands
        This class extends `commands.Cog` from discord.

        Args:
            bot: Discord API client
            logger: Logger object for logging purposes

        Attributes:
            bot: Discord API client
            logger: Logger object for logging purposes
            hidden (bool): Attribute that determines if this list of
                     command should show in the help command or not.
                     If `false`, will show in help.
            __cog_name__ (str): Command designation for the help command
    """

    def __init__(self, bot, logger):
        self.bot = bot
        self.logger = logger
        self.hidden = False
        self.__cog_name__ = 'Meme Commands'
        self.logger.info(f"Loaded {self.__cog_name__}")

    @commands.cooldown(1, 60, commands.BucketType.user)
    @commands.command(name='caption')
    async def caption(self, ctx, caption_text):
        """
        Command that places a caption from a given image
        Attention: You must send an image along with the command!

        Args:
            caption_text (str): Desired caption of the image

        Example:
            `!caption "Hello World"`

        Usage:
            `caption <image_caption>`
        """
        # Must have caption text
        if not caption_text:
            embed = skywizz.specific_error("Please include some caption "
                                           "text after the `!caption` "
                                           "command. "
                                           "For example "
                                           "`!caption \"Hello world!\"")
            await ctx.message.reply(embed=embed)
            return

        # Must have a file attached
        if ctx.message.attachments:
            image_url = ctx.message.attachments[0].url
        else:
            embed = skywizz.specific_error('Please attach '
                                           'an image for me to caption.')
            await ctx.message.reply(embed=embed)
            return

        # File must be an image
        if mimetypes.guess_type(image_url)[0] not in SUPPORTED_MIMETYPES:
            embed = skywizz.specific_error('Sorry, the file you attached is '
                                           'not a supported image format. '
                                           'Please upload a PNG, JPEG or '
                                           'WebP image.')
            await ctx.message.reply(embed=embed)
            return

        # Fetch image file
        response = requests.get(image_url)

        # Store image file name
        image_filename = ctx.message.attachments[0].filename

        # Caption image
        final_image = tools.caption_image(BytesIO(response.content),
                                          caption_text)

        # Send reply
        await ctx.message.reply(file=discord.File(BytesIO(final_image),
                                                  filename=f'captioned-{image_filename}'))

caption(ctx, caption_text) async

Command that places a caption from a given image Attention: You must send an image along with the command!

Parameters:
  • caption_text (str) –

    Desired caption of the image

Example

!caption "Hello World"

Usage

caption <image_caption>

/Users/caoma/Documents/Programming/GIT/SkyWizz/skywizz/cogs/meme_commands.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
@commands.cooldown(1, 60, commands.BucketType.user)
@commands.command(name='caption')
async def caption(self, ctx, caption_text):
    """
    Command that places a caption from a given image
    Attention: You must send an image along with the command!

    Args:
        caption_text (str): Desired caption of the image

    Example:
        `!caption "Hello World"`

    Usage:
        `caption <image_caption>`
    """
    # Must have caption text
    if not caption_text:
        embed = skywizz.specific_error("Please include some caption "
                                       "text after the `!caption` "
                                       "command. "
                                       "For example "
                                       "`!caption \"Hello world!\"")
        await ctx.message.reply(embed=embed)
        return

    # Must have a file attached
    if ctx.message.attachments:
        image_url = ctx.message.attachments[0].url
    else:
        embed = skywizz.specific_error('Please attach '
                                       'an image for me to caption.')
        await ctx.message.reply(embed=embed)
        return

    # File must be an image
    if mimetypes.guess_type(image_url)[0] not in SUPPORTED_MIMETYPES:
        embed = skywizz.specific_error('Sorry, the file you attached is '
                                       'not a supported image format. '
                                       'Please upload a PNG, JPEG or '
                                       'WebP image.')
        await ctx.message.reply(embed=embed)
        return

    # Fetch image file
    response = requests.get(image_url)

    # Store image file name
    image_filename = ctx.message.attachments[0].filename

    # Caption image
    final_image = tools.caption_image(BytesIO(response.content),
                                      caption_text)

    # Send reply
    await ctx.message.reply(file=discord.File(BytesIO(final_image),
                                              filename=f'captioned-{image_filename}'))