hopefully fix !vc

This commit is contained in:
kageru 2019-06-08 05:31:06 +02:00
parent 943e9fcaba
commit ffae758587
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
2 changed files with 19 additions and 14 deletions

View File

@ -56,9 +56,9 @@ func isAdmin(u *discordgo.User) bool {
return false return false
} }
func getServer() *discordgo.Guild { func getServer() (*discordgo.Guild, error) {
server, _ := state.Guild(config.ServerID) server, err := state.Guild(config.ServerID)
return server return server, err
} }
func remove(channels []*discordgo.Channel, position int) []*discordgo.Channel { func remove(channels []*discordgo.Channel, position int) []*discordgo.Channel {

View File

@ -46,22 +46,27 @@ func parseVoiceChannelCommand(session *discordgo.Session, message *discordgo.Mes
func checkAndDeleteUnusedChannels(session *discordgo.Session) { func checkAndDeleteUnusedChannels(session *discordgo.Session) {
for true { for true {
time.Sleep(15 * time.Second) time.Sleep(30 * time.Second)
for i, channel := range tempChannels { server, err := getServer()
if channelIsEmpty(channel.ID) { if err == nil {
session.ChannelDelete(channel.ID) for i, channel := range tempChannels {
tempChannels = remove(tempChannels, i) if channelIsEmpty(channel.ID, server.VoiceStates) {
log.Printf("Deleted channel %s", channel.ID) session.ChannelDelete(channel.ID)
log.Printf("Tempchannels: %d", len(tempChannels)) tempChannels = remove(tempChannels, i)
break log.Printf("Deleted channel %s", channel.ID)
log.Printf("Tempchannels: %d", len(tempChannels))
break
}
} }
} else {
log.Printf("Could not retrieve voice state from API, %s", err)
} }
} }
} }
func channelIsEmpty(channelID string) bool { func channelIsEmpty(channelID string, voiceStates []*discordgo.VoiceState) bool {
for _, voiceState := range getServer().VoiceStates { for _, state := range voiceStates {
if channelID == voiceState.ChannelID { if channelID == state.ChannelID {
return false return false
} }
} }