Kombo

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2023 License: MIT Imports: 11 Imported by: 0

README

Kombo

Simple, yet powerful command handler for the DisGo.

Installation

Install this module as any other, using the go get CLI command:

go get github.com/HoneycombsTeam/Kombo
Usage example
package main

import (
	"github.com/HoneycombsTeam/Kombo"
	"github.com/disgoorg/disgo"
	"github.com/disgoorg/disgo/bot"
)

func pingCommandHandler(ctx *Kombo.Context) error {
	return ctx.Reply(
		Kombo.WithContent(":ping_pong: Pong!"),
	)
}

func main() {
	// create your client as normal
	client, err := disgo.New("something", bot.WithDefaultGateway())
	if err != nil {
		panic(err)
	}

	// creating a new instance of command handler
	ch := Kombo.NewCommandHandler(client)

	// registering a new command with name "ping" and description "Pongs you!"
	ch.RegisterCommand(
		Kombo.WithName("ping"),
		Kombo.WithDescription("Pongs you!"),
		Kombo.WithHandler(pingCommandHandler),
	)

	// create earlier registered commands
	if err = ch.InitCommands(); err != nil {
		panic(err)
	}

	// open gateway, do other stuff as normal...
}
Localization

Kombo allows you to automatically load and map localization for your project. Since v0.3.0, CommandHandler struct automatically creates instance of the LocalizationManager, to allow you to load the messages.

Currently supported formats:
  • JSON (see format below);
{
  "group": {
    "key1": "value1",
    "key2": "value2"
  }
}
Registering a new locale:
...
err := LocalizationManager.RegisterLocale("en", "en.json")
if err != nil {
    panic(err)
}
...
Getting localized messages:
// ...
message, err := LocalizationManager.GetMessage("en", "group.key1")
// message will be "value1".
// ...

Also, you can get message via the Context, or use Kombo.WithLocalizedContent. See documentation for these functions for more information.

License

Kombo is licensed under the MIT License, see LICENSE for more information.

Documentation

Overview

Package Kombo is a simple, yet powerful command handler for the [DisGo].

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {

	// Base struct of Discord's slash command to build command on top of.
	*discord.SlashCommandCreate
	// contains filtered or unexported fields
}

Command struct represents a single command that are registered in command handler.

type CommandHandler

type CommandHandler struct {
	LocalizationManager LocalizationManager // Localization manager instance.
	// contains filtered or unexported fields
}

CommandHandler is a general struct that are used to register commands, handle events and more.

func NewCommandHandler

func NewCommandHandler(client bot.Client) *CommandHandler

NewCommandHandler creates a new instance of CommandHandler with the provided client. There should be only a one instance of the CommandHandler per runtime.

func (*CommandHandler) InitCommands

func (ch *CommandHandler) InitCommands() error

InitCommands sends the request to create all registered commands and attaches a new event listener to the client that listens to the slash command executions. It can return an error if we were unable to register a slash command for some reason (e.g. rate-limiting).

func (*CommandHandler) RegisterCommand

func (ch *CommandHandler) RegisterCommand(opts ...CommandOption)

RegisterCommand registers a new command with the provided options and adds it into the local mapping.

type CommandOption

type CommandOption func(*Command)

CommandOption is a type that represents a single option for the command construction.

func WithDescription

func WithDescription(description string) CommandOption

WithDescription sets the description of the current command to the provided string.

func WithDescriptionLocalization added in v0.3.1

func WithDescriptionLocalization(locale discord.Locale, description string) CommandOption

WithDescriptionLocalization adds a new localization for the description. Note that this function doesn't call LocalizationManager internally.

func WithHandler

func WithHandler(handler HandlerFunc) CommandOption

WithHandler sets the handler for the current command.

func WithLocalizedDescription added in v0.3.1

func WithLocalizedDescription(locale discord.Locale, key string) CommandOption

WithLocalizedDescription localizes command description using the LocalizationManager.

func WithLocalizedName added in v0.3.1

func WithLocalizedName(locale discord.Locale, key string) CommandOption

WithLocalizedName localizes command name using the LocalizationManager.

func WithNSFW

func WithNSFW(nsfw bool) CommandOption

WithNSFW sets whether command is NSFW or not. If it is, then it can be only used in NSFW channels.

func WithName

func WithName(name string) CommandOption

WithName sets the name of the current command to the provided string.

func WithNameLocalization added in v0.3.1

func WithNameLocalization(locale discord.Locale, name string) CommandOption

WithNameLocalization adds a new localization for the name. Note that this function doesn't call LocalizationManager internally.

func WithOption

WithOption adds a new option to the current command's options.

func WithOptions

func WithOptions(options []discord.ApplicationCommandOption) CommandOption

WithOptions sets current command's options to the provided slice.

