discordgoplus

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: May 23, 2026 License: GPL-3.0 Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Discordgo-Plus

Make Discord bots on Go with ease

About

Discordgo-Olus is lightweight Go framework for building bots on Discord.
It does all complicated things for you, keeping your code clean and helps you focus on important stuff.

Installation

To install the library you must have at least Go of version 1.15 and use master version of DiscordGo, otherwise you will end up with a lot of errors. And that's not good.

go get -u github.com/jurienhamaker/discordgoplus

Usage

For complicated usage examples you can visit our examples directory.

package main

import (
    "os"
    "signal"
    "syscall"
    "time"

    "github.com/jurienhamaker/discordgoplus"
    "github.com/bwmarrin/discordgo"
)

func main() {
    bot, err := discordgoplus.New("BOT-TOKEN")
    if err != nil {
        panic(err)
    }

    bot.Router.Register(&discordgoplus.Command{
        Name: "hello_world",
        Description: "Say hi to the world!",
        Handler: discordgoplus.CommandHandlerFunc(func(ctx *discordgoplus.Ctx) error {
            ctx.Respond(&discordgoplus.InteractionResponse {
                Type: discordgoplus.InteractionResponseChannelMessageWithSource,
                Data: &discordgoplus.InteractionResponseData{
                    Content: "Hello world!",
                },
            })
        })
    })

    err := bot.Router.Sync(bot.Session, "", "GUILD-TEST-ID")
    if err != nil {
        panic(err)
    }

    err := bot.Open()
    if err != nil {
        panic(err)
    }
    defer bot.Close()

    stchan := make(chan os.Signal, 1)
    signal.Notify(stchan, syscall.SIGTERM, os.Interrupt, syscall.SIGSEGV)
end:
 for {
  select {
  case <-stchan:
   break end
  default:
  }
  time.Sleep(time.Second)
 }
}

Documentation

Overview

All credits go to the pat package for these utilities https://github.com/bmizerany/pat/blob/0e6a57d3996914bbea76de5a2ce30fc1dbe82e9e/mux.go#L287

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCommandNotExists means that the requested command does not exist.
	ErrCommandNotExists = errors.New("command not exists")

	// 	ErrMessageComponentNotExists means that the requested message component does not exist.
	ErrMessageComponentNotExists = errors.New("message component not exists")
)

Functions

func Defer

func Defer(ctx *Ctx, ephemeral ...bool) (err error)

func ErrorResponse

func ErrorResponse(ctx *Ctx, ephemeral ...bool) error

func FollowUp

func FollowUp(
	ctx *Ctx,
	response *discordgo.WebhookParams,
	ephemeral ...bool,
) (err error)

func ForbiddenResponse

func ForbiddenResponse(ctx *Ctx) error

func GetInteractionName

func GetInteractionName(
	data *discordgo.ApplicationCommandInteractionData,
	delimeter ...string,
) string

func InteractionError

func InteractionError(ctx *Ctx, isFollowup bool)

func MessageComponentError

func MessageComponentError(ctx *Ctx)

func ModalRespond

func ModalRespond(
	ctx *Ctx,
	data *discordgo.InteractionResponseData,
	ephemeral ...bool,
) (err error)

func ParseModalData

func ParseModalData(
	modalData discordgo.ModalSubmitInteractionData,
) map[string]string

func ParseModalDataV2 added in v1.0.1

func ParseModalDataV2(
	modalData discordgo.ModalSubmitInteractionData,
) map[string]interface{}

func Respond

func Respond(
	ctx *Ctx,
	data *discordgo.InteractionResponseData,
	ephemeral ...bool,
) (err error)

func Update

func Update(
	ctx *Ctx,
	data *discordgo.InteractionResponseData,
) (err error)

Types

type Bot

type Bot struct {
	*discordgo.Session

	Router *Router
}

A Bot wraps discordgo.Session with configuration and a router.

func New

func New(token string) (*Bot, error)

New constructs a Bot, from a authentication token.

type BulkCommandSyncer

type BulkCommandSyncer struct{}

BulkCommandSyncer syncs all the commands using ApplicationCommandBulkOverwrite function.

func (BulkCommandSyncer) Sync

func (BulkCommandSyncer) Sync(
	r *Router,
	s *discordgo.Session,
	application string,
	guild string,
) error

Sync implements CommandSyncer interface.

type Command

type Command struct {
	Name               string
	Description        string
	Options            []*discordgo.ApplicationCommandOption
	Type               discordgo.ApplicationCommandType
	Handler            Handler
	GuildID            string
	Middlewares        []Handler
	MessageHandler     MessageHandler
	MessageMiddlewares []MessageHandler

	// NOTE: nesting of more than 3 level has no effect
	SubCommands *Router
	// Custom payload for the command. Useful for module names, and such stuff.
	Custom interface{}
}

Command represents a command.

func (Command) ApplicationCommand

func (cmd Command) ApplicationCommand() *discordgo.ApplicationCommand

ApplicationCommand converts Command to discordgo.ApplicationCommand.

