Commands in the message box
Declare commands to add slash commands the user types in Fluxer’s message box, such as /summarize. When the user sends a message that starts with one, FluxPlugs takes the send: Fluxer never sees it, the box is emptied, and your handler runs with what the user typed after the name. It needs FluxPlugs 0.5.0-beta.1 or later.
Register a command
Section titled “Register a command”This api-example adds /count, which reads the newest messages of the open conversation and shows how many came from other people. Reading messages needs message_content as well; see read recent messages.
cleanup.push( await api.commands.register( { name: "count", description: "Counts recent messages from other people", usage: "[how many]", }, async (invocation) => { const limit = Math.min(Math.max(Number(invocation.input) || 50, 1), 100); try { const messages = await api.messages.recent({ limit }); const others = messages.filter((message) => !message.flags.self); await api.ui.notify( `${others.length} of the last ${messages.length} messages are from other people.`, ); } catch { await api.ui.notify("Could not read the conversation. Try again."); } }, ),);The handler gets a CommandInvocation:
| Field | Value |
|---|---|
| name | The command’s name, without the slash |
| input | What followed the name, trimmed, up to 4000 characters; line breaks stay |
| channelId | The conversation the user had open |
| guildId | The community, when the conversation is in one |
If the handler throws or rejects, the user sees a notice that the command failed. Catch errors yourself to say what went wrong.
Names and descriptions
Section titled “Names and descriptions”- name starts with a lowercase letter, followed by lowercase letters, digits or dashes, up to 32 characters (
^[a-z][a-z0-9-]{0,31}$). - description is 1 to 100 characters, and usage, what goes after the name such as
<question>, is up to 60. Neither may contain control or text-direction characters. - A plugin may register 32 commands, and all plugins together 128.
| Error | Meaning |
|---|---|
command_name_reserved |
The name is one of Fluxer’s own commands: shrug, tableflip, unflip, me, spoiler, tts, nick, kick, ban, msg, saved, sticker and gif |
command_name_taken |
Another plugin registered it first; the first one keeps it |
resource_limit |
The plugin has 32 commands, or all plugins together have 128 |
commands_unavailable |
The runtime can’t watch the message box on this page; skip the feature |
What runs a command
Section titled “What runs a command”FluxPlugs looks at each send from the message box: Enter, Ctrl+Enter or Cmd+Enter, or a click on the send button. It takes the send when the text starts with / and the name of a registered command, followed by a space, a line break or nothing. Names are compared without case. Anything else, including an unknown command, goes to Fluxer as usual.
FluxPlugs leaves the send alone:
- on Shift+Enter, which adds a new line;
- while the user is composing text with an input method (IME);
- while Fluxer’s own suggestion list (commands, mentions or emoji) is open, because that list owns Enter (see below);
- in the box for editing a message;
- in a new forum post’s body and a new thread’s first message, where Fluxer runs no slash commands either (no command list appears there);
- when no conversation is open.
In Fluxer’s command list
Section titled “In Fluxer’s command list”While the user types / and part of a name, Fluxer lists its own commands above the message box. Registered commands whose names contain the typed text join the end of that list, up to 10, with their usage, description and the plugin’s name. When none of Fluxer’s commands match, they appear in a list drawn like Fluxer’s.
The arrow keys move through Fluxer’s commands and the plugins’ as one list. Enter, Tab or a click puts the highlighted command in the box followed by a space, as picking one of Fluxer’s does; Escape closes the list until the text changes. Once the name is typed out, a line above the box says: “Enter runs /name with Plugin. It won’t be posted.” The next Enter runs the command.
The list and that line are the user’s only sign that their text won’t reach the chat, so keep the description short and clear about what the command does.
A user action
Section titled “A user action”Running a command counts as a user action in your plugin, like a click in its UI. It opens a 1.5-second window that the first gated call uses up, such as messages.recent, messages.insertText, clipboard.writeText or network.requestServer. Make that call first, before anything slow. A second gated call needs another action: show the result in a view with buttons, and copy or type into the message box when the user clicks one.
While the plugin is off
Section titled “While the plugin is off”FluxPlugs takes a command only while the plugin that owns it is running. While the plugin is turned off, /name … is plain text and gets posted like any other message. Say so in your plugin’s description if your commands carry text the user wouldn’t want posted.
For 15 seconds after a plugin stops, for example while it restarts because a setting changed, a send of one of its commands is held back: the text stays in the box and a line above it says the plugin isn’t running. If FluxPlugs takes a send but can’t reach the plugin’s handler, it puts the text back in the box.
FluxPlugs is an independent project, not affiliated with or endorsed by Fluxer.