recv

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2021 License: MIT Imports: 3 Imported by: 0

README

recv

A simple message command library for DiscordGo, inspired by discord.py.

GoDoc

Features

  • Command aliases
  • Argument converters: convert a string input into output of any type
  • Command checks (for permissions etc)
  • Command usage generation
  • Easy to implement in an existing DiscordGo project

Installation

It is assumed that you have Go 1.17 installed.

go get github.com/quakecodes/recv

Usage

A full scale project can be found in the _example folder, including examples for checks and converters


Create a new command router
router := recv.NewCommandRouter()
Add a command
router.AddCommand(recv.Command{
  Name:        "Ping",
  Description: "Sends \"pong\"",
  Callback: func(c *recv.CommandCtx) {
    c.Session.ChannelMessageSend(c.Message.ChannelID, "pong")
  },
})
Process commands in a message
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
  res, err := router.ProcessCommands(".", s, m)
  if err != nil {
    fmt.Println(err)
    return
  } else if res.Command == nil {
    s.ChannelMessageSend(m.ChannelID, "command not found")
  }
  go res.Callback()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CheckError

type CheckError struct {
	// the check that failed
	Check *CommandCheck
}

returned when a command check fails

func (CheckError) Error

func (e CheckError) Error() string

type Command

type Command struct {
	Name        string
	Description string
	Args        []CommandArg
	Aliases     []string
	NoJoin      bool
	Checks      []CommandCheck
	Callback    func(*CommandCtx)
}

represents a command that users can use

func (Command) Usage

func (c Command) Usage() string

helper function that gets the usage for a command minus the prefix

type CommandArg

type CommandArg struct {
	Name        string
	Optional    bool
	Description string
	Converter   *Converter
}

argument for a command

type CommandCheck

type CommandCheck struct {
	Name     string
	Callback func(*Ctx) bool
}

a callback that decides whether a command can be run or not

type CommandCtx

type CommandCtx struct {
	*Ctx
	Args []interface{}
}

stores contextual information for a command such as arguments, session and message

type CommandRouter

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

stores command routes

func NewCommandRouter

func NewCommandRouter() CommandRouter

creates a new CommandRouter

func (CommandRouter) AddCommand

func (r CommandRouter) AddCommand(comm Command)

adds a command to the command router's map

func (CommandRouter) GetCommand

func (r CommandRouter) GetCommand(name string) (Command, bool)

fetches a command from the command router's map via name

func (CommandRouter) GetCommandList added in v1.0.1

func (r CommandRouter) GetCommandList() []Command

returns all command objects - use GetCommandNameList to get list of only command names

func (CommandRouter) GetCommandNameList added in v1.0.1

func (r CommandRouter) GetCommandNameList() []string

returns all command names in lowercase - use GetCommandList to return all command objects

func (CommandRouter) ProcessCommands

func (r CommandRouter) ProcessCommands(prefix string, session *discordgo.Session, message *discordgo.MessageCreate) (*ProcessResult, error)

uses the prefix of the bot, session and message object in order to determine the handler and context of the command

type ConversionError

type ConversionError struct {
	// The arg that the converter belongs to
	Arg *CommandArg
	// The input that failed to convert
	Input string
	// The position of the arg in the command arguments - indexed on 1
	ArgPosition int
}

returned when a converter doesn't convert input successfully

func (ConversionError) Error

func (e ConversionError) Error() string

type Converter

type Converter struct {
	Name     string
	Callback func(*ConverterCtx) (interface{}, bool)
}

a callback that "converts" a string input into something else

type ConverterCtx

type ConverterCtx struct {
	*Ctx
	Arg   *CommandArg
	Input string
}

stores contextual information for a converter such as the argument, input, session and message

type Ctx

type Ctx struct {
	Command *Command
	Session *discordgo.Session
	Message *discordgo.MessageCreate
}

stores basic context

type MissingRequiredArgumentError

type MissingRequiredArgumentError struct {
	Arg         *CommandArg
	ArgPosition int
}

returned when a required argument is missing

func (MissingRequiredArgumentError) Error

type ProcessResult

type ProcessResult struct {
	// command that was processed from the message, nil if no command was found
	Command *Command
	// callback function that runs the command callback
	Callback func()
}

the result of calling ProcessCommands, contains the handler for the function and command that was parsed

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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