commands

package
v0.25.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2025 License: MPL-2.0 Imports: 10 Imported by: 2

Documentation

Index

Constants

View Source
const ReactionCommandsKey = "fi.mau.reaction_commands"
View Source
const ReactionMultiUseKey = "fi.mau.reaction_multi_use"
View Source
const UnknownCommandName = "__unknown-command__"

UnknownCommandName is the name of the fallback handler which is used if no other handler is found. If even the unknown command handler is not found, the command is ignored.

Variables

View Source
var IDHTMLParser = &format.HTMLParser{
	PillConverter: func(displayname, mxid, eventID string, ctx format.Context) string {
		if len(mxid) == 0 {
			return displayname
		}
		if eventID != "" {
			return fmt.Sprintf("https://matrix.to/#/%s/%s", mxid, eventID)
		}
		return mxid
	},
	ItalicConverter: func(s string, c format.Context) string {
		return fmt.Sprintf("*%s*", s)
	},
	Newline: "\n",
}

Functions

func DeleteAllReactions added in v0.24.1

func DeleteAllReactions(ctx context.Context, client *mautrix.Client, evt *event.Event)

func DeleteAllReactionsCommandFunc added in v0.24.1

func DeleteAllReactionsCommandFunc[MetaType any](ce *Event[MetaType])

Types

type AllPreValidator

type AllPreValidator[MetaType any] []PreValidator[MetaType]

AllPreValidator can be used to combine multiple PreValidators, such that all of them must return true for the command to be processed further.

func (AllPreValidator[MetaType]) Validate

func (f AllPreValidator[MetaType]) Validate(ce *Event[MetaType]) bool

type AnyPreValidator

type AnyPreValidator[MetaType any] []PreValidator[MetaType]

AnyPreValidator can be used to combine multiple PreValidators, such that at least one of them must return true for the command to be processed further.

func (AnyPreValidator[MetaType]) Validate

func (f AnyPreValidator[MetaType]) Validate(ce *Event[MetaType]) bool

type CommandContainer added in v0.24.0

type CommandContainer[MetaType any] struct {
	// contains filtered or unexported fields
}

func NewCommandContainer added in v0.24.0

func NewCommandContainer[MetaType any]() *CommandContainer[MetaType]

func (*CommandContainer[MetaType]) GetHandler added in v0.24.0

func (cont *CommandContainer[MetaType]) GetHandler(name string) *Handler[MetaType]

func (*CommandContainer[MetaType]) Register added in v0.24.0

func (cont *CommandContainer[MetaType]) Register(handlers ...*Handler[MetaType])

Register registers the given command handlers.

func (*CommandContainer[MetaType]) Unregister added in v0.24.0

func (cont *CommandContainer[MetaType]) Unregister(handlers ...*Handler[MetaType])

type Event

type Event[MetaType any] struct {
	*event.Event
	// RawInput is the entire message before splitting into command and arguments.
	RawInput string
	// ParentCommands is the chain of commands leading up to this command.
	// This is only set if the command is a subcommand.
	ParentCommands []string
	ParentHandlers []*Handler[MetaType]
	// Command is the lowercased first word of the message.
	Command string
	// Args are the rest of the message split by whitespace ([strings.Fields]).
	Args []string
	// RawArgs is the same as args, but without the splitting by whitespace.
	RawArgs string

	Ctx     context.Context
	Log     *zerolog.Logger
	Proc    *Processor[MetaType]
	Handler *Handler[MetaType]
	Meta    MetaType
	// contains filtered or unexported fields
}

Event contains the data of a single command event. It also provides some helper methods for responding to the command.

func ParseEvent

func ParseEvent[MetaType any](ctx context.Context, evt *event.Event) *Event[MetaType]

ParseEvent parses a message into a command event struct.

func RawTextToEvent added in v0.24.1

func RawTextToEvent[MetaType any](ctx context.Context, evt *event.Event, text string) *Event[MetaType]

func (*Event[MetaType]) MarkRead

func (evt *Event[MetaType]) MarkRead()

func (*Event[MetaType]) React

func (evt *Event[MetaType]) React(emoji string) id.EventID

func (*Event[MetaType]) Redact

