go: Connect voice state update to the Kotlin plugin

master
Luther Wen Xu 2019-10-13 19:39:17 +07:00
parent f3c69655e2
commit 69e2a2736c
Signed by untrusted user: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
5 changed files with 107 additions and 2 deletions

@ -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)
}

@ -7,3 +7,4 @@ const VoteChannel = "627164246056239104"
const AnnounceCustomChannel = "627165467269922864" const AnnounceCustomChannel = "627165467269922864"
const AnnounceInviteChannel = "627165467269922864" const AnnounceInviteChannel = "627165467269922864"
const VoteSuggestionChannel = "627164561644191744" const VoteSuggestionChannel = "627164561644191744"
const VoiceChannel = "627165587759693827"

@ -6,6 +6,7 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"TerraOceanBot/discord/backend"
"TerraOceanBot/discord/modules" "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.") fmt.Println("Bot is now running. Press CTRL-C to exit.")
go modules.ListenToVoteFinishes(dg) go modules.ListenToVoteFinishes(dg)
go backend.UpdateVoiceChannelState(dg)
<-kill <-kill

@ -5,6 +5,7 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"TerraOceanBot/discord/backend"
"TerraOceanBot/discord/config" "TerraOceanBot/discord/config"
"TerraOceanBot/discord/modules" "TerraOceanBot/discord/modules"
) )
@ -29,3 +30,7 @@ func ProcessVote(s *discordgo.Session, m *discordgo.MessageCreate) {
modules.VoteSuggestion(s, m) modules.VoteSuggestion(s, m)
} }
} }
func ProcessVoiceStateUpdate(s *discordgo.Session, vs *discordgo.VoiceStateUpdate) {
backend.VoiceStateUpdate(vs.VoiceState)
}

@ -1,8 +1,14 @@
package server package server
import "github.com/gorilla/websocket" import (
"fmt"
"strings"
"github.com/gorilla/websocket"
)
var clients = make(map[*websocket.Conn]bool) var clients = make(map[*websocket.Conn]bool)
var connectedUsers = make(map[string]bool)
func newSession(who *websocket.Conn) { func newSession(who *websocket.Conn) {
clients[who] = true clients[who] = true
@ -13,5 +19,34 @@ func disconnectSession(who *websocket.Conn) {
} }
func handleIncomingMessage(who *websocket.Conn, msg string) { 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")
} }