Skip to content

Interaction Commands

Table of Contents


Introduction

Slash commands are supposedly “new generation” of bots by Discord’s vision. The idea is that all bots do rely on one prefix that is / and so people do not have to guess prefixes of any bot they encounter whether it would be in a server or a random bot they have just added it to an server they’re in. Slash commands has been around since April 2021 and has since been enforced into verified bots due to message content intent updates by DIscord!

Notes

  • This guide requires you to have put onInteractionCreate onto your events code otherwise the example code for replying to the slash command will not work AT ALL
  • You can create up to 100 slash commands both in global (across all guilds) and private (limited to one guild)
  • The function $createApplicationCommand must be only executed for once, otherwise, you’ll update the application command.
  • Highly recommend reading the usage for functions to get an idea
  • The command code examples here are for command handler setups, so please do not put them directly into your index.js or whatever your main file is. You can modify the commands to match with the index.js ones

Creating the slash command

Slash commands can be created using $createApplicationCommand, it is a function that creates an slash command based on what you like such as options, name, description, etc.

It is not a way to create a code to respond to a slash and SHOULD NOT be put under a aoi.js interaction command of a non existing slash command for example. Similar to running any regular functions, $createApplicationCommand can be run on anything but I recommend using eval command or at least a prefix command with it.

Here’s a example usage of this function:

1
$createApplicationCommand[global;name;description;true;true;slash]

The type global is the global (across all guilds) slash command, if you want to create a private (limited to one guild) slash command for one server then replace the word global with the server id manually or you can use $guildID to get the id automatically. Be aware that $guildID does not mean global (across all guilds) commands so therefore, you’re creating a private (limited to one guild) slash command for a server.

Let’s have a simple prefix command that executes $createApplicationCommand

1
module.exports = {
2
name: "create",
3
code: `$createApplicationCommand[global;ping;Test command.;true;true;slash]
4
Created the \`ping\` slash command.`
5
};

This will create a global (across all guilds) slash command named ping with our description called “Test command.”, running it will do nothing as we still need to create a aoi.js interaction command to respond to it! What if we also wanted a example of creating an private (limited to one guild) slash command?

Here’s an example of creating a private (limited to one guild) slash command:

1
module.exports = {
2
name: "create",
3
code: `$createApplicationCommand[$guildID;ping;Test command.;true;false;slash]
4
I have created a private (limited to one guild) slash command called \`ping\` for this one specific server.`
5
};

Deleting the slash command

To delete a slash command, you can use $deleteApplicationCommand function.

1
$deleteApplicationCommand[visibility;id of the slash command]

visibility here represents the type of the slash command, if it’s global (across all guilds) then you will replace it with global, otherwise, you will replace it with the server id the slash command was created in.

It is possible to get the id of the slash command by using $getApplicationCommandID!

1
$getApplicationCommandID[slash command name;visibility]

Specify the type of the slash command and put it in the second parameter of $deleteApplicationCommand thus our final code is done! Here’s a example.

1
$deleteApplicationCommand[global;$getApplicationCommandID[ping;global]]

Lastly, if it didn’t error the first time but second time it did, then it means that you have successfully deleted the slash command. Restart your Discord if it still appears for you in this case.

Responding to slash commands

We may use $interactionReply to respond to the slash command with the same name we used from $createApplicationCommand which is ping as it’s the slash command name we have chose!

Let’s create a interaction command for slash:

1
module.exports = {
2
name: "ping",
3
type: "interaction",
4
prototype: "slash",
5
code: `$interactionReply[Hi.]
6
`
7
};

Restart your commands using $updateCommands and the slash command should now respond with “Hi.”!

Creating options

$createApplicationCommand function has one extra parameter dedicated to options, it is usually a JSON format that goes like this:

1
$createApplicationCommand[global;exampleslash;Simple example slash command.;true;true;slash;[{
2
"name": "exampleoption",
3
"description": "example slash command option",
4
"required": true,
5
"type": 3
6
}]]

We will then use $slashOption to get the data inputted through the option from Discord. In this example, if our option starts with exampleoption then we will use the same name below on $slashOption thus resulting in $slashOption[exampleoption].

1
module.exports = {
2
name: "exampleslash",
3
type: "interaction",
4
prototype: "slash",
5
code: `$interactionReply[Hello! Your option input is $slashOption[exampleoption]]
6
`
7
};

Optionally, we can also create two options by separating each JSON format with a comma

1
$createApplicationCommand[global;exampleslash;Simple example slash command.;true;true;slash;[{
2
"name": "exampleoption1",
3
"description": "example slash command option",
4
"required": true,
5
"type": 3
6
},{
7
"name": "exampleoption2",
8
"description": "example slash command option2",
9
"required": true,
10
"type": 3
11
}]]

"type": is the type of the slash option, we’re using number 3 which is text type, more information on each option type can be seen at the end of FAQ.

Be aware that you can create up to 25 slash options, so make sure to avoid limits! Getting the data from both of the options should be easy as using multiple of $slashOption as well:

1
module.exports = {
2
name: "exampleslash",
3
type: "interaction",
4
prototype: "slash",
5
code: `$interactionReply[Hello!
6
Your option1 input is $slashOption[exampleoption1]
7
Your option2 input is $slashOption[exampleoption2]
8
]
9
`
10
};

Optional options

Note

