Skip to content

Commit 1833495

Browse files
committed
🎉 Initialize
0 parents  commit 1833495

File tree

7 files changed

+972
-0
lines changed

7 files changed

+972
-0
lines changed

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
[![stars](https://img.shields.io/github/stars/barbarbar338/go-lanyard?color=yellow&logo=github&style=for-the-badge)](https://github.com/barbarbar338/go-lanyard)
2+
[![license](https://img.shields.io/github/license/barbarbar338/go-lanyard?logo=github&style=for-the-badge)](https://github.com/barbarbar338/go-lanyard)
3+
[![supportServer](https://img.shields.io/discord/711995199945179187?color=7289DA&label=Support&logo=discord&style=for-the-badge)](https://discord.gg/BjEJFwh)
4+
[![forks](https://img.shields.io/github/forks/barbarbar338/go-lanyard?color=green&logo=github&style=for-the-badge)](https://github.com/barbarbar338/go-lanyard)
5+
[![issues](https://img.shields.io/github/issues/barbarbar338/go-lanyard?color=red&logo=github&style=for-the-badge)](https://github.com/barbarbar338/go-lanyard)
6+
7+
# 🚀 Go Lanyard
8+
9+
Use Lanyard API easily in your Go app!
10+
11+
# 📦 Installation
12+
13+
- Initialize your project (`go mod init example.com/example`)
14+
- Add package (`go get github.com/barbarbar338/go-lanyard`)
15+
16+
# 🤓 Usage
17+
18+
Using without websocket:
19+
20+
```golang
21+
package main
22+
23+
import (
24+
"fmt"
25+
)
26+
27+
func main() {
28+
// User ID here 👇
29+
res := FetchUser("331846231514939392")
30+
31+
// handle presence data here
32+
fmt.Println(res.Data.DiscordStatus)
33+
}
34+
```
35+
36+
Using with websocket:
37+
38+
```golang
39+
package main
40+
41+
import (
42+
"fmt"
43+
"os"
44+
"os/signal"
45+
"syscall"
46+
47+
"github.com/barbarbar338/go-lanyard"
48+
)
49+
50+
func main() {
51+
// User ID here 👇
52+
ws := lanyard.CreateWS("331846231514939392", func(data *LanyardData) {
53+
54+
// handle presence data here
55+
fmt.Println(data.DiscordStatus)
56+
})
57+
58+
sc := make(chan os.Signal, 1)
59+
signal.Notify(
60+
sc,
61+
syscall.SIGINT,
62+
syscall.SIGTERM,
63+
os.Interrupt,
64+
)
65+
<-sc
66+
67+
fmt.Println("Closing client.")
68+
69+
// destroy ws before exit
70+
ws.Destroy()
71+
}
72+
```
73+
74+
# 📄 License
75+
76+
Copyright © 2021 [Barış DEMİRCİ](https://github.com/barbarbar338).
77+
78+
Distributed under the [GPL-3.0](https://www.gnu.org/licenses/gpl-3.0.html) License. See `LICENSE` for more information.
79+
80+
# 🧦 Contributing
81+
82+
Feel free to use GitHub's features.
83+
84+
1. Fork the Project
85+
2. Create your Feature Branch (`git checkout -b feature/my-feature`)
86+
3. Commit your Changes (`git commit -m 'my awesome feature my-feature'`)
87+
4. Push to the Branch (`git push origin feature/my-feature`)
88+
5. Open a Pull Request
89+
90+
# 🔥 Show your support
91+
92+
Give a ⭐️ if this project helped you!
93+
94+
# 📞 Contact
95+
96+
97+
- Discord: https://discord.gg/BjEJFwh
98+
- Instagram: https://www.instagram.com/ben_baris.d/
99+
100+
# ✨ Special Thanks
101+
102+
- [Phineas](https://github.com/Phineas) - Creator of [Lanyard API](https://github.com/Phineas/lanyard)

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/barbarbar338/go-lanyard
2+
3+
go 1.16
4+
5+
require github.com/sacOO7/gowebsocket v0.0.0-20210515122958-9396f1a71e23

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
2+
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
3+
github.com/sacOO7/go-logger v0.0.0-20180719173527-9ac9add5a50d h1:5T+fbRuQbpi+WZtB2yfuu59r00F6T2HV/zGYrwX8nvE=
4+
github.com/sacOO7/go-logger v0.0.0-20180719173527-9ac9add5a50d/go.mod h1:L5EJe2k8GwpBoGXDRLAEs58R239jpZuE7NNEtW+T7oo=
5+
github.com/sacOO7/gowebsocket v0.0.0-20210515122958-9396f1a71e23 h1:yjnkNJTpQPCx10KF1jypuIhAVc6EYn2M9lJgwSTHQYs=
6+
github.com/sacOO7/gowebsocket v0.0.0-20210515122958-9396f1a71e23/go.mod h1:h00QywbM5Le22ESUiI8Yz2/9TVGD8eAz/cAk55Kcz/E=

rest.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package lanyard
2+
3+
import (
4+
"encoding/json"
5+
"io"
6+
"net/http"
7+
)
8+
9+
10+
const (
11+
API_URL = "https://api.lanyard.rest/v1/users/"
12+
)
13+
14+
func FetchUser(userId string) LanyardResponse {
15+
resp, err := http.Get(API_URL + userId)
16+
17+
if err != nil {
18+
panic(err)
19+
}
20+
21+
body, err := io.ReadAll(resp.Body)
22+
if err != nil {
23+
panic(err)
24+
}
25+
26+
var data LanyardResponse
27+
28+
err = json.Unmarshal(body, &data)
29+
if err != nil {
30+
panic(err)
31+
}
32+
33+
return data
34+
}

types.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package lanyard
2+
3+
// I'm not entirely sure if these are correct. You can review and open a PR before using it.
4+
// Go port of https://github.com/barbarbar338/react-use-lanyard/blob/main/src/types.ts
5+
type LanyardResponse struct {
6+
Success bool `json:"success"`
7+
Data *LanyardData `json:"data"`
8+
Error *LanyardError `json:"error"`
9+
}
10+
11+
type LanyardWSResponse struct {
12+
Op int `json:"op"`
13+
Seq int `json:"seq"`
14+
T string `json:"t"`
15+
D *LanyardData `json:"d"`
16+
}
17+
18+
19+
type LanyardData struct {
20+
Spotify Spotify `json:"spotify"`
21+
ListeningToSpotify bool `json:"listening_to_spotify"`
22+
DiscordUser DiscordUser `json:"discord_user"`
23+
DiscordStatus string `json:"discord_status"`
24+
Activities []Activity `json:"activities"`
25+
ActiveOnDiscordMobile bool `json:"active_on_discord_mobile"`
26+
ActiveOnDiscordDesktop bool `json:"active_on_discord_desktop"`
27+
}
28+
29+
type LanyardError struct {
30+
Message string `json:"message"`
31+
Code string `json:"code"`
32+
}
33+
34+
type Spotify struct {
35+
TrackId string `json:"track_id"`
36+
Timestamps Timestamps `json:"timestamps"`
37+
Song string `json:"song"`
38+
Artist string `json:"artist"`
39+
Album string `json:"album"`
40+
AlbumArtUrl string `json:"album_art_url"`
41+
}
42+
43+
type Timestamps struct {
44+
Start int `json:"start"`
45+
End int `json:"end"`
46+
}
47+
48+
type Activity struct {
49+
Type int `json:"type"`
50+
State string `json:"state"`
51+
Name string `json:"name"`
52+
Id string `json:"id"`
53+
Emoji *Emoji `json:"emoji"`
54+
CreatedAt int `json:"created_at"`
55+
ApplicationId string `json:"application_id"`
56+
Timestamps *Timestamps `json:"timestamps"`
57+
SessionId string `json:"session_id"`
58+
Details *string `json:"details"`
59+
Buttons *[]string `json:"buttons"`
60+
Assets *Assets `json:"assets"`
61+
}
62+
63+
type Assets struct {
64+
SmallText string `json:"small_text"`
65+
SmallImage string `json:"small_image"`
66+
LargeText string `json:"large_text"`
67+
LargeImage string `json:"large_image"`
68+
}
69+
70+
type Emoji struct {
71+
Name string `json:"name"`
72+
}
73+
74+
type DiscordUser struct {
75+
Id string `json:"id"`
76+
Username string `json:"username"`
77+
Avatar string `json:"avatar"`
78+
Discriminator string `json:"discriminator"`
79+
PublicFlags int `json:"public_flags"`
80+
}

websocket.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package lanyard
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
"strings"
7+
"time"
8+
9+
"github.com/sacOO7/gowebsocket"
10+
)
11+
12+
const (
13+
WS_URL = "wss://api.lanyard.rest/socket"
14+
PING_PERIOD = 30 * time.Second
15+
)
16+
17+
type WSClient struct {
18+
socket gowebsocket.Socket
19+
ticker *time.Ticker
20+
}
21+
22+
func (client WSClient) Destroy() {
23+
if client.ticker != nil {
24+
client.ticker.Stop()
25+
}
26+
client.socket.Close()
27+
}
28+
29+
func (client WSClient) ping() {
30+
client.ticker = time.NewTicker(PING_PERIOD)
31+
defer client.ticker.Stop()
32+
33+
for ; ; <-client.ticker.C {
34+
client.socket.SendText("{\"op\":3}")
35+
}
36+
}
37+
38+
func CreateWS(userId string, presenceUpdate func(data *LanyardData)) WSClient {
39+
client := WSClient {
40+
socket: gowebsocket.New(WS_URL),
41+
}
42+
43+
client.socket.OnConnected = func(socket gowebsocket.Socket) {
44+
client.socket.SendText("{\"op\":2,\"d\":{\"subscribe_to_id\":\"" + userId + "\"}}")
45+
go client.ping()
46+
};
47+
48+
client.socket.OnConnectError = func(err error, socket gowebsocket.Socket) {
49+
log.Println("An error occured while connecting to Lanyard websocket server", err)
50+
client.Destroy()
51+
};
52+
53+
client.socket.OnTextMessage = func(message string, socket gowebsocket.Socket) {
54+
if strings.Contains(message, "heartbeat_interval") {
55+
return
56+
}
57+
58+
var data LanyardWSResponse
59+
err := json.Unmarshal([]byte(message), &data)
60+
if err != nil {
61+
client.Destroy()
62+
return
63+
}
64+
65+
presenceUpdate(data.D)
66+
};
67+
68+
client.socket.Connect()
69+
70+
return client
71+
}

0 commit comments

Comments
 (0)