cmdlr2

package module
v0.1.17 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2021 License: MIT Imports: 11 Imported by: 3

README

cmdlr2

cmdlr2 is a command handler framework for the discord wrapper disgord. It is heavily inspired (some files are the same) to a framework for discordgo called dgc. I in no way claim to be the original creator of this, this is only a fork for disgord.

Example starter app:

package main

import (
	"github.com/andersfylling/disgord"
	"github.com/sirupsen/logrus"
	"github.com/zackartz/cmdlr2"
	"os"
)

var log = &logrus.Logger{
	Out:       os.Stderr,
	Formatter: new(logrus.TextFormatter),
	Hooks:     make(logrus.LevelHooks),
	Level:     logrus.DebugLevel,
}

func main() {
	// Set up a new Disgord client
	client := disgord.New(disgord.Config{
		BotToken: os.Getenv("DISCORD_TOKEN"),
		Logger:   log,
	})
	defer client.Gateway().StayConnectedUntilInterrupted()

	router := cmdlr2.Create(&cmdlr2.Router{
		Prefixes:         []string{"$"},
		Client:           client,
		BotsAllowed:      false,
		IgnorePrefixCase: true,
	})

	router.RegisterCMD(&cmdlr2.Command{
		Name:        "ping",
		Description: "It pings.. and yknow.. pongs",
		Usage:       "ping",
		Example:     "ping",
		Handler: func(ctx *cmdlr2.Ctx) {
			ctx.ResponseText("pong")
		},
	})

	router.RegisterDefaultHelpCommand(client)

	router.Initialize(client)
}

In this case make sure to set the DISCORD_TOKEN environment variable to the value of your discord token.

CONSIDER THIS BETA SOFTWARE

I have a bot that it is using this and it is working however, you may have problems with dgc's implementation of middleware.

Good luck.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	RegexArguments = regexp.MustCompile("(\"[^\"]+\"|[^\\s]+)")

	RegexUserMention = regexp.MustCompile("<@!?(\\d+)>")

	RegexRoleMention = regexp.MustCompile("<@&(\\d+)>")

	RegexChannelMention = regexp.MustCompile("<#(\\d+)>")

	RegexBigCodeblock = regexp.MustCompile("(?s)\\n*```(?:([\\w.\\-]*)\\n)?(.*)```")

	RegexSmallCodeblock = regexp.MustCompile("(?s)\\n*`(.*)`")

	CodeblockLanguages = []string{}/* 315 elements not displayed */

)

Functions

func BuildCheckPrefixes

func BuildCheckPrefixes(command *Command) []string

func Equals

func Equals(str1, str2 string, ignoreCase bool) bool

func StringArrayContains

func StringArrayContains(array []string, str string, ignoreCase bool) bool

func StringHasPrefix

func StringHasPrefix(str string, prefixes []string, ignoreCase bool) (bool, string)

StringHasPrefix checks whether or not the string contains one of the given prefixes and returns the string without the prefix

func StringTrimPreSuffix

func StringTrimPreSuffix(str string, preSuffix string) string

Types

type Argument

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

func (*Argument) AsBool

func (arg *Argument) AsBool() (bool, error)

AsBool parses the given argument into a boolean

func (*Argument) AsChannelMentionID

func (arg *Argument) AsChannelMentionID() string

AsChannelMentionID returns the ID of the mentioned channel or an empty string if it is no mention

func (*Argument) AsDuration

func (arg *Argument) AsDuration() (time.Duration, error)

AsDuration parses the given argument into a duration

func (*Argument) AsInt

func (arg *Argument) AsInt() (int, error)

AsInt parses the given argument into an int32

func (*Argument) AsInt64

func (arg *Argument) AsInt64() (int64, error)

AsInt64 parses the given argument into an int64

func (*Argument) AsRoleMentionID

func (arg *Argument) AsRoleMentionID() string

AsRoleMentionID returns the ID of the mentioned role or an empty string if it is no mention

func (*Argument) AsUserMentionID

func (arg *Argument) AsUserMentionID() string

AsUserMentionID returns the ID of the mentioned user or an empty string if it is no mention

func (*Argument) Raw

func (arg *Argument) Raw() string

Raw returns the raw string value of the argument

type Arguments

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

func ParseArguments

func ParseArguments(raw string) *Arguments

func (*Arguments) Amount

func (a *Arguments) Amount() int

func (*Arguments) AsCodeblock

func (a *Arguments) AsCodeblock() *Codeblock

AsCodeblock parses the given arguments as a codeblock

func (*Arguments) AsSingle

func (a *Arguments) AsSingle() *Argument

func (*Arguments) Get

func (a *Arguments) Get(n int) *Argument

func (*Arguments) Raw

func (a *Arguments) Raw() string

func (*Arguments) Remove

func (a *Arguments) Remove(n int)

type Codeblock

type Codeblock struct {
	Language string
	Content  string
}

type Command

type Command struct {
	Name        string
	Aliases     []string
	Description string
	Usage       string
	Example     string
	Flags       []string
	IgnoreCase  bool
	SubCommands []*Command
	Handler     ExecutionHandler
}

func (*Command) GetSubCommand

func (c *Command) GetSubCommand(name string) *Command

func (*Command) Trigger

func (c *Command) Trigger(ctx *Ctx)

type Ctx

type Ctx struct {
	Client  *disgord.Client
	Session *disgord.Session
	Event   *disgord.MessageCreate
	Args    *Arguments
	Command *Command
	Router  *Router
}

func (*Ctx) ResponseEmbed

func (ctx *Ctx) ResponseEmbed(embed *disgord.Embed) error

func (*Ctx) ResponseText

func (ctx *Ctx) ResponseText(text string) error

func (*Ctx) ResponseTextEmbed

func (ctx *Ctx) ResponseTextEmbed(text string, embed *disgord.Embed) error

type ExecutionHandler

type ExecutionHandler func(ctx *Ctx)

type Middleware

type Middleware struct {
	Trigger func(ctx Ctx)
}

type ObjectsMap

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

func NewObjectsMap

func NewObjectsMap() *ObjectsMap

func (*ObjectsMap) Delete

func (om *ObjectsMap) Delete(key string)

func (*ObjectsMap) Get

func (om *ObjectsMap) Get(key string) (interface{}, bool)

func (*ObjectsMap) MustGet

func (om *ObjectsMap) MustGet(key string) interface{}

func (*ObjectsMap) Set

func (om *ObjectsMap) Set(key string, val interface{})

type Router

type Router struct {
	Prefixes         []string
	IgnorePrefixCase bool
	BotsAllowed      bool
	Commands         []*Command
	Client           *disgord.Client
	Middlewares      []Middleware
	PingHandler      ExecutionHandler
	Storage          map[string]*ObjectsMap
}

func Create

func Create(router *Router) *Router

func (*Router) GetCmd

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

func (*Router) Handler

func (*Router) Initialize

func (r *Router) Initialize(client *disgord.Client)

func (*Router) InitializeStorage

func (r *Router) InitializeStorage(name string)

func (*Router) RegisterCMD

func (r *Router) RegisterCMD(command *Command)

func (*Router) RegisterCMDList

func (r *Router) RegisterCMDList(commands []*Command)

func (*Router) RegisterDefaultHelpCommand

func (r *Router) RegisterDefaultHelpCommand(c *disgord.Client)

func (*Router) RegisterMiddleware

func (r *Router) RegisterMiddleware(middleware Middleware)

Jump to

Keyboard shortcuts

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