Add changes to presences, remove Game type

This commit is contained in:
Carson Hoffman 2021-01-20 18:54:38 -05:00
parent 866ecccb2e
commit c41dc15a10
No known key found for this signature in database
GPG key ID: 05B660CB452C657F
5 changed files with 33 additions and 78 deletions

View file

@ -125,7 +125,7 @@ func TestOpenClose(t *testing.T) {
// UpdateStatus - maybe we move this into wsapi_test.go but the websocket // UpdateStatus - maybe we move this into wsapi_test.go but the websocket
// created here is needed. This helps tests that the websocket was setup // created here is needed. This helps tests that the websocket was setup
// and it is working. // and it is working.
if err = d.UpdateStatus(0, time.Now().String()); err != nil { if err = d.UpdateGameStatus(0, time.Now().String()); err != nil {
t.Errorf("UpdateStatus error: %+v", err) t.Errorf("UpdateStatus error: %+v", err)
} }

View file

@ -196,8 +196,7 @@ type PresencesReplace []*Presence
// PresenceUpdate is the data for a PresenceUpdate event. // PresenceUpdate is the data for a PresenceUpdate event.
type PresenceUpdate struct { type PresenceUpdate struct {
Presence Presence
GuildID string `json:"guild_id"` GuildID string `json:"guild_id"`
Roles []string `json:"roles"`
} }
// Resumed is the data for a Resumed event. // Resumed is the data for a Resumed event.

View file

@ -200,14 +200,10 @@ func (s *State) PresenceAdd(guildID string, presence *Presence) error {
//guild.Presences[i] = presence //guild.Presences[i] = presence
//Update status //Update status
guild.Presences[i].Game = presence.Game guild.Presences[i].Activities = presence.Activities
guild.Presences[i].Roles = presence.Roles
if presence.Status != "" { if presence.Status != "" {
guild.Presences[i].Status = presence.Status guild.Presences[i].Status = presence.Status
} }
if presence.Nick != "" {
guild.Presences[i].Nick = presence.Nick
}
//Update the optionally sent user information //Update the optionally sent user information
//ID Is a mandatory field so you should not need to check if it is empty //ID Is a mandatory field so you should not need to check if it is empty
@ -966,24 +962,12 @@ func (s *State) OnInterface(se *Session, i interface{}) (err error) {
// Member not found; this is a user coming online // Member not found; this is a user coming online
m = &Member{ m = &Member{
GuildID: t.GuildID, GuildID: t.GuildID,
Nick: t.Nick,
User: t.User, User: t.User,
Roles: t.Roles,
} }
} else { } else {
if t.Nick != "" {
m.Nick = t.Nick
}
if t.User.Username != "" { if t.User.Username != "" {
m.User.Username = t.User.Username m.User.Username = t.User.Username
} }
// PresenceUpdates always contain a list of roles, so there's no need to check for an empty list here
m.Roles = t.Roles
} }
err = s.MemberAdd(m) err = s.MemberAdd(m)

View file

@ -14,6 +14,7 @@ package discordgo
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"math"
"net/http" "net/http"
"strings" "strings"
"sync" "sync"
@ -692,39 +693,10 @@ type VoiceState struct {
// A Presence stores the online, offline, or idle and game status of Guild members. // A Presence stores the online, offline, or idle and game status of Guild members.
type Presence struct { type Presence struct {
User *User `json:"user"` User *User `json:"user"`
Status Status `json:"status"` Status Status `json:"status"`
Game *Game `json:"game"` Activities []*Activity `json:"activities"`
Activities []*Game `json:"activities"` Since *int `json:"since"`
Nick string `json:"nick"`
Roles []string `json:"roles"`
Since *int `json:"since"`
}
// GameType is the type of "game" (see GameType* consts) in the Game struct
type GameType int
// Valid GameType values
const (
GameTypeGame GameType = iota
GameTypeStreaming
GameTypeListening
GameTypeWatching
GameTypeCustom
)
// A Game struct holds the name of the "playing .." game for a user
type Game struct {
Name string `json:"name"`
Type GameType `json:"type"`
URL string `json:"url,omitempty"`
Details string `json:"details,omitempty"`
State string `json:"state,omitempty"`
TimeStamps TimeStamps `json:"timestamps,omitempty"`
Assets Assets `json:"assets,omitempty"`
ApplicationID string `json:"application_id,omitempty"`
Instance int8 `json:"instance,omitempty"`
// TODO: Party and Secrets (unknown structure)
} }
// A TimeStamps struct contains start and end times used in the rich presence "playing .." Game // A TimeStamps struct contains start and end times used in the rich presence "playing .." Game
@ -1153,7 +1125,7 @@ type ActivityType int
// Valid ActivityType values // Valid ActivityType values
const ( const (
ActivityTypeGame GameType = iota ActivityTypeGame ActivityType = iota
ActivityTypeStreaming ActivityTypeStreaming
ActivityTypeListening ActivityTypeListening
// ActivityTypeWatching // not valid in this use case? // ActivityTypeWatching // not valid in this use case?

View file

@ -322,10 +322,10 @@ func (s *Session) heartbeat(wsConn *websocket.Conn, listening <-chan interface{}
// UpdateStatusData ia provided to UpdateStatusComplex() // UpdateStatusData ia provided to UpdateStatusComplex()
type UpdateStatusData struct { type UpdateStatusData struct {
IdleSince *int `json:"since"` IdleSince *int `json:"since"`
Game *Game `json:"game"` Activities []*Activity `json:"activities"`
AFK bool `json:"afk"` AFK bool `json:"afk"`
Status string `json:"status"` Status string `json:"status"`
} }
type updateStatusOp struct { type updateStatusOp struct {
@ -333,7 +333,7 @@ type updateStatusOp struct {
Data UpdateStatusData `json:"d"` Data UpdateStatusData `json:"d"`
} }
func newUpdateStatusData(idle int, gameType GameType, game, url string) *UpdateStatusData { func newUpdateStatusData(idle int, activityType ActivityType, name, url string) *UpdateStatusData {
usd := &UpdateStatusData{ usd := &UpdateStatusData{
Status: "online", Status: "online",
} }
@ -342,12 +342,12 @@ func newUpdateStatusData(idle int, gameType GameType, game, url string) *UpdateS
usd.IdleSince = &idle usd.IdleSince = &idle
} }
if game != "" { if name != "" {
usd.Game = &Game{ usd.Activities = []*Activity{{
Name: game, Name: name,
Type: gameType, Type: activityType,
URL: url, URL: url,
} }}
} }
return usd return usd
@ -355,30 +355,30 @@ func newUpdateStatusData(idle int, gameType GameType, game, url string) *UpdateS
// UpdateStatus is used to update the user's status. // UpdateStatus is used to update the user's status.
// If idle>0 then set status to idle. // If idle>0 then set status to idle.
// If game!="" then set game. // If name!="" then set game.
// if otherwise, set status to active, and no game. // if otherwise, set status to active, and no activity.
func (s *Session) UpdateStatus(idle int, game string) (err error) { func (s *Session) UpdateGameStatus(idle int, name string) (err error) {
return s.UpdateStatusComplex(*newUpdateStatusData(idle, GameTypeGame, game, "")) return s.UpdateStatusComplex(*newUpdateStatusData(idle, ActivityTypeGame, name, ""))
} }
// UpdateStreamingStatus is used to update the user's streaming status. // UpdateStreamingStatus is used to update the user's streaming status.
// If idle>0 then set status to idle. // If idle>0 then set status to idle.
// If game!="" then set game. // If name!="" then set game.
// If game!="" and url!="" then set the status type to streaming with the URL set. // If name!="" and url!="" then set the status type to streaming with the URL set.
// if otherwise, set status to active, and no game. // if otherwise, set status to active, and no game.
func (s *Session) UpdateStreamingStatus(idle int, game string, url string) (err error) { func (s *Session) UpdateStreamingStatus(idle int, name string, url string) (err error) {
gameType := GameTypeGame gameType := ActivityTypeGame
if url != "" { if url != "" {
gameType = GameTypeStreaming gameType = ActivityTypeStreaming
} }
return s.UpdateStatusComplex(*newUpdateStatusData(idle, gameType, game, url)) return s.UpdateStatusComplex(*newUpdateStatusData(idle, gameType, name, url))
} }
// UpdateListeningStatus is used to set the user to "Listening to..." // UpdateListeningStatus is used to set the user to "Listening to..."
// If game!="" then set to what user is listening to // If name!="" then set to what user is listening to
// Else, set user to active and no game. // Else, set user to active and no activity.
func (s *Session) UpdateListeningStatus(game string) (err error) { func (s *Session) UpdateListeningStatus(name string) (err error) {
return s.UpdateStatusComplex(*newUpdateStatusData(0, GameTypeListening, game, "")) return s.UpdateStatusComplex(*newUpdateStatusData(0, ActivityTypeListening, name, ""))
} }
// UpdateStatusComplex allows for sending the raw status update data untouched by discordgo. // UpdateStatusComplex allows for sending the raw status update data untouched by discordgo.