discord event listeners use non-global state
This commit is contained in:
parent
83712bfe15
commit
ef8af89893
38
handlers.go
38
handlers.go
@ -15,13 +15,13 @@ type Listener struct {
|
||||
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")
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@ -29,7 +29,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
if m.Author.ID == s.State.User.ID {
|
||||
return
|
||||
}
|
||||
prefix := "!" + BridgeConf.Command
|
||||
prefix := "!" + l.BridgeConf.Command
|
||||
if strings.HasPrefix(m.Content, prefix+" link") {
|
||||
|
||||
// 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 {
|
||||
log.Printf("Trying to join GID %v and VID %v\n", g.ID, vs.ChannelID)
|
||||
die := make(chan bool)
|
||||
Bridge.ActiveConn = die
|
||||
//go startBridge(s, g.ID, vs.ChannelID, BridgeConf.Config, BridgeConf.MumbleAddr, BridgeConf.MumbleInsecure, die)
|
||||
l.Bridge.ActiveConn = die
|
||||
go startBridge(s, g.ID, vs.ChannelID, l, die)
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -78,8 +78,8 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
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)
|
||||
Bridge.ActiveConn <- true
|
||||
Bridge.ActiveConn = nil
|
||||
l.Bridge.ActiveConn <- true
|
||||
l.Bridge.ActiveConn = nil
|
||||
MumbleReset()
|
||||
DiscordReset()
|
||||
return
|
||||
@ -107,30 +107,30 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
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)
|
||||
Bridge.ActiveConn <- true
|
||||
l.Bridge.ActiveConn <- true
|
||||
MumbleReset()
|
||||
DiscordReset()
|
||||
time.Sleep(5 * time.Second)
|
||||
Bridge.ActiveConn = make(chan bool)
|
||||
//go startBridge(s, g.ID, vs.ChannelID, BridgeConf.Config, BridgeConf.MumbleAddr, BridgeConf.MumbleInsecure, Bridge.ActiveConn)
|
||||
l.Bridge.ActiveConn = make(chan bool)
|
||||
go startBridge(s, g.ID, vs.ChannelID, l, l.Bridge.ActiveConn)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if strings.HasPrefix(m.Content, prefix+" auto") {
|
||||
if BridgeConf.Mode != BridgeModeAuto {
|
||||
BridgeConf.Mode = BridgeModeAuto
|
||||
Bridge.AutoChan = make(chan bool)
|
||||
//go AutoBridge(s)
|
||||
if l.BridgeConf.Mode != BridgeModeAuto {
|
||||
l.BridgeConf.Mode = BridgeModeAuto
|
||||
l.Bridge.AutoChan = make(chan bool)
|
||||
go AutoBridge(s, l)
|
||||
} else {
|
||||
Bridge.AutoChan <- true
|
||||
BridgeConf.Mode = BridgeModeManual
|
||||
l.Bridge.AutoChan <- true
|
||||
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 {
|
||||
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.ChannelID == BridgeConf.CID {
|
||||
//get user
|
||||
|
44
main.go
44
main.go
@ -59,32 +59,13 @@ func main() {
|
||||
// log.Println("Unable to set priority. ", err)
|
||||
//}
|
||||
|
||||
// DISCORD Setup
|
||||
|
||||
//Connect to discord
|
||||
discord, err := discordgo.New("Bot " + *discordToken)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
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
|
||||
config := gumble.NewConfig()
|
||||
config.Username = *mumbleUsername
|
||||
@ -110,12 +91,33 @@ func main() {
|
||||
DiscordUsers: make(map[string]bool),
|
||||
}
|
||||
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 {
|
||||
case "auto":
|
||||
log.Println("bridge starting in automatic mode")
|
||||
Bridge.AutoChan = make(chan bool)
|
||||
BridgeConf.Mode = BridgeModeAuto
|
||||
//go AutoBridge(discord,l)
|
||||
go AutoBridge(discord, l)
|
||||
case "manual":
|
||||
log.Println("bridge starting in manual mode")
|
||||
BridgeConf.Mode = BridgeModeManual
|
||||
|
Loading…
Reference in New Issue
Block a user