2020-12-29 15:14:19 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-01-03 20:20:03 -05:00
|
|
|
"fmt"
|
2020-12-29 15:14:19 -05:00
|
|
|
"log"
|
|
|
|
"strings"
|
2021-01-06 19:12:56 -05:00
|
|
|
"sync"
|
2020-12-29 15:23:43 -05:00
|
|
|
"time"
|
2020-12-29 15:14:19 -05:00
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
2021-01-05 13:16:03 -05:00
|
|
|
"layeh.com/gumble/gumble"
|
2020-12-29 15:14:19 -05:00
|
|
|
)
|
|
|
|
|
2021-01-08 14:38:21 -05:00
|
|
|
//Listener holds references to the current BridgeConf
|
|
|
|
//and BridgeState for use by the event handlers
|
2021-01-05 19:43:55 -05:00
|
|
|
type Listener struct {
|
2021-01-06 19:12:56 -05:00
|
|
|
BridgeConf *BridgeConfig
|
|
|
|
Bridge *BridgeState
|
|
|
|
UserCountLock *sync.Mutex
|
|
|
|
ConnectedLock *sync.Mutex
|
2021-01-05 19:43:55 -05:00
|
|
|
}
|
|
|
|
|
2021-01-05 20:21:08 -05:00
|
|
|
func (l *Listener) ready(s *discordgo.Session, event *discordgo.Ready) {
|
2020-12-29 15:14:19 -05:00
|
|
|
log.Println("READY event registered")
|
2021-01-06 19:12:56 -05:00
|
|
|
//Setup initial discord state
|
|
|
|
var g *discordgo.Guild
|
2021-01-08 14:38:21 -05:00
|
|
|
g = nil
|
2021-01-06 19:12:56 -05:00
|
|
|
for _, i := range event.Guilds {
|
|
|
|
if i.ID == l.BridgeConf.GID {
|
|
|
|
g = i
|
|
|
|
}
|
|
|
|
}
|
2021-01-08 14:38:21 -05:00
|
|
|
if g == nil {
|
|
|
|
log.Println("bad guild on READY")
|
|
|
|
return
|
|
|
|
}
|
2021-01-06 19:12:56 -05:00
|
|
|
for _, vs := range g.VoiceStates {
|
|
|
|
if vs.ChannelID == l.BridgeConf.CID {
|
|
|
|
l.UserCountLock.Lock()
|
|
|
|
l.Bridge.DiscordUserCount = l.Bridge.DiscordUserCount + 1
|
|
|
|
u, err := s.User(vs.UserID)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error looking up username")
|
|
|
|
}
|
|
|
|
l.Bridge.DiscordUsers[u.Username] = true
|
|
|
|
if l.Bridge.Connected {
|
|
|
|
l.Bridge.Client.Do(func() {
|
|
|
|
l.Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has joined Discord channel\n", u.Username), false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
l.UserCountLock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
2020-12-29 15:14:19 -05:00
|
|
|
}
|
|
|
|
|
2021-01-05 20:21:08 -05:00
|
|
|
func (l *Listener) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
2020-12-29 15:14:19 -05:00
|
|
|
|
2021-01-08 14:38:21 -05:00
|
|
|
if l.Bridge.Mode == bridgeModeConstant {
|
2021-01-05 12:16:51 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-29 15:14:19 -05:00
|
|
|
// Ignore all messages created by the bot itself
|
|
|
|
if m.Author.ID == s.State.User.ID {
|
|
|
|
return
|
|
|
|
}
|
2021-01-08 14:38:21 -05:00
|
|
|
// Find the channel that the message came from.
|
|
|
|
c, err := s.State.Channel(m.ChannelID)
|
|
|
|
if err != nil {
|
|
|
|
// Could not find channel.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the guild for that channel.
|
|
|
|
g, err := s.State.Guild(c.GuildID)
|
|
|
|
if err != nil {
|
|
|
|
// Could not find guild.
|
|
|
|
return
|
|
|
|
}
|
2021-01-05 20:21:08 -05:00
|
|
|
prefix := "!" + l.BridgeConf.Command
|
2021-01-03 15:32:59 -05:00
|
|
|
if strings.HasPrefix(m.Content, prefix+" link") {
|
2020-12-29 15:14:19 -05:00
|
|
|
// Look for the message sender in that guild's current voice states.
|
|
|
|
for _, vs := range g.VoiceStates {
|
|
|
|
if vs.UserID == m.Author.ID {
|
|
|
|
log.Printf("Trying to join GID %v and VID %v\n", g.ID, vs.ChannelID)
|
|
|
|
die := make(chan bool)
|
2021-01-05 20:21:08 -05:00
|
|
|
l.Bridge.ActiveConn = die
|
|
|
|
go startBridge(s, g.ID, vs.ChannelID, l, die)
|
2020-12-29 15:14:19 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 15:32:59 -05:00
|
|
|
if strings.HasPrefix(m.Content, prefix+" unlink") {
|
2020-12-29 15:14:19 -05:00
|
|
|
// Look for the message sender in that guild's current voice states.
|
|
|
|
for _, vs := range g.VoiceStates {
|
|
|
|
if vs.UserID == m.Author.ID {
|
|
|
|
log.Printf("Trying to leave GID %v and VID %v\n", g.ID, vs.ChannelID)
|
2021-01-05 20:21:08 -05:00
|
|
|
l.Bridge.ActiveConn <- true
|
|
|
|
l.Bridge.ActiveConn = nil
|
2020-12-29 15:14:19 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-29 15:23:43 -05:00
|
|
|
|
2021-01-03 15:32:59 -05:00
|
|
|
if strings.HasPrefix(m.Content, prefix+" refresh") {
|
2020-12-29 15:23:43 -05:00
|
|
|
// Look for the message sender in that guild's current voice states.
|
|
|
|
for _, vs := range g.VoiceStates {
|
|
|
|
if vs.UserID == m.Author.ID {
|
|
|
|
log.Printf("Trying to refresh GID %v and VID %v\n", g.ID, vs.ChannelID)
|
2021-01-05 20:21:08 -05:00
|
|
|
l.Bridge.ActiveConn <- true
|
2020-12-29 18:19:44 -05:00
|
|
|
time.Sleep(5 * time.Second)
|
2021-01-05 20:21:08 -05:00
|
|
|
l.Bridge.ActiveConn = make(chan bool)
|
|
|
|
go startBridge(s, g.ID, vs.ChannelID, l, l.Bridge.ActiveConn)
|
2020-12-29 15:23:43 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-03 15:32:59 -05:00
|
|
|
|
|
|
|
if strings.HasPrefix(m.Content, prefix+" auto") {
|
2021-01-08 14:38:21 -05:00
|
|
|
if l.Bridge.Mode != bridgeModeAuto {
|
|
|
|
l.Bridge.Mode = bridgeModeAuto
|
2021-01-05 20:21:08 -05:00
|
|
|
l.Bridge.AutoChan = make(chan bool)
|
|
|
|
go AutoBridge(s, l)
|
2021-01-03 15:32:59 -05:00
|
|
|
} else {
|
2021-01-05 20:21:08 -05:00
|
|
|
l.Bridge.AutoChan <- true
|
2021-01-08 14:38:21 -05:00
|
|
|
l.Bridge.Mode = bridgeModeManual
|
2021-01-03 15:32:59 -05:00
|
|
|
}
|
|
|
|
}
|
2020-12-29 15:14:19 -05:00
|
|
|
}
|
|
|
|
|
2021-01-05 20:21:08 -05:00
|
|
|
func (l *Listener) guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {
|
2020-12-29 15:14:19 -05:00
|
|
|
|
|
|
|
if event.Guild.Unavailable {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, channel := range event.Guild.Channels {
|
|
|
|
if channel.ID == event.Guild.ID {
|
2020-12-29 15:23:43 -05:00
|
|
|
log.Println("Mumble-Discord bridge is active in new guild")
|
2020-12-29 15:14:19 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-03 15:32:59 -05:00
|
|
|
|
2021-01-05 20:21:08 -05:00
|
|
|
func (l *Listener) voiceUpdate(s *discordgo.Session, event *discordgo.VoiceStateUpdate) {
|
2021-01-06 19:12:56 -05:00
|
|
|
l.UserCountLock.Lock()
|
2021-01-05 20:25:38 -05:00
|
|
|
if event.GuildID == l.BridgeConf.GID {
|
|
|
|
if event.ChannelID == l.BridgeConf.CID {
|
2021-01-03 20:48:34 -05:00
|
|
|
//get user
|
|
|
|
u, err := s.User(event.UserID)
|
2021-01-03 20:29:56 -05:00
|
|
|
if err != nil {
|
2021-01-03 20:48:34 -05:00
|
|
|
log.Printf("error looking up user for uid %v", event.UserID)
|
2021-01-03 20:29:56 -05:00
|
|
|
}
|
2021-01-03 20:48:34 -05:00
|
|
|
//check to see if actually new user
|
2021-01-05 20:25:38 -05:00
|
|
|
if l.Bridge.DiscordUsers[u.Username] {
|
2021-01-03 20:48:34 -05:00
|
|
|
//not actually new user
|
2021-01-06 19:12:56 -05:00
|
|
|
l.UserCountLock.Unlock()
|
2021-01-03 20:48:34 -05:00
|
|
|
return
|
2021-01-03 20:29:56 -05:00
|
|
|
}
|
2021-01-03 15:32:59 -05:00
|
|
|
log.Println("user joined watched discord channel")
|
2021-01-06 19:12:56 -05:00
|
|
|
l.ConnectedLock.Lock()
|
2021-01-05 20:25:38 -05:00
|
|
|
if l.Bridge.Connected {
|
|
|
|
l.Bridge.Client.Do(func() {
|
|
|
|
l.Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has joined Discord channel\n", u.Username), false)
|
2021-01-05 19:08:17 -05:00
|
|
|
})
|
2021-01-03 20:20:03 -05:00
|
|
|
}
|
2021-01-06 19:12:56 -05:00
|
|
|
l.ConnectedLock.Unlock()
|
2021-01-05 20:25:38 -05:00
|
|
|
l.Bridge.DiscordUsers[u.Username] = true
|
|
|
|
l.Bridge.DiscordUserCount = l.Bridge.DiscordUserCount + 1
|
2021-01-06 19:12:56 -05:00
|
|
|
l.UserCountLock.Unlock()
|
2021-01-03 15:32:59 -05:00
|
|
|
}
|
|
|
|
if event.ChannelID == "" {
|
|
|
|
//leave event, trigger recount of active users
|
|
|
|
//TODO when next version of discordgo comes out, switch to PreviousState
|
|
|
|
g, err := s.State.Guild(event.GuildID)
|
|
|
|
if err != nil {
|
|
|
|
// Could not find guild.
|
2021-01-06 19:12:56 -05:00
|
|
|
l.UserCountLock.Unlock()
|
2021-01-03 15:32:59 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for current voice states in watched channel
|
|
|
|
count := 0
|
|
|
|
for _, vs := range g.VoiceStates {
|
2021-01-05 20:25:38 -05:00
|
|
|
if vs.ChannelID == l.BridgeConf.CID {
|
2021-01-03 15:32:59 -05:00
|
|
|
count = count + 1
|
|
|
|
}
|
|
|
|
}
|
2021-01-05 20:25:38 -05:00
|
|
|
if l.Bridge.DiscordUserCount > count {
|
2021-01-03 20:48:34 -05:00
|
|
|
u, err := s.User(event.UserID)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error looking up user for uid %v", event.UserID)
|
|
|
|
}
|
2021-01-05 20:25:38 -05:00
|
|
|
delete(l.Bridge.DiscordUsers, u.Username)
|
2021-01-03 15:32:59 -05:00
|
|
|
log.Println("user left watched discord channel")
|
2021-01-06 19:12:56 -05:00
|
|
|
l.ConnectedLock.Lock()
|
2021-01-05 20:25:38 -05:00
|
|
|
if l.Bridge.Connected {
|
|
|
|
l.Bridge.Client.Do(func() {
|
|
|
|
l.Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has left Discord channel\n", u.Username), false)
|
2021-01-05 19:08:17 -05:00
|
|
|
})
|
2021-01-05 11:41:33 -05:00
|
|
|
}
|
2021-01-06 19:12:56 -05:00
|
|
|
l.ConnectedLock.Unlock()
|
2021-01-05 20:25:38 -05:00
|
|
|
l.Bridge.DiscordUserCount = count
|
2021-01-03 15:32:59 -05:00
|
|
|
}
|
2021-01-09 16:38:36 -05:00
|
|
|
l.UserCountLock.Unlock()
|
2021-01-03 15:32:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-01-05 13:16:03 -05:00
|
|
|
|
2021-01-05 19:43:55 -05:00
|
|
|
func (l *Listener) mumbleConnect(e *gumble.ConnectEvent) {
|
|
|
|
if l.BridgeConf.MumbleChannel != "" {
|
2021-01-05 13:16:03 -05:00
|
|
|
//join specified channel
|
2021-01-05 20:25:38 -05:00
|
|
|
startingChannel := e.Client.Channels.Find(l.BridgeConf.MumbleChannel)
|
2021-01-05 13:16:03 -05:00
|
|
|
if startingChannel != nil {
|
|
|
|
e.Client.Self.Move(startingChannel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-05 19:00:28 -05:00
|
|
|
|
2021-01-05 19:43:55 -05:00
|
|
|
func (l *Listener) mumbleUserChange(e *gumble.UserChangeEvent) {
|
2021-01-06 19:12:56 -05:00
|
|
|
l.UserCountLock.Lock()
|
2021-01-05 19:00:28 -05:00
|
|
|
if e.Type.Has(gumble.UserChangeConnected) || e.Type.Has(gumble.UserChangeChannel) || e.Type.Has(gumble.UserChangeDisconnected) {
|
2021-01-05 20:25:38 -05:00
|
|
|
l.Bridge.MumbleUsers = make(map[string]bool)
|
2021-01-05 19:43:55 -05:00
|
|
|
for _, user := range l.Bridge.Client.Self.Channel.Users {
|
2021-01-05 19:00:28 -05:00
|
|
|
//note, this might be too slow for really really big channels?
|
|
|
|
//event listeners block while processing
|
|
|
|
//also probably bad to rebuild the set every user change.
|
2021-01-05 19:43:55 -05:00
|
|
|
if user.Name != l.Bridge.Client.Self.Name {
|
|
|
|
l.Bridge.MumbleUsers[user.Name] = true
|
2021-01-05 19:00:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-06 19:12:56 -05:00
|
|
|
l.UserCountLock.Unlock()
|
2021-01-05 19:00:28 -05:00
|
|
|
}
|