ashrouter

package module
v0.0.1-alpha Latest Latest
Warning

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

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

README

ashrouter

go.dev reference Go Report Card

Getting Started

Installation
go get github.com/mathlet/ashrouter

There's no set architecture but a good way to do it is to put commands in individual files.

Usage

Import:

import "github.com/mathlet/ashrouter"

Create a new router:

router := ashrouter.NewRouter()

Add global commands (call AddGuildCommands if you don't want the commands to be global):

router.AddGlobalCommands(
		ashrouter.Command{
			ApplicationCommand: &discordgo.ApplicationCommand{
				Name:        "ping",
				Description: "Pings the bot",
				Type:        discordgo.ChatApplicationCommand,
			},
			Handler: func(ctx *ashrouter.Context) error {
				err := ctx.InteractionRespond(ctx.Interaction, &discordgo.InteractionResponse{
					Type: discordgo.InteractionResponseChannelMessageWithSource,
					Data: &discordgo.InteractionResponseData{
						Flags:   64,
						Content: fmt.Sprintf("Pong %dms!", ctx.HeartbeatLatency()/time.Millisecond),
					},
				})
				return err
			},
		},
	)

Add default router handlers to session:

router.StartWithDefault()

This should be called before opening the session. You don't have to call this. This just makes it easy if you don't want to setup custom functionality for how the commands are loaded.

For more examples check the examples directory.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CanBan

func CanBan(s *discordgo.Session, guildID string, m *discordgo.Member) bool

func HasPerm

func HasPerm(userPerms, hasPerm int64) bool

func IsChannelAdmin

func IsChannelAdmin(s *discordgo.Session, channelID, userID string) bool

func WaitForMessage

func WaitForMessage(s *discordgo.Session) chan *discordgo.MessageCreate

func WaitForUserMessage

func WaitForUserMessage(s *discordgo.Session, userID string) chan *discordgo.MessageCreate

func WaitForUserReact

func WaitForUserReact(s *discordgo.Session, userID string) chan *discordgo.MessageReactionAdd

Types

type Command

type Command struct {
	// Discordgo slash command
	*discordgo.ApplicationCommand
	// The command's middlewares
	Middlewares []Middleware
	// The command's handler function
	Handler CommandHandler
}

A Command has an underlying discordgo.ApplicationCommand pointer, middlewares, and a handler.

func (*Command) ApplyMiddlewares

func (c *Command) ApplyMiddlewares() CommandHandler

ApplyMiddlewares applies all of the commands middlewares to it's handler

type CommandHandler

type CommandHandler func(*Context) error

A CommandHandler is just a function that handles command invokes

func IsAdmin

func IsAdmin(h CommandHandler) CommandHandler

IsAdmin is a builtin middleware that checks if the caller of the command is an admin. It will only run the root handler if they are. If not, it will inform them they lack permissions.

type Context

type Context struct {
	// Discordgo session
	*discordgo.Session
	// Interaction of command
	*discordgo.Interaction
	// An interface for any data you want to pass to your commands
	Vars interface{}
}

Context is a command context passed into all command handlers.

type Middleware

type Middleware func(CommandHandler) CommandHandler

Middlewares wrap your command handlers to perform checks before the root handler is called.

type Router

type Router struct {
	// The commands you want to add globally
	GlobalCommands []Command
	// The commands you want to add to guilds
	GuildCommands []Command
}

A router stores your commands and handles InteractionCreate events

func NewRouter

func NewRouter() *Router

NewRouter returns a pointer to a new router instance

func (*Router) AddGlobalCommands

func (r *Router) AddGlobalCommands(commands ...Command)

AddGlobalCommands appends your commands to router.GlobalCommands. commands: The commands you want to add.

func (*Router) AddGuildCommands

func (r *Router) AddGuildCommands(commands ...Command)

AddGuildCommands appends your commands to router.GuildCommands. commands: The commands you want to add.

func (*Router) CreateGlobalCommands

func (r *Router) CreateGlobalCommands(s *discordgo.Session)

CreateGlobalCommands deletes old global commands and adds all new ones. If you don't want to delete you can add commands manually. s: the session you want to create the commands on.

func (*Router) CreateGuildCommands

func (r *Router) CreateGuildCommands(s *discordgo.Session, guildID string)

CreateGuildCommands deletes old guild commands and adds all new ones. If you don't want to delete you can add commands manually. s: the session you want to create the commands on. guildID: the snowflake of the guild you want to add the commands to.

func (*Router) DefaultCommandExecutor

func (r *Router) DefaultCommandExecutor(session *discordgo.Session, vars interface{})

DefaultCommandExecutor adds the router's default command executor to the session. This will check InteractionCreate events for your commands being called, apply middlewares, and run the called command's handler. session: The session you want to add the executor to. vars: Can be anything you want (like a database you want your commands to have access to).

func (*Router) StartWithDefault

func (r *Router) StartWithDefault(session *discordgo.Session, vars interface{})

StartWithDefault adds handlers to session. Call this before openning session. session: The session you want to run the router on. vars: Can be anything you want (like a database you want your commands to have access to).

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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