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 debug
DiscordClient : setVerbose ( true )
Register a handler for the onReady
signal:
DiscordClient .eventManager. onReady : connect ( function ()
print ( ` ππ {DiscordClient.discordUser.username} is online! ππ ` )
Within the handler, we can now register our slash commands.
First, we request Discord for access to slash commands.
local permissions = DiscordLuau .PermissionsBuilder. new ()
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
: setGuildPermissions ( permissions )
: addContext ( DiscordLuau .CommandBuilder.Context.Guild)
: addIntegration ( DiscordLuau .CommandBuilder.IntegrationType.GuildCommand)
-- Register our slash commands
DiscordClient .discordApplication
: setSlashCommandsAsync ({ pingCommand })
print ( " Registering slash commands... " )
DiscordClient . discordApplication : fetchSlashCommandsAsync (): after ( function ( ...)
-- Optionally log information about our slash command
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 ()
DiscordLuau .PermissionsBuilder.Permissions.UseApplicationCommands
-- Create a slash command using `CommandBuilder`
local pingCommand = DiscordLuau .CommandBuilder
: setGuildPermissions ( permissions )
: addContext ( DiscordLuau .CommandBuilder.Context.Guild)
: setType ( DiscordLuau .CommandBuilder.Type.ChatInput)
-- Register our slash commands
DiscordClient .discordApplication
: setSlashCommandsAsync ({ pingCommand })
print ( " Registering slash commands... " )
DiscordClient . discordApplication : fetchSlashCommandsAsync (): after ( function ( ...)
-- Optionally log information about our slash command
Thatβs all! For more details, refer to the relevant docs;