msgbuilder

package
v0.0.0-...-4e18fe8 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2021 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package msgbuilder provides builders to create messages, embeds and components. Its main purpose is to abstract away the handling of message components, as well as to ease working with localized messages.

Index

Constants

View Source
const MaxChars = 6000

MaxChars is the maximum amount of characters Discord allows an embed to hold.

Variables

This section is empty.

Functions

func AddResponseMiddlewares

func AddResponseMiddlewares(ctx *plugin.Context, middlewares ...interface{})

AddResponseMiddlewares adds the passed middlewares to the list of the middlewares that are automatically added when awaiting a response from the user.

func BuildEmbeds

func BuildEmbeds(l *i18n.Localizer, builders ...*EmbedBuilder) ([]discord.Embed, error)

BuildEmbeds builds the passed *EmbedBuilders.

func CountEmbedChars

func CountEmbedChars(e discord.Embed) int

CountEmbedChars returns the number of characters in the embed.

func EditEmbedBuilders

func EditEmbedBuilders(
	ctx *plugin.Context, messageID discord.MessageID,
	builders ...*EmbedBuilder,
) (*discord.Message, error)

EditEmbedBuilders builds the discord.Embeds from the passed *EmbedBuilders, and replaces the embeds of the message with the passed id in the invoking channel with them.

func EditEmbedBuildersDM

func EditEmbedBuildersDM(
	ctx *plugin.Context, messageID discord.MessageID,
	builders ...*EmbedBuilder,
) (*discord.Message, error)

EditEmbedBuildersDM replaces the embeds of the message with the passed id in the direct message channel with the invoking user with those built from the passed *EmbedBuilders.

func ReplyEmbedBuilders

func ReplyEmbedBuilders(ctx *plugin.Context, builders ...*EmbedBuilder) (*discord.Message, error)

ReplyEmbedBuilders builds the discord.Embeds from the passed *EmbedBuilders and sends them in the channel the command was sent in.

func ReplyEmbedBuildersDM

func ReplyEmbedBuildersDM(ctx *plugin.Context, builders ...*EmbedBuilder) (*discord.Message, error)

ReplyEmbedBuildersDM builds the discord.Embeds from the passed *EmbedBuilders and sends them in a direct message to the invoking user.

Types

type ActionRowBuilder

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

ActionRowBuilder is a builder used to build a *discord.ActionRowComponent. It must not be used to wrap SelectMenuBuilders, as they automatically wrap themselves in an ActionRow.

func NewActionRow

func NewActionRow(resultVar interface{}) *ActionRowBuilder

NewActionRow creates a new *ActionRowBuilder that stores the value of its components in the passed resultVar. resultVar must be a pointer to a variable.

func (*ActionRowBuilder) Build

Build builds the ActionRowBuilder. Errors returned by Build will be of type *ActionRowError.

func (*ActionRowBuilder) With

With adds the passed ComponentBuilder to the ActionRowBuilder.

type ActionRowComponentBuilder

type ActionRowComponentBuilder interface {
	ComponentBuilder
	// contains filtered or unexported methods
}

ActionRowComponentBuilder is the abstraction of a builder that produces components that can be put into an ActionRowBuilder.

type ActionRowError

type ActionRowError struct {
	Index int
	Type  string

	Cause error
	// contains filtered or unexported fields
}

ActionRowError is the error returned if one of the ComponentBuilders inside an ActionRow failed to build.

func (*ActionRowError) Error

func (e *ActionRowError) Error() string

func (*ActionRowError) StackTrace

func (e *ActionRowError) StackTrace() errors.StackTrace

func (*ActionRowError) Unwrap

func (e *ActionRowError) Unwrap() error

type Builder

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

Builder is a message builder. After creating or editing a message, it must not be used again.

func New

func New(s *state.State, ctx *plugin.Context) *Builder

New creates a new *Builder that awaits a component interaction from the invoking user.

