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
// created here is needed. This helps tests that the websocket was setup
// 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)
}

View file

@ -196,8 +196,7 @@ type PresencesReplace []*Presence
// PresenceUpdate is the data for a PresenceUpdate event.
type PresenceUpdate struct {
Presence
GuildID string `json:"guild_id"`
Roles []string `json:"roles"`
GuildID string `json:"guild_id"`
}
// 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
//Update status
guild.Presences[i].Game = presence.Game
guild.Presences[i].Roles = presence.Roles
guild.Presences[i].Activities = presence.Activities
if presence.Status != "" {
guild.Presences[i].Status = presence.Status
}
if presence.Nick != "" {
guild.Presences[i].Nick = presence.Nick
}
//Update the optionally sent user information
//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
m = &Member{
GuildID: t.GuildID,
Nick: t.Nick,
User: t.User,
Roles: t.Roles,
}
} else {
if t.Nick != "" {
m.Nick = t.Nick
}
if 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)

View file

@ -14,6 +14,7 @@ package discordgo
import (
"encoding/json"
"fmt"
"math"
"net/http"
"strings"
"sync"
@ -692,39 +693,10 @@ type VoiceState struct {
// A Presence stores the online, offline, or idle and game status of Guild members.
type Presence struct {
User *User `json:"user"`
Status Status `json:"status"`
Game *Game `json:"game"`
Activities []*Game `json:"activities"`
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)
User *User `json:"user"`
Status Status `json:"status"`
Activities []*Activity `json:"activities"`
Since *int `json:"since"`
}
// 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
const (
ActivityTypeGame GameType = iota
ActivityTypeGame ActivityType = iota
ActivityTypeStreaming
ActivityTypeListening
// 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()
type UpdateStatusData struct {
IdleSince *int `json:"since"`
Game *Game `json:"game"`
AFK bool `json:"afk"`
Status string `json:"status"`
IdleSince *int `json:"since"`
Activities []*Activity `json:"activities"`
AFK bool `json:"afk"`
Status string `json:"status"`
}
type updateStatusOp struct {
@ -333,7 +333,7 @@ type updateStatusOp struct {
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{
Status: "online",
}
@ -342,12 +342,12 @@ func newUpdateStatusData(idle int, gameType GameType, game, url string) *UpdateS
usd.IdleSince = &idle
}
if game != "" {
usd.Game = &Game{
Name: game,
Type: gameType,
if name != "" {
usd.Activities = []*Activity{{
Name: name,
Type: activityType,
URL: url,
}
}}
}
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.
// If idle>0 then set status to idle.
// If game!="" then set game.
// if otherwise, set status to active, and no game.
func (s *Session) UpdateStatus(idle int, game string) (err error) {
return s.UpdateStatusComplex(*newUpdateStatusData(idle, GameTypeGame, game, ""))
// If name!="" then set game.
// if otherwise, set status to active, and no activity.
func (s *Session) UpdateGameStatus(idle int, name string) (err error) {
return s.UpdateStatusComplex(*newUpdateStatusData(idle, ActivityTypeGame, name, ""))
}
// UpdateStreamingStatus is used to update the user's streaming status.
// If idle>0 then set status to idle.
// If game!="" then set game.
// If game!="" and url!="" then set the status type to streaming with the URL set.
// If name!="" then set game.
// If name!="" and url!="" then set the status type to streaming with the URL set.
// if otherwise, set status to active, and no game.
func (s *Session) UpdateStreamingStatus(idle int, game string, url string) (err error) {
gameType := GameTypeGame
func (s *Session) UpdateStreamingStatus(idle int, name string, url string) (err error) {
gameType := ActivityTypeGame
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..."
// If game!="" then set to what user is listening to
// Else, set user to active and no game.
func (s *Session) UpdateListeningStatus(game string) (err error) {
return s.UpdateStatusComplex(*newUpdateStatusData(0, GameTypeListening, game, ""))
// If name!="" then set to what user is listening to
// Else, set user to active and no activity.
func (s *Session) UpdateListeningStatus(name string) (err error) {
return s.UpdateStatusComplex(*newUpdateStatusData(0, ActivityTypeListening, name, ""))
}
// UpdateStatusComplex allows for sending the raw status update data untouched by discordgo.