Skip to content

Getting started with hxdiscord

Furret edited this page May 11, 2023 · 10 revisions

Notice

Before we start, it is recommended to look at some Haxe tutorials if you're new at this, you won't probably understand most of the things if that's the case. Go watch some tutorials then come back later (https://www.youtube.com/watch?v=I50RiqbzApY&list=PLnweXVCg6yWodTlo3BQXicKJuGrTp_yeV totally recommended)

Getting started with hxdiscord

You will need Haxe installed on your computer (obviously), also it it recommended to install 4.2.5, not 4.3.0, the newer version is kinda broken for hxdiscord

You'll have to install the libraries, you can use these commands

haxelib git hxdiscord https://github.com/FurretDev/hxdiscord.git (requires git installed)

haxelib install hxdiscord

Then we can proceed to make a new Haxe file, there's a code in the library repo that you can use

How does Discord Bots work?

Basically, every Discord Bot has to be connected to the Discord Gateway, it's a WebSocket connection, and from there it tells you basically everything that happens. hxdiscord works in this way

If a new event has been detected. hxdiscord will send a event hook function related to that event (And if possible, it contains an object)

Example:

{"t": "SOMETHING_HAPPENED"} -> (hxdiscord notices) -> onSomethingHappened()

Another example (actually real):

{"t": "MESSAGE_CREATE"} -> (hxdiscord notices) -> onMessageCreate(m)

Responding to a message

Discord Bots works with events. For example, onMessageCreate, onMemberBan, etc..

If you made your Main.hx file or whatever it is called from scratch without copying it. You'll have to import this class import hxdiscord.types.Message;

Replying to a message will require the onMessageCreate function in your project. It is easy to add one of those. For example, let's say that your client variable is called "Client" (Previously declared when initializing the bot). To add a function, you have to add a event hook for incoming messages. It is done this way

Client.onMessageCreate = (m:Message) -> {
      if (m.content == "hello") {
          m.reply({content:"Hey there!"}, true);
      }
}

In this piece of code we added a incoming messages event hook, and it is made so if the message equals to "hello", the bot will send a Message Object replying back saying "Hey there!"