dmsg

package module
v0.2.0 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.ContainerSection(
            dmsg.SectionText("## 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
  • Clean API: Functional options pattern with intuitive naming
  • Direct: Uses discordgo types directly, no conversion needed

Quick Start

Simple Error Response
dmsg.Response(
    dmsg.Container(
        dmsg.AccentColor(16740978), // red
        dmsg.ContainerSection(
            dmsg.SectionText("## Error\n\nYou don't have enough coins"),
            dmsg.SectionAccessory(
                dmsg.Thumbnail(errorImageURL, "Error icon"),
            ),
        ),
        dmsg.ContainerSeparator(),
    ),
)
Success with Buttons
dmsg.Response(
    dmsg.Container(
        dmsg.AccentColor(5763719), // green
        dmsg.ContainerSection(
            dmsg.SectionText("## Success!\n\nYou won **1,000 coins**"),
            dmsg.SectionAccessory(
                dmsg.Thumbnail(trophyURL, trophyDesc),
            ),
        ),
        dmsg.ContainerSeparator(),
        dmsg.ContainerActionRow(
            dmsg.Button("Play Again", "play_again", dmsg.Style(dmsg.Success)),
            dmsg.Button("Quit", "quit", dmsg.Style(dmsg.Secondary)),
        ),
    ),
)
Ephemeral Response
dmsg.Ephemeral(
    dmsg.Container(
        dmsg.AccentColor(14197815), // gold
        dmsg.ContainerSection(
            dmsg.SectionText("This message is only visible to you"),
        ),
    ),
)
Update Message
dmsg.Update(
    dmsg.Container(
        dmsg.AccentColor(16777215), // white
        dmsg.ContainerSection(
            dmsg.SectionText("## Game Closed\n\nThanks for playing!"),
        ),
    ),
)

Component Reference

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

Components that can be passed directly to Response, Ephemeral, or Update:

  • Container() - Container with optional accent color and nested components
  • ActionRow() - Action row with buttons (top-level)
  • TextDisplay() - Text display component (top-level)
  • Separator() - Separator/divider component (top-level)
Container Components

Components for use inside Container():

ContainerSection - Section with text and optional accessory

dmsg.ContainerSection(
    dmsg.SectionText("content"),
    dmsg.SectionAccessory(component),
)

ContainerSeparator - Separator/divider

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

ContainerActionRow - Action row with buttons

dmsg.ContainerActionRow(
    dmsg.Button(...),
    dmsg.Button(...),
)

ContainerFile - File attachment

dmsg.ContainerFile(url,
    dmsg.Spoiler(),
)

ContainerGallery - Media gallery

dmsg.ContainerGallery(
    dmsg.Media(url, description, spoiler),
    dmsg.Media(url, description, spoiler),
)
Section Components

Components for use inside ContainerSection():

SectionText - Text display

dmsg.SectionText("## Markdown content")

SectionAccessory - Accessory (thumbnail or button)

dmsg.SectionAccessory(
    dmsg.Thumbnail(url, description),
)
Standalone Components

Thumbnail - Can be used as accessory or standalone

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

Media - Helper for gallery items

dmsg.Media(url, description, spoiler)
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.ContainerSection(...),  // Also ContainerOption
)

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

// ✅ Clear naming shows where components belong
dmsg.ContainerSection(        // For use in Container
    dmsg.SectionText(...),     // For use in Section
)

Design Patterns

Multiple Sections
dmsg.Container(
    dmsg.AccentColor(gold),
    dmsg.ContainerSection(
        dmsg.SectionText("## Section 1"),
        dmsg.SectionText("Content here"),
    ),
    dmsg.ContainerSeparator(),
    dmsg.ContainerSection(
        dmsg.SectionText("## Section 2"),
        dmsg.SectionText("More content"),
    ),
)
Section with Accessory
dmsg.ContainerSection(
    dmsg.SectionText("Main content"),
    dmsg.SectionAccessory(
        dmsg.Thumbnail(imageURL, imageDesc),
    ),
)
Multiple Buttons
dmsg.ContainerActionRow(
    dmsg.Button("Confirm", "confirm", dmsg.Style(dmsg.Success)),
    dmsg.Button("Cancel", "cancel", dmsg.Style(dmsg.Danger)),
    dmsg.LinkButton("Help", "https://example.com"),
)
Top-Level Components
dmsg.Update(
    dmsg.TextDisplay("Simple text message"),
    dmsg.Separator(),
    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 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 Spoiler

func Spoiler() interface {
	ContainerOption
	ThumbnailOption
	FileOption
}

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

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 ActionRow

func ActionRow(buttons ...Component) Component

ActionRow creates an action row with buttons for use at top-level

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 Separator added in v0.2.0

func Separator(opts ...SeparatorOption) Component

Separator creates a separator component for use at top-level

func TextDisplay added in v0.2.0

func TextDisplay(content string) Component

TextDisplay creates a text display component for use at top-level

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 ContainerActionRow added in v0.2.0

func ContainerActionRow(buttons ...Component) ContainerOption

ContainerActionRow creates an action row with buttons for use inside containers

func ContainerFile added in v0.2.0

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

ContainerFile creates a file component for use inside containers

func ContainerGallery added in v0.2.0

func ContainerGallery(items ...MediaItem) ContainerOption

ContainerGallery creates a media gallery component for use inside containers

func ContainerSection added in v0.2.0

func ContainerSection(opts ...SectionOption) ContainerOption

ContainerSection creates a section component for use inside containers

func ContainerSeparator added in v0.2.0

func ContainerSeparator(opts ...SeparatorOption) ContainerOption

ContainerSeparator creates a separator component for use inside 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 SectionAccessory added in v0.2.0

func SectionAccessory(component Component) SectionOption

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

func SectionText added in v0.2.0

func SectionText(content string) SectionOption

SectionText creates a text display component for use inside sections

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