func (Command) ApplicationCommandOption

func (cmd Command) ApplicationCommandOption() *discordgo.ApplicationCommandOption

ApplicationCommandOption converts Command to discordgo.ApplicationCommandOption (subcommand).

type CommandSyncer

type CommandSyncer interface {
	Sync(r *Router, s *discordgo.Session, application, guild string) error
}

A CommandSyncer syncs all the commands with Discord.

type Ctx

type Ctx struct {
	*discordgo.Session `json:"-"`
	Caller             *Command                                             `json:"caller"`
	MessageComponent   *MessageComponent                                    `json:"message_component"`
	Modal              *Modal                                               `json:"modal"`
	Interaction        *discordgo.Interaction                               `json:"interaction"`
	Options            OptionsMap                                           `json:"options"`
	OptionsRaw         []*discordgo.ApplicationCommandInteractionDataOption `json:"options_raw"`

	MessageComponentOptions map[string]string `json:"message_component_options"`

	ModalData    discordgo.ModalSubmitInteractionData `json:"modal_data"`
	ModalOptions map[string]string                    `json:"modal_options"`
	// contains filtered or unexported fields
}

Ctx is a context provided to a command. It embeds session for easier use, and contains interaction and preprocessed options.

func NewCtx

NewCtx constructs ctx from given parameters.

func NewMessageComponentCtx

func NewMessageComponentCtx(
	s *discordgo.Session,
	caller *MessageComponent,
	i *discordgo.Interaction,
	options map[string]string,
	handlers []Handler,
) *Ctx

NewCtx constructs ctx from given parameters.

func NewModalCTX

func NewModalCTX(
	s *discordgo.Session,
	caller *Modal,
	i *discordgo.Interaction,
	options map[string]string,
	data discordgo.ModalSubmitInteractionData,
	handlers []Handler,
) *Ctx

NewCtx constructs ctx from given parameters.

func (*Ctx) Next

func (ctx *Ctx) Next()

func (*Ctx) Respond

func (ctx *Ctx) Respond(response *discordgo.InteractionResponse) error

Respond is a wrapper for ctx.Session.InteractionRespond

func (*Ctx) String

func (ctx *Ctx) String() string

type Handler

type Handler interface {
	HandleCommand(ctx *Ctx)
}

A Handler processes the command

type HandlerFunc

type HandlerFunc func(ctx *Ctx)

HandlerFunc is a wrapper around Handler for functions

func (HandlerFunc) HandleCommand

func (f HandlerFunc) HandleCommand(ctx *Ctx)

HandleCommand implements Handler interface and calls the function with provided context

type MessageComponent

type MessageComponent struct {
	CustomID    string
	Handler     Handler
	Middlewares []Handler
}

MessageComponent represents a message component

type MessageCtx

type MessageCtx struct {
	*discordgo.Session
	Caller    *Command
	Message   *discordgo.Message
	Arguments []string
	// contains filtered or unexported fields
}

MessageCtx is a context provided to message command handler. It contains the message

func NewMessageCtx

func NewMessageCtx(
	s *discordgo.Session,
	caller *Command,
	m *discordgo.Message,
	arguments []string,
	handlers []MessageHandler,
) *MessageCtx

NewMessageCtx constructs context from a message. If argdelim is not empty it is a delimiter for the arguments, otherwise the arguments are split by a space.

func (*MessageCtx) Next

func (ctx *MessageCtx) Next()

Next calls the next middleware / command handler.

func (*MessageCtx) Reply

func (ctx *MessageCtx) Reply(
	content string,
	mention bool,
) (*discordgo.Message, error)

Reply sends and returns a simple (content-only) message replying to the command message. If mention is true the command author is mentioned in the reply. It is a wrapper for discordgo.Session.ChannelMessageSendReply.

func (*MessageCtx) ReplyComplex

func (ctx *MessageCtx) ReplyComplex(
	message *discordgo.MessageSend,
	mention bool,
) (*discordgo.Message, error)

ReplyComplex sends and returns a complex (with embds, attachments, etc) message replying to the command message. If mention is true the command author is mentioned in the reply. It is a wrapper for discordgo.Session.ChannelMessageSendComplex

type MessageHandler

type MessageHandler interface {
	HandleMessageCommand(ctx *MessageCtx)
}

A MessageHandler processes the message command

type MessageHandlerConfig

type MessageHandlerConfig struct {
	// Prefixes got will respond to
	Prefixes      []string
	MentionPrefix bool

	ArgumentDelimiter string
}

type MessageHandlerFunc

type MessageHandlerFunc func(ctx *MessageCtx)

HandlerFunc is a wrapper around MessageHandler for functions

func (MessageHandlerFunc) HandleMessageCommand

func (f MessageHandlerFunc) HandleMessageCommand(ctx *MessageCtx)

HandleCommand implements MessageHandler interface and calls the function with provided context

type Modal struct {
	CustomID    string
	Handler     Handler
	Middlewares []Handler
}

Modal represents a modal component

type OptionsMap

OptionsMap is an alias for map of discordgo.ApplicationCommandInteractionDataOption

