dmsg

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2026 License: MIT Imports: 1 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

dmsg

Type-safe Discord message component builder for Components v2.

Installation

go get github.com/thomasgtaylor/dmsg

Requirements

  • Go 1.21 or later
  • discordgo - Discord API wrapper for Go

Basic Usage

import (
    "github.com/bwmarrin/discordgo"
    "github.com/thomasgtaylor/dmsg"
)

response := dmsg.Response(
    dmsg.Container(
        dmsg.AccentColor(5763719), // green
        dmsg.Section(
            dmsg.TextDisplay("## Hello, World!"),
        ),
    ),
)

Features

  • Type-safe: Options are typed per component - can't pass wrong option to wrong component
  • Composable: Components nest naturally, reads top-to-bottom like the UI
  • Flexible: Components work in multiple contexts (top-level, in containers, in sections)
  • Clean API: Simple, intuitive naming without prefixes
  • Direct: Uses discordgo types directly, no conversion needed

Quick Start

Simple Error Response
dmsg.Response(
    dmsg.Container(
        dmsg.AccentColor(16740978), // red
        dmsg.Section(
            dmsg.TextDisplay("## Error\n\nYou don't have enough coins"),
            dmsg.Accessory(
                dmsg.Thumbnail(errorImageURL, "Error icon"),
            ),
        ),
        dmsg.Separator(),
    ),
)
Success with Buttons
dmsg.Response(
    dmsg.Container(
        dmsg.AccentColor(5763719), // green
        dmsg.Section(
            dmsg.TextDisplay("## Success!\n\nYou won **1,000 coins**"),
            dmsg.Accessory(
                dmsg.Thumbnail(trophyURL, trophyDesc),
            ),
        ),
        dmsg.Separator(),
        dmsg.ActionRow(
            dmsg.Button("Play Again", "play_again", dmsg.Style(dmsg.Success)),
            dmsg.Button("Quit", "quit", dmsg.Style(dmsg.Secondary)),
        ),
    ),
)
Simple Top-Level Message
dmsg.Update(
    dmsg.TextDisplay("It's dangerous to go alone!"),
    dmsg.Separator(),
    dmsg.TextDisplay("Take this."),
)
Ephemeral Response
dmsg.Ephemeral(
    dmsg.Container(
        dmsg.AccentColor(14197815), // gold
        dmsg.Section(
            dmsg.TextDisplay("This message is only visible to you"),
        ),
    ),
)

Component Reference

Entry Points
  • Response(components ...Component) - Standard response
  • Ephemeral(components ...Component) - Ephemeral response
  • Update(components ...Component) - Update message response
Layout Components

Container

dmsg.Container(
    dmsg.AccentColor(color int),
    dmsg.Spoiler(),
    // child components...
)

Container can contain: Section, TextDisplay, Separator, ActionRow, File, Gallery

Section

dmsg.Section(
    dmsg.TextDisplay("content"),
    dmsg.Accessory(component),
)

Section can be used:

  • As a top-level component in Response, Update, Ephemeral
  • Inside Container

Section can contain: TextDisplay Section can have an accessory: Button or Thumbnail

Separator

dmsg.Separator(
    dmsg.WithDivider(true),
    dmsg.Spacing(discordgo.SeparatorSpacingSizeSmall),
)

Separator can be used:

  • As a top-level component
  • Inside Container

ActionRow

dmsg.ActionRow(
    dmsg.Button(...),
    dmsg.Button(...),
)

ActionRow can be used:

  • As a top-level component
  • Inside Container

ActionRow can contain: Button, LinkButton

Content Components

TextDisplay

dmsg.TextDisplay("## Markdown content")

TextDisplay can be used:

  • As a top-level component
  • Inside Container
  • Inside Section

Thumbnail

dmsg.Thumbnail(url, description,
    dmsg.Spoiler(),
)

Thumbnail can be used as a Section accessory.

File

dmsg.File("attachment://file.txt",
    dmsg.Spoiler(),
)

File can be used inside Container.

Gallery

dmsg.Gallery(
    dmsg.Media(url, description, spoiler),
    dmsg.Media(url, description, spoiler),
)

Gallery can be used inside Container.

Interactive Components

Button

dmsg.Button(label, customID,
    dmsg.Style(dmsg.Primary),   // or Secondary, Success, Danger
    dmsg.Emoji(&discordgo.ComponentEmoji{Name: "🎉"}),
    dmsg.Disabled(),
)

Link Button

dmsg.LinkButton(label, url,
    dmsg.Emoji(&discordgo.ComponentEmoji{Name: "❤️"}),
)

Type Safety

Options are typed per component. The compiler prevents mistakes:

// ✅ This compiles
dmsg.Container(
    dmsg.AccentColor(red),  // ContainerOption
    dmsg.Section(...),      // Also ContainerOption
)

// ❌ This won't compile
dmsg.Container(
    dmsg.Style(dmsg.Primary),  // ButtonOption - wrong type!
)

Flexible Component Usage

Components automatically work in multiple contexts:

// TextDisplay works everywhere
dmsg.Response(
    dmsg.TextDisplay("Top level"),           // ✅ Top-level
    dmsg.Container(
        dmsg.TextDisplay("In container"),    // ✅ In Container
        dmsg.Section(
            dmsg.TextDisplay("In section"),  // ✅ In Section
        ),
    ),
)

// Section works at top-level and in containers
dmsg.Response(
    dmsg.Section(                            // ✅ Top-level
        dmsg.TextDisplay("Content"),
    ),
    dmsg.Container(
        dmsg.Section(                        // ✅ In Container
            dmsg.TextDisplay("Content"),
        ),
    ),
)

Design Patterns

