Skip to content

Commit

Permalink
Implement tags
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenChen committed Sep 4, 2021
1 parent 86597f8 commit f19444e
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
37 changes: 36 additions & 1 deletion cogs/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from typing import Union
from utils.checks import is_staff
from utils import crud
from utils.utils import parse_time
from utils.utils import parse_time, gen_color

python_version = sys.version.split()[0]

Expand Down Expand Up @@ -289,6 +289,41 @@ async def remindme(self, ctx, remind_in: str, *, reminder: str):
await crud.add_reminder(reminder_time, ctx.author.id, reminder)
await ctx.send("I will send you a reminder then.")

@commands.group(invoke_without_command=True)
async def tag(self, ctx, *, title: str = ""):
if ctx.invoked_subcommand is None:
if title:
if tag := await crud.get_tag(title):
return await ctx.send(tag.content)
else:
await ctx.send("This tag doesn't exists!")
else:
await ctx.send_help(ctx.command)

@is_staff('Helper')
@tag.command()
async def create(self, ctx, title: str, *, content: str):
if await crud.get_tag(title):
await ctx.send("This tag already exists!")
await crud.create_tag(title=title, content=content, author=ctx.author.id)
await ctx.send("Tag created successfully")

@tag.command()
async def search(self, ctx, query: str):
if tags := await crud.search_tags(query):
embed = discord.Embed(description='\n'.join(f'{n}. {tag.title}' for n, tag in enumerate(tags, start=1)), color=gen_color(ctx.author.id))
await ctx.send(embed=embed)
else:
await ctx.send("No tags found.")

@is_staff('Helper')
@tag.command()
async def delete(self, ctx, *, title: str):
if not (await crud.get_tag(title)):
return await ctx.send("This tag doesn't exists!")
await crud.delete_tag(title=title)
await ctx.send("Tag deleted successfully")


def setup(bot):
bot.add_cog(Extras(bot))
36 changes: 36 additions & 0 deletions migrations/versions/3eb5fe5f18d4_add_tags_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Add tags table
Revision ID: 3eb5fe5f18d4
Revises: bbd00345a74a
Create Date: 2021-09-03 22:11:34.286026
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '3eb5fe5f18d4'
down_revision = 'bbd00345a74a'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('tags',
sa.Column('id', sa.BigInteger(), nullable=False),
sa.Column('title', sa.String(), nullable=False),
sa.Column('content', sa.String(), nullable=False),
sa.Column('author', sa.BigInteger(), nullable=False),
sa.ForeignKeyConstraint(['author'], ['members.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('title')
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('tags')
# ### end Alembic commands ###
18 changes: 18 additions & 0 deletions utils/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,21 @@ async def get_reminders() -> list[models.RemindMeEntry]:
async def remove_reminder(reminder_id: int):
db_reminder = await models.RemindMeEntry.get(reminder_id)
await db_reminder.delete()


async def create_tag(title: str, content: str, author: int):
await add_dbmember_if_not_exist(author)
await models.Tag.create(id=generate_id(), title=title, content=content, author=author)


async def get_tag(title: str) -> models.Tag:
return await models.Tag.query.where(models.Tag.title == title).gino.first()


async def search_tags(query: str) -> list[models.Tag]:
return await models.Tag.query.where(models.Tag.title.ilike(f"%{query}%")).limit(10).gino.all()


async def delete_tag(title: str):
db_tag = await get_tag(title)
await db_tag.delete()
8 changes: 8 additions & 0 deletions utils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,11 @@ class RemindMeEntry(db.Model):
date = db.Column(db.TIMESTAMP, nullable=False)
author = db.Column(db.BigInteger, db.ForeignKey("members.id"), nullable=False)
reminder = db.Column(db.String, nullable=False)


class Tag(db.Model):
__tablename__ = "tags"
id = db.Column(db.BigInteger(), primary_key=True)
title = db.Column(db.String, unique=True, nullable=False)
content = db.Column(db.String, nullable=False)
author = db.Column(db.BigInteger, db.ForeignKey("members.id"), nullable=False)

0 comments on commit f19444e

Please sign in to comment.