Skip to content

Making application commands

Furret edited this page Apr 16, 2023 · 1 revision

Application commands are basically interactions. This can be a slash command, a button, a user interaction, a message interaction.. You can make these in hxdiscord

Remember I said earlier that Discord Bots works with events? There's an event for interactions, but I'm going to talk about these later. First of all we have to import the Application Command class import hxdiscord.types.ApplicationCommand;

Once we imported it, we have to add an onReady function, if you don't have one then make one

Bot.onReady = () -> {
    // code here
}

Inside the onReady function, we have to make those commands. For example

var myCommand:ApplicationCommand = new ApplicationCommand();
myCommand.setTitle("my_command");
myCommand.setDescription("This is my first command, wow!");

This is how you make a command! These are slash commands by default, you can change this by using the change type function. Now, we have the command but, we have to add them into the bot. This is how it's done

Bot.setInteractionCommands([myCommand]);

This is how we push our commands into the bot. We can add multiple commands like this

Bot.setInteractionCommands([myCommand, mySecondCommand]);

you can name them however you want, myCommand and mySecondCommand are just examples

We've got our commands into the bot. Cool, now, let's make the bot respond to them Now, we have to use the onInteractionCreate() function, since, this will be the event that will get triggered when an interaction has been sent. To make them reply, we have to use the reply function, but, we have to specify which command we wanna reply. So, this is how it's done

Bot.onInteractionCreate = (i:Interaction) -> {
      switch(i.name) {
         case "my_command":
             i.reply({content:"Hello world!", true);
      }
}

This is how we reply to an interaction, however, the way I showed in the code is an ephemeral response. What's an ephemeral response? It's when the bot send something back to you, but, it's only visible for you

We can make it non-ephemeral by turning the true parameter to false, like this i.reply({content:"Hello world!"}, false);

There we go! We've successfully made a new command, and also, replied to it

Clone this wiki locally