drpc

package module
v0.0.0-...-0a4e3a3 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2023 License: MIT Imports: 12 Imported by: 2

README

DRPC

GoDoc

Discord RPC implementation in Go.

$ go get -u github.com/altfoxie/drpc

Usage

You need to create a client.

client, err := drpc.New("APP_ID")
if err != nil { /* handle error */ }

Now you can use the client to set player's activity. DRPC maintains a persistent connection to Discord RPC, so you don't need to worry about reconnecting.

err := client.SetActivity(drpc.Activity{
    State: "Playing",
    Details: "Unranked PvP",
    Timestamps: &drpc.Timestamps{
        Start: time.Now(),
    },
    // Other fields...
})
if err != nil { /* handle error */ }

Roadmap

  • Support Windows
  • Support events
  • Match responses with method calls by nonce

License

MIT

Documentation

Overview

Package drpc implements the Discord RPC protocol.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConnFailed = errors.New("drpc: connection failed")
)

Functions

This section is empty.

Types

type Activity

type Activity struct {
	// What the player is currently doing.
	Details string `json:"details,omitempty"`
	// The user's current state.
	State string `json:"state,omitempty"`
	// Timestamps for the start and end of the activity.
	Timestamps *Timestamps `json:"timestamps,omitempty"`
	// Profile artworks.
	Assets *Assets `json:"assets,omitempty"`
	// Party information.
	Party *Party `json:"party,omitempty"`
	// Secrets data. It cannot be used with the Buttons field.
	Secrets *Secrets `json:"secrets,omitempty"`
	// Buttons to display. It cannot be used with the Secrets field.
	Buttons []Button `json:"buttons,omitempty"`
}

Activity describes an user's current activity.

type Assets

type Assets struct {
	// Name of the uploaded image for the large profile artwork.
	LargeImage string `json:"large_image,omitempty"`
	// Tooltip for the large profile artwork.
	LargeText string `json:"large_text,omitempty"`
	// Name of the uploaded image for the small profile artwork.
	SmallImage string `json:"small_image,omitempty"`
	// Tooltip for the small profile artwork.
	SmallText string `json:"small_text,omitempty"`
}

Assets contains an user's profile artwork.

type Button

type Button struct {
	// Name of the button.
	Label string `json:"label,omitempty"`
	// URL to open when the button is clicked.
	URL string `json:"url,omitempty"`
}

Button contains a button data. Both fields are required.

type Client

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

Client is a Discord RPC client.

func New

func New(id string) (*Client, error)

New creates a new Discord RPC client with the given application ID.

The application ID given must not be empty.

func (*Client) Close

func (c *Client) Close() error

Close closes the client connection.

func (*Client) Connect

func (c *Client) Connect() (err error)

Connect connects the client to the Discord RPC server and sends a handshake. It returns nil if the client is already connected.

func (*Client) Read

func (c *Client) Read() (*Message, error)

Read reads a message from the connection. It automatically reconnects if the connection is closed.

func (*Client) SetActivity

func (c *Client) SetActivity(activity Activity) (err error)

SetActivity sets the player's activity.

func (*Client) Write

func (c *Client) Write(opcode Opcode, payload interface{}) error

Write writes a message to the connection. It automatically reconnects if the connection is closed.

type FrameHeader

type FrameHeader struct {
	Command string `json:"cmd"`
	Event   string `json:"evt,omitempty"`
	Nonce   string `json:"nonce,omitempty"`
}

FrameHeader is a header for a frame.

func NewFrameHeader

func NewFrameHeader(command string) FrameHeader

NewFrameHeader creates a new frame header with the given command.

func NewFrameHeaderWithEvent

func NewFrameHeaderWithEvent(command, event string) FrameHeader

NewFrameHeaderWithEvent creates a new frame header with the given command and event.

type FrameSetActivity

type FrameSetActivity struct {
	FrameHeader
	Args FrameSetActivityArgs `json:"args"`
}

FrameSetActivity is a frame of the SetActivity message.

type FrameSetActivityArgs

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

FrameSetActivityArgs is a set of arguments for the SetActivity message.

type Handshake

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

Handshake is a handshake message.

type Message

type Message struct {
	Opcode
	Payload []byte
}

Message describes structure of a message.

type Opcode

type Opcode = uint32
const (
	OpHandshake Opcode = iota
	OpFrame
	OpClose
	OpPing
	OpPong
)

type Party

type Party struct {
	// ID of the player's party, lobby, or group.
	ID string `json:"id,omitempty"`
	// The first element is the current party size, the second element is the max party size.
	Size [2]int `json:"size,omitempty"`
}

Party contains party information.

type Secrets

type Secrets struct {
	// Unique hashed string for chat invitations and Ask to Join.
	Join string `json:"join,omitempty"`
	// Unique hashed string for Spectate button.
	Spectate string `json:"spectate,omitempty"`
	// (For future use) Unique hashed string for a player's match.
	Match string `json:"match,omitempty"`
}

Secrets contains secrets for joining and spectating.

type Timestamps

type Timestamps struct {
	// Epoch seconds for game start - including will show time as "elapsed".
	Start time.Time `json:"start,omitempty"`
	// Epoch seconds for game end - including will show time as "remaining".
	End time.Time `json:"end,omitempty"`
}

Timestamps contains timestamps for the start and end of the activity.

Sending End timestamp will always have the time displayed as "remaining" until the given time. Sending Start timestamp will show "elapsed" as long as there is no End sent.

func (Timestamps) MarshalJSON

func (t Timestamps) MarshalJSON() ([]byte, error)

Custom marshal method for Timestamps. It is required because protocol accepts only epoch seconds.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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