dbot

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2020 License: MIT Imports: 13 Imported by: 0

README

dbot

Docs Go Report

A flexible Discord bot framework built upon discordgo. Originally forked from dgc v1.0.6, released under the MIT license.

The Goal™

My main reasons for making this other than procrastination is to streamline the process of making bots. Ideally, once the library is feature-complete, you should be able to make bots without having to import discordgo, or libraries to handle the basic stuff like parsing command line arguments or logging. Eventually I also want to port all the other bots I've made so far to use this library, which means I need to re-implement all the weird systems I have set up in them.

At some point I might also try to make this package have no external dependencies other than discordgo itself, but I'm not making that a top priority.

TODO

  • Clean up existing code, comments, etc
  • Simplify code, remove processes that aren't needed
  • Create a more generalised message interactions
  • Make some new examples
  • Add commands that trigger at regular times (cron-style)
  • Command line argument parsing
  • Logging to the console and to file
  • Rewrite and simplify the rate limiter system
  • Add template engine support for command responses (maybe?)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// RegexArguments defines the regex the argument string has to match
	RegexArguments = regexp.MustCompile("(\"[^\"]+\"|[^\\s]+)")

	// RegexUserMention defines the regex a user mention has to match
	RegexUserMention = regexp.MustCompile("<@!?(\\d+)>")

	// RegexRoleMention defines the regex a role mention has to match
	RegexRoleMention = regexp.MustCompile("<@&(\\d+)>")

	// RegexChannelMention defines the regex a channel mention has to match
	RegexChannelMention = regexp.MustCompile("<#(\\d+)>")
)

Functions

This section is empty.

Types

type Argument

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

Argument represents a single argument

func (*Argument) AsBool

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

AsBool parses the given argument into a boolean

func (*Argument) AsChannelMentionID

func (argument *Argument) AsChannelMentionID() string

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

func (*Argument) AsDuration

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

AsDuration parses the given argument into a duration

func (*Argument) AsInt

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

AsInt parses the given argument into an int32

func (*Argument) AsInt64

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

AsInt64 parses the given argument into an int64

func (*Argument) AsRoleMentionID

func (argument *Argument) AsRoleMentionID() string

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

func (*Argument) AsUserMentionID

func (argument *Argument) AsUserMentionID() string

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

func (*Argument) Raw

func (argument *Argument) Raw() string

Raw returns the raw string value of the argument

type Arguments

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

Arguments represents the arguments that may be used in a command context

func ParseArguments

func ParseArguments(raw string) *Arguments

ParseArguments parses the raw string into several arguments

func (*Arguments) Amount

func (arguments *Arguments) Amount() int

Amount returns the amount of given arguments

func (*Arguments) AsSingle

func (arguments *Arguments) AsSingle() *Argument

AsSingle parses the given arguments as a single one

func (*Arguments) Get

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

Get returns the n'th argument

func (*Arguments) Raw

func (arguments *Arguments) Raw() string

Raw returns the raw string value of the arguments

func (*Arguments) Remove

func (arguments *Arguments) Remove(n int)

Remove removes the n'th argument

type Bot

type Bot struct {
	Name        string `json:"name"`
	Version     string `json:"version"`
	Description string `json:"description"`

	GuildConfigs map[string]*Config
	UserConfigs  map[string]*Config
	// contains filtered or unexported fields
}

Bot represents a collection of commands

func New

func New(options Options) (*Bot, error)

New creates a new Bot from the provided options and returns a pointer to it

func (*Bot) AddCommands

func (bot *Bot) AddCommands(commands ...*Command) error

func (*Bot) Run

func (bot *Bot) Run() error

type Command

type Command struct {
	Name        string
	Description string
	Aliases     []string
	Examples    []string
	Tags        []string
	IgnoreCase  bool

	SubCommands []*Command
	RateLimiter RateLimiter
	Handler     ExecutionHandler
}

Command represents a simple command

func (*Command) GetSubCommand

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

GetSubCommand returns the sub command with the given name if it exists

func (*Command) NotifyRateLimiter

func (command *Command) NotifyRateLimiter(ctx *Ctx) bool

NotifyRateLimiter notifies the rate limiter about a new execution and returns false if the user is being rate limited

type Config

