async def search_member(app: hikari.GatewayBotAware, guild_id: int, name: str) -> typing.Optional[hikari.Member]: members = _member_cache.values() username, _, discriminator = name.rpartition("#") valid_discriminator = username and len(discriminator) == 4 name = username if valid_discriminator else name for i in range(2): if valid_discriminator and (member := utils.get( members, username=name, discriminator=discriminator)): return member if not valid_discriminator and (member := utils.find( members, lambda m: name in (m.username, m.nickname))): return member
async def category_converter(arg: WrappedArg) -> hikari.GuildCategory: """ Converter to transform a command argument into a :obj:`~hikari.GuildCategory` object. Args: arg (:obj:`WrappedArg`): Argument to transform. Returns: :obj:`~hikari.GuildCategory`: The category object resolved from the argument. Raises: :obj:`~.errors.ConverterFailure`: If the argument could not be resolved into a category object. """ try: channel_id = _resolve_id_from_arg(arg.data, CHANNEL_MENTION_REGEX) except ValueError: channels = arg.context.bot.cache.get_guild_channels_view_for_guild( arg.context.guild_id) channel = utils.get(channels.values(), name=arg.data) else: channel = await _get_or_fetch_guild_channel_from_id(arg, channel_id) if not isinstance(channel, hikari.GuildCategory): raise errors.ConverterFailure return channel
async def role_converter(arg: WrappedArg) -> hikari.Role: """ Converter to transform a command argument into a :obj:`~hikari.Role` object. Args: arg (:obj:`WrappedArg`): Argument to transform. Returns: :obj:`~hikari.Role`: The role object resolved from the argument. Raises: :obj:`~.errors.ConverterFailure`: If the argument could not be resolved into a role object. """ try: role_id = _resolve_id_from_arg(arg.data, CHANNEL_MENTION_REGEX) except ValueError: roles = arg.context.bot.cache.get_roles_view_for_guild( arg.context.guild_id) role = utils.get(roles.values(), name=arg.data) else: role = arg.context.bot.cache.get_role(role_id) if role is None: roles = await arg.context.bot.rest.fetch_roles(arg.context.guild_id ) roles = dict([(r.id, r) for r in roles]) role = roles[role_id] return _raise_if_not_none(role)
async def guild_converter(arg: WrappedArg) -> hikari.GuildPreview: """ Converter to transform a command argument into a :obj:`~hikari.Guild` object. Args: arg (:obj:`WrappedArg`): Argument to transform. Returns: :obj:`~hikari.Guild`: The guild object resolved from the argument. Raises: :obj:`~.errors.ConverterFailure`: If the argument could not be resolved into a guild object. """ if arg.data.isdigit(): guild_id = int(arg.data) guild = await arg.context.bot.rest.fetch_guild_preview(guild_id) else: guilds = arg.context.bot.cache.get_available_guilds_view() guild = utils.get(guilds.values(), name=arg.data) guild = await arg.context.bot.rest.fetch_guild_preview(guild.id) return _raise_if_not_none(guild)
def set_presence(self, presence: presences.MemberPresence, /) -> None: if (spotify := utils.get(presence.activities, name="Spotify", type=ActivityType.LISTENING)) is None: self.delete_presence(presence.guild_id, presence.user_id) return None