tuxbot-bot/tuxbot/cogs/Tags/functions/utils.py

52 lines
1.2 KiB
Python
Raw Normal View History

2021-05-16 21:21:27 +00:00
from typing import Optional, List
2021-05-15 22:32:14 +00:00
import discord
from tuxbot.cogs.Tags.models import Tag
from tuxbot.core.utils.functions.extra import ContextPlus
async def get_tag(guild_id: int, name: str) -> Tag:
return await Tag.get(server_id=guild_id, name=name)
async def get_all_tags(
guild_id: int, author: Optional[discord.Member] = None
2021-05-16 21:21:27 +00:00
) -> List[Tag]:
2021-05-15 22:32:14 +00:00
if author is not None:
return (
await Tag.filter(server_id=guild_id, author_id=author.id)
.all()
.order_by("-uses")
)
return await Tag.filter(server_id=guild_id).all().order_by("-uses")
2021-05-16 21:21:27 +00:00
async def search_tags(guild_id: int, q: str) -> List[Tag]:
return (
await Tag.filter(server_id=guild_id, name__icontains=q)
.all()
.order_by("-uses")
)
2021-05-15 22:32:14 +00:00
async def create_tag(ctx: ContextPlus, name: str, content: str):
tag_row = await Tag()
tag_row.server_id = ctx.guild.id
tag_row.author_id = ctx.author.id
tag_row.name = name # type: ignore
tag_row.content = content # type: ignore
await tag_row.save()
async def edit_tag(ctx: ContextPlus, name: str, content: str):
tag_row = await get_tag(ctx.guild.id, name)
tag_row.content = content # type: ignore
await tag_row.save()