def ranged(low, high=None, *, type=int):
'Converter to check if an argument is in a certain range INCLUSIVELY'
if high is None:
low, high = 0, low
def ranged_argument(arg):
result = type(arg)
if low <= result <= high:
return result
raise commands.BadArgument(f'Value must be between {low} and {high}, '
f'or equal to {low} or {high}.')
return ranged_argument
python类Converter()的实例源码
def union(*classes):
class Union(commands.Converter):
async def convert(self, ctx, argument):
for cls in classes:
try:
if cls in converters:
cls = converters[cls]
return await cls.convert(self, ctx, argument)
except Exception as e:
pass
else:
raise e
return Union
def __init__(self, converter=None, *, default=None):
if isinstance(converter, type) and issubclass(converter, commands.Converter):
converter = converter()
if converter is not None and not isinstance(converter, commands.Converter):
raise TypeError('commands.Converter subclass necessary.')
self.converter = converter
self.default = default