mtn_go_discord_framework

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: AGPL-3.0 Imports: 7 Imported by: 0

README

MTN Go Discord Framework

Framework to simplifly the creation of discord bots with slash commands and buttons in go build ontop of discordgo. All discordgo features are still available and can be used directly via discordgo, this just helps with the startup and creation of slash commands and buttons.

Example Bot with SlashCommand and Button

Full Example Bot

func main() {

    log.Println("Starting bot")
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
    defer cancel()

    // Init the framework and returns a discordgo.Session if you need it
    discord := mtn_go_discord_framework.InitFramework(os.Getenv("DEBUG"), os.Getenv("TESTING_GUILD_ID"), os.Getenv("BOT_TOKEN"))

    // Register Button Handlers
    mtn_go_discord_framework.RegisterButtonHandlersWithFramework(getButtonHandlers())
    // Register Slash Commands
	mtn_go_discord_framework.RegisterSlashCommandsWithFramework(getCommands())

    // Launch Framework and Start Bot
	mtn_go_discord_framework.StartFramework()

    // Wait for interrupt
    <-ctx.Done()

    // Stop Bot
    mtn_go_discord_framework.ShutdownFramework()
}

func getButtonHandlers() []mtn_go_discord_framework.ButtonHandler {
	handlers := make([]mtn_go_discord_framework.ButtonHandler, 0)
    // example button handler
	handlers = append(handlers, mtn_go_discord_framework.ButtonHandler{
		Handler:  func(s *discordgo.Session, i *discordgo.InteractionCreate, args ...string) {
            // do something
        },
		CustomID: "example_button", // must be mapped, use - seperator to append args to custom id, can then be used in the handler with args[0] etc.
	})
	return handlers
}


func getCommands() []mtn_go_discord_framework.SlashCommand {
	commandList := make([]mtn_go_discord_framework.SlashCommand, 0)
	commandList = append(commandList, testCommand())
	return commandList
}

func testCommand() mtn_go_discord_framework.SlashCommand {
	// define handler function
	handler := func(s *discordgo.Session, i *discordgo.InteractionCreate, options *mtn_go_discord_framework.OptionContainer) {
		mtn_go_discord_framework.SendEphemeralResponse(s, i, "Test command")
	}
	command := mtn_go_discord_framework.SlashCommand{
		Name:        "test",
		Description: "Test command",
		Handler:     handler,
	}
	return command
}

Example Slash Command with Options

func exampleOptionsCommand() mtn_go_discord_framework.SlashCommand {
	handler := func(s *discordgo.Session, i *discordgo.InteractionCreate, options *mtn_go_discord_framework.OptionContainer) {
		// get options from container. If required options are not provided, the handler will not be called, so no need to check for nil. If required is false default values will be used if not provided by user
		stringvalue := options.Options["stringvalue"].GetValue().(string)
		numbervalue := options.Options["numbervalue"].GetValue().(float64)
		boolvalue := options.Options["boolvalue"].GetValue().(bool)

        // check if user had roles or admin if needed
		var authenticated bool
        authenticated = mtn_go_discord_framework.CheckForRoles(s, i, GROUP_ID1, GROUP_ID2) // unlimited roles can be provided
        authenticated = mtn_go_discord_framework.CheckForAdmin(s, i)
        authenticated = mtn_go_discord_framework.CheckForRolesOrAdmin(s, i, GROUP_ID1, GROUP_ID2) // unlimited roles can be provided

        // do something

	}
	command := mtn_go_discord_framework.SlashCommand{
		Name:        "exampleoptions",
		Description: "Example command with options",
		RequiredOptions: []mtn_go_discord_framework.OptionRequirement{
			{
				Name:        "stringvalue",
				Description: "StringValue",
				Type:        discordgo.ApplicationCommandOptionString,
				Required:    true,
			},
			{
				Name:        "numbervalue",
				Description: "NumberValue",
				Type:        discordgo.ApplicationCommandOptionInteger,
				Required:    true,
			},
            // If Required is false, you must provide a default value
			{
				Name:        "boolvalue",
				Description: "BoolValue",
				Type:        discordgo.ApplicationCommandOptionBoolean,
				Required:    false,
				Default: mtn_go_discord_framework.BooleanOption{
					Value: true,
					Name:  "boolvalue",
				},
			},
		},
		Handler: handler,
	}
	return command
}
Example Responses
// Send a response to the user that is only visible to them
mtn_go_discord_framework.SendEphemeralResponse(s *discordgo.Session, i *discordgo.InteractionCreate, "Ephemeral Response")

// Send embed to user that is only visible to them
mtn_go_discord_framework.SendEphemeralEmbed(s *discordgo.Session, i *discordgo.InteractionCreate, embed *discordgo.MessageEmbed)

// Send defer response to discord, that the interaction is handled without a response
mtn_go_discord_framework.SendDeferResponse(s *discordgo.Session, i *discordgo.InteractionCreate)

// Other reponses to be done directly via discordgo

Documentation

Index

Constants

View Source
const (
	EphemeralFlag = 64
)

Variables

View Source
var (
	ErrInvalidOptionType     = errors.New("invalid option type")
	ErrMissingRequiredOption = errors.New("missing required option")
)

declare errors

Functions

func BusySleep

func BusySleep()

busySleep pauses execution for up to 10 seconds if the system is busy

func CheckForAdmin added in v0.1.13

func CheckForAdmin(s *discordgo.Session, i *discordgo.InteractionCreate) bool