func (evt *Event[MetaType]) Redact() id.EventID

func (*Event[MetaType]) Reply

func (evt *Event[MetaType]) Reply(msg string, args ...any) id.EventID

func (*Event[MetaType]) Respond

func (evt *Event[MetaType]) Respond(msg string, opts ReplyOpts) id.EventID

func (*Event[MetaType]) ShiftArg added in v0.24.0

func (evt *Event[MetaType]) ShiftArg() string

ShiftArg removes the first argument from the Args list and RawArgs data and returns it. RawInput will not be modified.

func (*Event[MetaType]) UnshiftArg added in v0.24.0

func (evt *Event[MetaType]) UnshiftArg(arg string)

UnshiftArg reverses ShiftArg by adding the given value to the beginning of the Args list and RawArgs data.

type FuncPreValidator

type FuncPreValidator[MetaType any] func(*Event[MetaType]) bool

FuncPreValidator is a simple function that implements the PreValidator interface.

func (FuncPreValidator[MetaType]) Validate

func (f FuncPreValidator[MetaType]) Validate(ce *Event[MetaType]) bool

type Handler

type Handler[MetaType any] struct {
	// Func is the function that is called when the command is executed.
	Func func(ce *Event[MetaType])

	// Name is the primary name of the command. It must be lowercase.
	Name string
	// Aliases are alternative names for the command. They must be lowercase.
	Aliases []string
	// Subcommands are subcommands of this command.
	Subcommands []*Handler[MetaType]
	// PreFunc is a function that is called before checking subcommands.
	// It can be used to have parameters between subcommands (e.g. `!rooms <room ID> <command>`).
	// Event.ShiftArg will likely be useful for implementing such parameters.
	PreFunc func(ce *Event[MetaType])
	// contains filtered or unexported fields
}

func MakeUnknownCommandHandler added in v0.24.0

func MakeUnknownCommandHandler[MetaType any](prefix string) *Handler[MetaType]

type PreValidator

type PreValidator[MetaType any] interface {
	Validate(*Event[MetaType]) bool
}

A PreValidator contains a function that takes an Event and returns true if the event should be processed further.

The PreValidator field in Processor is called before the handler of the command is checked. It can be used to modify the command or arguments, or to skip the command entirely.

The primary use case is removing a static command prefix, such as requiring all commands start with `!`.

func ValidatePrefixCommand

func ValidatePrefixCommand[MetaType any](prefix string) PreValidator[MetaType]

ValidatePrefixCommand checks that the first word in the input is exactly the given string, and if so, removes it from the command and sets the command to the next word.

For example, `ValidateCommandPrefix("!mybot")` would only allow commands in the form `!mybot foo`, where `foo` would be used to look up the command handler.

func ValidatePrefixSubstring

func ValidatePrefixSubstring[MetaType any](prefix string) PreValidator[MetaType]

ValidatePrefixSubstring checks that the command starts with the given prefix, and if so, removes it from the command.

For example, `ValidatePrefixSubstring("!")` would only allow commands in the form `!foo`, where `foo` would be used to look up the command handler.

type Processor

type Processor[MetaType any] struct {
	*CommandContainer[MetaType]

	Client       *mautrix.Client
	LogArgs      bool
	PreValidator PreValidator[MetaType]
	Meta         MetaType

	ReactionCommandPrefix string
}

Processor implements boilerplate code for splitting messages into a command and arguments, and finding the appropriate handler for the command.

func NewProcessor

func NewProcessor[MetaType any](cli *mautrix.Client) *Processor[MetaType]

func (*Processor[MetaType]) ParseReaction added in v0.24.1

func (proc *Processor[MetaType]) ParseReaction(ctx context.Context, evt *event.Event) *Event[MetaType]

func (*Processor[MetaType]) Process

func (proc *Processor[MetaType]) Process(ctx context.Context, evt *event.Event)

type ReplyOpts

type ReplyOpts struct {
	AllowHTML        bool
	AllowMarkdown    bool
	Reply            bool
	Thread           bool
	SendAsText       bool
	Edit             id.EventID
	OverrideMentions *event.Mentions
	Extra            map[string]any
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL