Hikari

Language: Python

Web

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.

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.

Installation

pip: pip install hikari
conda: conda install -c conda-forge hikari

Usage

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.

Creating a simple bot

import hikari

bot = hikari.GatewayBot(token='YOUR_BOT_TOKEN')

@bot.listen()
async def on_ready(event: hikari.ShardReadyEvent):
    print('Bot is ready!')

bot.run()

Creates a basic Hikari bot that prints a message when the bot is ready.

Sending a message

await bot.rest.create_message(channel=CHANNEL_ID, content='Hello from Hikari!')

Sends a message to a specific channel using the bot's REST client.

Listening to messages

@bot.listen(hikari.MessageCreateEvent)
async def on_message(event: hikari.MessageCreateEvent):
    if event.content == '!ping':
        await event.message.respond('Pong!')

Listens to new messages and responds to a '!ping' command with 'Pong!'.

Using commands with Lightbulb

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)

Demonstrates creating a slash command using the Lightbulb extension for Hikari.

Handling reactions

@bot.listen(hikari.ReactionAddEvent)
async def on_reaction(event: hikari.ReactionAddEvent):
    print(f'{event.user_id} reacted with {event.emoji}')

Logs whenever a user reacts to a message.

Bot presence and status

await bot.update_presence(activity=hikari.Activity(name='with Hikari', type=hikari.ActivityType.PLAYING))

Updates the bot's presence to show a custom activity message.

Working with guild members

member = await bot.rest.fetch_member(guild=GUILD_ID, user=USER_ID)
print(member.username)

Fetches information about a specific guild member.

Error Handling

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.