tuxbot-bot/tuxbot/cogs/Network/functions/converters.py
Romain J 7962205d16 feat(commands|Network>iplocalise): feat iplocalise command
todo:
- fix l'INET6 qui s'affiche comme 10 plutot que 6
- suppr le message Retrieving info si la commande n'a pas pu etre faite
- this shit https://canary.discord.com/channels/767804368233037886/768097484655689758/803299217279811634
2021-01-25 17:28:59 +01:00

40 lines
1.1 KiB
Python

import re
from discord.ext import commands
from tuxbot.cogs.Network.functions.exceptions import InvalidIp
def _(x):
return x
DOMAIN_PATTERN = "^([A-Za-z0-9]\.|[A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9]\.){1,3}[A-Za-z]{2,6}$"
IP_PATTERN = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
class IPConverter(commands.Converter):
async def convert(self, ctx, argument):
argument = argument.replace("http://", "").replace("https://", "")
check_domain = re.match(DOMAIN_PATTERN, argument)
check_ip = re.match(IP_PATTERN, argument)
if check_domain or check_ip:
return argument
raise InvalidIp(_("Invalid ip or domain"))
class IPVersionConverter(commands.Converter):
async def convert(self, ctx, argument):
if not argument:
return argument
argument = argument.replace("-", "").replace("p", "").replace("v", "")
if argument not in ["4", "6"]:
raise InvalidIp(_("Invalid ip version"))
return argument