router

package module
v0.0.0-...-608579b Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2020 License: MIT Imports: 7 Imported by: 0

README

Router

Features

  • Automatic command routing using function names
  • Dynamic command arguments
  • Argument validation
  • Help message generator

Usage

package main

import (
	"fmt"
	"github.com/andersfylling/disgord"
	"go.matthewp.io/router"
	"os"
)

func main() {
	// Get the bot token using an environment variable.
	// DO NOT PREFIX THE TOKEN WITH "Bot ", disgord ALREADY DOES!
	var token = os.Getenv("BOT_TOKEN")
	if token == "" {
		panic("missing $BOT_TOKEN")
		return
	}

	client := disgord.New(disgord.Config{
		BotToken: token,
		Logger:   disgord.DefaultLogger(true),
	})

	r, err := router.NewRouter(client, ".", &commands{s: client})
	if err != nil {
		panic(err)
		return
	}

	client.On(disgord.EvtMessageCreate, func(s disgord.Session, e *disgord.MessageCreate) {
		err := r.Handle(e)
		if err != nil {
			_, err := s.SendMsg(e.Ctx, e.Message.ChannelID, "<@"+e.Message.Author.ID.String()+">, "+err.Error())
			if err != nil {
				fmt.Printf("failed to send error message to user: %v\n", err)
				return
			}
		}
	})
}

type commands struct {
	s disgord.Session
}

// Any exported functions on the struct will be registered as commands
// unless they are apart of the Registrar interface or if they do not
// accept a *disgord.MessageCreate as the first argument.
func (c *commands) Help(_ *disgord.MessageCreate) error {
	return nil
}

// This method will not be registered as command, because reflection does not support
// unexported methods when you are outside of the package it is defined in.
func (c *commands) test(_ *disgord.MessageCreate) error {
	return nil
}

// This method will also not be registered as a command but is still exported,
// however it will be registered as a command if it's first argument is
// *disgord.MessageCreate
func (c *commands) ThisIsNotACommand() {
	// Do something!
}

// Registrar interface method, it will be ignored.
func (c *commands) Descriptions() map[string]string {
	return map[string]string{
		"help": "Prints this help message",
	}
}

// Registrar interface method, it will be ignored.
func (c *commands) Arguments() map[string][]string {
	return map[string][]string{}
}

Additional Information

Interfaces
Parseable

Allows a custom argument type to get the string argument to handle it's own parsing.

type Parseable interface {
	Parse(string) error
}
Example (refer to args/user.go)
ManualParseable

Allows a custom argument type to get the entire argument string after any preceding arguments, useful for getting long user inputs.

type ManualParseable interface {
	ParseContent(string) error
}
Example (refer to args/raw.go)
Formatter

Allows a custom argument type to have a custom usage string.

type Formatter interface {
	Format(string) string
}
Example (refer to args/raw.go or args/user.go)

Documentation

Overview

Package router ...

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMissingClient .
	ErrMissingClient = errors.New("router: missing client")
	// ErrInvalidPrefix .
	ErrInvalidPrefix = errors.New("router: invalid prefix")
	// ErrMissingRegistrar .
	ErrMissingRegistrar = errors.New("router: missing registrar")
	// ErrInterfaceIsNotAPointer represents an Interface Is Not A Pointer error.
	ErrInterfaceIsNotAPointer = errors.New("router: interface is not a pointer")
	// ErrCommandIsNil represents a Command Is Nil error.
	ErrCommandIsNil = errors.New("router: command is nil")
	// ErrMethodHasNoErrorReturn .
	ErrMethodHasNoErrorReturn = errors.New("router: method has no error return")
	// ErrMethodHasNoArguments .
	ErrMethodHasNoArguments = errors.New("router: method has no arguments")
	// ErrMissingMessageCreateArgument .
	ErrMissingMessageCreateArgument = errors.New("router: missing *disgord.MessageCreate as the first method argument")
)
View Source
var (
	ErrInvalidBool = errors.New("invalid bool [true/false]")
)

Functions

This section is empty.

Types

type Command

type Command struct {
	Description string
	// contains filtered or unexported fields
}

func (*Command) Name

func (c *Command) Name() string

Name returns the command's name.

func (*Command) Usage

func (c *Command) Usage() string

Usage returns the command's usage.

type ErrCommandExecution

type ErrCommandExecution struct {
	Command *Command
	// contains filtered or unexported fields
}

ErrCommandExecution represents an unexpected error during a Command Execution.

func (*ErrCommandExecution) Error

func (err *ErrCommandExecution) Error() string

type ErrInvalidUsage

type ErrInvalidUsage struct {
	Prefix     string
	Command    string
	Usage      string
	ArgumentID int
}

ErrInvalidUsage represents an Invalid Usage error.

func (*ErrInvalidUsage) Error

func (err *ErrInvalidUsage) Error() string

type ErrMissingArguments

type ErrMissingArguments struct {
	Prefix  string
	Command string
	Usage   string
}

ErrMissingArguments represents a Missing Arguments error.

func (*ErrMissingArguments) Error

func (err *ErrMissingArguments) Error() string

type ErrUnknownCommand

type ErrUnknownCommand struct {
	Command string
}

ErrUnknownCommand represents an Unknown Command error.

func (*ErrUnknownCommand) Error

func (err *ErrUnknownCommand) Error() string

type Formatter

type Formatter interface {
	Format(field string) string
}

Formatter represents an argument that can be formatted for use in a usage string.

type ManualParseable

type ManualParseable interface {
	ParseContent(arg string) error
}

ManualParseable represents a manually parseable argument.

type Parseable

type Parseable interface {
	Parse(arg string) error
}

Parseable represents a parseable argument.

type Registrar

type Registrar interface {
	Descriptions() map[string]string
	Arguments() map[string][]string
}

Registrar represents a Command Registrar.

type Router

type Router struct {
	*disgord.Client

	Prefix string

	Commands []*Command
	// contains filtered or unexported fields
}

Router .

func NewRouter

func NewRouter(client *disgord.Client, prefix string, i Registrar) (*Router, error)

NewRouter .

func (*Router) GetCommandByName

func (r *Router) GetCommandByName(name string) *Command

GetCommandByName attempts to get a *Command by matching it's name.

func (*Router) Handle

func (r *Router) Handle(e *disgord.MessageCreate) error

Handle handles an incoming *disgord.MessageCreate event.

Directories

Path Synopsis
Package args ...
Package args ...

Jump to

Keyboard shortcuts

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