我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用discord.ext.commands.Converter()。
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
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