anpan

package module
v0.0.0-...-309b673 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2021 License: MIT Imports: 8 Imported by: 0

README

Actions results Go Report Card GoDoc

anpan

anpan is a command handler for discordgo.

Usage

Check the example directory for an example for usage of anpan, in addition to an example of a help command.

Example

An example bot can be found within the example folder.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBotBlocked is thrown when the message handler encounters a bot, but ignoring bots was set to true.
	ErrBotBlocked = errors.New("anpan: The given author was a bot and the IgnoreBots setting is true")

	// ErrCommandAlreadyRegistered is thrown when a command by the same name was registered previously.
	ErrCommandAlreadyRegistered = errors.New("anpan: Another command was already registered by this name")

	// ErrCommandNotFound is thrown when a message tries to invoke an unknown command, or when an attempt at removing an unregistered command was made.
	ErrCommandNotFound = errors.New("anpan: Command not found")

	// ErrDataUnavailable is thrown when data is unavailable, like channels, users or something else.
	ErrDataUnavailable = errors.New("anpan: Necessary data couldn't be fetched")

	// ErrDMOnly is thrown when a DM-only command is executed on a guild.
	ErrDMOnly = errors.New("anpan: DM-Only command on guild")

	// ErrGuildOnly is thrown when a guild-only command is executed in direct messages.
	ErrGuildOnly = errors.New("anpan: Guild-Only command in DMs")

	// ErrOwnerOnly is thrown when an owner-only command is executed.
	ErrOwnerOnly = errors.New("anpan: Owner-Only command")

	// ErrSelfInsufficientPermissions is thrown when the bot itself does not have enough permissions.
	ErrSelfInsufficientPermissions = errors.New("anpan: Insufficient permissions for the bot")

	// ErrUserInsufficientPermissions is thrown when the user doesn't meet the required permissions.
	ErrUserInsufficientPermissions = errors.New("anpan: Insufficient permissions for the user")
)

Functions

func WaitForInterrupt

func WaitForInterrupt()

WaitForInterrupt makes your application wait for an interrupt. A SIGINT, SIGTERM or a console interrupt will make this function stop. Note that the Exit function in the os package will make this function stop, too.

Types

type Command

type Command struct {
	Aliases     []string
	Description string
	Name        string

	Hidden    bool
	OwnerOnly bool

	SelfPermissions int64
	UserPermissions int64

	Run CommandFunc

	Type CommandType
}

Command represents a command. Refer to AddCommand for help.

type CommandFunc

type CommandFunc func(Context, []string) error

CommandFunc defines a normal command's function.

Parameters: Context -> The context supplied by the command handler. Refer to Context for help. []string -> The arguments sent along with the command, basically the rest of the message after the command and the prefix. Note that this is split by spaces.

type CommandHandler

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

CommandHandler contains all the data needed for the handler to function. Anything inside here must be controlled with the appropriate Get/Set/Remove function.

func New

func New(prefixes []string, owners []string, useState, ignoreBots, respondToPings, checkPermssions bool, prerunFunc PrerunFunc, errorFunc OnErrorFunc, debugFunc DebugFunc) CommandHandler

New creates a new command handler.

Parameters: prefixes - The prefixes to use for the command handler. owners - The owners of this application; these are used for Owner-Only commands. useState - Whether to use the session's state th fetch data or not. The state will be ignored if the State field of the session used in the message handler is set false. ignoreBots - Whether to ignore users marked as bots or not. checkPermissions - Whether to check permissions or not. useRoutines - Whether to execute commands outside the event's routine.

Notes: Refer to MessageHandler to properly activate the command handler.

func (*CommandHandler) AddCommand

func (c *CommandHandler) AddCommand(name, desc string, aliases []string, owneronly, hidden bool, selfperms, userperms int64, cmdtype CommandType, run CommandFunc) error

AddCommand adds a command to the Commands map.

Parameters: name - The name of the this command. description - The description for this command. aliases - Additional aliases used for this command. owneronly - Whether only owners can access this command or not. hidden - Whether a help command should hide this command or not. selfperms - The necessary permissions for this command. Set this to "0" if any level is fine. userperms - The necessary permissions for the user to meet to use this command. Set this to "0" if any level is fine. cmdtype - The appropriate command type for this command. Use this to limit commands to direct messages or guilds. Refer to CommandType for help. function - The command itself. Refer to CommandFunc for help.

Errors: ErrCommandAlreadyRegistered -> There's already a (help) command with this name.

func (*CommandHandler) AddOwner

func (c *CommandHandler) AddOwner(id string)

AddOwner adds a user ID as an owner.

func (*CommandHandler) AddPrefix

func (c *CommandHandler) AddPrefix(prefix string)

AddPrefix adds a prefix to the handler.

func (*CommandHandler) ClearDebugFunc

func (c *CommandHandler) ClearDebugFunc()

ClearDebugFunc clears the Debug function Refer to DebugFunc for more information.

