harmonia

package module
v0.7.4 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: MIT Imports: 6 Imported by: 0

README

Harmonia

Go Reference Go Report Card

Harmonia is a Go module that is built on top of DiscordGo. It aims to make the developing process of a Go based Discord bot easier by giving developers the tools needed to effectively and quickly design bots.

Anything you can do in DiscordGo is doable in Harmonia.

This module is still in heavy development!

Getting Started with Harmonia

Installing

Assuming you already have a working Go environment, run the following in your project folder.

go get github.com/Moonlington/harmonia

This pulls the latest tagged release from the master branch.

Usage

Import Harmonia into your project.

import "github.com/Moonlington/harmonia"

You can now create a new Harmonia client, which holds most of the features of this project in addition to having the same functionality of a DiscordGo session.

harmonia, err := harmonia.New("authentication token")

Please refer to the documentation and take a look at the examples for more detailed information.

Documentation

Index

Constants

View Source
const VERSION = "0.7.4"

VERSION of Harmonia, follows Semantic Versioning. (http://semver.org/)

Variables

This section is empty.

Functions

func ParseComponentMatrix added in v0.2.0

func ParseComponentMatrix(components [][]discordgo.MessageComponent) []discordgo.MessageComponent

ParseComponentMatrix parses a 2D slice of MessageComponents and returns a 1D slice of MessageComponents with ActionsRows.

func RolesFromMember added in v0.7.0

func RolesFromMember(h *Harmonia, member *discordgo.Member) ([]*discordgo.Role, error)

RolesFromMember returns a slice of *discordgo.Role from a *discordgo.Member.

Types

type Author

type Author struct {
	*discordgo.User
	IsMember     bool
	Guild        *discordgo.Guild
	JoinedAt     time.Time
	Nick         string
	Deaf         bool
	Mute         bool
	Roles        []*discordgo.Role
	PremiumSince *time.Time
}

An Author describes either a User or Member, depending if the message was sent in a Guild or DMs.

func AuthorFromInteraction added in v0.7.0

func AuthorFromInteraction(h *Harmonia, i *discordgo.Interaction) (a *Author, err error)

AuthorFromInteraction uses the information obtained from the Interaction to create an Author.

func AuthorFromMember added in v0.7.0

func AuthorFromMember(h *Harmonia, member *discordgo.Member) (*Author, error)

AuthorFromMember returns an Author from a *discordgo.Member.

func AuthorFromUser added in v0.5.0

func AuthorFromUser(user *discordgo.User) *Author

AuthorFromUser returns an Author from a *discordgo.User.

type CommandFunc added in v0.7.0

type CommandFunc func(h *Harmonia, i *Invocation)

type CommandHandler

type CommandHandler interface {
	// GetName returns the name of the Command.
	GetName() string

	// Do executes the Command.
	Do(h *Harmonia, i *Invocation)
	// contains filtered or unexported methods
}

type GroupSlashCommand added in v0.7.0

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

GroupSlashCommand describes a group of slash commands.

func NewGroupSlashCommand added in v0.7.0

func NewGroupSlashCommand(name string) *GroupSlashCommand

NewGroupSlashCommand returns a GroupSlashCommand with a given name

func (*GroupSlashCommand) Do added in v0.7.0

func (s *GroupSlashCommand) Do(h *Harmonia, i *Invocation)

func (*GroupSlashCommand) GetName added in v0.7.0

func (s *GroupSlashCommand) GetName() string

func (*GroupSlashCommand) WithDMPermission added in v0.7.0

func (s *GroupSlashCommand) WithDMPermission(isAllowed bool) *GroupSlashCommand

WithDMPermission changes the DM Permission of the GroupSlashCommand and returns itself, so that it can be chained.

func (*GroupSlashCommand) WithDefaultPermissions added in v0.7.0

func (s *GroupSlashCommand) WithDefaultPermissions(defaultPermissions int64) *GroupSlashCommand

WithDefaultPermissions changes the DefaultPermissions of the GroupSlashCommand and returns itself, so that it can be chained.

func (*GroupSlashCommand) WithDescription added in v0.7.0

func (s *GroupSlashCommand) WithDescription(description string) *GroupSlashCommand

WithDescription changes the description of the GroupSlashCommand and returns itself, so that it can be chained.

func (*GroupSlashCommand) WithGuildID added in v0.7.0

func (s *GroupSlashCommand) WithGuildID(guildID string) *GroupSlashCommand

WithGuildID changes the guildID of the GroupSlashCommand and returns itself, so that it can be chained.

func (*GroupSlashCommand) WithSubCommands added in v0.7.0

func (s *GroupSlashCommand) WithSubCommands(subcommands ...CommandHandler) *GroupSlashCommand

type Harmonia

type Harmonia struct {
	*discordgo.Session
	Commands          map[string]CommandHandler
	ComponentHandlers map[string]CommandFunc
}

A Harmonia represents a connection to the Discord API and contains the slash commands and component handlers used by Harmonia.

func New

func New(token string) (h *Harmonia, err error)

New creates a new Discord session with the provided token and wraps the Harmonia struct around it.

func (*Harmonia) AddCommand added in v0.7.0

func (h *Harmonia) AddCommand(command CommandHandler) (err error)

AddCommand adds a command to Harmonia.

func (*Harmonia) AddComponentHandler added in v0.2.0

func (h *Harmonia) AddComponentHandler(customID string, handler CommandFunc) error

AddComponentHandler adds a handler for a component. I suggest this is only used for globally used components, and not for components used on a message by message basis. See AddComponentHandlerToInteractionMessage

func (*Harmonia) AddComponentHandlerToInteractionMessage added in v0.3.0

func (h *Harmonia) AddComponentHandlerToInteractionMessage(f *InteractionMessage, customID string, handler CommandFunc) error

AddComponentHandlerToInteractionMessage adds a handler for a component, but will be handled only on its original Interaction. This is done by prepending the InteractionMessage's ID to the customID. Harmonia will do the heavy lifting from there.

func (*Harmonia) DeferResponse added in v0.3.0

func (h *Harmonia) DeferResponse(i *Invocation) error

DeferResponse sends an acknowledgement to the DiscordAPI, allowing you to send a follow-up message later. See Followup for that.

func (*Harmonia) DeleteFollowup

func (h *Harmonia) DeleteFollowup(f *InteractionMessage) error

DeleteFollowup deletes a follow-up message.

func (*Harmonia) DeleteResponse added in v0.2.0

func (h *Harmonia) DeleteResponse(i *Invocation) error

DeleteResponse deletes a response.

func (*Harmonia) EditFollowup

func (h *Harmonia) EditFollowup(f *InteractionMessage, content string) (*InteractionMessage, error)

EditFollowup allows you to edit a follow-up message.

func (*Harmonia) EditFollowupWithComponents added in v0.2.0

func (h *Harmonia) EditFollowupWithComponents(f *InteractionMessage, content string, components [][]discordgo.MessageComponent) (*InteractionMessage, error)

EditFollowupWithComponents does the same as EditFollowup, but also takes in a 2D slice of discordgo.MessageComponents that will be added to the follow-up message.

func (*Harmonia) EditResponse added in v0.2.0

func (h *Harmonia) EditResponse(i *Invocation, content string) (*InteractionMessage, error)

EditResponse edits an already sent response.

func (*Harmonia) EditResponseWithComponents added in v0.2.0

func (h *Harmonia) EditResponseWithComponents(i *Invocation, content string, components [][]discordgo.MessageComponent) (*InteractionMessage, error)

EditResponseWithComponents does the same as EditResponse, but also takes in a 2D slice of discordgo.MessageComponents that will be added to the response.

func (*Harmonia) EphemeralFollowup added in v0.2.0

func (h *Harmonia) EphemeralFollowup(i *Invocation, content string) (*InteractionMessage, error)

EphemeralFollowup does the same as Followup, but sets the flag such that only the invoker can see the message.

func (*Harmonia) EphemeralRespond added in v0.2.0

func (h *Harmonia) EphemeralRespond(i *Invocation, content string) (*InteractionMessage, error)

EphemeralRespond does the same as Respond, but sets the flag such that only the invoker can see the message.

func (*Harmonia) Followup

func (h *Harmonia) Followup(i *Invocation, content string) (*InteractionMessage, error)

Followup sends a follow-up message to the Interaction, this does require you to have used DeferResponse before.

func (*Harmonia) FollowupComplex added in v0.2.0

func (h *Harmonia) FollowupComplex(i *Invocation, params *discordgo.WebhookParams) (*InteractionMessage, error)

FollowupComplex allows you full freedom to follow-up with whatever you'd like.

func (*Harmonia) FollowupWithComponents added in v0.2.0

func (h *Harmonia) FollowupWithComponents(i *Invocation, content string, components [][]discordgo.MessageComponent) (*InteractionMessage, error)

FollowupWithComponents does the same as Followup, but also takes in a 2D slice of discordgo.MessageComponents that will be added to the response.

func (*Harmonia) RemoveAllCommands

func (h *Harmonia) RemoveAllCommands() error

RemoveAllCommands does removes all registered commands from the Discord API.

func (*Harmonia) RemoveCommand

func (h *Harmonia) RemoveCommand(name string) error

RemoveCommand removes a slash command from Harmonia and from the Discord API.

func (*Harmonia) RemoveComponentHandler added in v0.2.0

func (h *Harmonia) RemoveComponentHandler(customID string) error

RemoveComponentHandler removes a component handler.

func (*Harmonia) RemoveComponentHandlerFromInteractionMessage added in v0.3.0

func (h *Harmonia) RemoveComponentHandlerFromInteractionMessage(f *InteractionMessage, customID string) error

RemoveComponentHandlerFromInteractionMessage removes a component handler from an InteractionMessage.

func (*Harmonia) Respond

func (h *Harmonia) Respond(i *Invocation, content string) (*InteractionMessage, error)

Respond allows Harmonia to easily respond to an Invocation with a string.

func (*Harmonia) RespondComplex added in v0.2.0

func (h *Harmonia) RespondComplex(i *Invocation, resp *discordgo.InteractionResponse) (*InteractionMessage, error)

RespondComplex allows you full freedom to respond with whatever you'd like.

func (*Harmonia) RespondWithComponents added in v0.2.0

func (h *Harmonia) RespondWithComponents(i *Invocation, content string, components [][]discordgo.MessageComponent) (*InteractionMessage, error)

RespondWithComponents does the same as Respond, but also takes in a 2D slice of discordgo.MessageComponents that will be added to the response.

func (*Harmonia) Run

func (h *Harmonia) Run() error

Run starts the Harmonia bot up and does the handling for slash commands and components for you.

type InteractionMessage added in v0.3.0

type InteractionMessage struct {
	*discordgo.Message
	Interaction *discordgo.Interaction
	Channel     *discordgo.Channel
	Guild       *discordgo.Guild
}

InteractionMessage describes a message sent as a follow-up or response to an Interaction.

type Invocation

type Invocation struct {
	*discordgo.Interaction
	Guild   *discordgo.Guild
	Channel *discordgo.Channel
	Author  *Author

	// Only when the incoming Interaction is from a SelectMenu component.
	Values []string
	// contains filtered or unexported fields
}

An Invocation describes an incoming Interaction.

func (*Invocation) GetOption

GetOption returns a specific option from an Invocation.

func (*Invocation) GetOptionMap

GetOptionMap returns a map of options passed through the Invocation.

func (*Invocation) TargetAuthor added in v0.7.0

func (i *Invocation) TargetAuthor(h *Harmonia) (*Author, error)

TargetAuthor takes the targetID from the invocation and returns an Author struct from it.

func (*Invocation) TargetMessage added in v0.7.0

func (i *Invocation) TargetMessage(h *Harmonia) (*discordgo.Message, error)

TargetMessage takes the targetID from the invocation and returns an Message struct from it.

type MessageCommand added in v0.7.0

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

A MessageCommand describes a user command or USER application command.

func NewMessageCommand added in v0.7.0

func NewMessageCommand(name string) *MessageCommand

NewMessageCommand returns a MessageCommand with a given name

func (*MessageCommand) Do added in v0.7.0

func (s *MessageCommand) Do(h *Harmonia, i *Invocation)

func (*MessageCommand) GetName added in v0.7.0

func (s *MessageCommand) GetName() string

func (*MessageCommand) WithCommand added in v0.7.0

func (s *MessageCommand) WithCommand(commandFunc CommandFunc) *MessageCommand

WithCommand changes the CommandFunc that is called when the MessageCommand is executed and returns itself, so that it can be chained.

func (*MessageCommand) WithDMPermission added in v0.7.0

func (s *MessageCommand) WithDMPermission(isAllowed bool) *MessageCommand

WithDMPermission changes the DM Permission of the MessageCommand and returns itself, so that it can be chained.

func (*MessageCommand) WithDefaultPermissions added in v0.7.0

func (s *MessageCommand) WithDefaultPermissions(defaultPermissions int64) *MessageCommand

WithDefaultPermissions changes the DefaultPermissions of the MessageCommand and returns itself, so that it can be chained.

func (*MessageCommand) WithGuildID added in v0.7.0

func (s *MessageCommand) WithGuildID(guildID string) *MessageCommand

WithGuildID changes the guildID of the MessageCommand and returns itself, so that it can be chained.

type Option

type Option struct {
	*discordgo.ApplicationCommandOption
}

An Option is a wrapper around an ApplicationCommandOption with added functionality.

func NewOption added in v0.7.0

NewOption returns an option with given name and type.

func (*Option) AddChoice

func (o *Option) AddChoice(name string, value interface{}) *Option

AddChoice adds a choice to an option, value should be the same as the choice's type and returns itself, so that it can be chained.

func (*Option) IsRequired added in v0.7.0

func (o *Option) IsRequired() *Option

IsRequired sets the requirement of the Option to true and returns itself, so that it can be chained. By default an Option is not required.

func (*Option) WithDescription added in v0.7.0

func (o *Option) WithDescription(description string) *Option

WithDescription changes the description of the Option and returns itself, so that it can be chained.

type SlashCommand

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

A SlashCommand describes a slash command or CHAT_INPUT application command.

func NewSlashCommand added in v0.7.0

func NewSlashCommand(name string) *SlashCommand

NewSlashCommand returns a SlashCommand with a given name

func (*SlashCommand) Do added in v0.7.0

func (s *SlashCommand) Do(h *Harmonia, i *Invocation)

func (*SlashCommand) GetName added in v0.7.0

func (s *SlashCommand) GetName() string

func (*SlashCommand) WithCommand added in v0.7.0

func (s *SlashCommand) WithCommand(commandFunc CommandFunc) *SlashCommand

WithCommand changes the CommandFunc that is called when the SlashCommand is executed and returns itself, so that it can be chained.

func (*SlashCommand) WithDMPermission added in v0.7.0

func (s *SlashCommand) WithDMPermission(isAllowed bool) *SlashCommand

WithDMPermission changes the DM Permission of the SlashCommand and returns itself, so that it can be chained.

func (*SlashCommand) WithDefaultPermissions added in v0.7.0

func (s *SlashCommand) WithDefaultPermissions(defaultPermissions int64) *SlashCommand

WithDefaultPermissions changes the DefaultPermissions of the SlashCommand and returns itself, so that it can be chained.

func (*SlashCommand) WithDescription added in v0.7.0

func (s *SlashCommand) WithDescription(description string) *SlashCommand

WithDescription changes the description of the SlashCommand and returns itself, so that it can be chained.

func (*SlashCommand) WithGuildID added in v0.7.0

func (s *SlashCommand) WithGuildID(guildID string) *SlashCommand

WithGuildID changes the guildID of the SlashCommand and returns itself, so that it can be chained.

func (*SlashCommand) WithOptions added in v0.7.0

func (s *SlashCommand) WithOptions(options ...*Option) *SlashCommand

WithOptions changes the options in the SlashCommand and returns itself, so that it can be chained.

type UserCommand added in v0.7.0

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

A UserCommand describes a user command or USER application command.

func NewUserCommand added in v0.7.0

func NewUserCommand(name string) *UserCommand

NewUserCommand returns a UserCommand with a given name

func (*UserCommand) Do added in v0.7.0

func (s *UserCommand) Do(h *Harmonia, i *Invocation)

func (*UserCommand) GetName added in v0.7.0

func (s *UserCommand) GetName() string

func (*UserCommand) WithCommand added in v0.7.0

func (s *UserCommand) WithCommand(commandFunc CommandFunc) *UserCommand

WithCommand changes the CommandFunc that is called when the UserCommand is executed and returns itself, so that it can be chained.

func (*UserCommand) WithDMPermission added in v0.7.0

func (s *UserCommand) WithDMPermission(isAllowed bool) *UserCommand

WithDMPermission changes the DM Permission of the UserCommand and returns itself, so that it can be chained.

func (*UserCommand) WithDefaultPermissions added in v0.7.0

func (s *UserCommand) WithDefaultPermissions(defaultPermissions int64) *UserCommand

WithDefaultPermissions changes the DefaultPermissions of the UserCommand and returns itself, so that it can be chained.

func (*UserCommand) WithGuildID added in v0.7.0

func (s *UserCommand) WithGuildID(guildID string) *UserCommand

WithGuildID changes the guildID of the UserCommand and returns itself, so that it can be chained.

Jump to

Keyboard shortcuts

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