diff --git a/GoBot/discord/backend/plugin.go b/GoBot/discord/backend/plugin.go new file mode 100644 index 0000000..ec5ef12 --- /dev/null +++ b/GoBot/discord/backend/plugin.go @@ -0,0 +1,62 @@ +package backend + +import ( + "fmt" + + "github.com/bwmarrin/discordgo" + + "TerraOceanBot/db" + "TerraOceanBot/discord/config" + "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 { + //TODO Use auditError() once it's moved to its own package. + fmt.Println(err) + continue + } + server.ConnectUser(username) + } + } +} + +func VoiceStateUpdate(vs *discordgo.VoiceState) { + username, err := db.GetMinecraftUsername(vs.UserID) + if err == db.ErrNotFound { + return + } + if err != nil { + //TODO Use auditError() once it's moved to its own package. + fmt.Println(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) +} diff --git a/GoBot/discord/config/discordID.go b/GoBot/discord/config/discordID.go index 9fd25c0..9e81745 100644 --- a/GoBot/discord/config/discordID.go +++ b/GoBot/discord/config/discordID.go @@ -7,3 +7,4 @@ const VoteChannel = "627164246056239104" const AnnounceCustomChannel = "627165467269922864" const AnnounceInviteChannel = "627165467269922864" const VoteSuggestionChannel = "627164561644191744" +const VoiceChannel = "627165587759693827" diff --git a/GoBot/discord/discord.go b/GoBot/discord/discord.go index 0167c94..09c06ef 100644 --- a/GoBot/discord/discord.go +++ b/GoBot/discord/discord.go @@ -6,6 +6,7 @@ import ( "github.com/bwmarrin/discordgo" + "TerraOceanBot/discord/backend" "TerraOceanBot/discord/modules" ) @@ -28,6 +29,7 @@ func StartBot(token string, kill chan bool) { fmt.Println("Bot is now running. Press CTRL-C to exit.") go modules.ListenToVoteFinishes(dg) + go backend.UpdateVoiceChannelState(dg) <-kill diff --git a/GoBot/discord/listener.go b/GoBot/discord/listener.go index ab0f995..bc42c38 100644 --- a/GoBot/discord/listener.go +++ b/GoBot/discord/listener.go @@ -5,6 +5,7 @@ import ( "github.com/bwmarrin/discordgo" + "TerraOceanBot/discord/backend" "TerraOceanBot/discord/config" "TerraOceanBot/discord/modules" ) @@ -29,3 +30,7 @@ func ProcessVote(s *discordgo.Session, m *discordgo.MessageCreate) { modules.VoteSuggestion(s, m) } } + +func ProcessVoiceStateUpdate(s *discordgo.Session, vs *discordgo.VoiceStateUpdate) { + backend.VoiceStateUpdate(vs.VoiceState) +} diff --git a/GoBot/server/hub.go b/GoBot/server/hub.go index 196aacf..bfd37ff 100644 --- a/GoBot/server/hub.go +++ b/GoBot/server/hub.go @@ -1,8 +1,14 @@ package server -import "github.com/gorilla/websocket" +import ( + "fmt" + "strings" + + "github.com/gorilla/websocket" +) var clients = make(map[*websocket.Conn]bool) +var connectedUsers = make(map[string]bool) func newSession(who *websocket.Conn) { clients[who] = true @@ -13,5 +19,34 @@ func disconnectSession(who *websocket.Conn) { } func handleIncomingMessage(who *websocket.Conn, msg string) { - //TODO + message := strings.SplitN(msg, " ", 2) + switch message[0] { + case "query": + if len(message) < 2 { + return + } + if !connectedUsers[message[1]] { + who.WriteMessage(websocket.TextMessage, []byte("leave "+message[1])) + } + default: + fmt.Println("Unknown message:" + msg) + } +} + +func broadcast(message string) { + for k, _ := range clients { + k.WriteMessage(websocket.TextMessage, []byte(message)) + } +} + +func ConnectUser(mcUsername string) { + connectedUsers[mcUsername] = true +} + +func DisconnectUser(mcUsername string) { + delete(connectedUsers, mcUsername) +} + +func RequestSync() { + broadcast("sync") }