1
0
mirror of https://github.com/stryan/mumble-discord-bridge.git synced 2024-11-14 19:45:41 -05:00
mumble-discord-bridge/handlers.go

267 lines
6.8 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"log"
"strings"
"sync"
2020-12-29 15:23:43 -05:00
"time"
"github.com/bwmarrin/discordgo"
"layeh.com/gumble/gumble"
)
type Listener struct {
BridgeConf *BridgeConfig
Bridge *BridgeState
UserCountLock *sync.Mutex
ConnectedLock *sync.Mutex
}
func (l *Listener) ready(s *discordgo.Session, event *discordgo.Ready) {
log.Println("READY event registered")
//Setup initial discord state
var g *discordgo.Guild
for _, i := range event.Guilds {
if i.ID == l.BridgeConf.GID {
g = i
}
}
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()
}
}
}
func (l *Listener) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if l.Bridge.Mode == BridgeModeConstant {
2021-01-05 12:16:51 -05:00
return
}
// Ignore all messages created by the bot itself
if m.Author.ID == s.State.User.ID {
return
}
prefix := "!" + l.BridgeConf.Command
if strings.HasPrefix(m.Content, prefix+" link") {
// 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
}
// 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)
l.Bridge.ActiveConn = die
go startBridge(s, g.ID, vs.ChannelID, l, die)
return
}
}
}
if strings.HasPrefix(m.Content, prefix+" unlink") {
// 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
}
// 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)
l.Bridge.ActiveConn <- true
l.Bridge.ActiveConn = nil
MumbleReset()
DiscordReset()
return
}
}
}
2020-12-29 15:23:43 -05:00
if strings.HasPrefix(m.Content, prefix+" refresh") {
2020-12-29 15:23:43 -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
}
// 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)
l.Bridge.ActiveConn <- true
MumbleReset()
DiscordReset()
time.Sleep(5 * time.Second)
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
}
}
}
if strings.HasPrefix(m.Content, prefix+" auto") {
if l.Bridge.Mode != BridgeModeAuto {
l.Bridge.Mode = BridgeModeAuto
l.Bridge.AutoChan = make(chan bool)
go AutoBridge(s, l)
} else {
l.Bridge.AutoChan <- true
l.Bridge.Mode = BridgeModeManual
}
}
}
func (l *Listener) guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {
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")
return
}
}
}
func (l *Listener) voiceUpdate(s *discordgo.Session, event *discordgo.VoiceStateUpdate) {
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)
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: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
l.UserCountLock.Unlock()
2021-01-03 20:48:34 -05:00
return
}
log.Println("user joined watched discord channel")
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)
})
}
l.ConnectedLock.Unlock()
2021-01-05 20:25:38 -05:00
l.Bridge.DiscordUsers[u.Username] = true
log.Println(l.Bridge.DiscordUsers)
l.Bridge.DiscordUserCount = l.Bridge.DiscordUserCount + 1
l.UserCountLock.Unlock()
}
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.
l.UserCountLock.Unlock()
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 {
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)
log.Println("user left watched discord channel")
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)
})
}
l.ConnectedLock.Unlock()
2021-01-05 20:25:38 -05:00
l.Bridge.DiscordUserCount = count
l.UserCountLock.Unlock()
}
}
}
return
}
func (l *Listener) mumbleConnect(e *gumble.ConnectEvent) {
if l.BridgeConf.MumbleChannel != "" {
//join specified channel
2021-01-05 20:25:38 -05:00
startingChannel := e.Client.Channels.Find(l.BridgeConf.MumbleChannel)
if startingChannel != nil {
e.Client.Self.Move(startingChannel)
}
}
}
func (l *Listener) mumbleUserChange(e *gumble.UserChangeEvent) {
l.UserCountLock.Lock()
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)
for _, user := range l.Bridge.Client.Self.Channel.Users {
//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.
if user.Name != l.Bridge.Client.Self.Name {
l.Bridge.MumbleUsers[user.Name] = true
}
}
}
l.UserCountLock.Unlock()
}