Add previous state to VoiceStateUpdate event (#823)

This commit is contained in:
Nicholas McCurry 2020-11-05 16:43:55 -07:00
parent 92c52f3db1
commit a6d7b88762
2 changed files with 29 additions and 0 deletions

View file

@ -252,6 +252,8 @@ type VoiceServerUpdate struct {
// VoiceStateUpdate is the data for a VoiceStateUpdate event. // VoiceStateUpdate is the data for a VoiceStateUpdate event.
type VoiceStateUpdate struct { type VoiceStateUpdate struct {
*VoiceState *VoiceState
// BeforeUpdate will be nil if the VoiceState was not previously cached in the state cache.
BeforeUpdate *VoiceState `json:"-"`
} }
// MessageDeleteBulk is the data for a MessageDeleteBulk event // MessageDeleteBulk is the data for a MessageDeleteBulk event

View file

@ -732,6 +732,26 @@ func (s *State) voiceStateUpdate(update *VoiceStateUpdate) error {
return nil return nil
} }
// VoiceState gets a VoiceState by guild and user ID.
func (s *State) VoiceState(guildID, userID string) (*VoiceState, error) {
if s == nil {
return nil, ErrNilState
}
guild, err := s.Guild(guildID)
if err != nil {
return nil, err
}
for _, state := range guild.VoiceStates {
if state.UserID == userID {
return state, nil
}
}
return nil, ErrStateNotFound
}
// Message gets a message by channel and message ID. // Message gets a message by channel and message ID.
func (s *State) Message(channelID, messageID string) (*Message, error) { func (s *State) Message(channelID, messageID string) (*Message, error) {
if s == nil { if s == nil {
@ -921,6 +941,13 @@ func (s *State) OnInterface(se *Session, i interface{}) (err error) {
} }
case *VoiceStateUpdate: case *VoiceStateUpdate:
if s.TrackVoice { if s.TrackVoice {
var old *VoiceState
old, err = s.VoiceState(t.GuildID, t.UserID)
if err == nil {
oldCopy := *old
t.BeforeUpdate = &oldCopy
}
err = s.voiceStateUpdate(t) err = s.voiceStateUpdate(t)
} }
case *PresenceUpdate: case *PresenceUpdate: