Table of Contents

Class CommandModule

Namespace
Signal.Application.Commands
Assembly
Signal.Application.dll

Groups several commands as [Command] methods. Parameters are bound from the message text by type; CommandContext and CancellationToken parameters are injected. A new module instance is created (with constructor injection from the message scope) for every execution and disposed afterwards.

public abstract class CommandModule
Inheritance
CommandModule
Derived
Inherited Members

Remarks

Methods may return void, Task, Task<TResult>, ValueTask or ValueTask<TResult>. Preconditions on the module class apply to all its commands.

Properties

Context

The context of the executing command. Set by the framework before the method is invoked.

public CommandContext Context { get; }

Property Value

CommandContext

Methods

ReactAsync(string)

Reacts to the triggering message with an emoji.

protected Task ReactAsync(string emoji)

Parameters

emoji string

The reaction emoji.

Returns

Task

A task that completes when the reaction was sent.

ReplyAsync(string, bool?)

Replies into the conversation of the triggering message.

protected Task<SendResult> ReplyAsync(string text, bool? quote = null)

Parameters

text string

The reply text.

quote bool?

Quote the triggering message; defaults to Signal:Commands:QuoteReplies.

Returns

Task<SendResult>

The send result.

RunInBackgroundAsync(Func<BackgroundWork, Task>)

Runs slow work after the command returns, so later messages in the conversation aren't held up. The work gets its own DI scope; resolve services from Services rather than capturing the module's.

protected ValueTask RunInBackgroundAsync(Func<BackgroundWork, Task> work)

Parameters

work Func<BackgroundWork, Task>

The work; reply with ReplyAsync(string).

Returns

ValueTask

A task that completes when the work was queued (not when it ran).

Examples

[Command("report")]
public async Task ReportAsync()
{
    await ReplyAsync("Working on it…");
    await RunInBackgroundAsync(async work => await work.ReplyAsync(await BuildReportAsync(work.CancellationToken)));
}

ScheduleReplyAsync(string, DateTimeOffset, TimeSpan?)

Schedules a reply into the conversation of the triggering message (reminders, digests).

protected Task<ScheduledMessage> ScheduleReplyAsync(string text, DateTimeOffset dueAt, TimeSpan? repeatEvery = null)

Parameters

text string

The text.

dueAt DateTimeOffset

When to send it.

repeatEvery TimeSpan?

Repeat interval (at least one minute), or null to send once.

Returns

Task<ScheduledMessage>

The scheduled message; keep its id to cancel it with IMessageScheduler.