Part 1 - Hikari Bot#

First, grab your bot’s token from the Discord Developer Portal (refer to Making your Discord Bot Application) and put it in the .env file, like so:

BOT_TOKEN=your_bot_token

Next, in bot.py paste the following:

 1import asyncio
 2import os
 3
 4import dotenv
 5import hikari
 6
 7dotenv.load_dotenv()
 8
 9bot = hikari.GatewayBot(
10    os.environ["BOT_TOKEN"],
11    intents=hikari.Intents.ALL,
12)
13
14
15@bot.listen()
16async def on_message_create(event: hikari.GuildMessageCreateEvent) -> None:
17    if not event.is_human or not event.content:
18        return
19
20    if event.content.strip() == "+ping":
21        await event.message.respond(
22            f"Pong! Latency: {bot.heartbeat_latency*1000:.2f}ms"
23        )
24
25
26if __name__ == "__main__":
27    if os.name == "nt":
28        # we are running on a Windows machine, and we have to add this so
29        # the code doesn't error :< (it most likely will error without this)
30        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
31
32    bot.run()

Now save bot.py and run it:

python bot.py

You should see an output similar to this:

oooo         o8o  oooo                            o8o       光 2.0.0.dev110 [47bf3fcb]
`888         `"'  `888                            `"'       © 2021-present davfsa - MIT license
 888 .oo.   oooo   888  oooo   .oooo.   oooo d8b oooo       interpreter:   CPython 3.10.5
 888P"Y88b  `888   888 .8P'   `P  )88b  `888""8P `888       running on:    AMD64 Windows 10
 888   888   888   888888.     .oP"888   888      888       installed at:  C:\Users\Nova\Documents\my_bot\.venv\lib\site-packages\hikari
 888   888   888   888 `88b.  d8(  888   888      888       documentation: https://hikari-py.dev/hikari
o888o o888o o888o o888o o888o `Y888""8o d888b    o888o      support:       https://discord.gg/Jx4cNGG

I 2022-08-13 16:38:07,798 hikari.bot: you can start 999 sessions before the next window which starts at 2022-08-13 17:38:11.748231+00:00; planning to start 1 session...
I 2022-08-13 16:38:08,282 hikari.gateway.0: shard is ready: 1 guilds, Hikari#1093 (1007678609466601492), session '1868778c46c81d612853915354a51f37' on v8 gateway
I 2022-08-13 16:38:08,291 hikari.bot: started successfully in approx 0.79 seconds

Now go into the server you invited your bot to, and send +ping.

The bot should respond with Pong! and it’s heartbeat latency:

_static/ping_1.png

Congratulations, you’ve just run your first Hikari bot!

Now let’s go through what everything does

  • Line 1-5 - Import the asyncio, os, dotenv and hikari modules

  • Line 7 - Load the .env file

  • Line 9-12 - Create a bot using that token, and all Discord intents
  • Line 15-23 - The bot listens for messages sent in guilds (servers)
    • If the message author is not a human or the message has no text content (though it may have attachments), it ignores it

    • Otherwise, it checks if the message content is +ping and if it is, the bot responds with Pong! and it’s heartbeat latency

  • Line 26-32
    • If we are on a Windows machine, we have to add line 30 to stop a possible asyncio error from occuring

    • And finally, run the bot!

This bot works, but to add more commands other than +ping would be a huge hassle, so this is where Lightbulb comes in…