When creating multiple options for each slash command, the very first option must be required otherwise it will error out.

You can also make slash command options not required, this can be done by setting "required": option to use false

1
$createApplicationCommand[global;exampleslash;Simple example slash command.;true;true;slash;[{
2
"name": "exampleoption",
3
"description": "example slash command option",
4
"required": false,
5
"type": 3
6
}]]

When there’s no input or if the option hasn’t being touched yet, $slashOption will return nothing as expected!

DM Support

Note

This feature is only available for global (across all guilds) slash commands under the global type from createApplicationCommand. You may need to set the parameter allowDm to false in case of creating a private (limited to one guild) slash command for an server (though, it won’t do anything if it was set to true).

Configuring DM Support

Since aoi.js 6.7.0, it is possible to choose whether or not a global slash command will appear in the bot’s DM itself. This can be done by setting allowDm to either true or false depending on your needs.

1
$createApplicationCommand[global;name;description;true;allowDm (true/false);slash]

Setting the parameter to true will allow your bot’s users to run your bot’s slash commands in it’s DMs, this can be useful for some bots otherwise, it might not be the best idea when it comes to economy bots that has robbing features.

You can disable DM Support for a slash command by setting the said parameter to false.

1
$createApplicationCommand[global;name;description;true;false;slash]

That’s how you configure DM support for a specific global slash command!

That’s it for basic level of slash commands understanding! There’s more to the slash commands feature and if you would like to know on any other guides about slash commands then please, let me know through the comments here!

Frequently asked questions

I have created a slash command but Discord does not show it for me

Discord often has cache problems so it may not display the newly created slash command as a result. Restarting your Discord should fix the problem.

If it still persists then be sure to double check that your slash command is not private (limited to one guild) or at least have executed $createApplicationCommand function!

Is $createApplicationCommand associated with onInteractionCreate?

No, $createApplicationCommand is not part of any event as it can be ran on anything (much like what I said in the very beginning of this post).

However, because slash commands are interactions, any slash commands created from the function are part of onInteractionCreate!

How do I make the slash option at user type return the author id if there’s no one selected?

Make sure that your slash option is not required:

1
$createApplicationCommand[global;exampleslash;Simple example slash command.;true;false;slash;[{
2
"name": "exampleoption",
3
"description": "example slash command option",
4
"required": false,
5
"type": 6
6
}]]

Using $replaceText and $checkCondition, you can replace the empty input from $slashOption with $authorID and so the end result is this code:

1
$replaceText[$replaceText[$checkCondition[$slashOption[exampleoption]==];true;$authorID];false;$slashOption[exampleoption]]

This code does so that if the option is empty then it replaces it with $authorID! You do not have to put the entire code around the entire command so you can use $let with $get to return the code quickly!

1
// Return the code
2
$get[option]
3
4
// Store the code
5
$let[option;$replaceText[$replaceText[$checkCondition[$slashOption[exampleoption]==];true;$authorID];false;$slashOption[exampleoption]]]

This is how the final result should look like

1
module.exports = {
2
name: "exampleslash",
3
type: "interaction",
4
prototype: "slash",
5
code: `$interactionReply[The selected user is $get[option]]
6
7
$let[option;$replaceText[$replaceText[$checkCondition[$slashOption[exampleoption]==];true;$authorID];false;$slashOption[exampleoption]]]
8
`
9
};

Can I have two slash commands under the same name in global (across all guilds) and private (limited to one guild)?

It is possible to create two slash commands under the same name in global (across all guilds) and private (limited to one guild) as Discord allows this by default. Note that you should refrain from accidentally modifying the current one if each one of them has different code than the other one!

What if I wanted to create a slash command under the same name?

Attempting to create a slash command under the same name with it’s current visibility will overwrite the current one! For example if you have used the function to create the same global slash command but with options then it will overwrite the current one to include the options as well!

Consider this as a way to edit your slash commands created (even tho $modifyApplicationCommand does exist)!

How to create a slash command without using any command to execute createApplicationCommand function?

This seems common for aoi.js verified bot’s owners where they’re unable to straight use prefix. As of now, there’s no way around this except for using a ready event. Ready event is a way to execute things when bot starts. This way is NOT RECOMMENDED AT ALL and can result in spamming if you forget to remove the event that has the function itself to create a slash command.

In case you accept the risk, you can make a ready event like this

1
module.exports = {
2
name: "Create slash",
3
type: "ready",
4
channel: "",
5
code: `$createApplicationCommand[global;ping;Test command.;true;true;slash]
6
$log[the slash command ping has been created successfully!]
7
`
8
};

Be sure to remove the command after that.

What list of slash option types can i use?

Here’s a list consisting of types you can use

TypesDescription
SUB_COMMAND (1)Sub command type
SUB_COMMAND_GROUP (2)Same as sub command type but with groups instead
STRING (3)Text Type
INTEGER (4)Integer type. Any Number between -2^53 and 2^53
BOOLEAN (5)Boolean Type. Presents a “true” and “false” as options to choose from
USER (6)User type. Includes all users + bots to select from
CHANNEL (7)Includes all channel types + categories
ROLE (8)Role type. Includes all roles to select from
MENTIONABLE (9)Same as User type but Includes users and roles all in one
NUMBER (10)Number type. Any double between -2^53 and 2^53
ATTACHMENT (11)Attachment type. Let’s user upload an attachment to a option