Skip to content

Commit

Permalink
fix singleton dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
TomRomeo committed Feb 22, 2022
1 parent 366d46a commit fefc822
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions internal/listeners/autovc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@ import (
"strings"
)

var autovcCache = map[string]string{}
var voiceStateCache = map[string]*discordgo.VoiceState{}

type ListenerAutoVoice struct {
db database.Database
st *dgrs.State
pmw *permissions.Permissions
db database.Database
st *dgrs.State
pmw *permissions.Permissions
autovcCache map[string]string
voiceStateCache map[string]*discordgo.VoiceState
}

func NewListenerAutoVoice(container di.Container) *ListenerAutoVoice {
return &ListenerAutoVoice{
db: container.Get(static.DiDatabase).(database.Database),
st: container.Get(static.DiState).(*dgrs.State),
pmw: container.Get(static.DiPermissions).(*permissions.Permissions),
db: container.Get(static.DiDatabase).(database.Database),
st: container.Get(static.DiState).(*dgrs.State),
pmw: container.Get(static.DiPermissions).(*permissions.Permissions),
autovcCache: map[string]string{},
voiceStateCache: map[string]*discordgo.VoiceState{},
}
}

Expand All @@ -33,10 +34,10 @@ func (l *ListenerAutoVoice) Handler(s *discordgo.Session, e *discordgo.VoiceStat
if err != nil || !allowed {
return
}
vsOld, _ := voiceStateCache[e.UserID]
vsOld, _ := l.voiceStateCache[e.UserID]
vsNew := e.VoiceState

voiceStateCache[e.UserID] = vsNew
l.voiceStateCache[e.UserID] = vsNew

ids, err := l.db.GetGuildAutoVC(e.GuildID)
if err != nil {
Expand All @@ -57,10 +58,10 @@ func (l *ListenerAutoVoice) Handler(s *discordgo.Session, e *discordgo.VoiceStat
} else if vsOld != nil && vsNew.ChannelID != "" && vsOld.ChannelID != vsNew.ChannelID {

// we don't want to delete the channel, if the user get's moved to their auto voicechannel
if vsNew.ChannelID == autovcCache[e.UserID] {
if vsNew.ChannelID == l.autovcCache[e.UserID] {

} else if strings.Contains(idString, vsNew.ChannelID) && autovcCache[e.UserID] == "" {
if autovcCache[e.UserID] == "" {
} else if strings.Contains(idString, vsNew.ChannelID) && l.autovcCache[e.UserID] == "" {
if l.autovcCache[e.UserID] == "" {
if err := l.createAutoVC(s, e.UserID, e.GuildID, vsNew.ChannelID); err != nil {
return
}
Expand All @@ -69,14 +70,14 @@ func (l *ListenerAutoVoice) Handler(s *discordgo.Session, e *discordgo.VoiceStat
return
}
}
} else if autovcCache[e.UserID] != "" {
} else if l.autovcCache[e.UserID] != "" {
if err := l.deleteAutoVC(s, e.UserID); err != nil {
return
}
}

} else if vsOld != nil && vsNew.ChannelID == "" {
if autovcCache[e.UserID] != "" {
if l.autovcCache[e.UserID] != "" {
if err := l.deleteAutoVC(s, e.UserID); err != nil {
return
}
Expand Down Expand Up @@ -105,19 +106,19 @@ func (l *ListenerAutoVoice) createAutoVC(s *discordgo.Session, userID, guildID,
if err != nil {
return err
}
autovcCache[userID] = ch.ID
l.autovcCache[userID] = ch.ID
if err := s.GuildMemberMove(guildID, userID, &ch.ID); err != nil {
return err
}
return nil
}

func (l *ListenerAutoVoice) deleteAutoVC(s *discordgo.Session, userID string) error {
vcID := autovcCache[userID]
vcID := l.autovcCache[userID]
_, err := s.ChannelDelete(vcID)
if err != nil {
return err
}
delete(autovcCache, userID)
delete(l.autovcCache, userID)
return nil
}

0 comments on commit fefc822

Please sign in to comment.