dbot

package module
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2020 License: MIT Imports: 16 Imported by: 0

README

dbot

Docs Go Report

A flexible Discord bot framework built upon discordgo, heavily influenced by Lukaesebrot's dgc.

1.0 checklist

  • Command responses (using embeds) added in 0.1.0
  • Time-based responses (cron-like syntax? idk)
  • Message reaction responses (possibly called "Reactives", not sure yet) added in 0.2.0
  • Document everything properly
  • Get golint score up to 100%
  • Write package tests
  • Test with more complex bots and optimise where there are significant slowdowns
  • Default commands added in 0.4.0, see defaults
  • Example projects added in 0.4.0, see examples

Please note that before 1.0 there will be a considerable number of API changes because I don't know what I'm doing.

Documentation

Overview

Package dbot is a flexible bot framework for creating Discord bots. TODO: unexport things that aren't used outside the package. TODO: package tests.

Index

Constants

This section is empty.

Variables

View Source
var (
	// RegexChannelMention matches strings that are channel mentions.
	RegexChannelMention = regexp.MustCompile(`<#(\d+)>`)

	// RegexUserMention matches strings that are user mentions.
	RegexUserMention = regexp.MustCompile(`<@!?(\d+)>`)

	// RegexRoleMention matches strings that are role mentions.
	RegexRoleMention = regexp.MustCompile(`<@&(\d+)>`)

	// RegexEmoji matches strings that are emojis.
	RegexEmoji = regexp.MustCompile(`<:([a-zA-Z0-9_\-]+):(\d+)>`)
)

Functions

func FormatBotToken

func FormatBotToken(t string) string

FormatBotToken ensures that a bot token begins with "Bot", as required by the Discord API.

Types

type Argument

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

Argument represents a single argument in a command call.

func (*Argument) Channel

func (arg *Argument) Channel(session *dgo.Session) (*dgo.Channel, error)

Channel parses the argument and returns a pointer to a channel struct, if the argument is of the correct type.

func (*Argument) ChannelID

func (arg *Argument) ChannelID() (string, error)

ChannelID returns the argument as a channel ID, if the argument is of the correct type.

func (*Argument) EmojiID added in v0.4.1

func (arg *Argument) EmojiID() (string, error)

EmojiID returns the argument as an emoji ID, if the argument is of the correct type.

func (*Argument) EmojiName added in v0.4.1

func (arg *Argument) EmojiName() (string, error)

EmojiName returns the argument as an emoji name, if the argument is of the correct type.

func (*Argument) Float

func (arg *Argument) Float() (float64, error)

Float returns the argument as a float64, if the argument is of the correct type.

func (*Argument) Int

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

Int returns the argument as an int, if the argument is of the correct type.

func (*Argument) Member

func (arg *Argument) Member(session *dgo.Session, guildID string) (*dgo.Member, error)

Member parses the argument and returns a pointer to a Member struct, if the argument is of the correct type.

func (*Argument) Role

func (arg *Argument) Role(session *dgo.Session, guildID string) (*dgo.Role, error)

Role parses the argument and returns a pointer to a Role struct, if the argument is of the correct type.

func (*Argument) RoleID

func (arg *Argument) RoleID() (string, error)

RoleID returns the argument as a role ID, if the argument is of the correct type.

func (*Argument) String

func (arg *Argument) String() string

String returns the argument as a string.

func (*Argument) Type

func (arg *Argument) Type() ArgumentType

Type returns the type of the argument.

func (*Argument) URL added in v0.4.1

func (arg *Argument) URL() (*url.URL, error)

URL returns the argument as an URL.

func (*Argument) User

func (arg *Argument) User(session *dgo.Session) (*dgo.User, error)

User parses the argument and returns a pointer to a User struct, if the argument is of the correct type.

func (*Argument) UserID

func (arg *Argument) UserID() (string, error)

UserID returns the argument as a user ID, if the argument is of the correct type.

type ArgumentType

type ArgumentType string

ArgumentType represents the argument type.

const (
	ArgumentTypeString          ArgumentType = "string"
	ArgumentTypeInt             ArgumentType = "int"
	ArgumentTypeFloat           ArgumentType = "float"
	ArgumentTypeURL             ArgumentType = "url"
	ArgumentTypeChannelMention  ArgumentType = "channelMention"
	ArgumentTypeEveryoneMention ArgumentType = "everyoneMention"
	ArgumentTypeUserMention     ArgumentType = "userMention"
	ArgumentTypeRoleMention     ArgumentType = "roleMention"
	ArgumentTypeEmoji           ArgumentType = "emoji"
)

type Arguments

type Arguments struct {
	Arguments []*Argument
	// contains filtered or unexported fields
}

Arguments represents a collection of arguments along with the raw string.

func ParseArguments

func ParseArguments(s string) *Arguments

ParseArguments parses a prefix-less string into a set of arguments.

type Bot

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

	Config *BotConfig `json:"config"`

	Commands  []*Command  `json:"commands"`
	Reactives []*Reactive `json:"reactives"`

	Log loggo.Logger `json:"logger"`
	// 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)

func (*Bot) AddReactives added in v0.2.0

func (bot *Bot) AddReactives(reactives ...*Reactive)

func (*Bot) DefaultGuildConfig

func (bot *Bot) DefaultGuildConfig(guildID string) *GuildConfig

func (*Bot) DefaultUserConfig

func (bot *Bot) DefaultUserConfig(userID string) *UserConfig

func (*Bot) GetCommand

func (bot *Bot) GetCommand(name string) *Command

func (*Bot) GetGuildConfig

