lanyard

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2022 License: GPL-3.0 Imports: 8 Imported by: 0

README

stars license supportServer forks issues

🚀 Go Lanyard

Use Lanyard API easily in your Go app!

📦 Installation

  • Initialize your project (go mod init example.com/example)
  • Add package (go get github.com/barbarbar338/go-lanyard)

🤓 Usage

Using without websocket:

package main

import (
	"fmt"

	"github.com/barbarbar338/go-lanyard"
)

func main() {
	//                              User ID here 👇
	res, err := lanyard.FetchUser("331846231514939392")

	// Handle error
	if err != nil {
		// ...
		panic(err)
	}

	// Handle presence data here
	fmt.Println(res.Data.DiscordStatus)
}

Using with websocket:

package main

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"

	"github.com/barbarbar338/go-lanyard"
)

func main() {
	//                          User ID here 👇
	ws := lanyard.ListenUser("331846231514939392", func(data *lanyard.LanyardData) {
		// Handle presence data here
		fmt.Println(data.DiscordStatus)
	})

	sc := make(chan os.Signal, 1)
	signal.Notify(
		sc,
		syscall.SIGINT,
		syscall.SIGTERM,
		os.Interrupt,
	)
	<-sc

	fmt.Println("Closing client.")

	// Destroy WS before exit
	ws.Destroy()
}

Using websocket for watching multiple users:

package main

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"

	"github.com/barbarbar338/go-lanyard"
)

func main() {
	//                                                     User IDs here 👇
	ws := lanyard.ListenMultipleUsers([]string{"866849747603816468", "331846231514939392"}, func(data *lanyard.LanyardData) {
		fmt.Println(data.DiscordStatus)
	})

	sc := make(chan os.Signal, 1)
	signal.Notify(
		sc,
		syscall.SIGINT,
		syscall.SIGTERM,
		os.Interrupt,
	)
	<-sc

	fmt.Println("Closing client.")

	// Destroy WS before exit
	ws.Destroy()
}

📄 License

Copyright © 2021 Barış DEMİRCİ.

Distributed under the GPL-3.0 License. See LICENSE for more information.

🧦 Contributing

Feel free to use GitHub's features.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/my-feature)
  3. Commit your Changes (git commit -m 'my awesome feature my-feature')
  4. Push to the Branch (git push origin feature/my-feature)
  5. Open a Pull Request

🔥 Show your support

Give a ⭐️ if this project helped you!

📞 Contact

✨ Special Thanks

Documentation

Index

Constants

View Source
const (
	API_URL = "https://api.lanyard.rest/v1/users/"
)
View Source
const (
	PING_PERIOD = 30 * time.Second
)
View Source
const (
	WS_URL = "wss://api.lanyard.rest/socket"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Activity

type Activity struct {
	Type          int         `json:"type"`
	State         string      `json:"state"`
	Name          string      `json:"name"`
	Id            string      `json:"id"`
	Emoji         *Emoji      `json:"emoji"`
	CreatedAt     int         `json:"created_at"`
	ApplicationId string      `json:"application_id"`
	Timestamps    *Timestamps `json:"timestamps"`
	SessionId     string      `json:"session_id"`
	Details       *string     `json:"details"`
	Buttons       *[]string   `json:"buttons"`
	Assets        *Assets     `json:"assets"`
}

type Assets

type Assets struct {
	SmallText  string `json:"small_text"`
	SmallImage string `json:"small_image"`
	LargeText  string `json:"large_text"`
	LargeImage string `json:"large_image"`
}

type DiscordUser

type DiscordUser struct {
	Id            string `json:"id"`
	Username      string `json:"username"`
	Avatar        string `json:"avatar"`
	Discriminator string `json:"discriminator"`
	PublicFlags   int    `json:"public_flags"`
}

type Emoji

type Emoji struct {
	Name string `json:"name"`
}

type LanyardData

type LanyardData struct {
	Spotify                Spotify           `json:"spotify"`
	ListeningToSpotify     bool              `json:"listening_to_spotify"`
	DiscordUser            DiscordUser       `json:"discord_user"`
	DiscordStatus          string            `json:"discord_status"`
	Kv                     map[string]string `json:"kv"`
	Activities             []Activity        `json:"activities"`
	ActiveOnDiscordMobile  bool              `json:"active_on_discord_mobile"`
	ActiveOnDiscordDesktop bool              `json:"active_on_discord_desktop"`
}

type LanyardError

type LanyardError struct {
	Message string `json:"message"`
	Code    string `json:"code"`
}

type LanyardResponse

type LanyardResponse struct {
	Success bool          `json:"success"`
	Data    *LanyardData  `json:"data"`
	Error   *LanyardError `json:"error"`
}

I'm not entirely sure if these are correct. You can review and open a PR before using it. Go port of https://github.com/barbarbar338/react-use-lanyard/blob/main/src/types.ts

func FetchUser

func FetchUser(userId string) (LanyardResponse, error)

type LanyardWSResponse

type LanyardWSResponse struct {
	Op  int          `json:"op"`
	Seq int          `json:"seq"`
	T   string       `json:"t"`
	D   *LanyardData `json:"d"`
}

type Spotify

type Spotify struct {
	TrackId     string     `json:"track_id"`
	Timestamps  Timestamps `json:"timestamps"`
	Song        string     `json:"song"`
	Artist      string     `json:"artist"`
	Album       string     `json:"album"`
	AlbumArtUrl string     `json:"album_art_url"`
}

type Timestamps

type Timestamps struct {
	Start int `json:"start"`
	End   int `json:"end"`
}

type WSClient

type WSClient struct {
	// contains filtered or unexported fields
}

func ListenMultipleUsers added in v0.2.0

func ListenMultipleUsers(userIds []string, presenceUpdate func(data *LanyardData)) WSClient

func ListenUser added in v0.2.0

func ListenUser(userId string, presenceUpdate func(data *LanyardData)) WSClient

func (WSClient) Destroy

func (client WSClient) Destroy()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL