mirror of
https://github.com/mgerb/ServerStatus
synced 2026-01-10 03:03:04 +00:00
init
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
config.json
|
||||||
53
bot/bot.go
Normal file
53
bot/bot.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
31
config.template.json
Normal file
31
config.template.json
Normal file
@@ -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
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
45
config/config.go
Normal file
45
config/config.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
28
main.go
Normal file
28
main.go
Normal file
@@ -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()
|
||||||
|
}
|
||||||
23
readme.md
Normal file
23
readme.md
Normal file
@@ -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!
|
||||||
|
```
|
||||||
73
serverstatus/serverstatus.go
Normal file
73
serverstatus/serverstatus.go
Normal file
@@ -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!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user