func CheckForRoles

func CheckForRoles(s *discordgo.Session, i *discordgo.InteractionCreate, roles ...string) bool

checkForRoles checks if a member has one of the provided roles

func CheckForRolesOrAdmin added in v0.1.13

func CheckForRolesOrAdmin(s *discordgo.Session, i *discordgo.InteractionCreate, roles ...string) bool

func InitFramework

func InitFramework(debugMode bool, testingGuildId string, botToken string) *discordgo.Session

Initializes the framework and returns a discord session, needed for the other functions

func RegisterButtonHandlerWithFramework

func RegisterButtonHandlerWithFramework(handler ButtonHandler)

Registers a button handler with the framework

func RegisterButtonHandlersWithFramework added in v0.1.3

func RegisterButtonHandlersWithFramework(handlers []ButtonHandler)

Registers multiple button handlers with the framework

func RegisterSlashCommandWithFramework

func RegisterSlashCommandWithFramework(command SlashCommand)

Registers a slash command with the framework

func RegisterSlashCommandsWithFramework added in v0.1.3

func RegisterSlashCommandsWithFramework(commands []SlashCommand)

Registers multiple slash commands with the framework

func ReleaseSystem

func ReleaseSystem()

Set systemBusy to false. Should be deferred after a successful tryAcquireSystem.

func SendDeferResponse

func SendDeferResponse(s *discordgo.Session, i *discordgo.InteractionCreate)

sendDeferResponse sends a deferred response to an interaction

func SendEphemeralEmbed added in v0.1.4

func SendEphemeralEmbed(s *discordgo.Session, i *discordgo.InteractionCreate, embed *discordgo.MessageEmbed)

func SendEphemeralResponse

func SendEphemeralResponse(s *discordgo.Session, i *discordgo.InteractionCreate, msg string)

sendEphemeralResponse sends a response to an interaction that only the user can see

func ShutdownFramework

func ShutdownFramework()

Shuts down the framework and closes the discord session

func StartFramework

func StartFramework()

Launches the framework and registers all commands and handlers

func TryAcquireSystem

func TryAcquireSystem() bool

Check if the system is busy. If not, set it to busy and return true. If it is busy, return false.

Types

type BooleanOption added in v0.1.6

type BooleanOption struct {
	Value bool
	Name  string
}

func (BooleanOption) GetName added in v0.1.6

func (s BooleanOption) GetName() string

func (BooleanOption) GetValue added in v0.1.6

func (s BooleanOption) GetValue() any

type ButtonHandler

type ButtonHandler struct {
	CustomID string
	Handler  func(s *discordgo.Session, i *discordgo.InteractionCreate, args ...string)
}

type ChannelOption added in v0.1.6

type ChannelOption struct {
	Value *discordgo.Channel
	Name  string
}

func (ChannelOption) GetName added in v0.1.6

func (s ChannelOption) GetName() string

func (ChannelOption) GetValue added in v0.1.6

func (s ChannelOption) GetValue() any

type CommandOption added in v0.1.6

type CommandOption interface {
	GetValue() any
	GetName() string
}

type FloatOption added in v0.1.6

type FloatOption struct {
	Value float64
	Name  string
}

func (FloatOption) GetName added in v0.1.6

func (s FloatOption) GetName() string

func (FloatOption) GetValue added in v0.1.6

func (s FloatOption) GetValue() any

type IntegerOption added in v0.1.6

type IntegerOption struct {
	Value int64
	Name  string
}

func (IntegerOption) GetName added in v0.1.6

func (s IntegerOption) GetName() string

func (IntegerOption) GetValue added in v0.1.6

func (s IntegerOption) GetValue() any

type OptionContainer added in v0.1.6

type OptionContainer struct {
	Options map[string]CommandOption
}

type OptionRequirement added in v0.1.6

type OptionRequirement struct {
	Required    bool
	Name        string
	Description string
	Type        discordgo.ApplicationCommandOptionType
	Default     CommandOption
}

type RoleOption added in v0.1.6

type RoleOption struct {
	Value *discordgo.Role
	Name  string
}

func (RoleOption) GetName added in v0.1.6

func (s RoleOption) GetName() string

func (RoleOption) GetValue added in v0.1.6

func (s RoleOption) GetValue() any

type SlashCommand added in v0.1.4

type SlashCommand struct {
	Name            string
	Description     string
	Handler         func(s *discordgo.Session, i *discordgo.InteractionCreate, options *OptionContainer)
	RequiredOptions []OptionRequirement
	// contains filtered or unexported fields
}

type StringOption added in v0.1.6

type StringOption struct {
	Value string
	Name  string
}

func (StringOption) GetName added in v0.1.6

func (s StringOption) GetName() string

func (StringOption) GetValue added in v0.1.6

func (s StringOption) GetValue() any

type UnsignedIntergerOption added in v0.1.6

type UnsignedIntergerOption struct {
	Value uint64
	Name  string
}

func (UnsignedIntergerOption) GetName added in v0.1.6

func (s UnsignedIntergerOption) GetName() string

func (UnsignedIntergerOption) GetValue added in v0.1.6

func (s UnsignedIntergerOption) GetValue() any

type UserOption added in v0.1.6

type UserOption struct {
	Value *discordgo.User
	Name  string
}

func (UserOption) GetName added in v0.1.6

func (s UserOption) GetName() string

func (UserOption) GetValue added in v0.1.6

func (s UserOption) GetValue() any

Jump to

Keyboard shortcuts

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