How hikari-py/hikari works
Purpose and Public Entry Points
Hikari is a statically typed, asyncio-based Python microframework for Discord API v10. Users primarily import hikari and instantiate either a gateway-connected bot, an HTTP interaction endpoint bot, or a reusable REST application. Public API aggregation and CLI/module entry points are in hikari/__init__.py, hikari/__main__.py, and hikari/cli.py.
API/Implementation Boundary
Abstract protocols define substitutable service contracts, while impl provides default concrete implementations. This separates user-facing type/API dependencies from HTTP, gateway, cache, and event machinery.
• Contracts: hikari/api/rest.py, hikari/api/event_manager.py, hikari/api/shard.py
• Implementations: hikari/impl/rest.py, hikari/impl/event_manager.py, hikari/impl/shard.py
Gateway Bot Lifecycle
GatewayBot is the stateful orchestration entry point for long-lived Discord gateway clients. It composes REST access, shard management, event dispatch, optional caching, configuration, startup/shutdown handling, and the asyncio run lifecycle.
• hikari/impl/gateway_bot.py
• hikari/impl/config.py
• hikari/impl/cache.py
Control flow: start bot services → establish/manage one or more Gateway WebSocket shards → receive gateway payloads → deserialize to events/entities → update cache and dispatch listeners → handlers may call REST APIs.
Gateway Transport and Event Pipeline
Shard instances implement Discord Gateway connectivity, session/heartbeat/reconnect behavior, and payload intake. Event factories translate gateway payloads into typed event objects; the event manager routes those objects to registered listeners, including GatewayBot.listen() registrations.
• hikari/impl/shard.py
• hikari/impl/event_factory.py
• hikari/impl/event_manager_base.py
Typed event families are organized under hikari/events/, while interaction event payloads and models are represented under hikari/interactions/.
REST Client, Routes, and Rate Limits
The REST layer exposes Discord resources and endpoint builders, serializes requests, manages pooled HTTP access, and turns responses into library entities. Route definitions are kept internal; rate-limit buckets coordinate request execution against Discord limits.
• hikari/impl/rest.py
• hikari/internal/routes.py
• hikari/impl/rate_limits.py
RESTApp supports REST-only integrations and token-scoped acquired clients; RESTBot layers application startup and interaction serving over REST services.
• hikari/impl/rest_bot.py
• hikari/api/special_endpoints.py
HTTP Interaction Server Path
RESTBot hosts an interaction endpoint for Discord-delivered commands, components, and modals. The server validates/parses inbound interaction requests, uses the event factory/model layer, and dispatches handlers registered by interaction type. Response builders and REST follow-ups form the outbound boundary.
• hikari/impl/interaction_server.py
• hikari/impl/rest_bot.py
• hikari/interactions/command_interactions.py
This runtime requires externally reachable hosting and Discord application endpoint configuration; tunneling services such as ngrok are development-only deployment aids.
Domain Model and Entity Construction
Top-level modules define immutable/typed Discord resource models—users, guilds, channels, messages, commands, webhooks, permissions, and more. Entity factories centrally map REST and Gateway JSON into these models, insulating transports from domain representation.
• hikari/channels.py
• hikari/messages.py
• hikari/impl/entity_factory.py
Runtime and External Dependencies
The primary runtime is Python 3.10–3.14 with asyncio; Discord’s REST and Gateway APIs are the core external services. HTTP/proxy/timeout behavior is configured through hikari/impl/config.py, networking support lives in hikari/internal/net.py, and optional performance deployments use aiohttp speedups, orjson, ciso8601, and Unix uvloop.
Main entry points: gateway_bot.py GatewayBot · rest.py REST client · interaction_server.py Interaction server · event_factory.py Event factory
Components 18
Public API
- hikari package · Aggregates the user-facing library API.
hikari/__init__.py - CLI and module entry
hikari/cli.py
Gateway runtime
- GatewayBot · Owns the long-lived asyncio gateway lifecycle.
hikari/impl/gateway_bot.py - Runtime config · Provides HTTP, proxy, timeout, and bot settings.
hikari/impl/config.py - Gateway shards · Manages sessions, heartbeats, reconnects, and payload intake.
hikari/impl/shard.py - Event factory · Converts gateway payloads into typed events.
hikari/impl/event_factory.py - Event manager · Routes typed events to registered listeners.
hikari/impl/event_manager.py - Cache
hikari/impl/cache.py
REST and interactions
- REST client · Serializes Discord API requests and builds response entities.
hikari/impl/rest.py - Routes
hikari/internal/routes.py - Rate-limit buckets · Coordinates execution against Discord limits.
hikari/impl/rate_limits.py - RESTBot · Starts REST services and hosts interaction handling.
hikari/impl/rest_bot.py - Interaction server · Validates and parses Discord interaction requests.
hikari/impl/interaction_server.py
Models and events
- Entity factory · Maps REST and gateway JSON into domain models.
hikari/impl/entity_factory.py - Discord entities · Immutable resource models such as channels and messages.
hikari/channels.py - Event families
- Interaction models
hikari/interactions/command_interactions.py
Other components
- Discord APIs · Discord REST API and Gateway service.
Connections 24
- hikari package → GatewayBot: exports
- hikari package → RESTBot: exports
- CLI and module entry → GatewayBot: starts
- GatewayBot → Runtime config: uses
- GatewayBot → Gateway shards: manages
- GatewayBot → Event manager: registers listeners with
- GatewayBot → Cache: owns
- GatewayBot → REST client: composes
- Gateway shards → Discord APIs: Gateway WebSocket
- Gateway shards → Event factory: payloads
- Event factory → Event families: creates
- Event factory → Entity factory: uses
- Event manager → Event families: dispatches
- Event manager → Cache: updates through events
- REST client → Routes: builds requests from
- REST client → Rate-limit buckets: schedules through
- REST client → Entity factory: deserializes with
- REST client → Discord APIs: HTTPS API
- Entity factory → Discord entities: constructs
- RESTBot → REST client: runs
- RESTBot → Interaction server: hosts
- Interaction server → Interaction models: parses
- Interaction server → Event factory: uses
- Interaction server → Discord APIs: interaction responses