Exemplo n.º 1
0
async def inspect_image(event: hikari.GuildMessageCreateEvent, what: str) -> None:
    """Inspect the image and respond to the user."""
    # Show the avatar for the given user ID:
    if user_match := re.match(r"<@!?(\d+)>", what):
        user_id = hikari.Snowflake(user_match.group(1))
        user = bot.cache.get_user(user_id) or await bot.rest.fetch_user(user_id)
        await event.message.reply("User avatar", attachment=user.avatar_url or user.default_avatar_url)
Exemplo n.º 2
0
    def from_mapping(cls, mapping: collections.Mapping[str, typing.Any], /) -> FullConfig:
        log_level = mapping.get("log_level", logging.INFO)
        if not isinstance(log_level, (str, int)):
            raise ValueError("Invalid log level found in config")

        elif isinstance(log_level, str):
            log_level = log_level.upper()

        declare_global_commands = mapping.get("declare_global_commands", True)
        if not isinstance(declare_global_commands, bool):
            declare_global_commands = hikari.Snowflake(declare_global_commands)

        return cls(
            cache=_cast_or_else(mapping, "cache", hikari.CacheComponents, DEFAULT_CACHE),
            database=DatabaseConfig.from_mapping(mapping["database"]),
            emoji_guild=_cast_or_else(mapping, "emoji_guild", hikari.Snowflake, None),
            intents=_cast_or_else(mapping, "intents", hikari.Intents, DEFAULT_INTENTS),
            log_level=log_level,
            mention_prefix=bool(mapping.get("mention_prefix", True)),
            owner_only=bool(mapping.get("ownerA_only", False)),
            prefixes=frozenset(map(str, mapping["prefixes"])) if "prefixes" in mapping else frozenset(),
            ptf=_cast_or_else(mapping, "ptf", PTFConfig.from_mapping, None),
            tokens=Tokens.from_mapping(mapping["tokens"]),
            declare_global_commands=declare_global_commands,
        )
Exemplo n.º 3
0
async def mentions_command(
    ctx: tanjun.abc.Context,
    message_id: hikari.Snowflake,
    channel: hikari.SnowflakeishOr[hikari.PartialChannel] | None,
) -> None:
    """Get a list of the users who were pinged by a message.

    Arguments
        * message: ID of the message to get the ping list for.

    Options
        * channel: ID or mention of the channel the message is in.
            If this isn't provided then the command will assume the message is in the current channel.
    """
    channel_id = hikari.Snowflake(channel) if channel else ctx.channel_id
    message = await ctx.rest.fetch_message(channel_id, message_id)
    mentions: str | None = None
    if message.mentions.users:
        assert not isinstance(message.mentions.users, hikari.UndefinedType)
        mentions = ", ".join(map(str, message.mentions.users.values()))

    await ctx.respond(
        content=f"Pinging mentions: {mentions}"
        if mentions else "No pinging mentions.",
        component=utility.delete_row(ctx),
    )
Exemplo n.º 4
0
 def _make_shard(self, shard_id: int, /) -> shards_.Shard:
     return shards_.Shard(
         receiver=self._receiver,
         shard_id=shard_id,
         shard_count=self._shard_count,
         intents=self.intents,
         user_id=hikari.Snowflake(0),
     )
Exemplo n.º 5
0
        seq (:obj:`str`): The argument text.
        context (:obj:`~.context.Context`): The command invocation context for the argument.
    """

    __slots__ = ["context"]

    def __init__(self, seq: str, context: context_.Context) -> None:
        super().__init__(seq)
        self.context: context_.Context = context


def _resolve_id_from_arg(arg_string: str,
                         regex: typing.Pattern) -> hikari.Snowflake:
    if match := regex.match(arg_string):
        arg_string = match.group(1)
    return hikari.Snowflake(arg_string)


async def _get_or_fetch_guild_channel_from_id(
        arg: WrappedArg, channel_id: hikari.Snowflake) -> hikari.GuildChannel:
    channel = arg.context.bot.cache.get_guild_channel(channel_id)
    if channel is None:
        channel = await arg.context.bot.rest.fetch_channel(channel_id)
    return channel


def _raise_if_not_none(obj: typing.Optional[T]) -> T:
    if obj is None:
        raise errors.ConverterFailure
    return obj
Exemplo n.º 6
0
 def replace(match):
     user = self.bot.cache.get_user(hikari.Snowflake(match.group(1)))
     return f"@{user}" if user is not None else self.prefix
Exemplo n.º 7
0
async def guild_converter(arg: WrappedArg):
    if not arg.isdigit():
        raise ValueError(f"Invalid guild id: {arg!r}")
    snowflake = hikari.Snowflake(arg)
    return arg.context.bot.cache.get_available_guild(
        snowflake) or await arg.context.bot.rest.fetch_guild(snowflake)
Exemplo n.º 8
0
    def from_env(cls) -> FullConfig:
        dotenv.load_dotenv()

        return cls(
            cache=_cast_or_else(os.environ, "cache", hikari.CacheComponents, DEFAULT_CACHE),
            database=DatabaseConfig.from_env(),
            emoji_guild=_cast_or_else(os.environ, "emoji_guild", hikari.Snowflake, None),
            intents=_cast_or_else(os.environ, "intents", hikari.Intents, DEFAULT_INTENTS),
            log_level=_cast_or_else(os.environ, "log_level", lambda v: int(v) if v.isdigit() else v, logging.INFO),
            mention_prefix=_cast_or_else(os.environ, "mention_prefix", bool, True),
            owner_only=_cast_or_else(os.environ, "owner_only", bool, False),
            prefixes=_cast_or_else(os.environ, "prefixes", lambda v: frozenset(map(str, v)), frozenset[str]()),
            ptf=PTFConfig.from_env() if os.getenv("ptf_username") else None,
            tokens=Tokens.from_env(),
            declare_global_commands=_cast_or_else(
                os.environ, "declare_global_commands", lambda v: v if isinstance(v, bool) else hikari.Snowflake(v), True
            ),
        )