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.
-
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 debugDiscordClient:setVerbose(true) -
Register a handler for the
onReady
signal:DiscordClient.eventManager.onReady:connect(function()print(`ππ {DiscordClient.discordUser.username} is online! ππ`)end) -
Within the handler, we can now register our slash commands.
-
First, we request Discord for access to slash commands.
local permissions = DiscordLuau.PermissionsBuilder.new():addPermission(DiscordLuau.PermissionsBuilder.Permissions.UseApplicationCommands) -
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 commandsDiscordClient.discordApplication:setSlashCommandsAsync({ pingCommand }):after(function(data)print("Registering slash commands...")DiscordClient.discordApplication:fetchSlashCommandsAsync():after(function(...)-- Optionally log information about our slash commandprint(...)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 debugDiscordClient: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;