It adds the middlewares stored in the context under the ResponseMiddlewareKey to the builder's response middlewares, to be used when awaiting a response.

func (*Builder) AsTTS

func (b *Builder) AsTTS() *Builder

AsTTS sends the message as a TTS message.

Actions: send

func (*Builder) Await

func (b *Builder) Await(timeout time.Duration, disable bool) error

Await calls AwaitContext with a context with the given timeout.

func (*Builder) AwaitContext

func (b *Builder) AwaitContext(ctx context.Context, disable bool) (err error)

AwaitContext waits until the first awaited component is interacted with, or, if awaiting a response, a response is sent.

Subsequent calls to AwaitContext will await further interactions/responses.

To use await, the message that this builder builds must have been sent.

If disable is set to true, all components will be disabled after the function returns, making subsequent calls impossible. You should do this when calling AwaitContext for the last time, to make clear that no further interactions with the components in the message are possible.

Errors that occur when disabling components will be handled silently and will not be returned, as disabling the components is just cosmetic and has no influence over the successful execution of AwaitContext.

If disable is set to true, and AwaitContext returns with an error, all components will still be disabled.

Interactions that happened before AwaitContext was called, or those that happened between calls will not be evaluated.

func (*Builder) AwaitingUser

func (b *Builder) AwaitingUser(userID discord.UserID) *Builder

AwaitingUser sets the id of the user, that is expected to interact with the components and to respond to the message, to the passed id.

func (*Builder) DisableComponents

func (b *Builder) DisableComponents() error

DisableComponents disables all components, by editing the sent message.

func (*Builder) Edit

func (b *Builder) Edit(channelID discord.ChannelID, messageID discord.MessageID) (*discord.Message, error)

Edit edits the message with the passed channel and message id.

func (*Builder) EditReply

func (b *Builder) EditReply(messageID discord.MessageID) (*discord.Message, error)

EditReply edits the message with the given id using the plugin.Context's plugin.Replier.

func (*Builder) EditReplyDM

func (b *Builder) EditReplyDM(messageID discord.MessageID) (*discord.Message, error)

EditReplyDM edits the direct message with the given id using the plugin.Context's plugin.Replier.

func (*Builder) KeepAttachment

func (b *Builder) KeepAttachment(attachment discord.Attachment) *Builder

KeepAttachment adds the passed discord.Attachment to the list of attachments to keep.

Actions: edit

func (*Builder) MentionRepliedUser

func (b *Builder) MentionRepliedUser() *Builder

MentionRepliedUser mentions the user that is being replied.

Actions: send and edit

func (*Builder) RemoveAttachments

func (b *Builder) RemoveAttachments() *Builder

RemoveAttachments removes all attachments from the message.

Actions: edit

func (*Builder) RemoveComponents

func (b *Builder) RemoveComponents() *Builder

RemoveComponents removes the components from the message.

Actions: edit

func (*Builder) RemoveContent

func (b *Builder) RemoveContent() *Builder

RemoveContent removes the content from the message.

Actions: edit

func (*Builder) RemoveEmbeds

func (b *Builder) RemoveEmbeds() *Builder

RemoveEmbeds removes the embeds from the message.

Actions: edit

func (*Builder) Reply

func (b *Builder) Reply() (*discord.Message, error)

Reply sends the message using the plugin.Context's plugin.Replier.

func (*Builder) ReplyAndAwait

func (b *Builder) ReplyAndAwait(timeout time.Duration) (*discord.Message, error)

ReplyAndAwait sends the message using the plugin.Context's plugin.Replier and then waits for a component interaction. Upon returning, it disables all components.

func (*Builder) ReplyDM

func (b *Builder) ReplyDM() (*discord.Message, error)

ReplyDM sends the message in a DM using the plugin.Context's plugin.Replier.

func (*Builder) Send

func (b *Builder) Send(channelID discord.ChannelID) (msg *discord.Message, err error)

Send creates a new message in the passed channel.

func (*Builder) WithAllowedMentionTypes

