discordant

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2022 License: MIT Imports: 9 Imported by: 0

README

discordant

GitHub Build Coverage Go Report Card GoDoc

Minimalist Discord bot framework in Go programming language.

Documentation

Index

Constants

View Source
const (
	ChannelAdmin   = "admin"
	ChannelGeneral = "general"
)

Channel types.

View Source
const (
	DefaultCommandPrefix    = "!"
	DefaultCommandDelimiter = " "
)

Defaults.

View Source
const (
	ResponseMessageFail       = "```fail```"
	ResponseMessageFormatJSON = "```json\n%s\n```"
)

Response massage layouts.

View Source
const DiscordMaxMessageLen = 2000

DiscordMaxMessageLen max discord message length.

Variables

View Source
var (
	// ErrEmptyToken is returned when discord bot token is empty with enabled hook.
	ErrEmptyToken = errors.New("discord bot token is empty")

	// ErrEmptyPrefix is returned when discord bot prefix is empty.
	ErrEmptyPrefix = errors.New("discord bot prefix is empty")

	// ErrEmptyChannelID is returned when discord channel id is empty with enabled hook.
	ErrEmptyChannelID = errors.New("discord channel id is empty")

	// ErrMessageTooLong is returned when message that has been sent to discord longer
	// than 2000 characters.
	ErrMessageTooLong = errors.New("discord message too long")

	// ErrCommandNotFound is returned if an unknown command was received.
	ErrCommandNotFound = errors.New("command not found")
)

Functions

This section is empty.

Types

type Command

type Command struct {
	Name        string   `json:"name"`
	Arg         string   `json:"arg"`
	Description string   `json:"description"`
	Help        string   `json:"help"`
	Access      []string `json:"access"`
	// contains filtered or unexported fields
}

Command is the Discord command.

type CommandOption

type CommandOption func(*Command)

CommandOption describes command option func.

func MiddlewareAccess

func MiddlewareAccess(access ...string) CommandOption

MiddlewareAccess adds access levels.

func MiddlewareDescription

func MiddlewareDescription(description string) CommandOption

MiddlewareDescription adds description to command..

type Config

type Config struct {
	Token       string            `json:"token" yaml:"token"`
	Prefix      string            `json:"prefix" yaml:"prefix"`
	Safemode    bool              `json:"safemode" yaml:"safemode"`
	Channels    map[string]string `json:"channels" yaml:"channels"`
	AccessOrder []string          `json:"access_order" yaml:"access_order"`
}

Config contains credentials for Discord server.

func (*Config) Validate

func (cfg *Config) Validate() error

Validate checks required fields and validates for allowed values.

type Context

type Context interface {
	Command() *Command
	Discordant() *Discordant
	Request() *discordgo.MessageCreate
	ChannelID() string
	Send(msg string, params ...string) error
}

Context is an interface represents the context of the current Discord command.

type Discordant

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

Discordant represents a connection to the Discord API.

func New

func New(cfg *Config, options ...Option) (*Discordant, error)

New creates a new Discord session and will automate some startup tasks if given enough information to do so. Currently, you can pass zero arguments, and it will return an empty Discord session.

func (*Discordant) ADMIN

func (d *Discordant) ADMIN(name string, handler HandlerFunc, options ...CommandOption)

ADMIN adds route handler to admin channel.

func (*Discordant) ALL

func (d *Discordant) ALL(name string, handler HandlerFunc, options ...CommandOption)

ALL adds route handler to any channel.

func (*Discordant) Add

func (d *Discordant) Add(name string, handler HandlerFunc, options ...CommandOption)

Add adds route handler.

func (*Discordant) AddHandler

func (d *Discordant) AddHandler(handler interface{}) func()

AddHandler allows you to add an event handler that will be fired anytime the Discord WSAPI event that matches the function fires.

func (*Discordant) CheckAccess

func (d *Discordant) CheckAccess(id string, channels ...string) bool

CheckAccess returns true if access is allowed.

func (*Discordant) Close

func (d *Discordant) Close() error

Close closes discord connection.

func (*Discordant) Commands

func (d *Discordant) Commands() map[string]Command

Commands returns commands list.

func (*Discordant) GENERAL added in v0.1.3

func (d *Discordant) GENERAL(name string, handler HandlerFunc, options ...CommandOption)

GENERAL adds route handler to general channel.

func (*Discordant) GetCommand

func (d *Discordant) GetCommand(message string) (*Command, error)

GetCommand returns command by received message.

func (*Discordant) ID

func (d *Discordant) ID() string

ID returns stored bot id.

func (*Discordant) NewContext

func (d *Discordant) NewContext(message *discordgo.MessageCreate, command *Command) Context

NewContext creates new Context.

func (*Discordant) Run

func (d *Discordant) Run()

Run runs discord bot handlers.

func (*Discordant) Session

func (d *Discordant) Session() *discordgo.Session

Session returns discord Session.

type HandlerFunc

type HandlerFunc func(Context) error

HandlerFunc defines a function to serve HTTP requests.

type Logger

type Logger interface {
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warningf(format string, args ...interface{})
	Errorf(format string, args ...interface{})

	Debug(args ...interface{})
	Info(args ...interface{})
	Warning(args ...interface{})
	Error(args ...interface{})

	Debugln(args ...interface{})
	Infoln(args ...interface{})
	Warningln(args ...interface{})
	Errorln(args ...interface{})
}

Logger is implemented by any logging system that is used for standard logs.

func NewDefaultLog

func NewDefaultLog() Logger

NewDefaultLog creates and returns default logger to stderr.

type Option

type Option func(d *Discordant)

Option can be used to a create a customized connections.

func SetLogger

func SetLogger(logger Logger) Option

SetLogger sets logger to Discordant.

func SetSession

func SetSession(session *discordgo.Session) Option

SetSession sets discordgo session to Discordant.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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