vendor/github.com/tr1xem/go-discordrpc/client/inputMapper.go

121 lines · 3142 bytes

  1package client
  2
  3import (
  4	"time"
  5)
  6
  7// Activity holds the data for discord rich presence
  8type Activity struct {
  9	// Activity Type; 0=Playing, 1=Streaming, 2=Listening, 3=Watching
 10	Type int
 11	// What the player is currently doing
 12	Name string
 13	// What the player is currently doing
 14	Details string
 15	// The user's current party status
 16	State string
 17	// The id for a large asset of the activity, usually a snowflake
 18	LargeImage string
 19	// Text displayed when hovering over the large image of the activity
 20	LargeText string
 21	// The id for a small asset of the activity, usually a snowflake
 22	SmallImage string
 23	// Text displayed when hovering over the small image of the activity
 24	SmallText string
 25	// Information for the current party of the player
 26	Party *Party
 27	// Unix timestamps for start and/or end of the game
 28	Timestamps *Timestamps
 29	// Secrets for Rich Presence joining and spectating
 30	Secrets *Secrets
 31	// Clickable buttons that open a URL in the browser
 32	Buttons []*Button
 33}
 34
 35// Button holds a label and the corresponding URL that is opened on press
 36type Button struct {
 37	// The label of the button
 38	Label string
 39	// The URL of the button
 40	Url string
 41}
 42
 43// Party holds information for the current party of the player
 44type Party struct {
 45	// The ID of the party
 46	ID string
 47	// Used to show the party's current size
 48	Players int
 49	// Used to show the party's maximum size
 50	MaxPlayers int
 51}
 52
 53// Timestamps holds unix timestamps for start and/or end of the game
 54type Timestamps struct {
 55	// unix time (in milliseconds) of when the activity started
 56	Start *time.Time
 57	// unix time (in milliseconds) of when the activity ends
 58	End *time.Time
 59}
 60
 61// Secrets holds secrets for Rich Presence joining and spectating
 62type Secrets struct {
 63	// The secret for a specific instanced match
 64	Match string
 65	// The secret for joining a party
 66	Join string
 67	// The secret for spectating a game
 68	Spectate string
 69}
 70
 71func mapActivity(activity *Activity) *PayloadActivity {
 72	final := &PayloadActivity{
 73		Name:    activity.Name,
 74		Details: activity.Details,
 75		State:   activity.State,
 76		Assets: PayloadAssets{
 77			LargeImage: activity.LargeImage,
 78			LargeText:  activity.LargeText,
 79			SmallImage: activity.SmallImage,
 80			SmallText:  activity.SmallText,
 81		},
 82		Type: activity.Type,
 83	}
 84
 85	if activity.Timestamps != nil && activity.Timestamps.Start != nil {
 86		start := uint64(activity.Timestamps.Start.UnixNano() / 1e6)
 87		final.Timestamps = &PayloadTimestamps{
 88			Start: &start,
 89		}
 90		if activity.Timestamps.End != nil {
 91			end := uint64(activity.Timestamps.End.UnixNano() / 1e6)
 92			final.Timestamps.End = &end
 93		}
 94	}
 95
 96	if activity.Party != nil {
 97		final.Party = &PayloadParty{
 98			ID:   activity.Party.ID,
 99			Size: [2]int{activity.Party.Players, activity.Party.MaxPlayers},
100		}
101	}
102
103	if activity.Secrets != nil {
104		final.Secrets = &PayloadSecrets{
105			Join:     activity.Secrets.Join,
106			Match:    activity.Secrets.Match,
107			Spectate: activity.Secrets.Spectate,
108		}
109	}
110
111	if len(activity.Buttons) > 0 {
112		for _, btn := range activity.Buttons {
113			final.Buttons = append(final.Buttons, &PayloadButton{
114				Label: btn.Label,
115				Url:   btn.Url,
116			})
117		}
118	}
119
120	return final
121}