func (b *Builder) WithAllowedMentionTypes(allowed ...api.AllowedMentionType) *Builder

WithAllowedMentionTypes adds the passed api.AllowedMentionTypes to the allowed mentions of the message.

Actions: send and edit

func (*Builder) WithAwaitedComponent

func (b *Builder) WithAwaitedComponent(component TopLevelComponentBuilder) *Builder

WithAwaitedComponent adds the passed TopLevelComponentBuilder to the message, and waits for an interaction for that component when Await is called.

Actions: send and edit

func (*Builder) WithAwaitedResponse

func (b *Builder) WithAwaitedResponse(
	responseVar *discord.Message, initialTimeout, typingTimeout time.Duration,
) *Builder

WithAwaitedResponse will cause the message to await a response from the user when Await or AwaitContext is called.

The builder will wait for a response until the initial timeout expires and the user is not typing, or until the user stops typing and the typing timeout is reached.

If typingTimeout is set to 0, typing will not be monitored.

Note that you need the typing intent to monitor typing. If typingTimeout is > 0 and the current shard has no typing intent for guilds or direct messages, wherever the response is being awaited, Await and AwaitContext will return with an error.

If one of the timeouts is reached, a *TimeoutError will be returned.

The typing timeout will start after the user first starts typing. Because Discord sends the typing event in an interval of about 10 seconds, Await and AwaitContext might, in the worst case, only notice that the user ceased typing 10 seconds later. In the best case, it will be noticed almost immediately.

The timeout given to Await, or the cancellation of the context given to AwaitContext serves as a maximum timeout. If it is reached, the await functions will return no matter if the user is still typing. It will also serve as the only timeout, if the typing timeout was disabled.

Besides that, a reply can also be canceled through a middleware.

func (*Builder) WithComponent

func (b *Builder) WithComponent(component TopLevelComponentBuilder) *Builder

WithComponent adds the passed TopLevelComponentBuilder to the message.

Actions: send and edit

func (*Builder) WithContent

func (b *Builder) WithContent(content string) *Builder

WithContent sets the content of the message to the given content. It may be no longer than 2000 characters.

Actions: send and edit

func (*Builder) WithContentl

func (b *Builder) WithContentl(content *i18n.Config) *Builder

WithContentl sets the content of the message to the given content. It may be no longer than 2000 characters.

Actions: send and edit

func (*Builder) WithEmbed

func (b *Builder) WithEmbed(embed *EmbedBuilder) *Builder

WithEmbed adds the passed embed to the message.

Actions: send and edit

func (*Builder) WithFile

func (b *Builder) WithFile(name string, reader io.Reader) *Builder

WithFile adds the passed file to the message.

Actions: send and edit

func (*Builder) WithFlags

func (b *Builder) WithFlags(flags ...discord.MessageFlags) *Builder

WithFlags sets the passed messages flags.

Actions: edit

func (*Builder) WithReference

func (b *Builder) WithReference(messageID discord.MessageID) *Builder

WithReference references the message with the passed id.

Actions: send

func (*Builder) WithResponseMiddlewares

func (b *Builder) WithResponseMiddlewares(middlewares ...interface{}) *Builder

WithResponseMiddlewares adds the passed middlewares to the builder to be executed before every message create event processed.

Any errors returned by the middlewares will be returned by the await function that awaited a response.

All middlewares of invalid type will be discarded.

The following types are permitted:

  • func(*state.State, interface{})
  • func(*state.State, interface{}) error
  • func(*state.State, *event.Base)
  • func(*state.State, *event.Base) error
  • func(*state.State, *state.MessageCreateEvent)
  • func(*state.State, *state.MessageCreateEvent) error

func (*Builder) WithRoleMentions

func (b *Builder) WithRoleMentions(roleIDs ...discord.RoleID) *Builder

WithRoleMentions adds the passed role ids to the allowed mentions.

Actions: send and edit

func (*Builder) WithUserMentions

