disgobed

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2020 License: MIT Imports: 4 Imported by: 0

README

Disgobed — A Disgord Embed Wrapper

Go Report Card

About

This module wraps the Disgord embed structure with a set of helper functions. This allows for the easy construction and sending of Disgord Embeds in a more idiomatic way.

This library has been written to comply with the specification at the API docs. & by that I mean: I spent way too long looking at how exactly Discord likes their embeds, then worked out a set of ways to validate that!

How to use

If using go modules:

require github.com/Nightmarlin/disgobed

Else

go get github.com/Nightmarlin/disgobed

Then whenever you want to send an embedded message:

package mypackage

import (
    `github.com/Nightmarlin/disgobed`
    `github.com/andersfylling/disgord`
)

[...]
  res, errs := disgobed.NewEmbed(). // Generate new Embed
    SetType(embeds.RichEmbedType).
    SetTitle(`Test Embed`).
    SetDescription(`A very interesting text embed`).
    SetThumbnail(disgobed.NewThumbnail().
      SetURL(`https://upload.wikimedia.org/wikipedia/commons/thumb/5/5a/DOM-model.svg/1024px-DOM-model.svg.png`).
      SetHW(917, 886)).
    Finalize()

  if errs == nil {
    client.CreateMessage(context.Background(), channelID, &disgord.CreateMessageParams{
      Embed: &res
    }
  }
[...]

Interesting Information

Finalize() is a really important function! The Embed struct caches all errors that may occur, due to validation failures or other reasons - this means that you have to actively check for errors. Finalize() makes this easy by returning the cached errors or nil as well as the final embed. You might find you still want to send an embed that is invalid…

Documentation

Overview

Package disgobed wraps the discordgo embed with helper functions to facilitate easier construction. Note that all methods in this module act ByReference, directly changing the embed they are called on, instead of creating and returning a new embed

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthorBuilder

type AuthorBuilder struct {
	*disgord.EmbedAuthor
	Errors *[]error
}

AuthorBuilder wraps the discordgo.MessageEmbedAuthor type and adds features

func NewAuthor

func NewAuthor() *AuthorBuilder

NewAuthor creates and returns a blank author struct

func (*AuthorBuilder) Finalize

func (a *AuthorBuilder) Finalize() (*disgord.EmbedAuthor, *[]error)

Finalize strips away the extra functions and returns the wrapped type. It should always be called before an author is sent. Finalize will also purge the error cache!

func (*AuthorBuilder) SetIconURL

func (a *AuthorBuilder) SetIconURL(iconUrl string) *AuthorBuilder

SetIconURL takes an image address string prefixed with https:// / http:// / attachment:// and adds it to the AuthorBuilder (if the string does not start with one of these, no URL will be added). It then returns the pointer to the AuthorBuilder structure (This function fails silently)

func (*AuthorBuilder) SetName

func (a *AuthorBuilder) SetName(name string) *AuthorBuilder

SetName takes a string and sets the AuthorBuilder's name to that value. It then returns the pointer to the AuthorBuilder. The discord API limits AuthorBuilder names to 256 characters, so this function will do nothing if len(name) > 256 (This function fails silently)

func (*AuthorBuilder) SetProxyIconURL

func (a *AuthorBuilder) SetProxyIconURL(proxyIconUrl string) *AuthorBuilder

SetProxyIconURL takes an image address string prefixed with https:// / http:// / attachment:// and adds it to the AuthorBuilder (if the string does not start with one of these, no URL will be added). It then returns the pointer to the AuthorBuilder structure (This function fails silently)

func (*AuthorBuilder) SetURL

func (a *AuthorBuilder) SetURL(url string) *AuthorBuilder

SetURL sets the author field link to the value given, then returns the pointer to the AuthorBuilder

type EmbedBuilder

type EmbedBuilder struct {
	*disgord.Embed
	Errors *[]error
}

EmbedBuilder wraps the disgord.EmbedBuilder type and adds features. Never create it directly, instead use the NewEmbed function

embed := NewEmbed()

and call the methods to set the properties, allowing for chains that look like this

embed := NewEmbed()
	.SetTitle(`example`)
	.SetDescription(`test`)
	.SetURL(`example.com`)
	.Finalize()

for healthy embedment!

func NewEmbed

func NewEmbed() *EmbedBuilder

NewEmbed creates and returns an empty embed

func (*EmbedBuilder) AddField

func (e *EmbedBuilder) AddField(field *FieldBuilder) *EmbedBuilder

AddField takes a FieldBuilder structure and adds it to the embed, then returns the pointer to the embed. Note that the FieldBuilder structure is `Finalize`d once added and should not be changed after being added. The discord API limits embeds to having 25 Fields, so this function will not add any fields if the limit has already been reached. All errors are propagated to the main embed (This function fails silently)

func (*EmbedBuilder) AddFields

func (e *EmbedBuilder) AddFields(fields ...*FieldBuilder) *EmbedBuilder

AddFields takes N FieldBuilder structures and adds them to the embed, then returns the pointer to the embed. Note that FieldBuilder structures are `Finalize`d once added and should not be changed after being added. The discord API limits embeds to having 25 Fields, so this function will add the first items from the list until that limit is reached (This function fails silently)

func (*EmbedBuilder) AddRawField

func (e *EmbedBuilder) AddRawField(field *disgord.EmbedField) *EmbedBuilder

AddRawField takes a disgord.EmbedField structure and adds it to the embed, then returns the pointer to the embed. The discord API limits embeds to having 25 Fields, so this function will not add any fields if the limit has already been reached (This function fails silently)

func (*EmbedBuilder) AddRawFields

func (e *EmbedBuilder) AddRawFields(fields ...*disgord.EmbedField) *EmbedBuilder

AddRawFields takes N disgord.EmbedField structures and adds them to the embed, then returns the pointer to the embed. The discord API limits embeds to having 25 Fields, so this function will add the first items from the list until that limit is reached (This function fails silently)

func (*EmbedBuilder) Finalize

func (e *EmbedBuilder) Finalize() (*disgord.Embed, *[]error)

Finalize strips away the extra functions and returns the wrapped type. It should always be called before an embed is sent. Finalize will also purge the error cache!

func (*EmbedBuilder) Generate

func (e *EmbedBuilder) Generate() *disgord.Embed

Generate strips aways the extra functions and returns the wrapped type without the cached validation errors. Allows for immediate addition to a message. This method does not purge the error cache

func (*EmbedBuilder) InlineAllFields

func (e *EmbedBuilder) InlineAllFields() *EmbedBuilder

InlineAllFields sets the Inline property on all currently attached fields to true and returns the pointer to the embed

func (*EmbedBuilder) OutlineAllFields

func (e *EmbedBuilder) OutlineAllFields() *EmbedBuilder

OutlineAllFields sets the Inline property on all currently attached fields to false and returns the pointer to the embed

func (*EmbedBuilder) SetAuthor

func (e *EmbedBuilder) SetAuthor(author *AuthorBuilder) *EmbedBuilder

SetAuthor takes an AuthorBuilder structure and sets the embed's author field to it, then returns the pointer to the embed. Note that the AuthorBuilder structure is `Finalize`d once added and should not be changed after being added. All errors are propagated to the main embed

func (*EmbedBuilder) SetColor

func (e *EmbedBuilder) SetColor(color int) *EmbedBuilder

SetColor edits the embed's highlight colour and returns the pointer to the embed. Color values must be between 0 and 16777215 otherwise the change will not be registered (This function fails silently)

func (*EmbedBuilder) SetCurrentTimestamp

func (e *EmbedBuilder) SetCurrentTimestamp() *EmbedBuilder

SetCurrentTimestamp sets the embed's timestamp to the current UTC time in the appropriate discord format and returns the pointer to the embed

func (*EmbedBuilder) SetCustomTimestamp

func (e *EmbedBuilder) SetCustomTimestamp(t time.Time) *EmbedBuilder

SetCustomTimestamp sets the embed's timestamp to that specified by the time.Time structure passed to it. The value stored is the corresponding UTC time in the appropriate discord format. SetCustomTimestamp returns the pointer to the embed

func (*EmbedBuilder) SetDescription

func (e *EmbedBuilder) SetDescription(desc string) *EmbedBuilder

SetDescription edits the embed's description and returns the pointer to the embed. The discord API limits embed descriptions to 2048 characters, so this function will do nothing if len(desc) > 2048 (This function fails silently)

func (*EmbedBuilder) SetFooter

func (e *EmbedBuilder) SetFooter(footer *FooterBuilder) *EmbedBuilder

SetFooter sets the embed's footer property to the FooterBuilder passed to it, then returns the pointer to the embed. Note that the FooterBuilder structure is `Finalize`d once added and should not be changed after being added. FooterBuilder errors will be propagated into the embed struct

func (*EmbedBuilder) SetImage

func (e *EmbedBuilder) SetImage(img *ImageBuilder) *EmbedBuilder

SetImage sets the embed's image property to the ImageBuilder passed to it, then returns the pointer to the embed. Note that the ImageBuilder structure is `Finalize`d once added and should not be changed after being added. ImageBuilder errors will be propagated into the embed struct

func (*EmbedBuilder) SetProvider

func (e *EmbedBuilder) SetProvider(provider *ProviderBuilder) *EmbedBuilder

SetProvider allows you to set the provider of an embed. It will then return the pointer to the embed. See the providerBuilder.go docs for some extra information

func (*EmbedBuilder) SetRawAuthor

func (e *EmbedBuilder) SetRawAuthor(author *disgord.EmbedAuthor) *EmbedBuilder

SetRawAuthor takes a disgord.EmbedAuthor and sets the embed's author field to it, then returns the pointer to the embed

func (*EmbedBuilder) SetRawFooter

func (e *EmbedBuilder) SetRawFooter(footer *disgord.EmbedFooter) *EmbedBuilder

SetRawFooter takes a disgord.EmbedThumbnail and sets the embed's thumbnail field to it, then returns the pointer to the embed

func (*EmbedBuilder) SetRawImage

func (e *EmbedBuilder) SetRawImage(img *disgord.EmbedImage) *EmbedBuilder

SetRawImage takes a disgord.EmbedImage and sets the embed's image field to it, then returns the pointer to the embed

func (*EmbedBuilder) SetRawProvider

func (e *EmbedBuilder) SetRawProvider(provider *disgord.EmbedProvider) *EmbedBuilder

SetRawProvider allows you to set the disgord.EmbedProvider of an embed. It will then return the pointer to the embed. See the providerBuilder.go docs for some extra information

func (*EmbedBuilder) SetRawThumbnail

func (e *EmbedBuilder) SetRawThumbnail(thumb *disgord.EmbedThumbnail) *EmbedBuilder

SetRawThumbnail takes a disgord.EmbedThumbnail and sets the embed's thumbnail field to it, then returns the pointer to the embed

func (*EmbedBuilder) SetRawVideo

func (e *EmbedBuilder) SetRawVideo(vid *disgord.EmbedVideo) *EmbedBuilder

SetRawVideo takes a disgord.EmbedVideo and sets the embed's thumbnail field to it, then returns the pointer to the embed

func (*EmbedBuilder) SetThumbnail

func (e *EmbedBuilder) SetThumbnail(thumb *ThumbnailBuilder) *EmbedBuilder

SetThumbnail takes a ThumbnailBuilder structure and sets the embed's thumbnail field to it, then returns the pointer to the embed. Note that the ThumbnailBuilder structure is `Finalize`d once added and should not be changed after being added

func (*EmbedBuilder) SetTitle

func (e *EmbedBuilder) SetTitle(title string) *EmbedBuilder

SetTitle edits the embed's title and returns the pointer to the embed. The discord API limits embed titles to 256 characters, so this function will do nothing if len(title) > 256 (This function fails silently)

func (*EmbedBuilder) SetType

func (e *EmbedBuilder) SetType(embedType string) *EmbedBuilder

SetType checks if the embed type passed to it is valid. If it is, it sets the embed's type to that, otherwise it does nothing. It then returns the pointer to the embed (This function fails silently)

func (*EmbedBuilder) SetURL

func (e *EmbedBuilder) SetURL(url string) *EmbedBuilder

SetURL edits the embed's main URL and returns the pointer to the embed

func (*EmbedBuilder) SetVideo

func (e *EmbedBuilder) SetVideo(vid *VideoBuilder) *EmbedBuilder

SetVideo sets the embed's video property to the VideoBuilder passed to it, then returns the pointer to the embed. Note that the VideoBuilder structure is `Finalize`d once added and should not be changed after being added

func (*EmbedBuilder) Validate

func (e *EmbedBuilder) Validate(msg *disgord.Message) *[]error

Validate returns whether or not discord is likely accept the embed attached to it. If discord is unlikely to accept the embed, it returns a list of reasons why. If msg is not nil, the checker will also validate `attachment://` urls message

type FieldBuilder

type FieldBuilder struct {
	*disgord.EmbedField
	Errors *[]error
}

FieldBuilder wraps the disgord.EmbedField type and adds features

func NewField

func NewField() *FieldBuilder

NewField creates and returns a new empty field object

func (*FieldBuilder) Finalize

func (f *FieldBuilder) Finalize() (*disgord.EmbedField, *[]error)

Finalize strips away the extra functions and returns the wrapped type. It should always be called before a field is added. Finalize will also purge the error cache!

func (*FieldBuilder) SetInline

func (f *FieldBuilder) SetInline(isInline bool) *FieldBuilder

SetInline sets whether the field is inline or not then returns the pointer to the FieldBuilder

func (*FieldBuilder) SetName

func (f *FieldBuilder) SetName(name string) *FieldBuilder

SetName sets the name of the field then returns the pointer to the FieldBuilder. The discord API limits FieldBuilder names to 256 characters, so this function will do nothing if len(name) > 256. FieldBuilder names must also not be empty, so this function will do nothing if name == “ (This function fails silently)

func (*FieldBuilder) SetValue

func (f *FieldBuilder) SetValue(val string) *FieldBuilder

SetValue sets the value of the field then returns the pointer to the FieldBuilder. The discord API limits FieldBuilder values to 1024 characters, so this function will do nothing if len(name) > 1024. FieldBuilder values must not be empty, so this function will do nothing if val == “ (This function fails silently)

type FooterBuilder

type FooterBuilder struct {
	*disgord.EmbedFooter
	Errors *[]error
}

FooterBuilder wraps the disgord.EmbedFooter type and adds features

func NewFooter

func NewFooter() *FooterBuilder

NewFooter creates and returns a new empty footer

func (*FooterBuilder) Finalize

func (f *FooterBuilder) Finalize() (*disgord.EmbedFooter, *[]error)

Finalize strips away the extra functions and returns the wrapped type. It should always be called before a footer is attached. Finalize will also purge the error cache!

func (*FooterBuilder) SetIconURL

func (f *FooterBuilder) SetIconURL(iconUrl string) *FooterBuilder

SetIconURL takes an image address string prefixed with https:// / http:// / attachment:// and adds it to the FooterBuilder (if the string does not start with one of these, no URL will be added). It then returns the pointer to the FooterBuilder structure (This function fails silently)

func (*FooterBuilder) SetProxyIconURL

func (f *FooterBuilder) SetProxyIconURL(proxyIconUrl string) *FooterBuilder

SetProxyIconURL takes an image address string prefixed with https:// / http:// / attachment:// and adds it to the FooterBuilder (if the string does not start with one of these, no URL will be added). It then returns the pointer to the FooterBuilder structure (This function fails silently)

func (*FooterBuilder) SetText

func (f *FooterBuilder) SetText(val string) *FooterBuilder

SetText takes a string and sets the FooterBuilder's text to that value. It then returns the pointer to the FooterBuilder. The discord API limits FooterBuilder values to 2048 characters, so this function will do nothing if len(val) > 2048 (This function fails silently)

type ImageBuilder

type ImageBuilder struct {
	*disgord.EmbedImage
	Errors *[]error
}

ImageBuilder wraps the disgord.EmbedImage type and adds features

func NewImage

func NewImage() *ImageBuilder

NewImage creates and returns an empty image structure

func (*ImageBuilder) Finalize

func (i *ImageBuilder) Finalize() (*disgord.EmbedImage, *[]error)

Finalize strips away the extra functions and returns the wrapped type. It should always be called before an image is sent. Finalize will also purge the error cache!

func (*ImageBuilder) SetHW

func (i *ImageBuilder) SetHW(h int, w int) *ImageBuilder

SetHW sets the image embed height and width to the values given then returns a pointer to the ImageBuilder structure. If either h <= 0 or w <= 0, this operation does nothing (This function fails silently)

func (*ImageBuilder) SetHeight

func (i *ImageBuilder) SetHeight(h int) *ImageBuilder

SetHeight sets the image embed height to the value given then returns a pointer to the ImageBuilder structure. If h <= 0, this operation does nothing (This function fails silently)

func (*ImageBuilder) SetProxyURL

func (i *ImageBuilder) SetProxyURL(proxyUrl string) *ImageBuilder

SetProxyURL takes an image address string prefixed with https:// / http:// / attachment:// and adds it to the ImageBuilder (if the string does not start with one of these, no URL will be added). It then returns the pointer to the ImageBuilder structure (This function fails silently)

func (*ImageBuilder) SetURL

func (i *ImageBuilder) SetURL(url string) *ImageBuilder

SetURL takes an image address string prefixed with https:// / http:// / attachment:// and adds it to the ImageBuilder (if the string does not start with one of these, no URL will be added). It then returns the pointer to the ImageBuilder structure (This function fails silently)

func (*ImageBuilder) SetWidth

func (i *ImageBuilder) SetWidth(w int) *ImageBuilder

SetWidth sets the image embed width to the value given then returns a pointer to the ImageBuilder structure. If w <= 0, this operation does nothing (This function fails silently)

type ProviderBuilder

type ProviderBuilder struct {
	*disgord.EmbedProvider
	Errors *[]error
}

ProviderBuilder wraps the disgord.EmbedProvider type and adds features. ProviderBuilder is an esoteric part of the discord API, and is likely to be deprecated in a future version. It is recommended you don't use it... Use at your own risk. No ProviderBuilder fields are validated

func NewProvider

func NewProvider() *ProviderBuilder

NewProvider creates and returns a pointer to an empty provider struct

func (*ProviderBuilder) Finalize

func (p *ProviderBuilder) Finalize() (*disgord.EmbedProvider, *[]error)

Finalize strips away the extra functions and returns the wrapped type. ProviderBuilder does not perform validation on inputs, so Finalize() should always return nil for its errors

func (*ProviderBuilder) SetName

func (p *ProviderBuilder) SetName(name string) *ProviderBuilder

SetName sets the provider's Name field and returns the pointer to the ProviderBuilder

func (*ProviderBuilder) SetURL

func (p *ProviderBuilder) SetURL(url string) *ProviderBuilder

SetURL sets the provider's URL field and returns the pointer to the ProviderBuilder

type ThumbnailBuilder

type ThumbnailBuilder struct {
	*disgord.EmbedThumbnail
	Errors *[]error
}

ThumbnailBuilder wraps the disgord.EmbedThumbnail type and adds features

func NewThumbnail

func NewThumbnail() *ThumbnailBuilder

NewThumbnail creates and returns a pointer to a new empty thumbnail

func (*ThumbnailBuilder) Finalize

func (t *ThumbnailBuilder) Finalize() (*disgord.EmbedThumbnail, *[]error)

Finalize strips away the extra functions and returns the wrapped type. It should always be called before an thumbnail is attached. Finalize will also purge the error cache!

func (*ThumbnailBuilder) SetHW

func (t *ThumbnailBuilder) SetHW(h int, w int) *ThumbnailBuilder

SetHW sets the ThumbnailBuilder embed height and width to the values given then returns a pointer to the ThumbnailBuilder structure. If either h <= 0 or w <= 0, this operation does nothing (This function fails silently)

func (*ThumbnailBuilder) SetHeight

func (t *ThumbnailBuilder) SetHeight(h int) *ThumbnailBuilder

SetHeight sets the ThumbnailBuilder embed height to the value given then returns a pointer to the ThumbnailBuilder structure. If h <= 0, this operation does nothing (This function fails silently)

func (*ThumbnailBuilder) SetProxyURL

func (t *ThumbnailBuilder) SetProxyURL(proxyUrl string) *ThumbnailBuilder

SetProxyURL takes an image address string prefixed with https:// / http:// / attachment:// and adds it to the ThumbnailBuilder (if the string does not start with one of these, no URL will be added). It then returns the pointer to the ThumbnailBuilder structure (This function fails silently)

func (*ThumbnailBuilder) SetURL

func (t *ThumbnailBuilder) SetURL(url string) *ThumbnailBuilder

SetURL takes an image address string prefixed with https:// / http:// / attachment:// and adds it to the ThumbnailBuilder (if the string does not start with one of these, no URL will be added). It then returns the pointer to the ThumbnailBuilder structure (This function fails silently)

func (*ThumbnailBuilder) SetWidth

func (t *ThumbnailBuilder) SetWidth(w int) *ThumbnailBuilder

SetWidth sets the ThumbnailBuilder embed width to the value given then returns a pointer to the ThumbnailBuilder structure. If w <= 0, this operation does nothing (This function fails silently)

type VideoBuilder

type VideoBuilder struct {
	*disgord.EmbedVideo
	Errors *[]error
}

VideoBuilder wraps the disgord.EmbedVideo type and adds features. This wrapper ignores MessageEmbedVideo.ProxyURL as the API would ignore that field if present

func (*VideoBuilder) Finalize

func (v *VideoBuilder) Finalize() (*disgord.EmbedVideo, *[]error)

Finalize strips away the extra functions and returns the wrapped type. It should always be called before an thumbnail is attached. Finalize will also purge the error cache!

func (*VideoBuilder) SetHW

func (v *VideoBuilder) SetHW(h int, w int) *VideoBuilder

SetHW sets the video embed height and width to the values given then returns a pointer to the VideoBuilder structure. If either h <= 0 or w <= 0, this operation does nothing (This function fails silently)

func (*VideoBuilder) SetHeight

func (v *VideoBuilder) SetHeight(h int) *VideoBuilder

SetHeight sets the video embed height to the value given then returns a pointer to the VideoBuilder structure. If h <= 0, this operation does nothing (This function fails silently)

func (*VideoBuilder) SetURL

func (v *VideoBuilder) SetURL(url string) *VideoBuilder

SetURL sets the video source url to the value given to it then returns a pointer to the VideoBuilder structure

func (*VideoBuilder) SetWidth

func (v *VideoBuilder) SetWidth(w int) *VideoBuilder

SetWidth sets the video embed width to the value given then returns a pointer to the VideoBuilder structure. If w <= 0, this operation does nothing (This function fails silently)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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