type Router

type Router struct {
	// Commands is a map of registered commands.
	// Key is command name. Value is command instance.
	//
	// NOTE: it is not recommended to use it directly, use Register, Get, Update, Unregister functions instead.
	Commands map[string]*Command

	// MessageComponents is a map of registered message components
	// Key is the components custom ID. Value is the MessageComponent instance.
	//
	// NOTE: it is not recommended to use it directly, use RegisterMessageComponent, GetMessageComponent, UpdateMessageComponent, UnregisterMessageComponent functions instead.
	MessageComponents map[string]*MessageComponent

	// Modals is a map of registered modal components
	// Key is the components custom ID. Value is the MessageComponent instance.
	//
	// NOTE: it is not recommended to use it directly, use RegisterMessageComponent, GetMessageComponent, UpdateMessageComponent, UnregisterMessageComponent functions instead.
	Modals map[string]*Modal

	Syncer CommandSyncer
}

A Router stores all the commands and routes the interactions

func NewRouter

func NewRouter(initial []*Command) (r *Router)

NewRouter constructs a router from a set of predefined commands.

func (*Router) Count

func (r *Router) Count() (c int)

Count returns amount of commands stored

func (*Router) CountMessageComponents

func (r *Router) CountMessageComponents() (c int)

CountMessageComponents returns amount of message components stored

func (*Router) CountModals added in v1.0.2

func (r *Router) CountModals() (c int)

CountModals returns amount of modals stored

func (*Router) Get

func (r *Router) Get(name string) *Command

Get returns a command by specified name.

func (*Router) GetMessageComponent

func (r *Router) GetMessageComponent(
	customID string,
) (*MessageComponent, map[string]string)

GetMessageComponent returns a message component by specified CustomID. It also checks for params

func (*Router) GetModal

func (r *Router) GetModal(
	customID string,
) (*Modal, map[string]string)

GetModal returns a message component by specified CustomID. It also checks for params

func (*Router) HandleInteraction

func (r *Router) HandleInteraction(
	s *discordgo.Session,
	i *discordgo.InteractionCreate,
)

HandleInteraction is an interaction handler passed to discordgo.Session.AddHandler.

func (*Router) HandleInteractionMessageComponent

func (r *Router) HandleInteractionMessageComponent(
	s *discordgo.Session,
	i *discordgo.InteractionCreate,
)

HandleInteractionMessageComponent is an interaction handler passed to discordgo.Session.AddHandler.

func (*Router) HandleInteractionModalSubmit

func (r *Router) HandleInteractionModalSubmit(
	s *discordgo.Session,
	i *discordgo.InteractionCreate,
)

HandleInteractionModalSubmit is an interaction handler passed to discordgo.Session.AddHandler.

func (*Router) List

func (r *Router) List() (list []*Command)

List returns all registered commands

func (*Router) ListMessageComponents

func (r *Router) ListMessageComponents() (list []*MessageComponent)

ListMessageComponents returns all registered message components

func (*Router) ListModals

func (r *Router) ListModals() (list []*Modal)

ListModals returns all registered modals

func (*Router) MakeMessageHandler

func (r *Router) MakeMessageHandler(
	cfg *MessageHandlerConfig,
) func(s *discordgo.Session, m *discordgo.MessageCreate)

func (*Router) Register

func (r *Router) Register(cmd *Command)

Register registers the command.

func (*Router) RegisterMessageComponent

func (r *Router) RegisterMessageComponent(cmpnt *MessageComponent)

RegisterMessageComponent registers the message component.

func (*Router) RegisterModal

func (r *Router) RegisterModal(cmpnt *Modal)

RegisterMessageComponent registers the message component.

func (*Router) Sync

func (r *Router) Sync(s *discordgo.Session, application, guild string) error

Sync wraps Router.Syncer and automatically detects application id.

func (*Router) Unregister

func (r *Router) Unregister(name string) (command *Command, existed bool)

Unregister removes a command from router

func (*Router) UnregisterMessageComponent

func (r *Router) UnregisterMessageComponent(
	customID string,
) (messageComponent *MessageComponent, existed bool)

UnregisterMessageComponent removes a message component from router

func (*Router) UnregisterModal

func (r *Router) UnregisterModal(
	customID string,
) (modal *Modal, existed bool)

UnregisterModal removes a modal from router

func (*Router) Update

func (r *Router) Update(
	name string,
	newcmd *Command,
) (cmd *Command, err error)

Update updates the command and does all behind-the-scenes work.

func (*Router) UpdateMessageComponent

func (r *Router) UpdateMessageComponent(
	customID string,
	newcmpnt *MessageComponent,
) (cmd *MessageComponent, err error)

UpdateMessageComponent updates the message component and does all behind-the-scenes work.

func (*Router) UpdateModal

func (r *Router) UpdateModal(
	customID string,
	newcmpnt *Modal,
) (cmd *Modal, err error)

UpdateMessageComponent updates the message component and does all behind-the-scenes work.

Jump to

Keyboard shortcuts

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