func (bot *Bot) GetGuildConfig(guildID string) *GuildConfig

func (*Bot) GetUserConfig

func (bot *Bot) GetUserConfig(userID string) *UserConfig

func (*Bot) Run

func (bot *Bot) Run() error

type BotConfig

type BotConfig struct {
	DefaultPrefix string `json:"default_prefix"`

	IgnoreDMs      bool `json:"ignore_dms"`
	IgnoreMentions bool `json:"ignore_mentions"`
	IgnoreCase     bool `json:"ignore_prefix_case"`

	Guilds map[string]*GuildConfig `json:"guilds"`
	Users  map[string]*UserConfig  `json:"users"`

	MessageStorage map[string]*Storage `json:"message_storage"`
}

type Command

type Command struct {
	*Config

	Name        string        `json:"name"`
	Description string        `json:"description"`
	Aliases     []string      `json:"aliases"`
	Examples    []string      `json:"usage"`
	Tags        []string      `json:"tags"`
	Cooldown    time.Duration `json:"cooldown"`

	Log loggo.Logger

	Func func(*MessageContext) error
}

func NewCommand added in v0.4.0

func NewCommand(name string) *Command

func (*Command) AddAliases added in v0.4.0

func (command *Command) AddAliases(aliases ...string) *Command

func (*Command) AddExamples added in v0.4.0

func (command *Command) AddExamples(examples ...string) *Command

func (*Command) AddTags added in v0.4.0

func (command *Command) AddTags(tags ...string) *Command

func (*Command) SetCooldown added in v0.4.0

func (command *Command) SetCooldown(duration time.Duration) *Command

func (*Command) SetDescription added in v0.4.0

func (command *Command) SetDescription(description string) *Command

func (*Command) SetFunc added in v0.4.0

func (command *Command) SetFunc(f func(*MessageContext) error) *Command

type Config

type Config struct {
	*VarFolder

	Storage map[string]*Storage `json:"storage"`
}

Config is a generic config struct which contains vars and storage.

type GuildConfig

type GuildConfig struct {
	*Config

	GuildID  string   `json:"guild_id"`
	Prefixes []string `json:"prefixes"`

	Tags []string `json:"tags"`
}

type MessageContext added in v0.2.0

type MessageContext struct {
	Arguments []*Argument
	Command   *Command

	GuildConfig *GuildConfig
	UserConfig  *UserConfig

	Message *dgo.Message
	Session *dgo.Session

	Response *dgo.Message

	Bot *Bot

	// Log is a pointer to ctx.Command.Log
	Log *loggo.Logger
	// contains filtered or unexported fields
}

MessageContext is the context for message-related events.

func (*MessageContext) ResponseStorage added in v0.2.0

func (ctx *MessageContext) ResponseStorage() *Storage

func (*MessageContext) SetContent added in v0.2.0

func (ctx *MessageContext) SetContent(content string) error

func (*MessageContext) SetField added in v0.2.0

func (ctx *MessageContext) SetField(title, content string) error

type Options

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

	Token         string `json:"token"`
	DefaultPrefix string `json:"default_prefix"`

	GetTokenFromEnvironment bool `json:"get_token_from_environment"`

	IgnoreDMs      bool `json:"ignore_dms"`
	IgnoreMentions bool `json:"ignore_mentions"`
	IgnoreCase     bool `json:"ignore_prefix_case"`

	LogLevel loggo.Level `json:"log_level"`
}

Options provides a set of options for initialising Discord bots.

type ReactionContext added in v0.2.0

type ReactionContext struct {
	Reactive    *Reactive
	BotConfig   *BotConfig
	GuildConfig *GuildConfig
	UserConfig  *UserConfig
	Storage     *Storage

	Reaction *dgo.MessageReaction
	Session  *dgo.Session

	Log *loggo.Logger

	Response *dgo.Message
}

ReactionContext is the context for reaction-related events.

type Reactive added in v0.2.0

type Reactive struct {
	*Config

	Name        string   `json:"name"`
	Description string   `json:"description"`
	Tags        []string `json:"tags"`

	Log loggo.Logger

	FuncMap map[string]func(ctx *ReactionContext) error
}

type Storage

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

func NewStorage added in v0.2.0

func NewStorage() *Storage

NewStorage returns a pointer to a properly initialised messageStorage struct.

func (*Storage) Delete

func (storage *Storage) Delete(key string)

func (*Storage) Get

func (storage *Storage) Get(key string) (interface{}, bool)

func (*Storage) Load

func (storage *Storage) Load(path string) error

func (*Storage) Save

func (storage *Storage) Save(path string) error

func (*Storage) Set

func (storage *Storage) Set(key string, i interface{})

type UserConfig

type UserConfig struct {
	*Config

	UserID string `json:"user_id"`
	Admin  bool   `json:"admin"`
	Ignore bool   `json:"ignored"`

	Tags []string `json:"tags"`
}

type Var

type Var struct {
	*VarFolder

	Key   string      `json:"key"`
	Value interface{} `json:"value"`
	// contains filtered or unexported fields
}

Var implements a basic key-value structure that can also contain other Vars.

type VarFolder

type VarFolder struct {
	Vars []*Var `json:"vars"`
}

VarFolder is a collection of Vars.

func (*VarFolder) Find

func (vf *VarFolder) Find(key string) (*Var, bool)

Find searches the current folder for a var and returns it. If the var was not found, false is returned. Otherwise, true is returned.

func (*VarFolder) Set

func (vf *VarFolder) Set(key string, value interface{}) (*Var, bool)

Set a var in the current folder and return a pointer to it. If a new var was created, true is also returned. Otherwise, false is returned.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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