func (b *Builder) WithUserMentions(userIDs ...discord.UserID) *Builder

WithUserMentions adds the passed user ids to the allowed mentions.

Actions: send and edit

type ButtonBuilder

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

func NewButton

func NewButton(style discord.ButtonStyle, label string, val interface{}) *ButtonBuilder

NewButton creates a new *ButtonBuilder with the given label and the corresponding go value. val must be the element type of the ButtonBuilder's parent ActionRowBuilder.

func NewButtonl

func NewButtonl(style discord.ButtonStyle, label *i18n.Config, val interface{}) *ButtonBuilder

NewButtonl creates a new *ButtonBuilder with the given label and the corresponding go value. val must be the element type of the ButtonBuilder's parent ActionRowBuilder.

func (*ButtonBuilder) Build

func (b *ButtonBuilder) Build(l *i18n.Localizer) (c discord.Component, err error)

func (*ButtonBuilder) Disable

func (b *ButtonBuilder) Disable() *ButtonBuilder

Disable disables the button.

func (*ButtonBuilder) WithEmoji

func (b *ButtonBuilder) WithEmoji(emoji discord.ButtonEmoji) *ButtonBuilder

WithEmoji assigns the passed emoji to the button.

func (*ButtonBuilder) WithURL

func (b *ButtonBuilder) WithURL(url discord.URL) *ButtonBuilder

WithURL attaches the given url to the button. This must be called, and only if, the button is a link-style button.

func (*ButtonBuilder) WithUnicodeEmoji

func (b *ButtonBuilder) WithUnicodeEmoji(emoji string) *ButtonBuilder

WithUnicodeEmoji assigns the passed unicode emoji to the button.

type ComponentBuilder

type ComponentBuilder interface {
	Build(l *i18n.Localizer) (discord.Component, error)
	// contains filtered or unexported methods
}

ComponentBuilder is the abstraction of any component builder.

type EmbedBuilder

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

EmbedBuilder is used to build embeds.

func NewEmbed

func NewEmbed() *EmbedBuilder

NewEmbed creates a new EmbedBuilder.

func (*EmbedBuilder) Build

func (b *EmbedBuilder) Build(l *i18n.Localizer) (e discord.Embed, err error)

Build builds the discord.Embed.

func (*EmbedBuilder) Clone

func (b *EmbedBuilder) Clone() *EmbedBuilder

Clone creates a copy of the EmbedBuilder.

func (*EmbedBuilder) WithAuthor

func (b *EmbedBuilder) WithAuthor(name string) *EmbedBuilder

WithAuthor sets the author's name (max. 256 characters) to the passed name.

func (*EmbedBuilder) WithAuthorIcon

func (b *EmbedBuilder) WithAuthorIcon(icon discord.URL) *EmbedBuilder

WithAuthorIcon sets the icon of the author to the passed icon url.

func (*EmbedBuilder) WithAuthorURL

func (b *EmbedBuilder) WithAuthorURL(url discord.URL) *EmbedBuilder

WithAuthorURL assigns the author the passed discord.URL.

func (*EmbedBuilder) WithAuthorl

func (b *EmbedBuilder) WithAuthorl(name *i18n.Config) *EmbedBuilder

WithAuthorl sets the author's name (max. 256 characters) to the passed name.

func (*EmbedBuilder) WithColor

func (b *EmbedBuilder) WithColor(color discord.Color) *EmbedBuilder

WithColor sets the color to the passed discord.Color.

func (*EmbedBuilder) WithDescription

func (b *EmbedBuilder) WithDescription(description string) *EmbedBuilder

WithDescription sets the description (max. 2048 characters) to the passed description.

func (*EmbedBuilder) WithDescriptionl

func (b *EmbedBuilder) WithDescriptionl(description *i18n.Config) *EmbedBuilder

WithDescriptionl sets the description (max. 2048 characters) to the passed description.

func (*EmbedBuilder) WithField

