commandrouter

package module
v0.0.0-...-a0d6d79 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2020 License: GPL-3.0 Imports: 5 Imported by: 0

README

commandrouter

About

This repository is a part of projectdoll, which is developed by lukasl-dev.

This repository uses the following dependencies:


Getting started

It is assumed that you have already worked with the Go environment. If this is not the case, see this page first.

Installation
Installation with go get
go get -u github.com/projectdoll/commandrouter
Import
Standalone Import
import "github.com/projectdoll/commandrouter"
Multiple Import
import (
    "github.com/projectdoll/commandrouter"
)

Usage

Create a Router
Create a simple Router
router, err := commandrouter.Simple(PREFIXES...)
Create a Router from Options
router, err := commandrouter.New(commandrouter.Options{
    PrefixGenerator: func(session disgord.Session, message *disgord.Message) ([]string, error) {
        return PREFIXES..., nil
    },
    ...
})
Create a Command

Here the command is returned after the execution of the function Then and is simultaneously registered and can be used.

cmd := router.On(LABELS...).Then(func(ctx commandrouter.Context) error {
    panic("implement me")
})

or

An implemented version of a command is created and then registered. After registration the command can be used.

cmd := &commandrouter.CMD{
    Names: []string{LABELS...},
    Actions: []commandrouter.CommandFunc{
        func(ctx commandrouter.Context) error {
            panic("implement me")
        },
    },
    ...
}
router.Append(cmd)

Full example

/* create a Router */
 router, err := commandrouter.Simple("!")

if err != nil {
    log.Fatalln(err.Error())
}

/* create a disgord.Client and connect */
bot := disgord.New(disgord.Config{BotToken: TOKEN})
defer bot.StayConnectedUntilInterrupted(context.Background())

/* register handlers */
bot.On(disgord.EvtMessageCreate, router.HandlerMessageCreate)
bot.On(disgord.EvtMessageUpdate, router.HandlerMessageUpdate)

/* create ping command */
router.On("ping").Then(func(ctx commandrouter.Context) error {
    _, _ = ctx.Respond(context.Background(), &disgord.CreateMessageParams{Content: "PONG!"})
    return nil
})

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorBotAuthorIgnored = errors.New("bot-author is ignored")
	ErrorSelfIgnored      = errors.New("self-written-message are ignored")
	ErrorWebhooksIgnored  = errors.New("webhook-messages are ignored")
)
View Source
var (
	ErrorPrefixGeneratorUndefined = errors.New("prefix-generator is required")
	ErrorNoCommandMatching        = errors.New("no matching command found")
)

Functions

func MessageValidatorIgnoreBot

func MessageValidatorIgnoreBot(session disgord.Session, message *disgord.Message) error

func MessageValidatorIgnoreSelf

func MessageValidatorIgnoreSelf(session disgord.Session, message *disgord.Message) error

func MessageValidatorIgnoreWebhooks

func MessageValidatorIgnoreWebhooks(session disgord.Session, message *disgord.Message) error

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

func NewBuilder

func NewBuilder(register Register, labels ...string) *Builder

func (*Builder) ErrorHandlers

func (builder *Builder) ErrorHandlers(errorHandlers ...ErrorHandlerFunc) *Builder

func (*Builder) Labels

func (builder *Builder) Labels(labels ...string) *Builder

func (*Builder) Then

func (builder *Builder) Then(actions ...CommandFunc) Command

func (*Builder) Validators

func (builder *Builder) Validators(validators ...ContextValidatorFunc) *Builder

type CMD

type CMD struct {
	Names         []string
	ErrorHandlers []ErrorHandlerFunc
	Validators    []ContextValidatorFunc
	Actions       []CommandFunc
}

func (*CMD) HandleError

func (cmd *CMD) HandleError(ctx Context, err error)

func (*CMD) Labels

func (cmd *CMD) Labels() []string

func (*CMD) Primary

func (cmd *CMD) Primary() string

func (*CMD) Run

func (cmd *CMD) Run(ctx Context) error

func (*CMD) Self

func (cmd *CMD) Self() interface{}

func (*CMD) SetLabels

func (cmd *CMD) SetLabels(labels ...string) Command

func (*CMD) ValidateContext

func (cmd *CMD) ValidateContext(ctx Context) error

type Command

type Command interface {
	ContextValidator
	ErrorHandler

	Primary() string

	Labels() []string
	SetLabels(labels ...string) Command

	Self() interface{}
	Run(ctx Context) error
}

type CommandFunc

type CommandFunc func(ctx Context) error

type Context

type Context struct {
	gommandparser.Result
	disgord.Session
	*disgord.Message

	Self Command
}

func (Context) Respond

func (ctx Context) Respond(goCtx context.Context, params *disgord.CreateMessageParams) (*disgord.Message, error)

type ContextValidator

type ContextValidator interface {
	ValidateContext(ctx Context) error
}

type ContextValidatorFunc

type ContextValidatorFunc func(ctx Context) error

type ErrorHandler

type ErrorHandler interface {
	HandleError(ctx Context, err error)
}

type ErrorHandlerFunc

type ErrorHandlerFunc func(ctx Context, err error)

type Listener

type Listener interface {
	On(labels ...string) *Builder
}

type ListenerFunc

type ListenerFunc func(labels ...string) *Builder

type Logger

type Logger interface {
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
	Info(args ...interface{})
	Infof(format string, args ...interface{})
	Warn(args ...interface{})
	Warnf(format string, args ...interface{})
	Debug(args ...interface{})
	Debugf(format string, args ...interface{})
}

type MessageHandler

type MessageHandler interface {
	HandlerMessageCreate(session disgord.Session, create *disgord.MessageCreate)
	HandlerMessageUpdate(session disgord.Session, update *disgord.MessageUpdate)
	HandlerMessage(session disgord.Session, message *disgord.Message)
}

type MessageValidator

type MessageValidator interface {
	ValidateMessage(session disgord.Session, message *disgord.Message) error
}

type MessageValidatorFunc

type MessageValidatorFunc func(session disgord.Session, message *disgord.Message) error

type Options

type Options struct {
	Logger            Logger
	PrefixGenerator   PrefixGeneratorFunc
	ErrorHandlers     []ErrorHandlerFunc
	MessageValidators []MessageValidatorFunc
	Parser            gommandparser.Parser
}

type PrefixGenerator

type PrefixGenerator interface {
	GeneratePrefixes(session disgord.Session, message *disgord.Message) ([]string, error)
}

type PrefixGeneratorFunc

type PrefixGeneratorFunc func(session disgord.Session, message *disgord.Message) ([]string, error)

type Register

type Register interface {
	Len() int
	Values() []Command
	Foreach(action func(cmd Command))
	Filter(filter func(cmd Command) bool) []Command
	Find(label string, ignoreCase bool) []Command
	Append(add ...Command)
}

type Router

func New

func New(options Options) (Router, error)

func Simple

func Simple(prefixes ...string) (Router, error)

Jump to

Keyboard shortcuts

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