vendor/github.com/tr1xem/go-discordrpc/client/client.go

119 lines · 2569 bytes

  1package client
  2
  3import (
  4	"crypto/rand"
  5	"encoding/json"
  6	"fmt"
  7	"os"
  8
  9	"github.com/tr1xem/go-discordrpc/internal/ipc"
 10)
 11
 12// Client represents a single Discord RPC connection
 13type Client struct {
 14	ClientID string
 15	loggedIn bool
 16	socket   ipc.SocketInterface
 17}
 18
 19// NewClient creates a new Discord RPC client instance
 20func NewClient(clientID string) *Client {
 21	var sock ipc.SocketInterface
 22
 23	sock = ipc.DefaultSocket()
 24
 25	return &Client{
 26		ClientID: clientID,
 27		socket:   sock,
 28	}
 29}
 30
 31// Login performs a handshake with Discord and opens the IPC socket.
 32func (c *Client) Login() error {
 33	if c.loggedIn {
 34		return nil
 35	}
 36
 37	payload, err := json.Marshal(Handshake{"1", c.ClientID})
 38	if err != nil {
 39		return fmt.Errorf("failed to marshal handshake: %w", err)
 40	}
 41
 42	if err := c.socket.Open(); err != nil {
 43		return fmt.Errorf("failed to open IPC socket: %w", err)
 44	}
 45
 46	// Send handshake and handle response
 47	response, err := c.socket.Send(0, string(payload))
 48	if err != nil {
 49		return fmt.Errorf("failed to send handshake: %w", err)
 50	}
 51	if len(response) == 0 {
 52		return fmt.Errorf("empty response from Discord handshake")
 53	}
 54
 55	c.loggedIn = true
 56	return nil
 57}
 58
 59// Logout closes the IPC socket and marks the client as logged out.
 60func (c *Client) Logout() error {
 61	if !c.loggedIn {
 62		return nil
 63	}
 64
 65	if err := c.socket.Close(); err != nil {
 66		return fmt.Errorf("failed to close IPC socket: %w", err)
 67	}
 68
 69	c.loggedIn = false
 70	return nil
 71}
 72
 73// SetActivity updates the Discord Rich Presence activity.
 74func (c *Client) SetActivity(activity Activity) error {
 75	if !c.loggedIn {
 76		return fmt.Errorf("client is not logged in")
 77	}
 78
 79	nonce, err := generateNonce()
 80	if err != nil {
 81		return fmt.Errorf("failed to generate nonce: %w", err)
 82	}
 83
 84	payload, err := json.Marshal(Frame{
 85		"SET_ACTIVITY",
 86		Args{
 87			os.Getpid(),
 88			mapActivity(&activity),
 89		},
 90		nonce,
 91	})
 92	if err != nil {
 93		return fmt.Errorf("failed to marshal activity frame: %w", err)
 94	}
 95
 96	// Send activity frame and handle response
 97	response, err := c.socket.Send(1, string(payload))
 98	if err != nil {
 99		return fmt.Errorf("failed to send activity frame: %w", err)
100	}
101	if len(response) == 0 {
102		return fmt.Errorf("empty response from Discord activity")
103	}
104
105	return nil
106}
107
108// generateNonce creates a unique nonce for Discord RPC requests.
109func generateNonce() (string, error) {
110	buf := make([]byte, 16)
111	if _, err := rand.Read(buf); err != nil {
112		return "", err
113	}
114
115	// set version bits for UUIDv4
116	buf[6] = (buf[6] & 0x0f) | 0x40
117
118	return fmt.Sprintf("%x-%x-%x-%x-%x", buf[0:4], buf[4:6], buf[6:8], buf[8:10], buf[10:]), nil
119}