micup

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 15 Imported by: 0

README

micup.go

micup.go is a standalone Go client for Micup bots. It discovers the public REST and Gateway endpoints, authenticates bot requests, keeps a Gateway session connected and resumed, caches guild channels and voice states, joins voice channels, sends messages with embeds, responds privately to interactions, and can publish stereo PCM to a LiveKit voice room.

The module keeps platform transport separate from application behavior. It does not implement music search, queues, command syntax, persistence, or worker coordination.

Requirements

  • Go 1.26.8 or newer
  • A Micup bot token
  • FFmpeg with libopus enabled when publishing voice audio

Install

go get github.com/yestefir/micup.go@v0.3.0

The public repository is yestefir/micup.go. The Go module path matches that repository.

Quick start

client, err := micup.New(ctx, micup.Config{Token: os.Getenv("MICUP_BOT_TOKEN")})
if err != nil {
	log.Fatal(err)
}

session := client.NewSession(micup.SessionOptions{Handler: func(ctx context.Context, event micup.Event) error {
	if event.Name != micup.EventMessageCreate {
		return nil
	}
	var message micup.MessageCreate
	if err := event.Decode(&message); err != nil {
		return err
	}
	if message.Author.Bot || message.Content != "!ping" {
		return nil
	}
	_, err := client.SendMessage(ctx, message.ChannelID, "pong", message.ID)
	return err
}})
err = session.Run(ctx)

Bots in many guilds can replace client.NewSession with client.NewShardManager(micup.ShardManagerOptions{ShardCount: 4, Session: options}). See Shards.

See the guides for setup, Gateway events, voice publishing, security boundaries, and examples.

License

This project is free to use under the MIT License, including in commercial projects. Keep the copyright and license notice with copies or substantial portions of the software.

Repository layout

client.go          Config, endpoint discovery, bot authentication
rest.go            Authenticated REST requests and rate-limit retries
messages.go        Channel lookup and channel messages
interactions.go    Interaction responses, deferrals, follow-ups
events.go          Event type and Gateway event name constants
gateway.go         Opcodes, close codes, single-connection Gateway loop
session.go         Reconnecting, resuming session for one shard
session_voice.go   Voice join handshake, grants, voice-lost signal
shard.go           Shard mapping and multi-shard ShardManager
state.go           Guild channel and voice state cache
types.go           Micup REST and Gateway payload types
version.go         Module version and default User-Agent
voice/             LiveKit room and PCM-to-Opus publishing
docs/              User, API, architecture, and contributor guides

Run make check before sending a change. CI runs the same checks on Linux, macOS and Windows.

Go source files intentionally contain no comments. The documentation site is the source of user and API guidance.

Current scope

The module covers the public REST/Gateway transport, a supervised Gateway session with resume, a guild/voice state cache, the voice join handshake, the LiveKit PCM publishing helper, and a ShardManager that runs many shards in one process with paced IDENTIFY. Shard counts are configured by the application; the module does not discover them or move shards between processes.

The current release is v0.3.0. Release history is in CHANGELOG.md.

Documentation

Index

Constants

View Source
const (
	EventReady             = "READY"
	EventResumed           = "RESUMED"
	EventGuildCreate       = "GUILD_CREATE"
	EventGuildDelete       = "GUILD_DELETE"
	EventChannelCreate     = "CHANNEL_CREATE"
	EventChannelUpdate     = "CHANNEL_UPDATE"
	EventChannelDelete     = "CHANNEL_DELETE"
	EventMessageCreate     = "MESSAGE_CREATE"
	EventInteractionCreate = "INTERACTION_CREATE"
	EventVoiceStateUpdate  = "VOICE_STATE_UPDATE"
	EventVoiceServerUpdate = "VOICE_SERVER_UPDATE"
)
View Source
const (
	OpDispatch         = 0
	OpHeartbeat        = 1
	OpIdentify         = 2
	OpVoiceStateUpdate = 4
	OpResume           = 6
	OpReconnect        = 7
	OpInvalidSession   = 9
	OpHello            = 10
	OpHeartbeatAck     = 11
)
View Source
const (
	MessageTypeDefault = 0
	MessageTypeReply   = 19
)
View Source
const DefaultIdentifyInterval = 5 * time.Second
View Source
const (
	DefaultOrigin = "https://micup.gg"
)
View Source
const Version = "0.3.0"

Variables

View Source
var (
	ErrReconnectRequested = errors.New("Micup Gateway requested reconnect")
	ErrSessionInvalidated = errors.New("Micup Gateway invalidated the session")
	ErrGatewayClosed      = errors.New("Micup Gateway is disconnected")
)
View Source
var (
	ErrVoiceJoinPending = errors.New("Micup voice join is already pending")
	ErrVoiceGrant       = errors.New("invalid Micup voice grant")
	ErrVoiceMoved       = errors.New("Micup voice channel changed during join")
)
View Source
var (
	ErrShardNotOwned       = errors.New("Micup guild belongs to a shard this manager does not run")
	ErrShardManagerRunning = errors.New("Micup shard manager is already running")
)
View Source
var ErrNotReady = errors.New("Micup Gateway session is not ready")