func (*CommandHandler) ClearHelpCommand

func (c *CommandHandler) ClearHelpCommand()

ClearHelpCommand clears the current help command.

func (*CommandHandler) ClearOnErrorFunc

func (c *CommandHandler) ClearOnErrorFunc()

ClearOnErrorFunc removes the current OnError function. Refer to OnErrorFunc for more details.

func (*CommandHandler) ClearPrerunFunc

func (c *CommandHandler) ClearPrerunFunc()

ClearPrerunFunc removes the current PrerunFunc. Refer to PrerunFunc for more info.

func (*CommandHandler) GetAllPrefixes

func (c *CommandHandler) GetAllPrefixes() []string

GetAllPrefixes returns the current prefixes slice.

func (*CommandHandler) GetCheckPermissions

func (c *CommandHandler) GetCheckPermissions(enable bool) bool

GetCheckPermissions returns whether the message handler checks the permissions or not.

func (*CommandHandler) GetDebugFunc

func (c *CommandHandler) GetDebugFunc() DebugFunc

GetDebugFunc returns the current debugging function. Refer to DebugFunc for more information.

func (*CommandHandler) GetEnable

func (c *CommandHandler) GetEnable() bool

GetEnable returns whether the message handler is actually enabled or not.

func (*CommandHandler) GetHelpCommand

func (c *CommandHandler) GetHelpCommand() *HelpCommand

GetHelpCommand returns the current set help command. Refer to HelpCommandFunc for help.

func (*CommandHandler) GetIgnoreBots

func (c *CommandHandler) GetIgnoreBots() bool

GetIgnoreBots returns whether the bot ignores other users marked as bots or not.

func (*CommandHandler) GetOnErrorFunc

func (c *CommandHandler) GetOnErrorFunc() OnErrorFunc

GetOnErrorFunc returns the current OnError function. Refer to OnErrorFunc for more details.

func (*CommandHandler) GetOwners

func (c *CommandHandler) GetOwners() []string

GetOwners returns the current owner list.

func (*CommandHandler) GetPrerunFunc

func (c *CommandHandler) GetPrerunFunc() PrerunFunc

GetPrerunFunc returns the current PrerunFunc. Refer to PrerunFunc for more info.

func (*CommandHandler) GetUseState

func (c *CommandHandler) GetUseState() bool

GetUseState returns whether the command handler uses the cached State of the session or not.

func (*CommandHandler) IsOwner

func (c *CommandHandler) IsOwner(id string) bool

IsOwner checks whether the given ID is set as an owner.

func (*CommandHandler) MessageHandler

func (c *CommandHandler) MessageHandler(s *discordgo.Session, event *discordgo.MessageCreate)

MessageHandler handles incoming messages and runs commands. Pass this to your Session's AddHandler function.

func (*CommandHandler) RemoveCommand

func (c *CommandHandler) RemoveCommand(name string) error

RemoveCommand removes the supplied command from the command array by using its name.

Errors: ErrCommandNotFound -> The given name doesn't belong to any command.

func (*CommandHandler) RemoveOwner

func (c *CommandHandler) RemoveOwner(id string)

RemoveOwner removes a user ID from the owner list.

func (*CommandHandler) RemovePrefix

func (c *CommandHandler) RemovePrefix(prefix string)

RemovePrefix removes the prefix from the handler, if it exists.

func (*CommandHandler) SetAllPrefixes

func (c *CommandHandler) SetAllPrefixes(prefixes []string)

SetAllPrefixes overwrites all prefixes within the prefixes slice.

func (*CommandHandler) SetCheckPermissions

func (c *CommandHandler) SetCheckPermissions(enable bool)

SetCheckPermissions sets whether to check for permissions or not.

func (*CommandHandler) SetDebugFunc

func (c *CommandHandler) SetDebugFunc(df DebugFunc)

SetDebugFunc sets the given debug function as the debugging function for the command handler.

func (*CommandHandler) SetEnable

func (c *CommandHandler) SetEnable(enable bool)

SetEnable sets whether the message handler shall doing its job.

func (*CommandHandler) SetHelpCommand

func (c *CommandHandler) SetHelpCommand(name string, aliases []string, selfperms, userperms int64, function HelpCommandFunc) error

SetHelpCommand sets the help command.

Parameters: name - The name of the help command; this should be "help" under normal circumstances. aliases - Additional aliases used for the help command. selfperms - The necessary permissions for this help command. Set this to "0" if any level is fine. userperms - The necessary permissions for the user to meet to use this help command. Set this to "0" if any level is fine. function - The help command itself. Refer to HelpCommandFunc for help.

Notes: The command handler always checks for the help command first.

Errors: ErrCommandAlreadyRegistered -> There's already another command that has been registered with the same name.

func (*CommandHandler) SetIgnoreBots

func (c *CommandHandler) SetIgnoreBots(enable bool)

SetIgnoreBots sets whether to ignore other bots or not.

func (*CommandHandler) SetOnErrorFunc

