Skip to content

Commands

GrafDimenzio edited this page Mar 14, 2026 · 2 revisions

Creating a Command

Commands allow users to interact with your plugin through the server console or other command interfaces.

Each command is represented by a class.

To create a command you must:

  1. Create a new class
  2. Inherit from Command
  3. Apply the Command attribute to the class

The Command attribute contains the following properties:

  • CommandName
    The primary name used to call the command.

  • Aliases
    Alternative names that can also trigger the command.

  • Description
    A short description used by the help command.

  • Parameters
    A list of parameters used by the command.
    These are only displayed in the help command and are not enforced automatically.


Basic Command Example

You must override the Execute method.
This method will be called whenever the command is executed.

Example:

using DSMOOFramework.Commands;

[Command(
    CommandName = "example",
    Aliases = ["test", "awesome"],
    Description = "My Awesome Example Command",
    Parameters = []
    )]
public class ExampleCommand : Command
{
    public override CommandResult Execute(string command, string[] args, ICommandSender sender)
    {
        return "My Awesome Example Command";
    }
}

💡 Note
Similar to Managers and Configs, command classes are automatically created by the framework and support dependency injection.


CommandResult

In the previous example we returned a string, even though the method expects a CommandResult.

This works because CommandResult has an implicit conversion operator, which automatically converts a string into a CommandResult.

A CommandResult contains two properties:

  • Message – The text returned to the user
  • ResultType – Determines how the result is displayed

Available result types:

public enum ResultType
{
  Success,
  Error,
  NoPermission,
  NotFound,
  MissingParameter,
  InvalidParameter,
}

The ResultType affects how the response is displayed:

  • It determines the color of the message
  • It adds a prefix to the response

These should be used to clearly communicate incorrect command usage or errors.


Example Using Result Types

Here is an example that validates command parameters and returns appropriate result types.

using DSMOOFramework.Commands;
using DSMOOFramework.Logger;

[Command(
    CommandName = "example",
    Aliases = ["test", "awesome"],
    Description = "My Awesome Example Command",
    Parameters = ["message"]
    )]
public class ExampleCommand(ILogger logger) : Command
{
    public override CommandResult Execute(string command, string[] args, ICommandSender sender)
    {
        if (args.Length == 0)
            return new CommandResult
            {
                Message = "You must specify an message!",
                ResultType = ResultType.MissingParameter
            };

        var message = args[0];
        logger.Info(message);

        return $"Printed {message} to the console!";
    }
}

PreExecute

You can also override the PreExecute method.

This method is called before Execute.

Behavior:

  • If PreExecute returns ResultType.Success, Execute will be called.
  • If it returns any other result type, the result will be returned immediately and Execute will not run.

This makes PreExecute useful for:

  • validating arguments
  • checking permissions
  • performing early validation

Example:

using DSMOOFramework.Commands;
using DSMOOFramework.Logger;

[Command(
    CommandName = "example",
    Aliases = ["test", "awesome"],
    Description = "My Awesome Example Command",
    Parameters = ["message"]
    )]
public class ExampleCommand(ILogger logger) : Command
{
    public override CommandResult PreExecute(string command, string[] args, ICommandSender sender)
    {
        if (args.Length == 0)
            return new CommandResult
            {
                Message = "You must specify an message!",
                ResultType = ResultType.MissingParameter
            };

        // Returns a default success result and checks for permissions
        return base.PreExecute(command, args);
    }

    public override CommandResult Execute(string command, string[] args, ICommandSender sender)
    {
        var message = args[0];
        logger.Info(message);

        return $"Printed {message} to the console!";
    }
}

Next Steps

Next you should take a look at the available managers provided by the framework:

➡ https://github.com/GrafDimenzio/DSMOO/wiki/Managers

Clone this wiki locally