Skip to main content

Chat Commands

Namespace: DeadworksManaged.Api

Register chat commands using the [ChatCommand] attribute on plugin methods.

ChatCommandAttribute

Marks a plugin method as a handler for a chat command. Can be applied multiple times to map multiple commands to the same handler.

[ChatCommand("rtd")]
public HookResult OnRollTheDice(ChatCommandContext ctx)
{
// Player typed /rtd in chat
return HookResult.Handled;
}

Multiple Commands on One Handler

[ChatCommand("help")]
[ChatCommand("info")]
public HookResult OnHelp(ChatCommandContext ctx)
{
// Handles both /help and /info
return HookResult.Handled;
}

Properties

PropertyTypeDescription
CommandstringThe command string this attribute matches (e.g. "rtd")

ChatCommandContext

Context object passed to every chat command handler.

Properties

PropertyTypeDescription
MessageChatMessageThe raw chat message that triggered the command
CommandstringThe matched command string (e.g. "rtd")
Argsstring[]Arguments following the command, split by whitespace
ControllerCCitadelPlayerController?The player controller who sent the command, or null

Common Pattern

[ChatCommand("mycommand")]
public HookResult OnMyCommand(ChatCommandContext ctx)
{
// Get the player's pawn (in-game entity)
var pawn = ctx.Controller?.GetHeroPawn();
if (pawn == null)
return HookResult.Handled;

// Parse optional arguments
int duration = ctx.Args.Length > 0 && int.TryParse(ctx.Args[0], out var d) ? d : 30;

// Do something with the player...

return HookResult.Handled;
}

Accessing Sender Slot

The sender's player slot is available via the message:

int senderSlot = ctx.Message.SenderSlot;

This is useful for sending targeted messages:

NetMessages.Send(msg, RecipientFilter.Single(ctx.Message.SenderSlot));

ChatMessage

Incoming chat message from a player. Also passed to OnChatMessage() on the plugin base.

PropertyTypeDescription
SenderSlotintPlayer slot index of the sender

Return Values

Chat command handlers must return a HookResult:

  • HookResult.Handled — Command was processed, message consumed
  • HookResult.Stop — Block further processing

See Also