func ListenToDiscord(config *eqemuconfig.Config, disco *discord.Discord) (err error) { var session *discordgo.Session var guild *discordgo.Guild //log.Println("Listen to discord..") if session, err = disco.GetSession(); err != nil { log.Printf("[Discord] Failed to get instance %s: %s (Make sure bot is part of server)", config.Discord.ServerID, err.Error()) return } if guild, err = session.Guild(config.Discord.ServerID); err != nil { log.Printf("[Discord] Failed to get server %s: %s (Make sure bot is part of server)", config.Discord.ServerID, err.Error()) return } isNotAvail := true if guild.Unavailable == &isNotAvail { log.Printf("[Discord] Failed to get server %s: Server unavailable (Make sure bot is part of server, and has permission)", config.Discord.ServerID, err.Error()) return } session.StateEnabled = true session.AddHandler(messageCreate) log.Printf("[Discord] Connected\n") if err = session.Open(); err != nil { log.Printf("[Discord] Session closed: %s", err.Error()) return } select {} return }
func listenToDiscord(config *eqemuconfig.Config, disco *discord.Discord) (err error) { for { applog.Info.Println("[Discord] Connecting as", config.Discord.Username, "...") if err = listener.ListenToDiscord(config, disco); err != nil { applog.Error.Println("[Discord] Disconnected with error:", err.Error()) } applog.Info.Println("[Discord] Reconnecting in 5 seconds...") time.Sleep(5 * time.Second) err = disco.Connect(config.Discord.Username, config.Discord.Password) if err != nil { applog.Error.Println("[Discord] Error connecting to discord:", err.Error()) } } }
func checkForMessages(t *telnet.Conn, disco *discord.Discord) (err error) { data := []byte{} message := "" for { if data, err = t.ReadUntil("\n"); err != nil { err = fmt.Errorf("Error reading", err.Error()) return } message = string(data) //log.Printf("[DEBUG OOC] %s", message) if len(message) < 3 { //ignore small messages continue } if !strings.Contains(message, "says ooc,") { //ignore non-ooc continue } if strings.Index(message, ">") > 0 && strings.Index(message, ">") < strings.Index(message, " ") { //ignore prompts message = message[strings.Index(message, ">")+1:] } if message[0:1] == "*" { //ignore echo backs continue } sender := message[0:strings.Index(message, " says ooc,")] message = message[strings.Index(message, "says ooc, '")+11 : len(message)-3] sender = strings.Replace(sender, "_", " ", -1) message = convertLinks(config.Discord.ItemUrl, message) if _, err = disco.SendMessage(channelID, fmt.Sprintf("**%s OOC**: %s", sender, message)); err != nil { log.Printf("[OOC] Error sending message (%s: %s) %s", sender, message, err.Error()) continue } log.Printf("[OOC] %s: %s\n", sender, message) } }
func startService() { log.Println("Starting DiscordEQ v0.41") var option string //Load config config, err := eqemuconfig.GetConfig() if err != nil { applog.Error.Println("Error while loading eqemu_config.xml to start:", err.Error()) fmt.Println("press a key then enter to exit.") fmt.Scan(&option) os.Exit(1) } if config.Discord.RefreshRate == 0 { config.Discord.RefreshRate = 10 } if strings.ToLower(config.World.Tcp.Telnet) != "enabled" { log.Println("Telnet must be enabled for this tool to work. Check your eqemuconfig.xml, and please adjust.") fmt.Println("press a key then enter to exit.") fmt.Scan(&option) os.Exit(1) } if config.Discord.Username == "" { applog.Error.Println("I don't see a username set in your <discord><username> section of eqemu_config.xml, please adjust.") fmt.Println("press a key then enter to exit.") fmt.Scan(&option) os.Exit(1) } if config.Discord.Password == "" { applog.Error.Println("I don't see a password set in your <discord><password> section of eqemu_config.xml, please adjust.") fmt.Println("press a key then enter to exit.") fmt.Scan(&option) os.Exit(1) } if config.Discord.ServerID == "" { applog.Error.Println("I don't see a serverid set in your <discord><serverid> section of eqemuconfig.xml, please adjust.") fmt.Println("press a key then enter to exit.") fmt.Scan(&option) os.Exit(1) } if config.Discord.ChannelID == "" { applog.Error.Println("I don't see a channelid set in your <discord><channelid> section of eqemuconfig.xml, please adjust.") fmt.Println("press a key then enter to exit.") fmt.Scan(&option) os.Exit(1) } disco := discord.Discord{} err = disco.Connect(config.Discord.Username, config.Discord.Password) if err != nil { applog.Error.Println("Error connecting to discord:", err.Error()) fmt.Println("press a key then enter to exit.") fmt.Scan(&option) os.Exit(1) } go listenToDiscord(config, &disco) go listenToOOC(config, &disco) select {} }