presence

package module
v0.0.0-...-049c09d Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2026 License: BSD-2-Clause Imports: 6 Imported by: 0

README

gopresence

Discord presence for golang that sucks less.

example listening example playing example competing

Quick Start

go get -u github.com/dragsbruh/gopresence
Example

See hello.go for a proper working example.

client := presence.New()

// Connect to Discord's IPC socket
client.Connect()
// Defer disconnect
defer client.Close() 

// Send handshake with ClientID
readyEv, err := client.Login("<CLIENT_ID>")
log.Printf("connected to discord as user %s", readyEv.User.GlobalName)

// Send SET_ACTIVITY request (fire and forget)
client.SetActivity(presence.SetActivityArgs{
	PID: os.Getpid(),
	Activity: presence.Activity{
		Name:    "Fonta Drinking Simulator",
		Type:    presence.ActivityListening,
		State:   "Leading with the highest score",
		Details: "Drinking Fonta",
		Buttons: []presence.Button{
			{Label: "What is Hearth?", URL: "https://hearth.is-a.dev"},
		},
		Party: &presence.Party{
			ID:   uuid.NewString(),
			Size: [2]int{3, 4},
		},
		Assets: &presence.Assets{
			LargeImage: "https://files.hearth.is-a.dev/public/avatars/arle.png",
			LargeText:  "Join Palais Mermonia!",
			LargeURL:   "https://discord.gg/BK9sBWdWC4",
			SmallImage: "https://files.hearth.is-a.dev/public/avatars/iostpa.jpg",
			SmallText:  "Join is-a.dev!",
			SmallURL:   "https://discord.gg/DsdeaG3hRV",
		},
	},
})

NOTE: Above example does not handle errors to keep it short

Reference

I highly recommend reading the function signatures and source code directly, it's super small.

Currently the library only supports Login and SetActivity methods, so I recommend you use client.GetIPC to get the underlying IPC socket along with the helpers for advanced usage.

If you think said usage should be a feature in the library, feel free to open an issue or PR.

TO-DO

  • Support for Windows
  • MacOS and Linux (native + snap)

License

Under BSD-2-Clause

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Activity

type Activity struct {
	Name  string       `json:"name,omitempty"`
	Type  ActivityType `json:"type"`
	State string       `json:"state"`
	// Shown alongside `Party` size when `Type` is `ActivityPlaying` and `Party` is non-null
	Details string `json:"details"`
	// URL to navigate to when you click on `Details`
	DetailsURL string `json:"details_url,omitempty"`
	// Only shown when `Type` is `ActivityPlaying`
	Party  *Party  `json:"party,omitempty"`
	Assets *Assets `json:"assets,omitempty"`
	// Cannot be used with `Buttons`
	Secrets    *Secrets    `json:"secrets,omitempty"`
	Timestamps *Timestamps `json:"timestamps,omitempty"`
	// Maximum of two are allowed
	Buttons []Button `json:"buttons,omitempty"`
}

type ActivityType

type ActivityType int
const (
	ActivityPlaying   ActivityType = 0
	ActivityListening ActivityType = 2
	ActivityWatching  ActivityType = 3
	ActivityCompeting ActivityType = 5
)

type Assets

type Assets struct {
	LargeImage       string `json:"large_image,omitempty"`
	LargeText        string `json:"large_text,omitempty"`
	LargeURL         string `json:"large_url,omitempty"`
	SmallImage       string `json:"small_image,omitempty"`
	SmallText        string `json:"small_text,omitempty"`
	SmallURL         string `json:"small_url,omitempty"`
	InviteCoverImage string `json:"invite_cover_image,omitempty"`
}

type Button

type Button struct {
	Label string `json:"label"`
	URL   string `json:"url"`
}

type Client

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

func New

func New() *Client

func NewFrom

func NewFrom(ipc *ipc.Client) *Client

func (*Client) Close

func (c *Client) Close() error

func (*Client) Connect

func (c *Client) Connect() error

func (*Client) GetIPC

func (c *Client) GetIPC() *ipc.Client

Just giving this to allow advanced usage If you have any issues, after login create a goroutine that calls ipc.Read in a loop and logs the opcode and response data

func (*Client) Login

func (c *Client) Login(clientID string) (*ReadyEvent, error)

func (*Client) SetActivity

func (c *Client) SetActivity(args SetActivityArgs) error

Ignores errors if the rpc server returns one

func (*Client) WriteFrame

func (c *Client) WriteFrame(frame RequestFrame) error

Changes the nonce to a random uuid

type DiscordError

type DiscordError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

func (DiscordError) Error

func (d DiscordError) Error() string

type Handshake

type Handshake struct {
	Version  int    `json:"v"`
	ClientID string `json:"client_id"`
}

type Party

type Party struct {
	ID   string `json:"id,omitempty"`
	Size [2]int `json:"size,omitempty"`
}

type ReadyEvent

type ReadyEvent struct {
	Version int `json:"v"`
	Config  struct {
		CdnHost     string `json:"cdn_host"`
		ApiEndpoint string `json:"api_endpoint"`
		Environment string `json:"environment"`
	} `json:"config"`
	User ReadyUser `json:"user"`
}

type ReadyUser

type ReadyUser struct {
	ID                   string `json:"id"`
	Bot                  bool   `json:"bot"`
	Username             string `json:"username"`
	Discriminator        string `json:"discriminator"`
	GlobalName           string `json:"global_name"`
	Avatar               string `json:"avatar"`
	Flags                int    `json:"flags"`
	PremiumType          int    `json:"premium_type"`
	AvatarDecorationData *struct {
		Asset string `json:"asset"`
		SkuID string `json:"skuId"`
	} `json:"avatar_decoration_data,omitempty"`
}

type RequestFrame

type RequestFrame struct {
	Command string `json:"cmd"`
	Event   string `json:"evt,omitempty"`
	Args    any    `json:"args"`
	Nonce   string `json:"nonce"`
}

type ResponseFrame

type ResponseFrame struct {
	Command string          `json:"cmd"`
	Event   string          `json:"evt"`
	Data    json.RawMessage `json:"data"`
	Nonce   string          `json:"nonce"`
}

func ParseFrame

func ParseFrame(data []byte) (*ResponseFrame, error)

type Secrets

type Secrets struct {
	Join     string `json:"join,omitempty"`
	Spectate string `json:"spectate,omitempty"`
	Match    string `json:"match,omitempty"`
}

type SetActivityArgs

type SetActivityArgs struct {
	PID      int      `json:"pid"`
	Activity Activity `json:"activity"`
}

type Timestamps

type Timestamps struct {
	Start UnixTimestampMs `json:"start,omitempty"`
	End   UnixTimestampMs `json:"end,omitempty"`
}

type UnixTimestampMs

type UnixTimestampMs int64

Directories

Path Synopsis
examples
hello command

Jump to

Keyboard shortcuts

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