Functions

func ShardForGuild added in v0.3.0

func ShardForGuild(guildID string, shardCount int) (int, error)

Types

type APIError

type APIError struct{ StatusCode int }

func (*APIError) Error

func (e *APIError) Error() string

type Channel

type Channel struct {
	ID      string `json:"id"`
	GuildID string `json:"guild_id"`
	Name    string `json:"name"`
	Type    int    `json:"type"`
}

type Client

type Client struct {
	BotID                 string
	MessageContentEnabled *bool
	// contains filtered or unexported fields
}

func New

func New(ctx context.Context, config Config) (*Client, error)

func (*Client) Channel added in v0.3.0

func (c *Client) Channel(ctx context.Context, channelID string) (Channel, error)

func (*Client) DeferInteraction added in v0.2.0

func (c *Client) DeferInteraction(ctx context.Context, interaction Interaction, ephemeral bool) error

func (*Client) Gateway

func (c *Client) Gateway(ctx context.Context, handler EventHandler) error

func (*Client) GatewayEndpoint added in v0.2.0

func (c *Client) GatewayEndpoint() string

func (*Client) NewSession added in v0.3.0

func (c *Client) NewSession(options SessionOptions) *Session

func (*Client) NewShardManager added in v0.3.0

func (c *Client) NewShardManager(options ShardManagerOptions) (*ShardManager, error)

func (*Client) REST

func (c *Client) REST(ctx context.Context, method, path string, body, dest any) error

func (*Client) RespondToInteraction added in v0.2.0

func (c *Client) RespondToInteraction(ctx context.Context, interaction Interaction, options MessageOptions, ephemeral bool) error

func (*Client) SendInteractionFollowup added in v0.2.0

func (c *Client) SendInteractionFollowup(ctx context.Context, interaction Interaction, options MessageOptions, ephemeral bool) (Message, error)

func (*Client) SendMessage

func (c *Client) SendMessage(ctx context.Context, channelID, content, replyTo string) (Message, error)

func (*Client) SendMessageWithOptions added in v0.2.0

func (c *Client) SendMessageWithOptions(ctx context.Context, channelID string, options MessageOptions) (Message, error)

type CloseError added in v0.3.0

type CloseError struct {
	Code int
	Text string
}

func (*CloseError) Error added in v0.3.0

func (e *CloseError) Error() string

func (*CloseError) Fatal added in v0.3.0

func (e *CloseError) Fatal() bool

type Config

type Config struct {
	Token      string
	Origin     string
	HTTP       *http.Client
	ShardID    int
	ShardCount int
}

type Embed added in v0.2.0

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

type EmbedAuthor added in v0.2.0

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

type EmbedField added in v0.2.0

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

type EmbedFooter added in v0.2.0

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

type EmbedMedia added in v0.2.0

type EmbedMedia struct {
	URL string `json:"url"`
}

type Event

type Event struct {
	Name     string
	Sequence *int64
	ShardID  int
	Data     json.RawMessage
	// contains filtered or unexported fields
}

func (Event) Decode added in v0.3.0

func (e Event) Decode(dest any) error

func (Event) Send

func (e Event) Send(ctx context.Context, opcode int, data any) error

type EventHandler

type EventHandler func(context.Context, Event) error

type Guild

type Guild struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

type Interaction added in v0.2.0

type Interaction struct {
	ID            string          `json:"id"`
	ApplicationID string          `json:"application_id"`
	Type          int             `json:"type"`
	Data          json.RawMessage `json:"data,omitempty"`
	GuildID       string          `json:"guild_id,omitempty"`
	ChannelID     string          `json:"channel_id,omitempty"`
	Token         string          `json:"token"`
	Version       int             `json:"version,omitempty"`
	Member        json.RawMessage `json:"member,omitempty"`
	User          json.RawMessage `json:"user,omitempty"`
}

type Message

type Message struct {
	ID        string  `json:"id"`
	ChannelID string  `json:"channel_id"`
	GuildID   string  `json:"guild_id,omitempty"`
	Content   string  `json:"content"`
	Embeds    []Embed `json:"embeds,omitempty"`
	Flags     int     `json:"flags,omitempty"`
}

type MessageCreate

type MessageCreate struct {
	ID        string          `json:"id"`
	GuildID   string          `json:"guild_id"`
	ChannelID string          `json:"channel_id"`
	Content   string          `json:"content"`
	Type      int             `json:"type"`
	Author    User            `json:"author"`
	Mentions  []User          `json:"mentions"`
	Raw       json.RawMessage `json:"-"`
}

