Table of Contents

Class RequireContextAttribute

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

Requires the command to be invoked in a specified context (e.g. in guild, DM).

[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class RequireContextAttribute : PreconditionAttribute
Inheritance
RequireContextAttribute
Inherited Members

Constructors

RequireContextAttribute(ContextType)

Requires the command to be invoked in the specified context.

public RequireContextAttribute(ContextType contexts)

Parameters

contexts ContextType

The type of context the command can be invoked in. Multiple contexts can be specified by ORing the contexts together.

Examples

[Command("secret")]
[RequireContext(ContextType.DM | ContextType.Group)]
public Task PrivateOnlyAsync()
{
    return ReplyAsync("shh, this command is a secret");
}

Properties

Contexts

Gets the context required to execute the command.

public ContextType Contexts { get; }

Property Value

ContextType

ErrorMessage

When overridden in a derived class, uses the supplied string as the error message if the precondition doesn't pass. Setting this for a class that doesn't override this property is a no-op.

public override string ErrorMessage { get; set; }

Property Value

string

Methods

CheckPermissionsAsync(ICommandContext, CommandInfo, IServiceProvider)

Checks if the command has the sufficient permission to be executed.

public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)

Parameters

context ICommandContext

The context of the command.

command CommandInfo

The command being executed.

services IServiceProvider

The service collection used for dependency injection.

Returns

Task<PreconditionResult>