Skip to content

Slash commands & interactions ​

Slash commands, buttons, select menus, modals and autocomplete. Interactions arrive through the gateway, so this needs Config.Gateway.enabled = true. Registering a command without the gateway warns in the console: nobody would answer it.

Commands ​

lua
exports.np_discord:RegisterCommand({
  name = 'kick', description = 'Kick a player from the game server', permissions = { 'KICK_MEMBERS' },
  options = {
    { type = 4, name = 'id', description = 'Server id', required = true },
    { type = 3, name = 'reason', description = 'Reason' },
  },
}, function(interaction, options, source)
  if not GetPlayerName(options.id) then return { content = 'Nobody has that id.', ephemeral = true } end
  DropPlayer(options.id, options.reason or 'Kicked')
  return ('Kicked %d.'):format(options.id)
end)
  • Handlers get (interaction, argument, source). For a command, argument is a flat name → value table of the options (options._sub / options._group for subcommands). source is the in-game player that belongs to the Discord user, or false.
  • A string or message returned from the handler is sent as the reply. Answer within three seconds, or call InteractionDefer first and InteractionEdit later.
  • A handler that throws answers "Something went wrong." (ephemeral) and logs the error.
  • name: 1–32 letters, digits, _ or - (lower-cased). options are Discord's own option objects.
  • Commands are only re-sent to Discord when they changed, so restarting a resource does not re-register everything.
  • When the resource that registered a command stops, the command stays on Discord and answers "This is not available right now." until it is registered again. UnregisterCommand(name) removes it from Discord everywhere.

Where a command exists ​

def key Registered
none In the main guild
guild = 'lspd' In that guild
guilds = { 'main', 'lspd' } In each of these
guilds = 'all' In every configured guild
global = true Globally: every guild the bot is in, and DMs

One handler serves all of them; interaction.guild_id says where it was used.

A global command can be limited with contexts: { 'guild', 'dm', 'private' } (servers, DMs with the bot, group DMs). contexts = { 'dm' } makes a command that only shows up in DMs with the bot, for answers that are nobody else's business (np_passwords uses it). contexts on a command that is not global fails with invalid_command.

lua
exports.np_discord:RegisterCommand({
  name = 'mycar', description = 'Where did I park?', global = true, contexts = { 'dm' },
}, function(interaction, options, source) … end)

Who may use it ​

permissions only sets who sees the command by default; server admins can change that in Discord's integration settings. Check interaction.member (or HasRole(interaction.user.id, …)) yourself when a command is sensitive:

lua
}, function(interaction, options, source)
  if not exports.np_discord:HasRole(interaction.user.id, 'admin') then
    return { content = 'Admins only.', ephemeral = true }
  end
  …
end)

Buttons and select menus ​

lua
exports.np_discord:SendMessage('applications', {
  content = 'New application #42',
  buttons = { { id = 'app:accept:42', label = 'Accept', style = 'success' }, { id = 'app:deny:42', label = 'Deny', style = 'danger' } },
})

exports.np_discord:OnComponent('app:', function(interaction, values, source)
  local action, id = interaction.data.custom_id:match('^app:(%a+):(%d+)$')
  exports.np_discord:InteractionUpdate(interaction, { content = ('Application #%s: %s'):format(id, action) })
end)

OnComponent(prefix, handler) answers every button and select menu whose custom_id starts with prefix (the longest matching prefix wins). Put your data after the prefix. For a button values is {}, for a select menu the chosen values. Handlers registered by a resource are removed when that resource stops.

Modals ​

lua
exports.np_discord:InteractionModal(interaction, {
  id = 'report:' .. interaction.user.id, title = 'Report a player',
  fields = {
    { id = 'who', label = 'Player', style = 'short', required = true },
    { id = 'what', label = 'What happened', style = 'paragraph', max = 1000 },
  },
})

exports.np_discord:OnModal('report:', function(interaction, fields, source)
  exports.np_discord:Log('reports', { title = 'Report', description = fields.what, fields = { { 'Player', fields.who } } })
  return { content = 'Thanks, staff has it.', ephemeral = true }
end)

A modal has up to five fields; required defaults to true. OnModal(prefix, handler) gets { [field id] = text }.

Autocomplete ​

OnAutocomplete(command, handler) gets { name, value } of the focused option and returns up to 25 choices ({ { name = 'Shown', value = 'sent' } }). The option needs autocomplete = true in its definition.

Replying ​

Export
InteractionReply(interaction, message, ephemeral?) Reply. A second call sends a follow-up
InteractionDefer(interaction, ephemeral?) "Thinking…" for up to 15 minutes, then InteractionEdit or InteractionFollowup
InteractionEdit(interaction, message) Edit the original reply
InteractionFollowup(interaction, message, ephemeral?) Another message
InteractionDelete(interaction) Delete the original reply
InteractionUpdate(interaction, message) Components: change the message the button is on
InteractionDeferUpdate(interaction) Components: acknowledge only
InteractionModal(interaction, modal) Open a modal: { id, title, fields = { { id, label, style = 'short' | 'paragraph', required, value, placeholder, min, max } } }
InteractionAutocomplete(interaction, choices) Answer an autocomplete

ephemeral = true (argument, or a key in a returned message) shows the reply only to the user who used the command.

Every interaction also fires np_discord:interaction (interaction, source | false) before its handler runs.