-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
110 lines (83 loc) · 1.63 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package client
import (
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"os"
"github.com/bartick/discordrpc-go/ipc"
code "github.com/bartick/discordrpc-go/opcode"
)
type RPC struct {
ClientID string
RPCInterface
}
var logged bool = false
func (conn *RPC) Login() (string, error) {
if logged {
return "", nil
}
payload, err := json.Marshal(Handshake{RPCVersion1, conn.ClientID})
if err != nil {
return "", err
}
err = ipc.OpenSocket()
if err != nil {
return "", err
}
res := ipc.Send(code.DISPATCH, string(payload))
logged = true
return res, nil
}
func (conn *RPC) Logout() error {
if !logged {
return nil
}
logged = false
err := ipc.CloseSocket()
if err != nil {
return err
}
return nil
}
func (conn *RPC) SetActivity(activity Activity) (string, error) {
if !logged {
return "", errors.New("not logged in to discord client")
}
payload, err := json.Marshal(Frame{
Cmd: SET_ACTIVITY,
Args: Args{
Pid: os.Getpid(),
Activity: &activity,
},
Nonce: getNonce(),
})
if err != nil {
return "", err
}
res := ipc.Send(code.HEARTBEAT, string(payload))
return res, nil
}
func (conn *RPC) ClearActivity() (string, error) {
payload, err := json.Marshal(Frame{
Cmd: SET_ACTIVITY,
Args: Args{
Pid: os.Getpid(),
},
Nonce: getNonce(),
})
if err != nil {
return "", err
}
res := ipc.Send(code.HEARTBEAT, string(payload))
return res, nil
}
func getNonce() string {
buf := make([]byte, 16)
_, err := rand.Read(buf)
if err != nil {
fmt.Println(err)
}
buf[6] = (buf[6] & 0x0f) | 0x40
return fmt.Sprintf("%x-%x-%x-%x-%x", buf[0:4], buf[4:6], buf[6:8], buf[8:10], buf[10:])
}