diff options
Diffstat (limited to 'bot/converters.py')
| -rw-r--r-- | bot/converters.py | 66 |
1 files changed, 61 insertions, 5 deletions
diff --git a/bot/converters.py b/bot/converters.py index 3def4b07a..069e841f9 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -1,16 +1,20 @@ +import logging import random import socket from ssl import CertificateError import discord from aiohttp import AsyncResolver, ClientConnectorError, ClientSession, TCPConnector -from discord.ext.commands import BadArgument, Converter, UserConverter +from discord.ext.commands import BadArgument, Context, Converter from fuzzywuzzy import fuzz from bot.constants import DEBUG_MODE, Keys, URLs from bot.utils import disambiguate +log = logging.getLogger(__name__) + + class Snake(Converter): snakes = None special_cases = None @@ -167,11 +171,10 @@ class InfractionSearchQuery(Converter): @staticmethod async def convert(ctx, arg): try: - user_converter = UserConverter() - user = await user_converter.convert(ctx, arg) - except Exception: + maybe_snowflake = arg.strip("<@!>") + return await ctx.bot.get_user_info(maybe_snowflake) + except (discord.NotFound, discord.HTTPException): return arg - return user or arg class Subreddit(Converter): @@ -198,3 +201,56 @@ class Subreddit(Converter): ) return sub + + +class TagNameConverter(Converter): + @staticmethod + async def convert(ctx: Context, tag_name: str): + def is_number(value): + try: + float(value) + except ValueError: + return False + return True + + tag_name = tag_name.lower().strip() + + # The tag name has at least one invalid character. + if ascii(tag_name)[1:-1] != tag_name: + log.warning(f"{ctx.author} tried to put an invalid character in a tag name. " + "Rejecting the request.") + raise BadArgument("Don't be ridiculous, you can't use that character!") + + # The tag name is either empty, or consists of nothing but whitespace. + elif not tag_name: + log.warning(f"{ctx.author} tried to create a tag with a name consisting only of whitespace. " + "Rejecting the request.") + raise BadArgument("Tag names should not be empty, or filled with whitespace.") + + # The tag name is a number of some kind, we don't allow that. + elif is_number(tag_name): + log.warning(f"{ctx.author} tried to create a tag with a digit as its name. " + "Rejecting the request.") + raise BadArgument("Tag names can't be numbers.") + + # The tag name is longer than 127 characters. + elif len(tag_name) > 127: + log.warning(f"{ctx.author} tried to request a tag name with over 127 characters. " + "Rejecting the request.") + raise BadArgument("Are you insane? That's way too long!") + + return tag_name + + +class TagContentConverter(Converter): + @staticmethod + async def convert(ctx: Context, tag_content: str): + tag_content = tag_content.strip() + + # The tag contents should not be empty, or filled with whitespace. + if not tag_content: + log.warning(f"{ctx.author} tried to create a tag containing only whitespace. " + "Rejecting the request.") + raise BadArgument("Tag contents should not be empty, or filled with whitespace.") + + return tag_content |