Skip to content

Commit

Permalink
Some improvements about code style
Browse files Browse the repository at this point in the history
  • Loading branch information
hugolgst committed Jun 11, 2019
1 parent 32f39c0 commit 43993a0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 28 deletions.
46 changes: 36 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
18 changes: 12 additions & 6 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -49,6 +54,7 @@ func SetActivity(activity Activity) error {
},
getNonce(),
})

if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions client/inputMapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Secrets struct {
}

func mapActivity(activity *Activity) *PayloadActivity {
// Replace empty fields by "none"
if activity.LargeImage == "" {
activity.LargeImage = "none"
}
Expand Down
18 changes: 8 additions & 10 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions ipc/ipc_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 43993a0

Please sign in to comment.