discord event listeners use non-global state

This commit is contained in:
stryan 2021-01-05 20:21:08 -05:00
parent bcd7ee97ba
commit ac800167f6
2 changed files with 42 additions and 40 deletions

View File

@ -15,13 +15,13 @@ type Listener struct {
Bridge *BridgeState Bridge *BridgeState
} }
func ready(s *discordgo.Session, event *discordgo.Ready) { func (l *Listener) ready(s *discordgo.Session, event *discordgo.Ready) {
log.Println("READY event registered") log.Println("READY event registered")
} }
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { func (l *Listener) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if BridgeConf.Mode == BridgeModeConstant { if l.BridgeConf.Mode == BridgeModeConstant {
return return
} }
@ -29,7 +29,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID { if m.Author.ID == s.State.User.ID {
return return
} }
prefix := "!" + BridgeConf.Command prefix := "!" + l.BridgeConf.Command
if strings.HasPrefix(m.Content, prefix+" link") { if strings.HasPrefix(m.Content, prefix+" link") {
// Find the channel that the message came from. // Find the channel that the message came from.
@ -51,8 +51,8 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if vs.UserID == m.Author.ID { if vs.UserID == m.Author.ID {
log.Printf("Trying to join GID %v and VID %v\n", g.ID, vs.ChannelID) log.Printf("Trying to join GID %v and VID %v\n", g.ID, vs.ChannelID)
die := make(chan bool) die := make(chan bool)
Bridge.ActiveConn = die l.Bridge.ActiveConn = die
//go startBridge(s, g.ID, vs.ChannelID, BridgeConf.Config, BridgeConf.MumbleAddr, BridgeConf.MumbleInsecure, die) go startBridge(s, g.ID, vs.ChannelID, l, die)
return return
} }
} }
@ -78,8 +78,8 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
for _, vs := range g.VoiceStates { for _, vs := range g.VoiceStates {
if vs.UserID == m.Author.ID { if vs.UserID == m.Author.ID {
log.Printf("Trying to leave GID %v and VID %v\n", g.ID, vs.ChannelID) log.Printf("Trying to leave GID %v and VID %v\n", g.ID, vs.ChannelID)
Bridge.ActiveConn <- true l.Bridge.ActiveConn <- true
Bridge.ActiveConn = nil l.Bridge.ActiveConn = nil
MumbleReset() MumbleReset()
DiscordReset() DiscordReset()
return return
@ -107,30 +107,30 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
for _, vs := range g.VoiceStates { for _, vs := range g.VoiceStates {
if vs.UserID == m.Author.ID { if vs.UserID == m.Author.ID {
log.Printf("Trying to refresh GID %v and VID %v\n", g.ID, vs.ChannelID) log.Printf("Trying to refresh GID %v and VID %v\n", g.ID, vs.ChannelID)
Bridge.ActiveConn <- true l.Bridge.ActiveConn <- true
MumbleReset() MumbleReset()
DiscordReset() DiscordReset()
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
Bridge.ActiveConn = make(chan bool) l.Bridge.ActiveConn = make(chan bool)
//go startBridge(s, g.ID, vs.ChannelID, BridgeConf.Config, BridgeConf.MumbleAddr, BridgeConf.MumbleInsecure, Bridge.ActiveConn) go startBridge(s, g.ID, vs.ChannelID, l, l.Bridge.ActiveConn)
return return
} }
} }
} }
if strings.HasPrefix(m.Content, prefix+" auto") { if strings.HasPrefix(m.Content, prefix+" auto") {
if BridgeConf.Mode != BridgeModeAuto { if l.BridgeConf.Mode != BridgeModeAuto {
BridgeConf.Mode = BridgeModeAuto l.BridgeConf.Mode = BridgeModeAuto
Bridge.AutoChan = make(chan bool) l.Bridge.AutoChan = make(chan bool)
//go AutoBridge(s) go AutoBridge(s, l)
} else { } else {
Bridge.AutoChan <- true l.Bridge.AutoChan <- true
BridgeConf.Mode = BridgeModeManual l.BridgeConf.Mode = BridgeModeManual
} }
} }
} }
func guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) { func (l *Listener) guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {
if event.Guild.Unavailable { if event.Guild.Unavailable {
return return
@ -144,7 +144,7 @@ func guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {
} }
} }
func voiceUpdate(s *discordgo.Session, event *discordgo.VoiceStateUpdate) { func (l *Listener) voiceUpdate(s *discordgo.Session, event *discordgo.VoiceStateUpdate) {
if event.GuildID == BridgeConf.GID { if event.GuildID == BridgeConf.GID {
if event.ChannelID == BridgeConf.CID { if event.ChannelID == BridgeConf.CID {
//get user //get user

44
main.go
View File

@ -59,32 +59,13 @@ func main() {
log.Println("Unable to set priority. ", err) log.Println("Unable to set priority. ", err)
} }
// DISCORD Setup //Connect to discord
discord, err := discordgo.New("Bot " + *discordToken) discord, err := discordgo.New("Bot " + *discordToken)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return return
} }
// Open Websocket
discord.LogLevel = 2
discord.StateEnabled = true
discord.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsAllWithoutPrivileged)
// register handlers
discord.AddHandler(ready)
discord.AddHandler(messageCreate)
discord.AddHandler(guildCreate)
discord.AddHandler(voiceUpdate)
err = discord.Open()
if err != nil {
log.Println(err)
return
}
defer discord.Close()
log.Println("Discord Bot Connected")
log.Printf("Discord bot looking for command !%v", *discordCommand)
// Mumble setup // Mumble setup
config := gumble.NewConfig() config := gumble.NewConfig()
config.Username = *mumbleUsername config.Username = *mumbleUsername
@ -110,12 +91,33 @@ func main() {
DiscordUsers: make(map[string]bool), DiscordUsers: make(map[string]bool),
} }
l := &Listener{BridgeConf, Bridge} l := &Listener{BridgeConf, Bridge}
// Discord setup
// Open Websocket
discord.LogLevel = 2
discord.StateEnabled = true
discord.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsAllWithoutPrivileged)
// register handlers
discord.AddHandler(l.ready)
discord.AddHandler(l.messageCreate)
discord.AddHandler(l.guildCreate)
discord.AddHandler(l.voiceUpdate)
err = discord.Open()
if err != nil {
log.Println(err)
return
}
defer discord.Close()
log.Println("Discord Bot Connected")
log.Printf("Discord bot looking for command !%v", *discordCommand)
switch *mode { switch *mode {
case "auto": case "auto":
log.Println("bridge starting in automatic mode") log.Println("bridge starting in automatic mode")
Bridge.AutoChan = make(chan bool) Bridge.AutoChan = make(chan bool)
BridgeConf.Mode = BridgeModeAuto BridgeConf.Mode = BridgeModeAuto
//go AutoBridge(discord,l) go AutoBridge(discord, l)
case "manual": case "manual":
log.Println("bridge starting in manual mode") log.Println("bridge starting in manual mode")
BridgeConf.Mode = BridgeModeManual BridgeConf.Mode = BridgeModeManual