func (b *EmbedBuilder) WithField(name, value string) *EmbedBuilder

WithField adds a field (name: max. 256 characters, value: max 1024 characters) to the embed.

func (*EmbedBuilder) WithFieldl

func (b *EmbedBuilder) WithFieldl(name, value *i18n.Config) *EmbedBuilder

WithFieldl adds a field (name: max. 256 characters, value: max 1024 characters) to the embed.

func (*EmbedBuilder) WithFooter

func (b *EmbedBuilder) WithFooter(text string) *EmbedBuilder

WithFooter sets the text of the footer (max. 2048 characters) to the passed text.

func (*EmbedBuilder) WithFooterIcon

func (b *EmbedBuilder) WithFooterIcon(icon discord.URL) *EmbedBuilder

WithFooterIcon sets the icon of the footer to the passed icon url.

func (*EmbedBuilder) WithFooterl

func (b *EmbedBuilder) WithFooterl(text *i18n.Config) *EmbedBuilder

WithFooterl sets the text of the footer (max. 2048 characters) to the passed text.

func (*EmbedBuilder) WithImage

func (b *EmbedBuilder) WithImage(image discord.URL) *EmbedBuilder

WithImage sets the image to the passed image url.

func (*EmbedBuilder) WithInlinedField

func (b *EmbedBuilder) WithInlinedField(name, value string) *EmbedBuilder

WithInlinedField adds an inlined field (name: max. 256 characters, value: max 1024 characters) to the embed.

func (*EmbedBuilder) WithInlinedFieldl

func (b *EmbedBuilder) WithInlinedFieldl(name, value *i18n.Config) *EmbedBuilder

WithInlinedFieldl adds an inlined field (name: max. 256 characters, value: max 1024 characters) to the embed.

func (*EmbedBuilder) WithThumbnail

func (b *EmbedBuilder) WithThumbnail(thumbnail discord.URL) *EmbedBuilder

WithThumbnail sets the thumbnail to the passed thumbnail url.

func (*EmbedBuilder) WithTimestamp

func (b *EmbedBuilder) WithTimestamp(timestamp discord.Timestamp) *EmbedBuilder

WithTimestamp sets the timestamp to the passed discord.Timestamp.

func (*EmbedBuilder) WithTimestampNow

func (b *EmbedBuilder) WithTimestampNow() *EmbedBuilder

WithTimestampNow sets the timestamp to a timestamp of the current time.

func (*EmbedBuilder) WithTitle

func (b *EmbedBuilder) WithTitle(title string) *EmbedBuilder

WithTitle sets the title (max. 256 characters) to the passed title.

func (*EmbedBuilder) WithTitleURL

func (b *EmbedBuilder) WithTitleURL(url discord.URL) *EmbedBuilder

WithTitleURL assigns a discord.URL to the title.

func (*EmbedBuilder) WithTitlel

func (b *EmbedBuilder) WithTitlel(title *i18n.Config) *EmbedBuilder

WithTitlel sets the title (max. 256 characters) to the passed title.

type SelectBuilder

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

SelectBuilder is a builder used to build a *discord.SelectComponent.

func NewSelect

func NewSelect(resultVar interface{}) *SelectBuilder

NewSelect creates a new *SelectBuilder that stores the value (state) of its components in the passed resultVar. If using the default bounds (1, 1), or (0, 1), resultVar must be a pointer. Otherwise, resultVar must be a pointer to a slice.

func (*SelectBuilder) Build

func (b *SelectBuilder) Build(l *i18n.Localizer) (c discord.Component, err error)

func (*SelectBuilder) Disable

func (b *SelectBuilder) Disable() *SelectBuilder

Disable disables the select.

func (*SelectBuilder) With

With adds the passed *SelectOptionBuilder to the SelectBuilder.

func (*SelectBuilder) WithBounds

func (b *SelectBuilder) WithBounds(min, max int) *SelectBuilder

WithBounds sets the passed bounds as min and max values.

