LittleSpace Network Developers
Sign in

Commands

Fluxer doesn’t have slash commands for bots yet, so bots use prefix commands: a message that starts with ! and a command name. fluxkit also answers when someone mentions the bot instead, like @dicebot roll 20.

#Adding a command

JavaScript
bot.command('ping', ({ reply }) => reply('pong!'));

The handler gets one object with everything about the message:

NameWhat it is
reply(content)Replies to the message without pinging the person
messageThe full message, including author, channel_id and guild_id
argsThe words after the command, as an array
argTextEverything after the command as one string
commandThe name or alias that was typed
clientYour bot

#Arguments

Words are split on spaces, and quotes keep words together:

JavaScript
bot.command('say', ({ reply, args }) => reply(args.join(' | ')));

!say hello "big world" again replies hello | big world | again.

For a command that takes free text, use argText instead:

JavaScript
bot.command('echo', ({ reply, argText }) => reply(argText || 'Say something after !echo'));

#Descriptions and aliases

Pass options before the handler:

JavaScript
bot.command('roll', { aliases: ['r', 'dice'], description: 'Roll a die, like !roll 20' }, ({ reply, args }) => {
  const sides = Number(args[0] ?? 6);
  if (!Number.isInteger(sides) || sides < 2 || sides > 1000) return reply('Give me a number from 2 to 1000.');
  return reply(`🎲 ${Math.ceil(Math.random() * sides)}`);
});

bot.commands.list() gives you every command, which makes a help command short:

JavaScript
bot.command('help', ({ reply, client }) =>
  reply(client.commands.list().map((c) => `**!${c.name}** ${c.description}`).join('\n')));

Command names aren’t case-sensitive, so !PING works too.

#A different prefix

JavaScript
const bot = new FluxerClient({ instance: 'https://littlespace.chat', token, prefix: '?' });
// or more than one:
const bot2 = new FluxerClient({ instance: 'https://littlespace.chat', token, prefix: ['!', 'cub '] });

#Who can use a command

guildOnly ignores the command in DMs. check runs before the handler, and the command only runs if it returns true:

JavaScript
import { Permissions, hasPermission } from '@littlespace/fluxkit';

bot.command('clear', {
  guildOnly: true,
  check: ({ client, message }) => hasPermission(client.permissionsFor(message), Permissions.MANAGE_MESSAGES),
}, async ({ reply }) => {
  await reply('Only people who can manage messages see this.');
});

When a check fails, the bot says nothing. That keeps it from being noisy, but you can reply from inside check if you’d rather explain.

#Errors

If a handler throws, fluxkit emits commandError instead of crashing the bot:

JavaScript
bot.on('commandError', (err, message) => {
  console.error(`!${message.content} failed:`, err);
});

#What fluxkit ignores

Commands don’t run for messages from bots, from webhooks, or from your own bot, so two bots can’t set each other off.