diff --git a/README.md b/README.md index c76554b..7b8f274 100644 --- a/README.md +++ b/README.md @@ -12,19 +12,45 @@ $ go get github.com/ananagame/rich-go ## Usage -Login by sending the first handshake : -```crystal -client.Login("your_client_id") +First of all import rich-go +```golang +import "github.com/ananagame/rich-go/client" ``` -And you can set the Rich Presence activity (parameters can be found : -```crystal -client.SetActivity(client.Activity{ - State: "hey", - Details: "i'm running on go", - }) +then login by sending the first handshake +```golang +err := client.Login("DISCORD_APP_ID") +if err != nil { + panic(err) +} ``` +and you can set the Rich Presence activity (parameters can be found : +```golang +err = client.SetActivity(client.Activity{ + State: "Heyy!!!", + Details: "I'm running on rich-go :)", + LargeImage: "largeimageid", + LargeText: "This is the large image :D", + SmallImage: "smallimageid", + SmallText: "And this is the small image", + Party: &client.Party{ + ID: "-1", + Players: 15, + MaxPlayers: 24, + }, + Timestamps: &client.Timestamps{ + Start: time.Now().UnixNano() / 1e6, + }, +}) + +if err != nil { + panic(err) +} +``` + +More details in the [example](https://github.com/ananagame/rich-go/blob/master/example/main.go) + ## Contributing 1. Fork it (https://github.com/ananagame/rich-go/fork) @@ -35,6 +61,6 @@ client.SetActivity(client.Activity{ ## Contributors -- [AnanaGame](https://github.com/ananagame) - creator, maintainer +- [ananagame](https://github.com/ananagame) - creator, maintainer - [donovansolms](https://github.com/donovansolms) - contributor - [heroslender](https://github.com/heroslender) - contributor diff --git a/client/client.go b/client/client.go index 9624440..a03f370 100644 --- a/client/client.go +++ b/client/client.go @@ -9,10 +9,11 @@ import ( "github.com/ananagame/rich-go/ipc" ) -var isLoggedIn bool +var logged bool +// Login sends a handshake in the socket and returns an error or nil func Login(clientid string) error { - if isLoggedIn == false { + if !logged { payload, err := json.Marshal(Handshake{"1", clientid}) if err != nil { return err @@ -26,18 +27,22 @@ func Login(clientid string) error { // TODO: Response should be parsed ipc.Send(0, string(payload)) } - isLoggedIn = true + logged = true return nil } func Logout() { - isLoggedIn = false - ipc.CloseSocket() + logged = false + + err := ipc.CloseSocket() + if err != nil { + panic(err) + } } func SetActivity(activity Activity) error { - if isLoggedIn == false { + if !logged { return nil } @@ -49,6 +54,7 @@ func SetActivity(activity Activity) error { }, getNonce(), }) + if err != nil { return err } diff --git a/client/inputMapper.go b/client/inputMapper.go index 98d40bb..c3a0cc1 100644 --- a/client/inputMapper.go +++ b/client/inputMapper.go @@ -51,6 +51,7 @@ type Secrets struct { } func mapActivity(activity *Activity) *PayloadActivity { + // Replace empty fields by "none" if activity.LargeImage == "" { activity.LargeImage = "none" } diff --git a/example/main.go b/example/main.go index 9f6df0e..436b369 100644 --- a/example/main.go +++ b/example/main.go @@ -2,25 +2,23 @@ package main import ( "fmt" - "time" - "github.com/ananagame/rich-go/client" + "time" ) func main() { - - err := client.Login("YOUR_DISCORD_APP_ID") + err := client.Login("DISCORD_APP_ID") if err != nil { panic(err) } err = client.SetActivity(client.Activity{ - State: "hey", - Details: "i'm running on go", - LargeImage: "Unknown", // TODO: Add image - LargeText: "None", // TODO: Add image alt - SmallImage: "Unknown", // TODO: Add image - SmallText: "NoneSmall", // TODO: Add image alt + State: "Heyy!!!", + Details: "I'm running on rich-go :)", + LargeImage: "largeimageid", + LargeText: "This is the large image :D", + SmallImage: "smallimageid", + SmallText: "And this is the small image", Party: &client.Party{ ID: "-1", Players: 15, diff --git a/ipc/ipc_windows.go b/ipc/ipc_windows.go index 64381af..a49e4e5 100644 --- a/ipc/ipc_windows.go +++ b/ipc/ipc_windows.go @@ -3,14 +3,14 @@ package ipc import ( - "time" npipe "gopkg.in/natefinch/npipe.v2" + "time" ) // OpenSocket opens the discord-ipc-0 named pipe func OpenSocket() error { // Connect to the Windows named pipe, this is a well known name - // We use DialTimeout since it will block forever (or very very long) on Windows + // We use DialTimeout since it will block forever (or very very long) on Windows // if the pipe is not available (Discord not running) sock, err := npipe.DialTimeout(`\\.\pipe\discord-ipc-0`, time.Second*2) if err != nil {