Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

2. Making Commands

12944qwerty edited this page Sep 6, 2022 · 6 revisions

Commands

Creating Commands

In this Chapter, we create a "say" command.

Assuming you already have the Base from the last Chapter in your index.js, lets look at this Function

[...]
  startPlugin() {
  }
[...]

First, we have the startPlugin() method, we will register our Command in there. We do this by using Replugged's powercord.api.commands.registerCommand() Function, lets slap that into the startPlugin() method!

[...]
  startPlugin() {
    powercord.api.commands.registerCommand({
      command: 'say',
      description: 'Sends the specified arguments.',
      usage: '{c} [ ...arguments ]',
      executor: (args) => ({
        send: true,
        result: args.join(' ')
      })
    };)
  }
[...]

Lets see what we just did there. The property command specifies the name of the command the user has to type, the description property displays the assigned value next to your command.

usage tells the user how to use the Command, where {c} is a shortcut for the users command.

Now to the most important part, the executor, this part works and sends your result. The executor can take the arguments (things typed behind the command), that's where the (args)part comes from. It takes your arguments and stores it in an Array. Next we need to send the result, so we create another object to specify whether or not to send the args into chat. If send is true, it will send the value of result into chat, if set to false, it will create a message only visible to you with the value of result

here, result is args.join('') since by default, Replugged splits args by spaces, so "{c} Hello, my Name is Sam" would be seen as ["Hello", "my", "Name", "is", Sam"]

Unregistering Commands

It is important to Unregister your Commands when you Unload your plugin, so we're going to add this in our pluginWillUnload() function.

[...]
  pluginWillUnload() {
    powercord.api.commands.unregisterCommand('say'); // replace say with the command you registered
  }
[...]
Clone this wiki locally