From a2f39bdcc48f1e2e2b9095c17b802909bf39a2e0 Mon Sep 17 00:00:00 2001 From: mgerb42 Date: Sun, 15 Jan 2017 22:10:35 +0100 Subject: [PATCH] init --- .gitignore | 1 + bot/bot.go | 53 ++++++++++++++++++++++++++ config.template.json | 31 +++++++++++++++ config/config.go | 45 ++++++++++++++++++++++ main.go | 28 ++++++++++++++ readme.md | 23 ++++++++++++ serverstatus/serverstatus.go | 73 ++++++++++++++++++++++++++++++++++++ 7 files changed, 254 insertions(+) create mode 100644 .gitignore create mode 100644 bot/bot.go create mode 100644 config.template.json create mode 100644 config/config.go create mode 100644 main.go create mode 100644 readme.md create mode 100644 serverstatus/serverstatus.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0cffcb3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.json \ No newline at end of file diff --git a/bot/bot.go b/bot/bot.go new file mode 100644 index 0000000..48739e7 --- /dev/null +++ b/bot/bot.go @@ -0,0 +1,53 @@ +package bot + +import ( + "fmt" + "github.com/bwmarrin/discordgo" +) + +// Variables used for command line parameters +var ( + BotID string + Session *discordgo.Session +) + +func Connect(token string) { + // Create a new Discord session using the provided bot token. + var err error + Session, err = discordgo.New("Bot " + token) + + if err != nil { + fmt.Println("error creating Discord session,", err) + return + } + + // Get the account information. + u, err := Session.User("@me") + if err != nil { + fmt.Println("error obtaining account details,", err) + } + + // Store the account ID for later use. + BotID = u.ID + + fmt.Println("Bot connected") +} + +func Start() { + // Open the websocket and begin listening. + err := Session.Open() + if err != nil { + fmt.Println("error opening connection,", err) + return + } + + fmt.Println("Bot is now running. Press CTRL-C to exit.") + + // Simple way to keep program running until CTRL-C is pressed. + <-make(chan struct{}) + return +} + +func AddHandler(handler interface{}) { + Session.AddHandler(handler) +} diff --git a/config.template.json b/config.template.json new file mode 100644 index 0000000..714d0c4 --- /dev/null +++ b/config.template.json @@ -0,0 +1,31 @@ +{ + "Token": "", + "AlertRoomID": "", + "Servers": [ + { + "Name": "Elysium PvP", + "Address": "149.202.207.235", + "Port": 8099 + }, + { + "Name": "Zethkur PvP", + "Address": "151.80.103.221", + "Port": 8093 + }, + { + "Name": "Anathema PvP", + "Address": "149.202.211.5", + "Port": 8095 + }, + { + "Name": "Darrowshire PvE", + "Address": "164.132.233.125", + "Port": 8097 + }, + { + "Name": "Elysium Authentication Server", + "Address": "logon.elysium-project.org", + "Port": 3724 + } + ] +} \ No newline at end of file diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..b40c4cb --- /dev/null +++ b/config/config.go @@ -0,0 +1,45 @@ +package config + +import ( + "encoding/json" + "io/ioutil" + "log" + "os" +) + +// Variables used for command line parameters +var Config configStruct + +type configStruct struct { + Token string `json:"Token"` + AlertRoomID string `json:"AlertRoomID"` + Servers []server `json:"Servers"` +} + +type server struct { + Name string `json:"Name"` + Address string `json:"Address"` + Port int `json:"Port"` + Online bool `json:"Online,omitempty"` +} + +func Configure() { + + log.Println("Reading config file...") + + file, e := ioutil.ReadFile("./config.json") + + if e != nil { + log.Printf("File error: %v\n", e) + os.Exit(1) + } + + log.Printf("%s\n", string(file)) + + err := json.Unmarshal(file, &Config) + + if err != nil { + log.Println(err) + } + +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..0685c18 --- /dev/null +++ b/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "./bot" + "./config" + "./serverstatus" +) + +// Variables used for command line parameters +var ( + BotID string +) + +func main() { + //read config file + config.Configure() + + //connect bot to account with token + bot.Connect(config.Config.Token) + + //start side tasks + serverstatus.Start() + + bot.AddHandler(serverstatus.MessageHandler) + + //start websocket to listen for messages + bot.Start() +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..bf0b0f8 --- /dev/null +++ b/readme.md @@ -0,0 +1,23 @@ +## Server Status + +Scans the Elysium servers checking to see if they are up. +This bot will send a chat notification when the status of a server changes. + +## Configuration + +- Rename config.template.json to config.json +- Add your bot token +- Add the room ID in which you want the notifications +- Compile and run! + +## List server status in discord channel + +`!ServerStatus` + +``` +Elysium PvP is online! +Zethkur PvP is online! +Anathema PvP is online! +Darrowshire PvP is online! +Elysium Authentication Server is online! +``` \ No newline at end of file diff --git a/serverstatus/serverstatus.go b/serverstatus/serverstatus.go new file mode 100644 index 0000000..8083652 --- /dev/null +++ b/serverstatus/serverstatus.go @@ -0,0 +1,73 @@ +package serverstatus + +import ( + "../bot" + "../config" + "fmt" + "github.com/anvie/port-scanner" + "github.com/bwmarrin/discordgo" + "time" +) + +func Start() { + //set each server status as online to start + for i, _ := range config.Config.Servers { + config.Config.Servers[i].Online = true + } + + //start a new go routine + go loop() +} + +func loop() { + + //check if server are in config file + if len(config.Config.Servers) < 1 { + fmt.Println("No servers in config file.") + return + } + + for { + + for index, server := range config.Config.Servers { + prevServerUp := server.Online //set value to previous server status + + elysiumPvP := portscanner.NewPortScanner(server.Address, time.Second*2) + serverUp := elysiumPvP.IsOpen(server.Port) //check if the port is open + + if serverUp && serverUp != prevServerUp { + sendMessage("@here " + server.Name + " is now online!") + } else if !serverUp && serverUp != prevServerUp { + sendMessage("@here " + server.Name + " went offline!") + } + + config.Config.Servers[index].Online = serverUp + } + + time.Sleep(time.Second * 5) + } +} + +func sendMessage(message string) { + bot.Session.ChannelMessageSend(config.Config.AlertRoomID, message) +} + +// This function will be called every time a new +// message is created on any channel that the autenticated bot has access to. +func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) { + + // Ignore all messages created by the bot itself + if m.Author.ID == bot.BotID { + return + } + + if m.Content == "!ServerStatus" { + for _, server := range config.Config.Servers { + if server.Online { + s.ChannelMessageSend(m.ChannelID, server.Name+" is online!") + } else { + s.ChannelMessageSend(m.ChannelID, server.Name+"is down!") + } + } + } +}