Skip to content

Slash Commands

Slash commands are Discord’s officially supported method of creating commands. They make interacting with your bot easier for users. Slash commands also allow for validation of argument types, ephemeral responses, and so much more.

  1. Import the library and initialize the required modules:

    local DiscordLuau = require("DiscordLuau")
    local DiscordSettings = DiscordLuau.SettingsBuilder.new("<DISCORD_TOKEN>")
    local DiscordClient = DiscordLuau.DiscordClient.new(DiscordSettings)
    -- Optionally enable verbose logging, to make it easier to debug
    DiscordClient:setVerbose(true)
  2. Register a handler for the onReady signal:

    DiscordClient.eventManager.onReady:connect(function()
    print(`πŸŽ‰πŸŽ‰ {DiscordClient.discordUser.username} is online! πŸŽ‰πŸŽ‰`)
    end)
  3. Within the handler, we can now register our slash commands.

  4. First, we request Discord for access to slash commands.

    local permissions = DiscordLuau.PermissionsBuilder.new()
    :addPermission(
    DiscordLuau.PermissionsBuilder.Permissions.UseApplicationCommands
    )
  5. We can now register slash commands to our heart’s content!

    -- Create a slash command using `CommandBuilder`
    local pingCommand = DiscordLuau.CommandBuilder
    .new()
    :setName("ping")
    :setDescription("Pong?")
    :setGuildPermissions(permissions)
    :addContext(DiscordLuau.CommandBuilder.Context.Guild)
    :addIntegration(DiscordLuau.CommandBuilder.IntegrationType.GuildCommand)
    -- Register our slash commands
    DiscordClient.discordApplication
    :setSlashCommandsAsync({ pingCommand })
    :after(function(data)
    print("Registering slash commands...")
    DiscordClient.discordApplication:fetchSlashCommandsAsync():after(function(...)
    -- Optionally log information about our slash command
    print(...)
    end)
    end)
See The Entire Code
local DiscordLuau = require("DiscordLuau")
local DiscordSettings = DiscordLuau.SettingsBuilder.new("<DISCORD_TOKEN>")
local DiscordClient = DiscordLuau.DiscordClient.new(DiscordSettings)
-- Optionally enable verbose logging, to make it easier to debug
DiscordClient:setVerbose(true)
DiscordClient.eventManager.onReady:connect(function()
print(`πŸŽ‰πŸŽ‰ {DiscordClient.discordUser.username} is online! πŸŽ‰πŸŽ‰`)
local permissions = DiscordLuau.PermissionsBuilder.new()
:addPermission(
DiscordLuau.PermissionsBuilder.Permissions.UseApplicationCommands
)
-- Create a slash command using `CommandBuilder`
local pingCommand = DiscordLuau.CommandBuilder
.new()
:setName("ping")
:setDescription("Pong?")
:setGuildPermissions(permissions)
:addContext(DiscordLuau.CommandBuilder.Context.Guild)
:setType(DiscordLuau.CommandBuilder.Type.ChatInput)
-- Register our slash commands
DiscordClient.discordApplication
:setSlashCommandsAsync({ pingCommand })
:after(function(data)
print("Registering slash commands...")
DiscordClient.discordApplication:fetchSlashCommandsAsync():after(function(...)
-- Optionally log information about our slash command
print(...)
end)
end)
end)

That’s all! For more details, refer to the relevant docs;