webhook

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2024 License: MIT Imports: 6 Imported by: 0

README

Discord Webhook Library

A simple, flexible Go library for creating and sending Discord webhooks with rich embeds. This library supports customizable content, username, avatar URL, and multiple embeds with fields, authors, footers, images, and colors.

Discord Webhook

Features

  • 🌈 Customizable embed messages with rich content
  • 💬 Support for multiple fields, footers, authors, images, and colors
  • 🎨 Color support via Hex, RGB, or integers
  • 📅 ISO8601 timestamp validation

Installation

To install the library, use:

go get github.com/dozerokz/discord-webhook-go

Setup

You must first configure a Webhook on a Discord server before you can use this package. Instructions can be found on Discord's support website.

You can read more about webhooks structure here.

Quick Start

Here's a simple example to get you started:

import discordWebhook "github.com/dozerokz/discord-webhook-go"

...

// Create a webhook
webhook, err := discordWebhook.CreateWebhook("Hello, Discord!", "Bot", "SOME IMAGE URL") // replace with actual image url (string)
if err != nil {
    log.Fatal(err)
}

// Create an embed
embed, err := discordWebhook.CreateEmbed("Title", "Description", "https://example.com", "#ff5733")
if err != nil {
    log.Fatal(err)
}

// Add the embed to the webhook
discordWebhook.AddEmbed(embed)

// Send the webhook
err = discordWebhook.SendWebhook("YOUR_DISCORD_WEBHOOK_URL") // replace with your actual webhook url (string)
if err != nil {
    log.Fatal(err)
}

fmt.Println("Webhook sent successfully!")

For more detailed examples, check out the examples folder.

License

This project is open-source. You can use, modify, and distribute it under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SendWebhook

func SendWebhook(webhookUrl string, webhookPayload Webhook) error

SendWebhook sends the webhook payload to the specified Discord Webhook URL

Types

type Author

type Author struct {
	Name         string `json:"name,omitempty"`
	URL          string `json:"url,omitempty"`
	IconURL      string `json:"icon_url,omitempty"`
	ProxyIconURL string `json:"proxy_icon_url,omitempty"`
}

Author represents the author section of an embed

func CreateAuthor

func CreateAuthor(name string, url string, iconURL string, proxyIconURL string) Author

CreateAuthor creates an author object for an embed

type Embed

type Embed struct {
	Title       string    `json:"title,omitempty"`
	Description string    `json:"description,omitempty"`
	URL         string    `json:"url,omitempty"`
	Color       int       `json:"color,omitempty"`
	Timestamp   string    `json:"timestamp,omitempty"`
	Footer      Footer    `json:"footer,omitempty"`
	Image       Image     `json:"image,omitempty"`
	Thumbnail   Thumbnail `json:"thumbnail,omitempty"`
	Author      Author    `json:"author,omitempty"`
	Fields      []Field   `json:"fields,omitempty"`
}

Embed represents a rich embed object for Discord

func CreateEmbed

func CreateEmbed[T colorEmbedTypes](title, description, url string, color T) (Embed, error)

CreateEmbed creates an embed with title, description, URL, and color (supports string, int, or RGB)

func (*Embed) AddField

func (e *Embed) AddField(field Field)

AddField adds a field to the embed

func (*Embed) AddFields

func (e *Embed) AddFields(fields []Field)

AddFields adds multiple fields to the embed

func (*Embed) SetAuthor

func (e *Embed) SetAuthor(author Author)

SetAuthor sets the author for the embed

func (*Embed) SetCustomTimestamp added in v1.0.2

func (e *Embed) SetCustomTimestamp(timestamp string) error

SetCustomTimestamp validates and sets an ISO8601 timestamp for the embed

func (*Embed) SetFooter

func (e *Embed) SetFooter(footer Footer)

SetFooter sets the footer for the embed

func (*Embed) SetImage

func (e *Embed) SetImage(image Image)

SetImage sets the image for the embed

func (*Embed) SetThumbnail

func (e *Embed) SetThumbnail(thumbnail Thumbnail)

SetThumbnail sets the thumbnail for the embed

func (*Embed) SetTimestamp

func (e *Embed) SetTimestamp()

SetTimestamp sets current ISO8601 timestamp for the embed

type Field

type Field struct {
	Name   string `json:"name"`
	Value  string `json:"value"`
	Inline bool   `json:"inline,omitempty"`
}

Field represents a key-value pair in the embed's fields section

func CreateField

func CreateField(name string, value string, inline bool) Field

CreateField creates a field object for an embed

type Footer struct {
	Text         string `json:"text,omitempty"`
	IconURL      string `json:"icon_url,omitempty"`
	ProxyIconURL string `json:"proxy_icon_url,omitempty"`
}

Footer represents the footer section of an embed

func CreateFooter

func CreateFooter(text string, iconURL string, proxyIconURL string) Footer

CreateFooter creates a footer object for an embed

type Image

type Image struct {
	URL      string `json:"url,omitempty"`
	ProxyURL string `json:"proxy_url,omitempty"`
	Height   int    `json:"height,omitempty"`
	Width    int    `json:"width,omitempty"`
}

Image represents an image in an embed

func CreateImage

func CreateImage(url string, proxyURL string, height int, width int) Image

CreateImage creates an image object for an embed

type RGB

type RGB struct {
	R, G, B int
}

RGB is a struct representing an RGB color

type Thumbnail

type Thumbnail struct {
	URL      string `json:"url,omitempty"`
	ProxyURL string `json:"proxy_url,omitempty"`
	Height   int    `json:"height,omitempty"`
	Width    int    `json:"width,omitempty"`
}

Thumbnail represents a thumbnail image in an embed

func CreateThumbnail

func CreateThumbnail(url string, proxyURL string, height int, width int) Thumbnail

CreateThumbnail creates a thumbnail object for an embed

type Webhook

type Webhook struct {
	Content   string  `json:"content,omitempty"`
	Username  string  `json:"username,omitempty"`
	AvatarURL string  `json:"avatar_url,omitempty"`
	Embeds    []Embed `json:"embeds,omitempty"`
}

Webhook represents the structure for sending a message via Discord webhooks. It can include optional content, username, avatar URL, and an array of rich embed objects.

func CreateWebhook

func CreateWebhook(content, username, avatarURL string) (Webhook, error)

CreateWebhook creates a new Webhook with the specified content, username, and avatar URL

func (*Webhook) AddEmbed

func (w *Webhook) AddEmbed(embed Embed)

AddEmbed adds an embed to the webhook

Directories

Path Synopsis
examples
example1 command
Example 1: Simplest webhook that sends a message to a Discord channel
Example 1: Simplest webhook that sends a message to a Discord channel
example2 command
Example 2: More complicated webhook with author, thumbnail, image, footer and several fields.
Example 2: More complicated webhook with author, thumbnail, image, footer and several fields.

Jump to

Keyboard shortcuts

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