Skip to content

Event Handling

Cyteon edited this page Jul 17, 2024 · 3 revisions

Adding an event handler

Add the import

import discord_gleam/event_handler

Create a new function, that looks like this:

fn event_handler(bot, packet: event_handler.Packet() {
  
}

(the function name can be anything)

Now we will need to handle packets, to do add this into your function

case packet {
    event_handler.ReadyPacket(ready) -> {
      logging.log(logging.Info, "Logged in as " <> ready.d.user.username)

      Nil
    }
    _ -> Nil
}

Your function should now look like this:

fn event_handler(bot, packet: event_handler.Packet() {
  case packet {
    event_handler.ReadyPacket(ready) -> {
      logging.log(logging.Info, "Logged in as " <> ready.d.user.username)

      Nil
    }
    _ -> Nil
  }
}

Now you can add other packets as you can find in Package Docs under the Packet type

Then modify

discord_gleam.run(bot, [])

To

discord_gleam.run(bot, [event_handler]) //Or your function name

Our event handling system allows for multiple event handlers so you dont have to put all possible events you wanna handle in one function

Clone this wiki locally