bot_template

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2022 License: GPL-3.0 Imports: 5 Imported by: 0

README

Discord Bot Template

This package allows you to get up and running with disgord, with application command and handler support built in. It also is really easy to use components!

To start out with:

go get github.com/jacks0n9/bot_template

To make a bot

package main

import (
    "log"

	"github.com/andersfylling/disgord"
	"github.com/jacks0n9/bot_template"

)

var bot = bot_template.NewBotWithDefault()


func init() {
    // We set the client after the initial creation so the commands can be created properly
    // You may want to use the bot with a config file, in which case, you load the token in a init function or main function
    // these functions run after the commands are registered, using the new bot.
    // it will throw an error if we create a bot with an invalid or empty token, so to be as flexible as possible, please just do this.
	bot.Client = disgord.New(disgord.Config{
		BotToken: "your-token-here (don't put it here though, put it in a config file it's a best practice)",
		Logger:   logger,
		Intents:  disgord.IntentGuilds,
	})
}
func main() {
	err := bot.Run()
	if err != nil {
        log.Errorln("error running bot: "+err.Error())
	}
}

Commands

An interaction (command/button) handler in this package has the following function signature:
func(session disgord.Session, interaction *disgord.InteractionCreate) error

See how the function returns an error? Returning an error will give the user a simple error message you can specify in the client options (but those messages are already filled in by NewBotWithDefault), but also give you the actual error message in your logger.

Commands are normally created in a seperate file in an init function like so:

package main

import (
	"context"

	"github.com/andersfylling/disgord"
)

func init() {
	bot.AddCommand(&disgord.CreateApplicationCommand{
		Name:        "setup",
		Description: "Configure roles and permissions.",
	}, nil, func(session disgord.Session, interaction *disgord.InteractionCreate) error {
		session.SendInteractionResponse(context.Background(),interaction,&disgord.CreateInteractionResponse{
			Type: disgord.InteractionCallbackChannelMessageWithSource,
			Data: &disgord.CreateInteractionResponseData{
				Content: "This is a response, that's all",
			},
		})
		return nil
	})
}

REMEMBER: Bot is a variable and has to be defined so if it isn't working that might be why

Message Components

How does it work?

A message component is a thing on a message that you can interact with, like a button or select menu. Messages components must be wrapped in one action row to work. Every message component, besides an action row, has a custom id, which is sent to the bot server when it's interacted with. Bot template maps each of these components to a function that is called upon interaction.

Example:
package main

import (
	"context"

	"github.com/andersfylling/disgord"
)

func init() {
	bot.AddCommand(&disgord.CreateApplicationCommand{
		Name:        "setup",
		Description: "Configure roles and permissions.",
	}, nil, func(session disgord.Session, interaction *disgord.InteractionCreate) error {
		session.SendInteractionResponse(context.Background(), interaction, &disgord.CreateInteractionResponse{
			Type: disgord.InteractionCallbackChannelMessageWithSource,
			Data: &disgord.CreateInteractionResponseData{
				Content: "This is a response with components",
				Components: []*disgord.MessageComponent{
					{
						Type: disgord.MessageComponentActionRow,
						Components: []*disgord.MessageComponent{
							{
								Label: "I am a button",
								Style: disgord.Primary,
								CustomID: bot.NewComponentHandler(),
							},
						},
					},
				},
			},
		})
		return nil
	})
}

You may be wondering: What does bot.NewComponentHandler do?

This function generates a random number and uses it as a custom ID after linking it to your handler.

If you don't want to define the functions inline, you can create another init function:

package main

import (
	"context"

	"github.com/andersfylling/disgord"
	"github.com/jacks0n9/bot_template"
)

func init() {
	bot.LinkIDsToHandlers(map[string]bot_template.BotComponent{
		"coolbutton": {
			Handler: func(session disgord.Session, interaction *disgord.InteractionCreate) error {
				session.SendInteractionResponse(context.Background(), interaction, &disgord.CreateInteractionResponse{
					Type: disgord.InteractionCallbackChannelMessageWithSource,
					Data: &disgord.CreateInteractionResponseData{
						Content: "You clicked this button!",
					},
				})
				return nil
			}},
	})
}

And then in the customID field, you can just put "coolbutton".

Error handling in commands/components:

As I talked about earlier, error handling is as simple as returning an error and it will be handled for you.

In this package, the default config looks like this:

