refactor: clean up range_type

This commit is contained in:
Ryan 2024-12-30 20:33:44 -05:00
parent 1a71d83ebd
commit e032d04157
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
3 changed files with 8 additions and 8 deletions

View File

@ -35,8 +35,12 @@ class ArgumentParser:
def range_type(string, min=0, max=100): def range_type(string, min=0, max=100):
try:
value = int(string) value = int(string)
except ValueError:
raise argparse.ArgumentTypeError(f"value not a valid integer")
if min <= value <= max: if min <= value <= max:
return value return value
else: else:
raise argparse.ArgumentTypeError("value not in range %s-%s" % (min, max)) raise argparse.ArgumentTypeError(f"value not in range {min}-{max}")

View File

@ -1,7 +1,7 @@
import functools
import re import re
import arguments import arguments
import commands import commands
import utils import utils
@ -14,9 +14,7 @@ async def clear(message):
) )
parser.add_argument( parser.add_argument(
"count", "count",
type=int, type=functools.partial(arguments.range_type, min=1, max=1000),
choices=range(1, 1001),
metavar="[1-1000]",
help="amount of messages to delete", help="amount of messages to delete",
) )
parser.add_argument( parser.add_argument(

View File

@ -26,7 +26,6 @@ async def queue_or_play(message):
"--volume", "--volume",
default=50, default=50,
type=functools.partial(arguments.range_type, min=0, max=150), type=functools.partial(arguments.range_type, min=0, max=150),
metavar="[0-150]",
help="the volume level (0 - 150)", help="the volume level (0 - 150)",
) )
group.add_argument( group.add_argument(
@ -228,7 +227,6 @@ async def volume(message):
"volume", "volume",
nargs="?", nargs="?",
type=functools.partial(arguments.range_type, min=0, max=150), type=functools.partial(arguments.range_type, min=0, max=150),
metavar="[0-150]",
help="the volume level (0 - 150)", help="the volume level (0 - 150)",
) )
if not (args := await parser.parse_args(message, tokens)): if not (args := await parser.parse_args(message, tokens)):