func (c *CommandHandler) SetOnErrorFunc(oef OnErrorFunc)

SetOnErrorFunc sets the supplied OnErrorFunc as the one to use. Refer to OnErrorFunc for more details.

func (*CommandHandler) SetOwners

func (c *CommandHandler) SetOwners(ids []string)

SetOwners overwrites the current owner list with the given one.

func (*CommandHandler) SetPrerunFunc

func (c *CommandHandler) SetPrerunFunc(prf PrerunFunc)

SetPrerunFunc sets the supplied PrerunFunc as the one to use. Refer to PrerunFunc for more info.

func (*CommandHandler) SetUseState

func (c *CommandHandler) SetUseState(enable bool)

SetUseState sets whether the command handler should use the cached State of the session or not.

type CommandType

type CommandType int

CommandType defines where commands can be used.

const (
	// CommandTypeEverywhere defines a command that can be executed anywhere.
	CommandTypeEverywhere CommandType = iota

	// CommandTypeGuild defines a command that cannot be executed in direct messages.
	CommandTypeGuild

	// CommandTypePrivate defines a command that cannot be executed in a guild.
	CommandTypePrivate
)

type Context

type Context struct {
	// Handler represents the handler on which this command was registered on.
	Handler *CommandHandler

	// Channel defines the channel in which the command has been executed.
	Channel *discordgo.Channel

	// Guild defines the guild in which the command has been executed.
	// Note that this may be nil under certain circumstances.
	Guild *discordgo.Guild

	// Member defines the member in the guild in which the command has been executed.
	// Note that, if guild is nil, this will be nil too.
	Member *discordgo.Member

	// Message defines the message that has executed this command.
	Message *discordgo.Message

	// Session defines the session that this command handler run on top of.
	Session *discordgo.Session

	// User defines the user that has executed the command.
	User *discordgo.User
}

Context holds the data required for command execution.

func (*Context) Reply

func (c *Context) Reply(message string) (*discordgo.Message, error)

Reply directly replies with a message.

Parameters: message - The message content.

func (*Context) ReplyComplex

func (c *Context) ReplyComplex(message string, tts bool, embed *discordgo.MessageEmbed, files []*discordgo.File) (*discordgo.Message, error)

ReplyComplex combines Reply, ReplyEmbed and ReplyFile as a way to send a message with, for example, Text and an Embed together.

Parameters: message - The message content. tts - Whether the client should read the message out or not. embed - The embed for this message. Refer to discordgo.MessageEmbed for more info. files - The files to send across. These (collectively) cannot pass more than 8 Megabytes. Refer to discordgo.File for information.

func (*Context) ReplyEmbed

func (c *Context) ReplyEmbed(embed *discordgo.MessageEmbed) (*discordgo.Message, error)

ReplyEmbed directly replies with a embed, but not with a message.

Parameters: embed - The embed for this message. Refer to discordgo.MessageEmbed for more info.

func (*Context) ReplyFile

func (c *Context) ReplyFile(filename string, file io.Reader) (*discordgo.Message, error)

ReplyFile directly replies with a file, but not with a message.

Parameters: files - The files to send across. These (collectively) cannot pass more than 8 Megabytes.

type DebugFunc

type DebugFunc func(string)

DebugFunc is used for debugging output.

Parameters: string -> The returned message.

type HelpCommand

type HelpCommand struct {
	Aliases         []string
	Name            string
	SelfPermissions int64
	UserPermissions int64
	Run             HelpCommandFunc
}

HelpCommand defines a help command. Refer to SetHelpCommand for help.

type HelpCommandFunc

type HelpCommandFunc func(Context, []string, []*Command, []string) error

HelpCommandFunc defines a help command's function. Context -> The context supplied by the command handler. Refer to Context for help. []string -> The arguments sent along with the command, basically the rest of the message after the command and the prefix. Note that this is split by spaces. This can be used to show help for a specific command. []*Command -> The command slice, containing all commands. []string -> The prefixes used by the command handler.

type OnErrorFunc

type OnErrorFunc func(Context, *Command, []string, error)

OnErrorFunc is the type for the function that can be run.

Parameters: Context -> The context supplied by the command handler. Refer to Context for help. *Command -> The command in question. []string -> The arguments sent along with the command, basically the rest of the message after the command and the prefix. Note that this is split by spaces. This can be used to show help for a specific command. error -> The returned error.

type PrerunFunc

type PrerunFunc func(Context, *Command, []string) bool

PrerunFunc is the type for the function that can be run before command execution. It is executed after any permission checks and the likes but run before the command itself. If all goes well, return true. otherwise, false.

Parameters: Context -> The supplied content. *Command -> The command that is about to be executed. []string -> The arguments sent along with the command, basically the rest of the message after the command and the prefix. Note that this is split by spaces. This can be used to show help for a specific command.

Notes: This is executed before the actual command, unless the guild object is not nil, then it's run before the permission check.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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