BotConfig{
			DefaultPermissionErrorMessage: disgord.CreateInteractionResponse{
				Type: disgord.InteractionCallbackChannelMessageWithSource,
				Data: &disgord.CreateInteractionResponseData{
					Flags: disgord.MessageFlagEphemeral,
					Embeds: []*disgord.Embed{
						{
							Title: "You do not have the required permission for this command.",
							Color: 0xff0000,
						},
					},
				},
			},
			DefaultOutsideInteractionErrorMessage: disgord.CreateInteractionResponse{
				Type: disgord.InteractionCallbackChannelMessageWithSource,
				Data: &disgord.CreateInteractionResponseData{
					Flags: disgord.MessageFlagEphemeral,
					Embeds: []*disgord.Embed{
						{
							Title: "This isn't your button!",
							Color: 0xff0000,
						},
					},
				},
			},
			DefaultGeneralErrorMessage: disgord.CreateInteractionResponse{
				Type: disgord.InteractionCallbackChannelMessageWithSource,
				Data: &disgord.CreateInteractionResponseData{
					Flags: disgord.MessageFlagEphemeral,
					Embeds: []*disgord.Embed{
						{
							Title: "An error occured performing this action.",
							Color: 0xff0000,
						},
					},
				},
			},
		}

To use your own config refer to this example:

package main

import (
	"github.com/jacks0n9/bot_template"
)

func init() {
	bot_template.NewBotWithConfig(bot_template.BotConfig{...})
}

That's pretty much it!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bot

type Bot struct {
	Client                  *disgord.Client
	Config                  BotConfig
	Commands                []*disgord.CreateApplicationCommand
	CommandHandlers         map[string]BotCommand
	ActiveComponentHandlers map[string]BotComponent
}

func NewBotWithConfig added in v1.0.0

func NewBotWithConfig(clientConfig BotConfig) Bot

func NewBotWithDefault added in v1.0.0

func NewBotWithDefault() Bot

func (*Bot) AddCommand

func (b *Bot) AddCommand(command *disgord.CreateApplicationCommand, options *BotCommandOptions, handler InteractionHandler)

func (*Bot) LinkIDToHandler added in v1.0.0

func (b *Bot) LinkIDToHandler(ID string, comp BotComponent)

func (*Bot) LinkIDsToHandlers added in v1.0.0

func (b *Bot) LinkIDsToHandlers(compMap map[string]BotComponent)

func (*Bot) NewComponentHandler

func (b *Bot) NewComponentHandler(handler InteractionHandler) string

func (*Bot) NewComponentHandlerFromOptions added in v1.0.0

func (b *Bot) NewComponentHandlerFromOptions(options BotComponentOptions, handler InteractionHandler) string

func (*Bot) Run

func (b *Bot) Run() error

type BotCommand added in v1.0.0

type BotCommand struct {
	Handler InteractionHandler
	Options BotCommandOptions
}

type BotCommandOptions added in v1.0.0

type BotCommandOptions struct {
	// Only people who have this perm or above can use command
	RequiredPermission disgord.PermissionBit
	// Sent when you don't have perms
	PermissionErrorMessage disgord.CreateInteractionResponse
	// When any error occurs when running command
	GeneralErrorMessage disgord.CreateInteractionResponse
}

type BotComponent added in v1.0.0

type BotComponent struct {
	Handler InteractionHandler
	Options BotComponentOptions
}

type BotComponentOptions added in v1.0.0

type BotComponentOptions struct {
	// Applies to any error message when clicking a button
	GeneralErrorMessage disgord.CreateInteractionResponse
	// Only one user can click the button
	UserLockedTo disgord.Snowflake
	// Sent when somebody that isn't userlockedto clicks the button
	OutsideInteractionErrorMessage disgord.CreateInteractionResponse
	// Choose if the button will only respond to one click (won't disable)
	OneClickOnly bool
}

type BotConfig added in v1.0.0

type BotConfig struct {
	DefaultPermissionErrorMessage         disgord.CreateInteractionResponse
	DefaultGeneralErrorMessage            disgord.CreateInteractionResponse
	DefaultOutsideInteractionErrorMessage disgord.CreateInteractionResponse
}

type InteractionHandler added in v1.0.0

type InteractionHandler func(session disgord.Session, interaction *disgord.InteractionCreate) error

Jump to

Keyboard shortcuts

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