type Config struct {
	ID   string   `json:"guild_id"`
	Tags []string `json:"tags"`
}

func (*Config) AddTag

func (cfg *Config) AddTag(s string) bool

AddTag adds a tag to the config.

func (*Config) HasTag

func (cfg *Config) HasTag(s string) bool

func (*Config) RemoveTag

func (cfg *Config) RemoveTag(s string) bool

RemoveTag removes a tag from the config.

type Ctx

type Ctx struct {
	Session       *dgo.Session
	Event         *dgo.MessageCreate
	Arguments     *Arguments
	CustomObjects *ObjectsMap
	Router        *Router
	Command       *Command
}

Ctx represents the context for a command event

func (*Ctx) RespondEmbed

func (ctx *Ctx) RespondEmbed(embed *dgo.MessageEmbed) (*dgo.Message, error)

RespondEmbed responds with the given embed message

func (*Ctx) RespondText

func (ctx *Ctx) RespondText(text string) (*dgo.Message, error)

RespondText responds with the given text message

func (*Ctx) RespondTextEmbed

func (ctx *Ctx) RespondTextEmbed(text string, embed *dgo.MessageEmbed) (*dgo.Message, error)

RespondTextEmbed responds with the given text and embed message

type DefaultRateLimiter

type DefaultRateLimiter struct {
	Cooldown           time.Duration
	RateLimitedHandler ExecutionHandler
	// contains filtered or unexported fields
}

DefaultRateLimiter represents an internal rate limiter

func (*DefaultRateLimiter) NotifyExecution

func (rateLimiter *DefaultRateLimiter) NotifyExecution(ctx *Ctx) bool

NotifyExecution notifies the rate limiter about a new execution and returns whether or not the execution is allowed

type ExecutionHandler

type ExecutionHandler func(*Ctx)

ExecutionHandler represents a handler for a context execution

type Middleware

type Middleware func(following ExecutionHandler) ExecutionHandler

Middleware defines how a middleware looks like

type ObjectsMap

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

ObjectsMap wraps a map[string]interface to provide thread safe access endpoints.

func (*ObjectsMap) Delete

func (om *ObjectsMap) Delete(key string)

Delete removes a key-value pair from the map.

func (*ObjectsMap) Get

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

Get tries to get a value from the map. If a value was found, the value and true is returned. Else, nil and false is returned.

func (*ObjectsMap) MustGet

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

MustGet wraps Get but only returns the value, if found, or nil otherwise.

func (*ObjectsMap) Set

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

Set sets a value to the map by key.

type Options

type Options struct {
	Name        string `json:"name"`
	Description string `json:"description"`
	Version     string `json:"version"`

	Prefix string `json:"prefix"`

	IgnoreDMs            bool `json:"ignore_dms"`
	IgnoreMentions       bool `json:"ignore_mentions"`
	IgnorePrefixCase     bool `json:"ignore_prefix_case"`
	DeleteCommandMessage bool `json:"delete_command_message"`

	Token string `json:"token"`
}

Options provides a set of options for initialising Discord bots

type RateLimiter

type RateLimiter interface {
	NotifyExecution(*Ctx) bool
}

RateLimiter represents a general rate limiter

func NewRateLimiter

func NewRateLimiter(cooldown, cleanupInterval time.Duration, onRateLimited ExecutionHandler) RateLimiter

NewRateLimiter creates a new rate limiter

type Router

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

Router represents a DiscordGo command router

func (*Router) GetCommand

func (router *Router) GetCommand(name string) *Command

GetCommand returns the command with the given name if it exists

func (*Router) Handler

func (router *Router) Handler() func(*dgo.Session, *dgo.MessageCreate)

Handler provides the dgo handler for the given router

func (*Router) Initialise

func (router *Router) Initialise(session *dgo.Session)

Initialise initializes the message event listener

func (*Router) InitialiseStorage

func (router *Router) InitialiseStorage(name string)

InitialiseStorage initializes a storage map

func (*Router) RegisterCommand

func (router *Router) RegisterCommand(command *Command)

RegisterCommand registers a new command

func (*Router) RegisterMiddleware

func (router *Router) RegisterMiddleware(middleware Middleware)

RegisterMiddleware registers a new middleware

Jump to

Keyboard shortcuts

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