func WithSubCommand

func WithSubCommand(subCommand ...CommandOption) CommandOption

WithSubCommand turns the current command into the subcommand group and allows you to create a subcommands of the current command. You should add a single command per WithSubCommand call. See documentation for other functions that returns CommandOption for more information (e.g. WithName).

type Context

type Context struct {
	// LocalizationManager is an instance of the LocalizationManager.
	LocalizationManager LocalizationManager
	// Event is an event that Discord sent us for this specific command.
	Event *events.ApplicationCommandInteractionCreate
}

Context is a struct that contains information about the event and allows to reply to the received event.

func (*Context) Reply added in v0.2.0

func (ctx *Context) Reply(opts ...ReplyOption) error

Reply creates reply to the context's event with the provided ReplyOption(s).

type HandlerFunc

type HandlerFunc = func(*Context) error

HandlerFunc is a type that represents handler's function type.

type Locale added in v0.3.0

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

Locale is a type that contains all available messages for the specific locale.

func NewLocale added in v0.3.0

func NewLocale() *Locale

NewLocale creates a new instance of the Locale.

func (*Locale) GetMessage added in v0.3.0

func (l *Locale) GetMessage(key string, args ...any) (string, error)

GetMessage returns string by the provided path with formatted arguments if there are any. This method will return error != nil if you're trying to get message by unknown group or key.

func (*Locale) LoadMessages added in v0.3.0

func (l *Locale) LoadMessages(path string) error

LoadMessages loads messages from the provided path. It can return error != nil, if there was an error opening or reading file or while unmarshalling it from JSON string to an object.

type LocalizationManager added in v0.3.0

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

LocalizationManager is a manager for the localization module.

func NewLocalizationManager added in v0.3.0

func NewLocalizationManager() LocalizationManager

NewLocalizationManager creates a new instance of the LocalizationManager.

func (*LocalizationManager) GetLocale added in v0.3.0

func (lm *LocalizationManager) GetLocale(locale string) (*Locale, error)

GetLocale returns the locale by its name. It will return an error != nil if there are no locale by the provided name.

func (*LocalizationManager) GetMessage added in v0.3.0

func (lm *LocalizationManager) GetMessage(locale string, key string, args ...any) (string, error)

GetMessage gets message by the locale and key. See documentation for the GetLocale and GetMessage for more info.

func (*LocalizationManager) RegisterLocale added in v0.3.0

func (lm *LocalizationManager) RegisterLocale(localeName string, path string) error

RegisterLocale registers a new locale by its name and path to load the messages. This method will return error != nil if there was an error while loading messages.

type Reply added in v0.2.0

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

Reply is a struct that contains information about the reply that client should send.

type ReplyOption added in v0.2.0

type ReplyOption func(*Reply)

ReplyOption is a type that represents a single option for the reply construction.

func WithContent added in v0.2.0

func WithContent(content string, vars ...any) ReplyOption

WithContent sets a new content with possible variables formatting for the reply.

func WithEmbed added in v0.2.0

func WithEmbed(embed discord.Embed) ReplyOption

WithEmbed adds a new embed to the reply.

func WithEmbeds added in v0.2.0

func WithEmbeds(embeds ...discord.Embed) ReplyOption

WithEmbeds appends a slice of embeds to the reply.

func WithEphemeral added in v0.2.0

func WithEphemeral(ephemeral bool) ReplyOption

WithEphemeral sets whether message should be ephemeral or not.

func WithFile added in v0.2.0

func WithFile(file *discord.File) ReplyOption

WithFile adds a new file to the reply.

func WithFiles added in v0.2.0

func WithFiles(files ...*discord.File) ReplyOption

WithFiles appends a slice of files to the reply.

func WithFlags added in v0.2.0

func WithFlags(flags discord.MessageFlags) ReplyOption

WithFlags sets flags for the reply.

func WithLocalizedContent added in v0.3.0

func WithLocalizedContent(locale string, key string, args ...any) ReplyOption

WithLocalizedContent sets the content for this reply according to the provided localization. See LocalizationManager.GetMessage method for more information.

func WithSticker added in v0.2.0

func WithSticker(sticker discord.Sticker) ReplyOption

WithSticker adds a new sticker to the reply.

func WithStickers added in v0.2.0

func WithStickers(stickers ...discord.Sticker) ReplyOption

WithStickers appends a slice of stickers to the reply.

type TypeLocaleGroups added in v0.3.0

type TypeLocaleGroups = map[string]TypeMessageGroup

TypeLocaleGroups represents all available locales with their messages.

type TypeMessageGroup added in v0.3.0

type TypeMessageGroup = map[string]string

TypeMessageGroup represents a single group of messages.

Jump to

Keyboard shortcuts

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