type MessageOptions added in v0.2.0

type MessageOptions struct {
	Content      string  `json:"content,omitempty"`
	Embeds       []Embed `json:"embeds,omitempty"`
	ReplyTo      string  `json:"-"`
	Nonce        string  `json:"nonce,omitempty"`
	EnforceNonce bool    `json:"enforce_nonce,omitempty"`
}

type Session added in v0.3.0

type Session struct {
	// contains filtered or unexported fields
}

func (*Session) JoinVoice added in v0.3.0

func (s *Session) JoinVoice(ctx context.Context, guildID, channelID string, options VoiceJoin) (VoiceServerUpdate, error)

func (*Session) LeaveVoice added in v0.3.0

func (s *Session) LeaveVoice(guildID, connectionID string) error

func (*Session) Ready added in v0.3.0

func (s *Session) Ready() bool

func (*Session) ResolveGuild added in v0.3.0

func (s *Session) ResolveGuild(ctx context.Context, channelID string) (string, error)

func (*Session) Run added in v0.3.0

func (s *Session) Run(ctx context.Context) error

func (*Session) Send added in v0.3.0

func (s *Session) Send(opcode int, data any) error

func (*Session) ShardID added in v0.3.0

func (s *Session) ShardID() int

func (*Session) State added in v0.3.0

func (s *Session) State() *State

func (*Session) Voice added in v0.3.0

func (s *Session) Voice(guildID string) (VoiceServerUpdate, bool)

type SessionOptions added in v0.3.0

type SessionOptions struct {
	Handler     EventHandler
	OnVoiceLost func(guildID, connectionID string)
	UserAgent   string
	Browser     string
	MinBackoff  time.Duration
	MaxBackoff  time.Duration
}

type ShardManager added in v0.3.0

type ShardManager struct {
	// contains filtered or unexported fields
}

func (*ShardManager) ForGuild added in v0.3.0

func (m *ShardManager) ForGuild(guildID string) (*Session, error)

func (*ShardManager) JoinVoice added in v0.3.0

func (m *ShardManager) JoinVoice(ctx context.Context, guildID, channelID string, options VoiceJoin) (VoiceServerUpdate, error)

func (*ShardManager) LeaveVoice added in v0.3.0

func (m *ShardManager) LeaveVoice(guildID, connectionID string) error

func (*ShardManager) Ready added in v0.3.0

func (m *ShardManager) Ready() bool

func (*ShardManager) ResolveGuild added in v0.3.0

func (m *ShardManager) ResolveGuild(ctx context.Context, channelID string) (string, error)

func (*ShardManager) Run added in v0.3.0

func (m *ShardManager) Run(ctx context.Context) error

func (*ShardManager) Shard added in v0.3.0

func (m *ShardManager) Shard(shardID int) (*Session, bool)

func (*ShardManager) ShardCount added in v0.3.0

func (m *ShardManager) ShardCount() int

func (*ShardManager) Shards added in v0.3.0

func (m *ShardManager) Shards() []*Session

func (*ShardManager) Voice added in v0.3.0

func (m *ShardManager) Voice(guildID string) (VoiceServerUpdate, bool)

type ShardManagerOptions added in v0.3.0

type ShardManagerOptions struct {
	ShardCount       int
	ShardIDs         []int
	IdentifyInterval time.Duration
	Session          SessionOptions
}

type State added in v0.3.0

type State struct {
	// contains filtered or unexported fields
}

func NewState added in v0.3.0

func NewState(botID string) *State

func (*State) Apply added in v0.3.0

func (s *State) Apply(event Event)

func (*State) BotVoiceChannel added in v0.3.0

func (s *State) BotVoiceChannel(guildID string) string

func (*State) Channels added in v0.3.0

func (s *State) Channels() int

func (*State) GuildForChannel added in v0.3.0

func (s *State) GuildForChannel(channelID string) (string, bool)

func (*State) Guilds added in v0.3.0

func (s *State) Guilds() int

func (*State) VoiceChannel added in v0.3.0

func (s *State) VoiceChannel(guildID, userID string) string

type User

type User struct {
	ID       string `json:"id"`
	Username string `json:"username"`
	Bot      bool   `json:"bot"`
}

type VoiceJoin added in v0.3.0

type VoiceJoin struct {
	SelfMute bool
	SelfDeaf bool
}

type VoiceServerUpdate

type VoiceServerUpdate struct {
	GuildID      string `json:"guild_id"`
	ChannelID    string `json:"channel_id,omitempty"`
	Endpoint     string `json:"endpoint"`
	Token        string `json:"token"`
	ConnectionID string `json:"connection_id"`
}

type VoiceState

type VoiceState struct {
	GuildID   string `json:"guild_id"`
	UserID    string `json:"user_id"`
	ChannelID string `json:"channel_id"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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