func (*SelectBuilder) WithDefault

func (b *SelectBuilder) WithDefault(option *SelectOptionBuilder) *SelectBuilder

WithDefault adds the passed *SelectOptionBuilder as the default option to the SelectBuilder.

func (*SelectBuilder) WithPlaceholder

func (b *SelectBuilder) WithPlaceholder(placeholder string) *SelectBuilder

WithPlaceholder adds the passed placeholder to the select.

func (*SelectBuilder) WithPlaceholderl

func (b *SelectBuilder) WithPlaceholderl(placeholder *i18n.Config) *SelectBuilder

WithPlaceholderl adds the passed placeholder to the select.

func (*SelectBuilder) WithPlaceholderlt

func (b *SelectBuilder) WithPlaceholderlt(placeholder i18n.Term) *SelectBuilder

WithPlaceholderlt adds the passed placeholder to the select.

type SelectError

type SelectError struct {
	Index int
	Cause error
	// contains filtered or unexported fields
}

SelectError is the error returned if one of the SelectOptionBuilders inside a Select failed to build.

func (*SelectError) Error

func (e *SelectError) Error() string

func (*SelectError) StackTrace

func (e *SelectError) StackTrace() errors.StackTrace

func (*SelectError) Unwrap

func (e *SelectError) Unwrap() error

type SelectOptionBuilder

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

func NewSelectOption

func NewSelectOption(label string, val interface{}) *SelectOptionBuilder

NewSelectOption creates a new *SelectOptionBuilder with the given label and the corresponding go value.

If the parent SelectBuilder uses the bounds (0, 1) or (1, 1), val must be of the elem type of the SelectBuilder's resultVar. Otherwise, val must be of the element type of the SelectBuilder's slice type.

func NewSelectOptionl

func NewSelectOptionl(label *i18n.Config, val interface{}) *SelectOptionBuilder

NewSelectOptionl creates a new *SelectOptionBuilder with the given label and the corresponding go value.

If the parent SelectBuilder uses the bounds (0, 1) or (1, 1), val must be of the elem type of the SelectBuilder's resultVar. Otherwise, val must be of the element type of the SelectBuilder's slice type.

func (*SelectOptionBuilder) Build

func (b *SelectOptionBuilder) Build(l *i18n.Localizer) (selectOption discord.SelectComponentOption, err error)

func (*SelectOptionBuilder) WithDescription

func (b *SelectOptionBuilder) WithDescription(description string) *SelectOptionBuilder

WithDescription adds the passed description to the SelectOptionBuilder.

func (*SelectOptionBuilder) WithDescriptionl

func (b *SelectOptionBuilder) WithDescriptionl(description *i18n.Config) *SelectOptionBuilder

WithDescriptionl adds the passed description to the SelectOptionBuilder.

func (*SelectOptionBuilder) WithEmoji

WithEmoji assigns the passed emoji to the SelectOptionBuilder.

func (*SelectOptionBuilder) WithUnicodeEmoji

func (b *SelectOptionBuilder) WithUnicodeEmoji(emoji string) *SelectOptionBuilder

WithUnicodeEmoji assigns the passed unicode emoji to the select option.

type TimeoutError

type TimeoutError struct {
	UserID discord.UserID
	Cause  error
}

TimeoutError is an error that fulfills errors.As for *errors.UserInfo. It is returned by Builder's await methods if the context is canceled before a matching component interaction happens.

func (*TimeoutError) As

func (e *TimeoutError) As(target interface{}) bool

func (*TimeoutError) Error

func (e *TimeoutError) Error() string

func (*TimeoutError) Unwrap

func (e *TimeoutError) Unwrap() error

Unwrap returns the cause for the timeout.

type TopLevelComponentBuilder

type TopLevelComponentBuilder interface {
	ComponentBuilder
	// contains filtered or unexported methods
}

TopLevelComponentBuilder is the abstraction of a builder that produces top-level components.

Jump to

Keyboard shortcuts

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