def convert_channel(ann, ctx, arg: str) -> Channel: """ Converts an argument into a Channel. """ channel_id = None if arg.startswith("<#") and arg.endswith(">"): try: channel_id = int(arg[2:-1]) except ValueError: raise ConversionFailedError(ctx, arg, Channel, "Invalid channel ID") elif all(i.isdigit() for i in arg): channel_id = int(arg) if channel_id is not None: channel = ctx.guild.channels.get(channel_id) else: channel = next( filter(lambda c: c.name == arg, ctx.guild.channels.values()), None) if channel is None: raise ConversionFailedError(ctx, arg, Channel, "Could not find channel") return channel
def convert_channel(ctx, arg: str) -> Channel: """ Converts an argument into a Channel. """ if arg.startswith("<#") and arg.endswith(">"): id = arg[2:-1] try: id = int(id) except ValueError: raise ConversionFailedError(ctx, arg, Channel) channel = ctx.guild.channels.get(id) if not channel: raise ConversionFailedError(ctx, arg, Channel) else: try: channel = next( filter(lambda c: c.name == arg, ctx.guild.channels.values()), None) if channel is None: channel = ctx.guild.channels.get(int(arg)) except (StopIteration, ValueError): raise ConversionFailedError(ctx, arg, Channel) else: if channel is None: raise ConversionFailedError(ctx, arg, Channel) return channel
def convert_member(ann, ctx, arg: str) -> Member: """ Converts an argument into a Member. """ member_id = None if arg.startswith("<@") and arg.endswith(">"): id = arg[2:-1] if id[0] == "!": # strip nicknames id = id[1:] try: member_id = int(id) except ValueError: raise ConversionFailedError(ctx, arg, Member, "Invalid member ID") elif all(i.isdigit() for i in arg): member_id = int(arg) if member_id is not None: member = ctx.guild.members.get(member_id) else: member = ctx.guild.search_for_member(full_name=arg) if member is None: raise ConversionFailedError(ctx, arg, Member, "Could not find Member") return member
def valid_unsigned_char(annotation, ctx: Context, arg: str): """ Checks if given input is a number in the domain [0, 255]. 255 = 0xFF, which is the largest value for any component of an RGB(A) number. """ try: value = int(arg) except ValueError: raise ConversionFailedError(ctx, arg, annotation, 'Invalid number.') else: if not 0 <= value <= 255: raise ConversionFailedError(ctx, arg, annotation, 'Value must be within range (0 - 255)') return value
def _with_reraise(func, ann, ctx, arg): try: return func(ann, ctx, arg) except ConversionFailedError: raise except Exception as e: raise ConversionFailedError(ctx, arg, ann, message="Converter error") from e
def convert_float(ann, ctx, arg: str) -> float: """ Converts an argument into a float. """ try: return float(arg) except ValueError as e: raise ConversionFailedError(ctx, arg, float, "Invalid float") from e
def convert_int(ann, ctx, arg: str) -> int: """ Converts an argument into an integer. """ try: return int(arg, 0) except ValueError as e: raise ConversionFailedError(ctx, arg, int, "Invalid integer") from e
def convert_hex_colour(annotation, ctx: Context, arg: str) -> Colour: """ Converts a string representation of a hex colour into an instance of Colour. """ arg = colour_pattern.sub(r'\2', arg) try: value = int(arg, base=16) except ValueError: raise ConversionFailedError(ctx, arg, annotation, 'Invalid value.') else: return annotation(value)
def convert_role(ann, ctx, arg: str) -> Role: """ Converts an argument into a :class:`.Role`. """ role_id = None if arg.startswith("<@&") and arg.endswith(">"): try: role_id = int(arg[3:-1]) except ValueError: raise ConversionFailedError(ctx, arg, Role, "Invalid role ID") elif all(i.isdigit() for i in arg): role_id = int(arg) if role_id is not None: role = ctx.guild.roles.get(role_id) else: role = next(filter(lambda c: c.name == arg, ctx.guild.roles.values()), None) if role is None: raise ConversionFailedError(ctx, arg, Role, "Could not find role") return role
def convert_member(ctx, arg: str) -> Member: """ Converts an argument into a Member. """ if arg.startswith("<@") and arg.endswith(">"): # Parse the mention out id = arg[2:-1] if id[0] == "!": # nicknames id = id[1:] try: id = int(id) except ValueError: raise ConversionFailedError(ctx, arg, Member) member = ctx.guild.members.get(id) if not member: raise ConversionFailedError(ctx, arg, Member) else: member = ctx.guild.search_for_member(full_name=arg) if not member: raise ConversionFailedError(ctx, arg, Member) return member
def convert_union(ann, ctx, arg: str) -> Any: """ Converts a :class:`typing.Union`. This works by finding every type defined in the union, and trying each one until one returns a non-error. """ subtypes = typing_inspect.get_args(ann, evaluate=True) for subtype in subtypes: try: converter = ctx._lookup_converter(subtype) return converter(ann, ctx, arg) except ConversionFailedError: continue raise ConversionFailedError( ctx, arg, ann, message="Failed to convert to any of these types")