discord

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 6 Imported by: 0

README

Discord

A small go library to post messages to a Discord channel using Webhooks.

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.

Usage

You must first configure the Webhook URL that will be used

discord.WebhookURL = "https://discord.com/api/webhooks/.../..."

Then you can either send a simple text message or a more complex message

Simple Text Message

discord.Say("Hello, world!")

You can also use discord.SayTo() to use a specific webhook regardless of the global variable.

Complex Message

discord.Post(discord.PostOptions{
	Content: "Hello, world!",
	Embeds: []discord.Embed{
		{
			Author: discord.Author{
				Name: "ecnepsnai",
				URL:  "https://github.com/ecnepsnai",
			},
			Title:       "Amazing!",
			Description: "This is a cool embed",
		},
	},
})

You can also use discord.PostTo() to use a specific webhook regardless of the global variable.

File Attachment

Restrictions with Discords Webhook API only supports 1 file upload at 8MiB or less.

var f *io.Reader // Pretend we've opened a file
content := discord.PostOptions{
	Content: "Hello, world!",
}
fileOptions := discord.FileOptions{
	FileName: "my_hot_mixtape.mp3",
	Reader:   f,
}
discord.UploadFile(content, fileOptions)

You can also use discord.UploadFile() to use a specific webhook regardless of the global variable.

Documentation

For more information see the package's documentation.

This package is not endorsed by or affiliated with Discord, inc.

Documentation

Overview

Package discord is a go library to quickly send events to discord channels To get started: Create a Webhook on the server, noting down the webhook URL. Then, in your application set the webhook URL variable and then you can use `Say` for a simple text message, `Post` for a more complex message, or `UploadFile` to upload a file.

Index

Examples

Constants

This section is empty.

Variables

View Source
var WebhookURL string

WebhookURL your discord webhook URL

Functions

func Post

func Post(content PostOptions) error

Post will post a message to the channel for which the global webhook is configured. Unlike `discord.Say()`, Post gives you full control over the message.

Example
package main

import (
	"git.ecn.io/ian/discord"
)

func main() {
	discord.WebhookURL = "https://discord.com/api/webhooks/.../..."
	discord.Post(discord.PostOptions{
		Content: "Hello, world!",
		Embeds: []discord.Embed{
			{
				Color: 16777215,
				Author: &discord.Author{
					Name: "ecnepsnai",
					URL:  "https://github.com/ecnepsnai",
				},
				Title:       "Amazing!",
				Description: "This is a cool embed",
			},
		},
	})
}

func PostTo

func PostTo(content PostOptions, webhookUrl string) error

PostTo will post a message to the channel for which the webhook is configured. Unlike `discord.Say()`, Post gives you full control over the message.

Example
package main

import (
	"git.ecn.io/ian/discord"
)

func main() {
	discord.PostTo(discord.PostOptions{
		Content: "Hello, world!",
		Embeds: []discord.Embed{
			{
				Color: 16777215,
				Author: &discord.Author{
					Name: "ecnepsnai",
					URL:  "https://github.com/ecnepsnai",
				},
				Title:       "Amazing!",
				Description: "This is a cool embed",
			},
		},
	}, "https://discord.com/api/webhooks/.../...")
}

func Say

func Say(message string) error

Say sends the provided message to the channel for which the global webhook is configured. If WebhookURL is not set, this does nothing.

Example
package main

import (
	"git.ecn.io/ian/discord"
)

func main() {
	discord.WebhookURL = "https://discord.com/api/webhooks/.../..."
	discord.Say("Hello, world!")
}

func SayTo

func SayTo(message, webhookUrl string) error

Say sends the provided message to the channel for which the webhook is configured. If WebhookURL is not set, this does nothing.

Example
package main

import (
	"git.ecn.io/ian/discord"
)

func main() {
	discord.SayTo("Hello, world!", "https://discord.com/api/webhooks/.../...")
}

func UploadFile

func UploadFile(content PostOptions, file FileOptions) error

UploadFile will post a message to the channel for which the global webhook is configured and attach the specified file to your message. Rich embeds are not supported and will be ignored if any are specified.

Example
package main

import (
	"io"

	"git.ecn.io/ian/discord"
)

func main() {
	discord.WebhookURL = "https://discord.com/api/webhooks/.../..."
	var f io.Reader // Pretend we've opened a file
	content := discord.PostOptions{
		Content: "Hello, world!",
	}
	fileOptions := discord.FileOptions{
		FileName: "my_hot_mixtape.mp3",
		Reader:   f,
	}
	discord.UploadFile(content, fileOptions)
}

func UploadFileTo

func UploadFileTo(content PostOptions, file FileOptions, webhookUrl string) error

UploadFileTo will post a message to the channel for which the webhook is configured and attach the specified file to your message. Rich embeds are not supported and will be ignored if any are specified.

Example
package main

import (
	"io"

	"git.ecn.io/ian/discord"
)

func main() {
	var f io.Reader // Pretend we've opened a file
	content := discord.PostOptions{
		Content: "Hello, world!",
	}
	fileOptions := discord.FileOptions{
		FileName: "my_hot_mixtape.mp3",
		Reader:   f,
	}
	discord.UploadFileTo(content, fileOptions, "https://discord.com/api/webhooks/.../...")
}

Types

type Author

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

Author describes the author for an embed

type Embed

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

Embed describes embedded content within a message

type Field

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

Field describes a field for an embed

type FileOptions

type FileOptions struct {
	// The file name must include an extension and not include any directories
	FileName string
	Reader   io.Reader
}

FileOptions describes the options for uploading a file

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

Footer describes the footer for an embed

type Image

type Image struct {
	URL string `json:"url,omitempty"`
}

Image describes an image for an embed. If you need to upload an image you must use the `discord.UploadFile()` method, however that does not support rich embeds.

type PostOptions

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

PostOptions describes all possible options for a post

Source Files

  • discord.go

Directories

Path Synopsis
cmd
disco command
Command disco provides a command-line interface to post Discord messages to a channel using a familiar interface like sendmail.
Command disco provides a command-line interface to post Discord messages to a channel using a familiar interface like sendmail.

Jump to

Keyboard shortcuts

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