<commit_before># -*- coding: utf-8 -*-
import discord
from discord.ext import commands
from cogs.cog import Cog
import asyncpg
import hashlib

async def query(db: asyncpg.pool.Pool, tag: str, guild: int) -> str:
    ret = await db.fetchval("""SELECT content from tags where name=$1 AND guild=$2""", tag, guild)
    if ret is not None:
        return ret
    else:
        return "can not find that tag."

async def append(db: asyncpg.pool.Pool, tag: str, content: str, author: int, guild: int) -> bool:
    exists = await db.fetchrow("""SELECT name FROM tags WHERE name=$1""", tag)
    if exists:
        return False
    else:
        h = hashlib.sha256(f"{tag}{guild}".encode()).hexdigest()
        await db.execute("""INSERT INTO tags (hash, name, content, author, guild) 
                            VALUES ($1, $2, $3, $4, $5)""", h, tag, content, author, guild)
        return True

async def remove(db: asyncpg.pool.Pool, tag: str, author: int, guild: int) -> bool:
    if await db.fetchrow("""SELECT * FROM tags WHERE author=$1 AND guild=$2 AND name=$3""", author, guild, tag):
        await db.execute("""DELETE FROM tags WHERE author=$1 AND guild=$2 AND name=$3""", author, guild, tag)
        return True
    else:
        return False

async def list_tags(db: asyncpg.pool.Pool, guild: int) -> list:
    tags = await db.fetch("""SELECT * FROM tags WHERE guild=$1""", guild)
    return [(tag["name"], tag["author"]) for tag in tags]


class Tags(Cog):
    """The description for Tags goes here."""

    @commands.group(name="tag", invoke_without_command=True)
    async def tags(self, ctx, tag):
        await ctx.send(await query(self.bot.db, tag, ctx.guild.id))

    @tags.command()
    async def create(self, ctx, tag, *, content):
        if await append(self.bot.db, tag, content, ctx.author.id, ctx.guild.id):
            await ctx.send(f"tag {tag} was added to database.")
        else:
            await ctx.send(f"tag {tag} failed to be added \U0001f626")

    @tags.command()
    async def remove(self, ctx, tag):
        if await remove(self.bot.db, tag, ctx.author.id, ctx.guild.id):
            await ctx.send(f"tag '{tag}' removed successfully.")
        else:
            await ctx.send(f"tag was not removed. Are you the owner?")

    @tags.command()
    async def list(self, ctx):
        tags = list(await list_tags(self.bot.db, ctx.guild.id))
        tags = [(tag[0], self.bot.get_user(tag[1]).mention) for tag in tags]
        tags = discord.Embed(description=str(tags))
        await ctx.send(embed=tags)


def setup(bot):
    bot.add_cog(Tags(bot))
<commit_msg>require tag be shorter than its content.<commit_after># -*- coding: utf-8 -*-
import discord
from discord.ext import commands
from cogs.cog import Cog
import asyncpg
import hashlib

async def query(db: asyncpg.pool.Pool, tag: str, guild: int) -> str:
    ret = await db.fetchval("""SELECT content from tags where name=$1 AND guild=$2""", tag, guild)
    if ret is not None:
        return ret
    else:
        return "can not find that tag."

async def append(db: asyncpg.pool.Pool, tag: str, content: str, author: int, guild: int) -> bool:
    exists = await db.fetchrow("""SELECT name FROM tags WHERE name=$1""", tag)
    if exists:
        return False
    else:
        h = hashlib.sha256(f"{tag}{guild}".encode()).hexdigest()
        await db.execute("""INSERT INTO tags (hash, name, content, author, guild) 
                            VALUES ($1, $2, $3, $4, $5)""", h, tag, content, author, guild)
        return True

async def remove(db: asyncpg.pool.Pool, tag: str, author: int, guild: int) -> bool:
    if await db.fetchrow("""SELECT * FROM tags WHERE author=$1 AND guild=$2 AND name=$3""", author, guild, tag):
        await db.execute("""DELETE FROM tags WHERE author=$1 AND guild=$2 AND name=$3""", author, guild, tag)
        return True
    else:
        return False

async def list_tags(db: asyncpg.pool.Pool, guild: int) -> list:
    tags = await db.fetch("""SELECT * FROM tags WHERE guild=$1""", guild)
    return [(tag["name"], tag["author"]) for tag in tags]


class Tags(Cog):
    """The description for Tags goes here."""

    @commands.group(name="tag", invoke_without_command=True)
    async def tags(self, ctx, tag):
        await ctx.send(await query(self.bot.db, tag, ctx.guild.id))

    @tags.command()
    async def create(self, ctx, tag, *, content):
        try:
            assert len(tag) < len(content)
        except AssertionError:
            await ctx.send(f"failed to add tag {tag} to the database. content needs to be shorter than your tag name. <:lumaslime:340673841858740224>")
            return
        if await append(self.bot.db, tag, content, ctx.author.id, ctx.guild.id):
            await ctx.send(f"tag {tag} was added to database.")
        else:
            await ctx.send(f"tag {tag} failed to be added \U0001f626")

    @tags.command()
    async def remove(self, ctx, tag):
        if await remove(self.bot.db, tag, ctx.author.id, ctx.guild.id):
            await ctx.send(f"tag '{tag}' removed successfully.")
        else:
            await ctx.send(f"tag was not removed. Are you the owner?")

    @tags.command()
    async def list(self, ctx):
        tags = list(await list_tags(self.bot.db, ctx.guild.id))
        tags = [(tag[0], self.bot.get_user(tag[1]).mention) for tag in tags]
        tags = discord.Embed(description=str(tags))
        await ctx.send(embed=tags)


def setup(bot):
    bot.add_cog(Tags(bot))

{'fraction_non_alphanumeric': 0.1437968207564407, 'fraction_numerical': 0.013520920884341312, 'mean_word_length': 2.849507735583685, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5162895', 'n_tokens_mistral': 1804, 'n_tokens_neox': 1741, 'n_words': 568}