Documentation
¶
Index ¶
- Constants
- Variables
- func DeleteAllReactions(ctx context.Context, client *mautrix.Client, evt *event.Event)
- func DeleteAllReactionsCommandFunc[MetaType any](ce *Event[MetaType])
- type AllPreValidator
- type AnyPreValidator
- type CommandContainer
- type Event
- func (evt *Event[MetaType]) MarkRead()
- func (evt *Event[MetaType]) React(emoji string) id.EventID
- func (evt *Event[MetaType]) Redact() id.EventID
- func (evt *Event[MetaType]) Reply(msg string, args ...any) id.EventID
- func (evt *Event[MetaType]) Respond(msg string, opts ReplyOpts) id.EventID
- func (evt *Event[MetaType]) ShiftArg() string
- func (evt *Event[MetaType]) UnshiftArg(arg string)
- type FuncPreValidator
- type Handler
- type PreValidator
- type Processor
- type ReplyOpts
Constants ¶
const ReactionCommandsKey = "fi.mau.reaction_commands"
const ReactionMultiUseKey = "fi.mau.reaction_multi_use"
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 ¶
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 DeleteAllReactionsCommandFunc ¶ added in v0.24.1
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 ¶
ParseEvent parses a message into a command event struct.
func RawTextToEvent ¶ added in v0.24.1
func (*Event[MetaType]) ShiftArg ¶ added in v0.24.0
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
UnshiftArg reverses ShiftArg by adding the given value to the beginning of the Args list and RawArgs data.
type FuncPreValidator ¶
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
type PreValidator ¶
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.