Command Handlers#
Hikari does not have an inbuilt command handler, but you can pick from some commonly used ones for hikari:
Given below are basic example of how these command handlers work.
hikari-lightbulb#
Lightbulb is an easy to use command handler with a simple interface and many other utils & extensions.
Github: tandemdude/hikari-lightbulb
Example command
import lightbulb
from hikari import Intents
intents = Intents(Intents.ALL_UNPRIVILEGED | Intents.MESSAGE_CONTENT)
# lightbulb.BotApp is a subclass of hikari.GatewayBot
bot = lightbulb.BotApp("token", "prefix", intents=intents)
# adding the command to bot
@bot.command
# adding information about the command
@lightbulb.command("ping", "replies with pong")
# mentioning the command type
@lightbulb.implements(lightbulb.SlashCommand, lightbulb.PrefixCommand)
# command callback
async def ping(context: lightbulb.Context) -> None:
await context.respond("pong")
bot.run()
hikari-crescent#
An application command only command handler which allows you to make app commands using simple functions and classes.
Github: magpie-dev/hikari-crescent
Example Command
import crescent
bot = crescent.Bot("token")
# using command function
@bot.include # adding the command to the bot
@crescent.command # defining command type
async def ping(context: crescent.Context) -> None: # callback
await context.respond("Pong!")
# using classes
@bot.include
@crescent.command(name="echo")
class Echo:
text: crescent.option(type=str, description="the text to send.")
async def callback(self, context: crescent.Context) -> None: # callback function
await context.respond(self.text)
bot.run()
hikari-tanjun#
A flexible command framework designed to extend Hikari.
It allows you to have more control over the workflow than the other command handlers.
Github: FasterSpeeding/Tanjun
Example Command
import hikari
import tanjun
# defining a GatewayBot for tanjun handler to use
gatewaybot = hikari.GatewayBot(
"token",
intents=hikari.Intents(
hikari.Intents.ALL_UNPRIVILEGED | hikari.Intents.MESSAGE_CONTENT
),
)
client = tanjun.Client.from_gateway_bot(gatewaybot).add_prefix("prefix")
component = tanjun.Component(name="ping command component")
client.add_component(component) # adding the component to bot.
# a simple message ping command
@component.with_command
@tanjun.as_message_command("ping")
async def ping(context: tanjun.abc.Context) -> None:
await context.respond("pong!")
# a simple slash ping command
@component.with_command
@tanjun.as_slash_command("ping", "slash ping command")
async def ping_slash(context: tanjun.abc.Context) -> None:
await context.respond("pong!")
gatewaybot.run()