What it is
Hikari is a modern, fast, and type-safe Python library for building Discord bots. It is designed for high performance, asynchronous execution, and full support of Discord API features.
Hikari allows you to create bots that handle events, send messages, manage channels, and interact with Discord servers. It supports asynchronous programming, event-driven architecture, and integration with other libraries for commands, interactions, and more.
Installation
pip install hikariGetting started
The smallest useful thing you can do with it, and what each part means.
import hikari
bot = hikari.GatewayBot(token='YOUR_BOT_TOKEN')
@bot.listen()
async def on_ready(event: hikari.ShardReadyEvent):
print('Bot is ready!')
bot.run()await bot.rest.create_message(channel=CHANNEL_ID, content='Hello from Hikari!')Advanced usage
Where the library earns its place over a simpler alternative.
@bot.listen(hikari.MessageCreateEvent)
async def on_message(event: hikari.MessageCreateEvent):
if event.content == '!ping':
await event.message.respond('Pong!')import lightbulb
plugin = lightbulb.Plugin('example')
@plugin.command()
@lightbulb.command('hello', 'Say hello')
@lightbulb.implements(lightbulb.SlashCommand)
async def hello(ctx):
await ctx.respond('Hello!')
bot.add_plugin(plugin)@bot.listen(hikari.ReactionAddEvent)
async def on_reaction(event: hikari.ReactionAddEvent):
print(f'{event.user_id} reacted with {event.emoji}')await bot.update_presence(activity=hikari.Activity(name='with Hikari', type=hikari.ActivityType.PLAYING))member = await bot.rest.fetch_member(guild=GUILD_ID, user=USER_ID)
print(member.username)Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- hikari.errors.UnauthorizedError
- Occurs if the bot token is invalid or missing permissions. Check token and bot permissions.
- hikari.errors.RateLimitTooLongError
- Occurs when hitting Discord rate limits. Implement retry mechanisms or delays.
- hikari.errors.NotFoundError
- Occurs when a resource (channel, message, user) is not found. Verify IDs and availability.
Best practices
- Use async/await for all I/O operations to maintain performance.
- Leverage Lightbulb for command management and modularity.
- Handle exceptions in events to prevent bot crashes.
- Use environment variables for bot tokens and sensitive information.
- Monitor rate limits and respect Discord API constraints.
Background
Why it exists, and what it was reacting to.
Hikari was created to provide a highly performant and robust framework for building Discord bots in Python. It emphasizes type safety, concurrency, and modern Python async features. Developers use Hikari for building scalable, reliable, and feature-rich Discord bots.
