示例#1
0
    async def build_template(ctx, name, x, y, url, canvas):
        try:
            with await http.get_template(url, name) as data:
                size = await render.calculate_size(data)
                md5 = hashlib.md5(data.getvalue()).hexdigest()
                with Image.open(data).convert("RGBA") as tmp:
                    w, h = tmp.size
                    quantized = await Template.check_colors(
                        tmp, colors.by_name[canvas])
                if not quantized:
                    if not await utils.yes_no(ctx,
                                              ctx.s("template.not_quantized")):
                        return

                    template, bad_pixels = await render.quantize(
                        data, colors.by_name[canvas])
                    with io.BytesIO() as bio:
                        template.save(bio, format="PNG")
                        bio.seek(0)
                        f = discord.File(bio, "template.png")
                        new_msg = await ctx.send(
                            ctx.s("canvas.quantize").format(bad_pixels),
                            file=f)

                    url = new_msg.attachments[0].url
                    with await http.get_template(url, name) as data2:
                        md5 = hashlib.md5(data2.getvalue()).hexdigest()
                created = int(time.time())
                return DbTemplate(ctx.guild.id, name, url, canvas, x, y, w, h,
                                  size, created, created, md5, ctx.author.id)
        except aiohttp.client_exceptions.InvalidURL:
            raise UrlError
        except IOError:
            raise PilImageError
示例#2
0
def template_get_all():
    c.execute(
        'SELECT * FROM templates ORDER BY guild_id DESC, canvas ASC, name ASC')
    templates = []
    for t in c.fetchall():
        templates.append(DbTemplate(*t))
    return templates
示例#3
0
def template_get_all_public_by_guild_id(gid):
    c.execute(
        'SELECT * FROM templates WHERE guild_id=? AND private=0 ORDER BY canvas ASC, name ASC',
        (gid, ))
    templates = []
    for t in c.fetchall():
        templates.append(DbTemplate(*t))
    return templates
    async def build_template(ctx, name, x, y, url, canvas):
        """ Builds a template object from the given data.

        Arguments:
        ctx - commands.Context object.
        name - The name of the template, string.
        x - The x coordinate of the template, integer.
        y - The y coordinate of the template, integer.
        url - The url of the template’s image, string.
        canvas - The canvas this template is on, string.

        Returns:
        A template object.
        """
        try:
            with await http.get_template(url, name) as data:
                size = await render.calculate_size(data)
                md5 = hashlib.md5(data.getvalue()).hexdigest()
                with Image.open(data).convert("RGBA") as tmp:
                    w, h = tmp.size
                    quantized = await Template.check_colors(
                        tmp, colors.by_name[canvas])
                if not quantized:
                    if not await utils.yes_no(ctx,
                                              ctx.s("template.not_quantized")):
                        ctx.send(ctx.s("template.menuclose"))
                        return

                    template, bad_pixels = await render.quantize(
                        data, colors.by_name[canvas])
                    with io.BytesIO() as bio:
                        template.save(bio, format="PNG")
                        bio.seek(0)
                        f = discord.File(bio, "template.png")
                        new_msg = await ctx.send(
                            ctx.s("canvas.quantize").format(bad_pixels),
                            file=f)

                    url = new_msg.attachments[0].url
                    with await http.get_template(url, name) as data2:
                        md5 = hashlib.md5(data2.getvalue()).hexdigest()
                created = int(time.time())
                return DbTemplate(ctx.guild.id, name, url, canvas, x, y, w, h,
                                  size, created, created, md5, ctx.author.id)
        except aiohttp.client_exceptions.InvalidURL:
            raise UrlError
        except IOError:
            raise PilImageError
示例#5
0
def template_get_by_name(gid, name):
    c.execute('SELECT * FROM templates WHERE guild_id=? AND name=?', (gid, name))
    t = c.fetchone()
    return DbTemplate(*t) if t else None
示例#6
0
def template_get_by_hash(gid, md5):
    c.execute('SELECT * FROM templates WHERE guild_id=? AND md5=?', (gid, md5))
    templates = []
    for t in c.fetchall():
        templates.append(DbTemplate(*t))
    return templates