TerraOceanPlugin/GoBot/discord/backend/plugin.go

60 lines
1.2 KiB
Go

package backend
import (
"github.com/bwmarrin/discordgo"
"TerraOceanBot/db"
"TerraOceanBot/discord/config"
"TerraOceanBot/discord/message"
"TerraOceanBot/server"
)
func UpdateVoiceChannelState(s *discordgo.Session) {
for _, guild := range s.State.Guilds {
if guild.ID != config.GuildID {
continue
}
for _, vs := range guild.VoiceStates {
if vs.ChannelID != config.VoiceChannel {
continue
}
if vs.Deaf || vs.SelfDeaf {
continue
}
username, err := db.GetMinecraftUsername(vs.UserID)
if err == db.ErrNotFound {
continue
}
if err != nil {
message.AuditError(s, "", err)
continue
}
server.ConnectUser(username)
}
}
}
func VoiceStateUpdate(s *discordgo.Session, vs *discordgo.VoiceState) {
username, err := db.GetMinecraftUsername(vs.UserID)
if err == db.ErrNotFound {
return
}
if err != nil {
message.AuditError(s, "", err)
return
}
if vs.GuildID != config.GuildID {
server.DisconnectUser(username)
return
}
if vs.ChannelID != config.VoiceChannel {
server.DisconnectUser(username)
return
}
if vs.Deaf || vs.SelfDeaf {
server.DisconnectUser(username)
return
}
server.ConnectUser(username)
}