tuxbot-bot/tuxbot/cogs/Tags/functions/converters.py
2021-05-16 15:27:27 +02:00

49 lines
1.3 KiB
Python

from discord.ext import commands
from discord.ext.commands import Context
from tuxbot.cogs.Tags.functions.exceptions import (
UnknownTagException,
ExistingTagException,
TooLongTagException,
ReservedTagException,
)
from tuxbot.cogs.Tags.models import Tag
def _(x):
return x
class TagConverter(commands.Converter):
async def convert(self, ctx: Context, argument: str): # skipcq: PYL-W0613
arg = argument.lower()
tag_row = await Tag.get_or_none(server_id=ctx.guild.id, name=arg)
if not tag_row:
raise UnknownTagException(_("Unknown tag"))
return arg
class NewTagConverter(commands.Converter):
async def convert(self, ctx: Context, argument: str): # skipcq: PYL-W0613
arg = argument.lower()
if len(arg) > 50:
raise TooLongTagException(
_("Tag must be lower than 50 characters")
)
if arg.split(" ")[0] in ctx.bot.get_command("tag").all_commands:
raise ReservedTagException(
_("This tag name starts with a tag subcommand name")
)
tag_row = await Tag.get_or_none(server_id=ctx.guild.id, name=arg)
if tag_row:
raise ExistingTagException(_("Tag already exists"))
return arg