Multiple Sections
dmsg.Container(
    dmsg.AccentColor(gold),
    dmsg.Section(
        dmsg.TextDisplay("## Section 1"),
        dmsg.TextDisplay("Content here"),
    ),
    dmsg.Separator(),
    dmsg.Section(
        dmsg.TextDisplay("## Section 2"),
        dmsg.TextDisplay("More content"),
    ),
)
Section with Accessory
dmsg.Section(
    dmsg.TextDisplay("Main content"),
    dmsg.Accessory(
        dmsg.Thumbnail(imageURL, imageDesc),
    ),
)
Multiple Buttons
dmsg.ActionRow(
    dmsg.Button("Confirm", "confirm", dmsg.Style(dmsg.Success)),
    dmsg.Button("Cancel", "cancel", dmsg.Style(dmsg.Danger)),
    dmsg.LinkButton("Help", "https://example.com"),
)
Mixing Top-Level Components
dmsg.Response(
    dmsg.TextDisplay("Simple message"),
    dmsg.Separator(),
    dmsg.Section(
        dmsg.TextDisplay("Section content"),
    ),
    dmsg.ActionRow(
        dmsg.Button("Action", "action_id"),
    ),
)

Button Styles

  • dmsg.Primary - Blue (default)
  • dmsg.Secondary - Gray
  • dmsg.Success - Green
  • dmsg.Danger - Red

Documentation

Overview

Package dmsg provides a type-safe builder for Discord Components v2 messages.

It wraps discordgo types with a functional options pattern for clean, composable message construction. The API prevents common mistakes by using typed options that can only be applied to their corresponding components.

Basic usage:

response := dmsg.Response(
    dmsg.Container(
        dmsg.AccentColor(5763719),
        dmsg.Section(
            dmsg.Text("## Hello World"),
        ),
    ),
)

The package supports all Discord Components v2 features including containers, sections, buttons, thumbnails, galleries, and more.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ActionRow

func ActionRow(buttons ...Component) interface {
	Component
	ContainerOption
}

ActionRow creates an action row with buttons (can be used top-level or in containers)

func Ephemeral

func Ephemeral(components ...Component) *discordgo.InteractionResponse

Ephemeral creates an ephemeral interaction response

func Response

func Response(components ...Component) *discordgo.InteractionResponse

Response creates a standard interaction response

func Section

func Section(opts ...SectionOption) interface {
	Component
	ContainerOption
}

Section creates a section component (can be used top-level or in containers)

func Separator added in v0.2.0

func Separator(opts ...SeparatorOption) interface {
	Component
	ContainerOption
}

Separator creates a separator component (can be used top-level or in containers)

func Spoiler

func Spoiler() interface {
	ContainerOption
	ThumbnailOption
	FileOption
}

Spoiler marks a component as a spoiler (Container, Thumbnail, or File)

func TextDisplay added in v0.2.0

func TextDisplay(content string) interface {
	Component
	ContainerOption
	SectionOption
}

TextDisplay creates a text display component (can be used top-level, in containers, or in sections)

func Update

func Update(components ...Component) *discordgo.InteractionResponse

Update creates an update message response

Types

type ButtonOption

type ButtonOption interface {
	// contains filtered or unexported methods
}

ButtonOption configures a Button

func Disabled

func Disabled() ButtonOption

Disabled marks the button as disabled

func Emoji

Emoji sets the button emoji

func Style

func Style(style ButtonStyle) ButtonOption

Style sets the button style

type ButtonStyle

type ButtonStyle int

ButtonStyle represents button visual styles

const (
	Primary   ButtonStyle = 1
	Secondary ButtonStyle = 2
	Success   ButtonStyle = 3
	Danger    ButtonStyle = 4
)

type Component

type Component = discordgo.MessageComponent

Component is any Discord message component

func Button

func Button(label, customID string, opts ...ButtonOption) Component

Button creates an action button

func Container

func Container(opts ...ContainerOption) Component

Container creates a container component

func LinkButton

func LinkButton(label, url string, opts ...ButtonOption) Component

LinkButton creates a link button

func Thumbnail

func Thumbnail(url, description string, opts ...ThumbnailOption) Component

Thumbnail creates a thumbnail component

type ContainerOption

type ContainerOption interface {
	// contains filtered or unexported methods
}

ContainerOption configures a Container

func AccentColor

func AccentColor(color int) ContainerOption

AccentColor sets the container's accent color

func File

func File(url string, opts ...FileOption) ContainerOption

File creates a file component (for use in containers)

func Gallery(items ...MediaItem) ContainerOption

Gallery creates a media gallery component (for use in containers)

type FileOption

type FileOption interface {
	// contains filtered or unexported methods
}

FileOption configures a File

type MediaItem

type MediaItem struct {
	URL         string
	Description string
	Spoiler     bool
}

MediaItem represents a media gallery item

func Media

func Media(url, description string, spoiler bool) MediaItem

Media creates a media item for galleries

type SectionOption

type SectionOption interface {
	// contains filtered or unexported methods
}

SectionOption configures a Section

func Accessory

func Accessory(component Component) SectionOption

Accessory sets the section's accessory (Button or Thumbnail)

type SeparatorOption added in v0.2.0

type SeparatorOption interface {
	// contains filtered or unexported methods
}

SeparatorOption configures a Separator

func Spacing

Spacing sets the separator spacing (1 = small, 2 = large)

func WithDivider

func WithDivider(show bool) SeparatorOption

WithDivider sets whether to show the divider line

type ThumbnailOption

type ThumbnailOption interface {
	// contains filtered or unexported methods
}

ThumbnailOption configures a Thumbnail

Jump to

Keyboard shortcuts

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