Table of Contents

Class CommandContext

Namespace
Discord.Commands
Assembly
Discord.Net.Commands.dll

The context of a command which may contain the client, user, guild, channel, and message.

public class CommandContext : ICommandContext
Inheritance
CommandContext
Implements
Inherited Members

Examples

An example of how this class is used the command system can be seen below:

using Discord.Commands;

// Keep in mind your module **must** be public and inherit ModuleBase.
// If it isn't, it will not be discovered by AddModulesAsync!
public class InfoModule : ModuleBase<SocketCommandContext>
{
    
}
public class CommandHandler
{
    private readonly DiscordSocketClient _client;
    private readonly CommandService _commands;

    // Retrieve client and CommandService instance via ctor
    public CommandHandler(DiscordSocketClient client, CommandService commands)
    {
        _commands = commands;
        _client = client;
    }
    
    public async Task InstallCommandsAsync()
    {
        // Hook the MessageReceived event into our command handler
        _client.MessageReceived += HandleCommandAsync;

        // Here we discover all of the command modules in the entry 
        // assembly and load them. Starting from Discord.NET 2.0, a
        // service provider is required to be passed into the
        // module registration method to inject the 
        // required dependencies.
        //
        // If you do not use Dependency Injection, pass null.
        // See Dependency Injection guide for more information.
        await _commands.AddModulesAsync(assembly: Assembly.GetEntryAssembly(), 
                                        services: null);
    }

    private async Task HandleCommandAsync(SocketMessage messageParam)
    {
        // Don't process the command if it was a system message
        var message = messageParam as SocketUserMessage;
        if (message == null) return;

        // Create a number to track where the prefix ends and the command begins
        int argPos = 0;

        // Determine if the message is a command based on the prefix and make sure no bots trigger commands
        if (!(message.HasCharPrefix('!', ref argPos) || 
            message.HasMentionPrefix(_client.CurrentUser, ref argPos)) ||
            message.Author.IsBot)
            return;

        // Create a WebSocket-based command context based on the message
        var context = new SocketCommandContext(_client, message);

        // Execute the command with the command context we just
        // created, along with the service provider for precondition checks.
        await _commands.ExecuteAsync(
            context: context, 
            argPos: argPos,
            services: null);
    }
}

Constructors

CommandContext(IDiscordClient, IUserMessage)

Initializes a new CommandContext class with the provided client and message.

public CommandContext(IDiscordClient client, IUserMessage msg)

Parameters

client IDiscordClient

The underlying client.

msg IUserMessage

The underlying message.

Properties

Channel

Gets the IMessageChannel that the command is executed in.

public IMessageChannel Channel { get; }

Property Value

IMessageChannel

Client

Gets the IDiscordClient that the command is executed with.

public IDiscordClient Client { get; }

Property Value

IDiscordClient

Guild

Gets the IGuild that the command is executed in.

public IGuild Guild { get; }

Property Value

IGuild

IsPrivate

Indicates whether the channel that the command is executed in is a private channel.

public bool IsPrivate { get; }

Property Value

bool

Message

Gets the IUserMessage that the command is interpreted from.

public IUserMessage Message { get; }

Property Value

IUserMessage

User

Gets the IUser who executed the command.

public IUser User { get; }

Property Value

IUser